diff --git a/.github/workflows/linux-release.yml b/.github/workflows/linux-release.yml new file mode 100644 index 00000000..74e48e72 --- /dev/null +++ b/.github/workflows/linux-release.yml @@ -0,0 +1,68 @@ +name: Linux release archive +# Builds the x86_64 release archive of ScriptWidget for Linux on Arch Linux, +# the base of Omarchy, so that it links the libraries an Omarchy system has +# (an archive built on Ubuntu would ask for Ubuntu's ICU). Started by hand, by +# a `linux-v*` tag, or by a pull request that changes the packaging. +# +# The aarch64 archive is built on an aarch64 Omarchy machine with +# Linux/scripts/package.sh: there is no official Arch Linux image for it here. +on: + workflow_dispatch: + push: + tags: ['linux-v*'] + pull_request: + paths: ['.github/workflows/linux-release.yml', 'Linux/scripts/package.sh', 'Linux/packaging/**'] +permissions: + contents: read +concurrency: + group: linux-release-${{ github.ref }} + cancel-in-progress: true +jobs: + archive: + name: Archive (x86_64, Arch Linux) + runs-on: ubuntu-24.04 + container: archlinux:latest + timeout-minutes: 60 + steps: + - name: System packages + run: | + pacman -Syu --noconfirm --needed base-devel git rustup nodejs npm python zip unzip clang cmake pkgconf \ + icu fontconfig freetype2 libxkbcommon libxkbcommon-x11 libxcb wayland vulkan-icd-loader vulkan-headers \ + openssl zstd alsa-lib noto-fonts noto-fonts-cjk noto-fonts-emoji + rustup default stable + rustc -vV + pacman -Q icu fontconfig glibc + - uses: actions/checkout@v7 + - name: Install esbuild for the shared JavaScript bundle + working-directory: Web/studio + run: npm ci --no-audit --no-fund + - name: Shared JavaScript + run: node Linux/scripts/build-shared-js.mjs + - name: Test + working-directory: Linux + run: cargo test --locked --workspace + - name: Archive + run: sh Linux/scripts/package.sh + # The archive as a user unpacks it, with nothing from the checkout: the + # self-check, one rendered template, and the font rule. + - name: Check the unpacked archive + run: | + set -eu + work=$(mktemp -d) + tar -xzf Linux/artifacts/scriptwidget-*-linux-x86_64.tar.gz -C "$work" + root=$(echo "$work"/scriptwidget-*) + env -i HOME="$work/home" PATH=/usr/bin "$root/bin/scriptwidget" doctor > "$work/doctor.json" || { cat "$work/doctor.json"; exit 1; } + env -i HOME="$work/home" PATH=/usr/bin "$root/bin/scriptwidget" render "$root/share/scriptwidget/templates/template/Month Calendar" --family large --output "$work/calendar.png" > "$work/render.json" + python -c 'import json,sys; r=json.load(open(sys.argv[1])); assert r["status"]=="passed" and r["picture"]["visiblePixels"]>0, r' "$work/render.json" + "$root/bin/scriptwidget-studio" --version + ldd "$root/bin/scriptwidget" | grep -i icu + ! find "$root" -iname '*.ttf' -o -iname '*.otf' | grep . + cp "$work/calendar.png" Linux/artifacts/ + - uses: actions/upload-artifact@v7 + with: + name: scriptwidget-linux-x86_64 + path: | + Linux/artifacts/*.tar.gz + Linux/artifacts/*.sha256 + Linux/artifacts/calendar.png + if-no-files-found: error diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml new file mode 100644 index 00000000..f8ce253f --- /dev/null +++ b/.github/workflows/linux.yml @@ -0,0 +1,60 @@ +name: Linux +on: + pull_request: + paths: ['Linux/**', 'Web/preview-engine/**', 'Android/core/src/main/js/**', 'Android/core/src/main/cpp/quickjs/**', 'Android/core/src/main/assets/fonts/**', 'Scripts/lib/shared-js.mjs', 'Shared/ScriptWidgetRuntime/Widget/Resource/support.bundle/**', '.github/workflows/linux.yml'] + push: + branches: [main] + paths: ['Linux/**', 'Web/preview-engine/**', 'Android/core/src/main/js/**', 'Android/core/src/main/cpp/quickjs/**', 'Android/core/src/main/assets/fonts/**', 'Scripts/lib/shared-js.mjs', 'Shared/ScriptWidgetRuntime/Widget/Resource/support.bundle/**', '.github/workflows/linux.yml'] + workflow_dispatch: +permissions: + contents: read +concurrency: + group: linux-${{ github.ref }} + cancel-in-progress: true +jobs: + test: + name: Build and test (${{ matrix.name }}) + strategy: + fail-fast: false + matrix: + include: + - {name: x86_64, runner: ubuntu-24.04} + - {name: aarch64, runner: ubuntu-24.04-arm} + runs-on: ${{ matrix.runner }} + timeout-minutes: 45 + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: {node-version: 24} + # ICU for Intl, fontconfig and FreeType for the painter. The CJK and emoji fonts are what a + # desktop has; without them the font tests that need them report themselves as skipped. + - name: System libraries and fonts + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends build-essential pkg-config libicu-dev libfontconfig1-dev libfreetype-dev fonts-noto-cjk fonts-noto-color-emoji zip unzip + - name: Rust + run: | + rustup toolchain install stable --profile minimal --component clippy + rustup default stable + rustc -vV + pkg-config --modversion icu-i18n fontconfig + - name: Install esbuild for the shared JavaScript bundle + working-directory: Web/studio + run: npm ci --no-audit --no-fund + - name: Shared JavaScript + run: node Linux/scripts/build-shared-js.mjs + - name: Build + working-directory: Linux + run: cargo build --locked --workspace --all-targets + # No graphical session: the painter draws on a CPU surface. + - name: Test + working-directory: Linux + env: + DISPLAY: '' + WAYLAND_DISPLAY: '' + run: cargo test --locked --workspace -- --include-ignored + - name: Engine binding tests with AddressSanitizer in QuickJS + run: sh Linux/scripts/asan-test.sh + - name: Clippy + working-directory: Linux + run: cargo clippy --locked --workspace --all-targets diff --git a/.gitignore b/.gitignore index 34d3d4a2..6b0e1e19 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,13 @@ Tools/ScriptWidgetCLI/**/__pycache__/ # Functional Pages assembly output _site/ + +# Linux app: Cargo build output and generated JavaScript assets +Linux/target/ +Linux/assets/ +Linux/tests/assets/ +# Pictures the painter tests write for review +Linux/crates/sw-render/tests/out/ +# Pictures the command line tests and reviews write +Linux/crates/sw-cli/tests/out/ +Linux/artifacts/ diff --git a/AGENTS.md b/AGENTS.md index da7f03eb..118c7662 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -14,6 +14,7 @@ ScriptWidget lets users author native widgets with JavaScript and JSX: WidgetKit - `Editor/editorfe/`: Vite frontend with CodeMirror 6. Built files are copied into both platform `StudioEditor.bundle` directories. - `Web/preview-engine/`: browser preview engine (plain JavaScript with `.d.ts` types); `Web/studio/`: Web Studio, a React + strict TypeScript + shadcn/ui app published to GitHub Pages. Its `kernel.js` and `renderer.js` are also the Android runtime, so a change there affects the browser preview and Android together. - `Windows/`: the Windows 11 desktop app (preview). `native` builds `swcore.dll` from the QuickJS-NG sources under `Android/core/src/main/cpp/quickjs`; `src/ScriptWidget.Core` is a C# port of `Android/core` (engine wrapper, compiler, executor, packages, APIs, plugins, Win2D renderer); `src/ScriptWidget.App` is the always-running desktop host; `src/ScriptWidget.Studio` is the WinUI 3 management window; `src/ScriptWidget.Cli` is `swcli`. It runs the same shared JavaScript as Android, built by `Scripts/lib/shared-js.mjs`. `Windows/assets`, `Windows/native/out` and `Windows/artifacts` are generated; do not edit or commit them. +- `Linux/`: the Linux app (preview, for Omarchy), written in Rust. `crates/sw-quickjs` builds the QuickJS-NG sources under `Android/core/src/main/cpp/quickjs`; `crates/sw-core` is the runtime (packages, compiler, executor, APIs, plugins), ported from `Windows/src/ScriptWidget.Core`; `crates/sw-render` is the Skia painter; `crates/sw-host` is the background service that draws the widgets; `crates/sw-cli` is the `scriptwidget` command line, which also starts the service; `crates/sw-studio` is the GPUI management window (behind the `ui` feature); `omarchy-plugin` is the QML plugin of the Omarchy shell that shows the pictures and forwards clicks, speaking the protocol in `docs/linux/protocol.md`; `packaging` and `scripts/package.sh` build the release archive. It runs the same shared JavaScript as Android and Windows. `Linux/assets`, `Linux/tests/assets`, `Linux/target` and `Linux/artifacts` are generated; do not edit or commit them. The six display fonts are deliberately not shipped on Linux. - `Android/core`: QuickJS-NG with its JNI layer, the Kotlin host and native APIs, Canvas renderer, package store, Gallery, Skills, plugins and AI client. `Android/app`: Compose app, widget providers, tiles, shortcuts, Live Updates and push. `Android/core/src/main/assets/scriptwidget` is generated; do not edit it. `Android/app/src/main/assets/Script.android` holds the templates for what only Android has: the scrolling ``, the `material.` colors and the automation entry point, all described under "Only on Android" in `docs/android/README.md`. `Android/play`: Google Play listing kit. - `Tests/ScriptWidgetRuntimeTests/`: shared runtime, execution, performance, package/cache, iCloud-state, Gallery, AI, and security tests. - `Gallery/`: curated Gallery 1.0 index, JSON Schema, widget seeds, and Skill seeds. @@ -36,6 +37,8 @@ Widget packages live under `Scripts//`. Package 2.0 uses `widget.js - Windows (not covered by `release-readiness.sh`; needs Windows 11, Visual Studio with the C++ workload, the .NET 10 SDK and Node): `node Windows/scripts/build-shared-js.mjs` after changing `Web/preview-engine/src`, the support bundle or `Android/core/src/main/js`; `powershell -File Windows/scripts/build-native.ps1 -Arch x64`; `cd Windows && dotnet test tests/ScriptWidget.Core.Tests`; `powershell -File Windows/scripts/desktop-check.ps1` after touching the desktop host; `powershell -File Windows/scripts/package.ps1` for the MSIX (add `-FrameworkDependent` and the Partner Center identity for the Store); `powershell -File Windows/scripts/installer.ps1` for the NSIS installer published on GitHub Releases (tag `windows-v`), checked by `Windows/scripts/installer-check.ps1` and built for both processors by the hand-started "Windows installers" workflow. Record what ran, on which machine, in `docs/windows/testing.md`, and keep `docs/windows/TODO.md` current. +- Linux (not covered by `release-readiness.sh`; needs Rust, a C compiler, `pkg-config`, the ICU, fontconfig and FreeType development files, and Node): `node Linux/scripts/build-shared-js.mjs` after changing `Web/preview-engine/src`, the support bundle or `Android/core/src/main/js`; `cd Linux && cargo test --locked --workspace`; `sh Linux/scripts/asan-test.sh` on Linux after touching `sw-quickjs`; `sh Linux/scripts/package.sh` for the release archive (tag `linux-v`; x86_64 from the hand-started "Linux release archive" workflow, aarch64 on an Omarchy machine); `sh Linux/omarchy-plugin/tests/check.sh` in an Omarchy session after touching the plugin. Record what ran, on which machine, in `docs/linux/testing.md`, and keep `docs/linux/TODO.md` current. + The app uses `iCloud.ScriptWidget` and `group.everettjf.scriptwidget`. Signed real-container tests require a trusted, signed-in device; deterministic cache/iCloud-state tests do not. The Android application ID is `com.atomsprout.scriptwidget`; the Kotlin namespace stays `com.everettjf.scriptwidget`. Take the package name from `context.packageName` and class names from the class, never from a literal. `Android/keystore.properties`, `Android/firebase.properties`, `google-services.json` and keystores are git-ignored and must stay out of the repository; a build without `firebase.properties` has push turned off. @@ -88,7 +91,7 @@ Treat scripts, imported ZIPs, Gallery content, Skills, plugin manifests, network - Use concise, action-oriented commit subjects. Add a body when behavior, security tradeoffs, migrations, or generated artifacts need explanation. - PRs should state the user outcome, linked issue, security/compatibility impact, commands/schemes run, and any skipped physical-device or signed-iCloud checks. - Include before/after screenshots for visible UI changes and keep documentation/schema/generated bundles in the same PR as the behavior they describe. -- GitHub releases: the newest app release (Apple `v…`, Android `android-v…` or Windows `windows-v…`) is marked Latest; `cli-v…` releases and previews are created with `--latest=false`. See `docs/release-readiness.md`. +- GitHub releases: the newest app release (Apple `v…`, Android `android-v…`, Windows `windows-v…` or Linux `linux-v…`) is marked Latest; `cli-v…` releases and previews are created with `--latest=false`. See `docs/release-readiness.md`. - Never describe a skipped check as passing. Link the exact successful CI run or local evidence when claiming release readiness. ## Current Product Priorities diff --git a/Linux/Cargo.lock b/Linux/Cargo.lock new file mode 100644 index 00000000..3484a66c --- /dev/null +++ b/Linux/Cargo.lock @@ -0,0 +1,8768 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "accesskit" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3b7f7f85a7e5f68090000ed7622545829afd484d210358702ae4cb97dd0c320" +dependencies = [ + "enumn", + "uuid", +] + +[[package]] +name = "accesskit_atspi_common" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "023da0e5097f46df7092d5280b02efb9bbf8d93298daeced42652463e357d636" +dependencies = [ + "accesskit", + "accesskit_consumer", + "atspi-common", + "phf 0.13.1", + "serde", + "zvariant", +] + +[[package]] +name = "accesskit_consumer" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d10a236f96f87d70732e44520046785431ef01d5bcd6b041317bfadd2f88245" +dependencies = [ + "accesskit", + "hashbrown 0.16.1", +] + +[[package]] +name = "accesskit_macos" +version = "0.26.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce02dc63b43f0c9296af9ac946312a2dc8814427d7a64d2d600971dac55b6076" +dependencies = [ + "accesskit", + "accesskit_consumer", + "hashbrown 0.16.1", + "objc2 0.5.3", + "objc2-app-kit 0.2.2", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "accesskit_unix" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03e156ed3802e35eefe894ef2671bc6c889303d8a7e110b5e1b48f504b91362f" +dependencies = [ + "accesskit", + "accesskit_atspi_common", + "async-channel", + "async-executor", + "async-task", + "atspi", + "futures-lite", + "futures-util", + "serde", + "zbus 5.19.0", +] + +[[package]] +name = "accesskit_windows" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "106c2b961215864d1c2e703ee63269c25c4e80a577ffb2c1017b9c17dcdf83a1" +dependencies = [ + "accesskit", + "accesskit_consumer", + "hashbrown 0.16.1", + "static_assertions", + "windows 0.62.2", + "windows-core 0.62.2", +] + +[[package]] +name = "addr2line" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e567177890eb1617b1f774005b66b26b2377afd138a2ca37aae7d8f0c81429d4" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures 0.2.17", + "zeroize", +] + +[[package]] +name = "ahash" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" +dependencies = [ + "cfg-if", + "const-random", + "once_cell", + "version_check", + "zerocopy", +] + +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + +[[package]] +name = "aligned" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee4508988c62edf04abd8d92897fca0c2995d907ce1dfeaf369dac3716a40685" +dependencies = [ + "as-slice", +] + +[[package]] +name = "aligned-vec" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc890384c8602f339876ded803c97ad529f3842aba97f6392b3dba0dd171769b" +dependencies = [ + "equator", +] + +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + +[[package]] +name = "android_system_properties" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc" +dependencies = [ + "libc", +] + +[[package]] +name = "annotate-snippets" +version = "0.12.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0258346f92c78dbc5b336b9a5b27c64447aa10643bc64e651db2edb145361564" +dependencies = [ + "anstyle", + "memchr", + "unicode-width", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anyhow" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" + +[[package]] +name = "arbitrary" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" + +[[package]] +name = "arc-swap" +version = "1.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b5d51209f6de83d37a0b33d320acb8da88823e42a94c075140708f032a5" +dependencies = [ + "rustversion", +] + +[[package]] +name = "arg_enum_proc_macro" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ae92a5119aa49cdbcf6b9f893fe4e1d98b04ccbf82ee0584ad948a44a734dea" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "arraydeque" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d902e3d592a523def97af8f317b08ce16b7ab854c1985a0c671e6f15cebc236" + +[[package]] +name = "arrayref" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb" + +[[package]] +name = "arrayvec" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" + +[[package]] +name = "as-raw-xcb-connection" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b" + +[[package]] +name = "as-slice" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "516b6b4f0e40d50dcda9365d53964ec74560ad4284da2e7fc97122cd83174516" +dependencies = [ + "stable_deref_trait", +] + +[[package]] +name = "ash" +version = "0.38.0+1.3.281" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bb44936d800fea8f016d7f2311c6a4f97aebd5dc86f09906139ec848cf3a46f" +dependencies = [ + "libloading", +] + +[[package]] +name = "ashpd" +version = "0.13.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb8421aaa9644a5faf26735f258b669b15f063313ef8f8e2bdb28912a1a6f111" +dependencies = [ + "enumflags2", + "futures-channel", + "futures-util", + "getrandom 0.4.3", + "serde", + "serde_repr", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "zbus 5.19.0", +] + +[[package]] +name = "async-broadcast" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435a87a52755b8f27fcf321ac4f04b2802e337c8c4872923137471ec39c37532" +dependencies = [ + "event-listener", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-channel" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "924ed96dd52d1b75e9c1a3e6275715fd320f5f9439fb5a4a11fa51f4221158d2" +dependencies = [ + "concurrent-queue", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-executor" +version = "1.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f426a31126c735126297bc5ea7a521263c9faff4f63ca415267ee625651c4f37" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", + "futures-lite", + "pin-project-lite", + "slab", +] + +[[package]] +name = "async-fs" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8034a681df4aed8b8edbd7fbe472401ecf009251c8b40556b304567052e294c5" +dependencies = [ + "async-lock", + "blocking", + "futures-lite", +] + +[[package]] +name = "async-io" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456b8a8feb6f42d237746d4b3e9a178494627745c3c56c6ea55d92ba50d026fc" +dependencies = [ + "autocfg", + "cfg-if", + "concurrent-queue", + "futures-io", + "futures-lite", + "parking", + "polling", + "rustix", + "slab", + "windows-sys 0.61.2", +] + +[[package]] +name = "async-lock" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0876700b0d007ad25d5508865a4259c3af9af6d296bf663a0d586d0bc3369f6f" +dependencies = [ + "event-listener", + "event-listener-strategy", + "pin-project-lite", +] + +[[package]] +name = "async-net" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b948000fad4873c1c9339d60f2623323a0cfd3816e5181033c6a5cb68b2accf7" +dependencies = [ + "async-io", + "blocking", + "futures-lite", +] + +[[package]] +name = "async-process" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc50921ec0055cdd8a16de48773bfeec5c972598674347252c0399676be7da75" +dependencies = [ + "async-channel", + "async-io", + "async-lock", + "async-signal", + "async-task", + "blocking", + "cfg-if", + "event-listener", + "futures-lite", + "rustix", +] + +[[package]] +name = "async-recursion" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f8abc12baad266b1c8cec146854c195b5864b4221d4b2ca7296a7ae82d9e451" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "async-signal" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52b5aaafa020cf5053a01f2a60e8ff5dccf550f0f77ec54a4e47285ac2bab485" +dependencies = [ + "async-io", + "async-lock", + "atomic-waker", + "cfg-if", + "futures-core", + "futures-io", + "rustix", + "signal-hook-registry", + "slab", + "windows-sys 0.61.2", +] + +[[package]] +name = "async-task" +version = "4.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" + +[[package]] +name = "async-trait" +version = "0.1.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "atspi" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c77886257be21c9cd89a4ae7e64860c6f0eefca799bb79127913052bd0eefb3d" +dependencies = [ + "atspi-common", + "atspi-proxies", +] + +[[package]] +name = "atspi-common" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20c5617155740c98003016429ad13fe43ce7a77b007479350a9f8bf95a29f63d" +dependencies = [ + "enumflags2", + "serde", + "static_assertions", + "zbus 5.19.0", + "zbus-lockstep", + "zbus-lockstep-macros", + "zbus_names", + "zvariant", +] + +[[package]] +name = "atspi-proxies" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2230e48787ed3eb4088996eab66a32ca20c0b67bbd4fd6cdfe79f04f1f04c9fc" +dependencies = [ + "atspi-common", + "serde", + "zbus 5.19.0", +] + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "av-scenechange" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f321d77c20e19b92c39e7471cf986812cbb46659d2af674adc4331ef3f18394" +dependencies = [ + "aligned", + "anyhow", + "arg_enum_proc_macro", + "arrayvec", + "log", + "num-rational", + "num-traits", + "pastey", + "rayon", + "thiserror 2.0.21", + "v_frame", + "y4m", +] + +[[package]] +name = "av1-grain" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cfddb07216410377231960af4fcab838eaa12e013417781b78bd95ee22077f8" +dependencies = [ + "anyhow", + "arrayvec", + "log", + "nom 8.0.0", + "num-rational", + "v_frame", +] + +[[package]] +name = "avif-serialize" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7178fe5f7d460b13895ebb9dcb28a3a6216d2df2574a0806cb51b555d297f38" +dependencies = [ + "arrayvec", +] + +[[package]] +name = "backtrace" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f4220217f3288537b83684130f42fad43320b1c3b805f1910d683d52ded24f" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide 0.9.1", + "object", + "rustc-demangle", + "windows-link 0.2.1", +] + +[[package]] +name = "base62" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3a647dce9631f152a99ec16cfc09267ae55fe280d5dc288964027d458076323" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "base64" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" + +[[package]] +name = "bindgen" +version = "0.73.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "787ef8ef523575546b106a58213d6e6b06198a05c2f757258c68a74273670cfa" +dependencies = [ + "bitflags 2.13.2", + "cexpr", + "clang-sys", + "log", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash 2.1.3", + "shlex", + "syn 3.0.10", +] + +[[package]] +name = "bit-set" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2f926cc3060f09db9ebc5b52823d85268d24bb917e472c0c4bea35780a7d" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b71798fca2c1fe1086445a7258a4bc81e6e49dcd24c8d0dd9a1e57395b603f51" + +[[package]] +name = "bit_field" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb2907cc03d08cdb1c6724ad0670ce67c4a71e1c45a6d7635224011714cfc4b1" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "bitstream-io" +version = "4.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eff00be299a18769011411c9def0d827e8f2d7bf0c3dbf53633147a8867fd1f" +dependencies = [ + "no_std_io2", +] + +[[package]] +name = "block" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "block-padding" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8894febbff9f758034a5b8e12d87918f56dfc64a8e1fe757d65e29041538d93" +dependencies = [ + "generic-array", +] + +[[package]] +name = "block2" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c132eebf10f5cad5289222520a4a058514204aed6d791f1cf4fe8088b82d15f" +dependencies = [ + "objc2 0.5.3", +] + +[[package]] +name = "block2" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5" +dependencies = [ + "objc2 0.6.5", +] + +[[package]] +name = "blocking" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a70e4329df6cb94385eed412ec92375c3cdd8a6e502493d1229b6414e4036dfa" +dependencies = [ + "async-channel", + "async-task", + "futures-io", + "futures-lite", + "piper", +] + +[[package]] +name = "bstr" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bb31b46c14244e20ee9984b11bf5c992b91fb6939fea616e3512c8baecdbe5f" +dependencies = [ + "memchr", + "regex-automata", + "serde_core", +] + +[[package]] +name = "built" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c0e531d93d39c34eef561e929e8a7f86d77a5af08aac4f6d6e39976c51858e9" + +[[package]] +name = "bumpalo" +version = "3.20.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87a0cf66fd1eb2fb7b822cb78370d82303750809bc6ba855f3f58e517d3238db" + +[[package]] +name = "bytemuck" +version = "1.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a1f896587b6f2c069c73d2f0913e2d590c3990285cd2f0b6aa02b786b4c679c" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "byteorder-lite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495" + +[[package]] +name = "bytes" +version = "1.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "776dfebd015e2d8732c684a2c71003674ccb49457b539b9e82151ba38e96dd7c" + +[[package]] +name = "calloop" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b32313e937c8721c9d17ee9bf60699a6402b4b7c2cb6ac1bee0ffcaa4b4e5e" +dependencies = [ + "bitflags 2.13.2", + "polling", + "rustix", + "tracing", +] + +[[package]] +name = "calloop-wayland-source" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138efcf0940a02ebf0cc8d1eff41a1682a46b431630f4c52450d6265876021fa" +dependencies = [ + "calloop", + "rustix", + "wayland-backend", + "wayland-client", +] + +[[package]] +name = "cbc" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b52a9543ae338f279b96b0b9fed9c8093744685043739079ce85cd58f289a6" +dependencies = [ + "cipher", +] + +[[package]] +name = "cbindgen" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eadd868a2ce9ca38de7eeafdcec9c7065ef89b42b32f0839278d55f35c54d1ff" +dependencies = [ + "heck 0.4.1", + "indexmap", + "log", + "proc-macro2", + "quote", + "serde", + "serde_json", + "syn 2.0.119", + "tempfile", + "toml 0.8.23", +] + +[[package]] +name = "cc" +version = "1.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ef9e3e8154a45c7a95cff4c1299702106ad94062d7bb85fdc5d3a5afd91d45" +dependencies = [ + "find-msvc-tools", + "jobserver", + "libc", + "shlex", +] + +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom 7.1.3", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "cfg_aliases" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" + +[[package]] +name = "chacha20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "rand_core 0.10.1", +] + +[[package]] +name = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "serde", + "wasm-bindgen", + "windows-link 0.2.1", +] + +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common 0.1.7", + "inout", + "zeroize", +] + +[[package]] +name = "clang-sys" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "157a8ba7b480713b56f4c09fd13fc3e0a22a5dfab8097ba61cbc5feef950788a" +dependencies = [ + "glob", + "libc", + "libloading", +] + +[[package]] +name = "cocoa" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6140449f97a6e97f9511815c5632d84c8aacf8ac271ad77c559218161a1373c" +dependencies = [ + "bitflags 1.3.2", + "block", + "cocoa-foundation", + "core-foundation 0.9.4", + "core-graphics 0.23.2", + "foreign-types", + "libc", + "objc", +] + +[[package]] +name = "cocoa-foundation" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c6234cbb2e4c785b456c0644748b1ac416dd045799740356f8363dfe00c93f7" +dependencies = [ + "bitflags 1.3.2", + "block", + "core-foundation 0.9.4", + "core-graphics-types 0.1.3", + "libc", + "objc", +] + +[[package]] +name = "codespan-reporting" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af491d569909a7e4dee0ad7db7f5341fef5c614d5b8ec8cf765732aba3cff681" +dependencies = [ + "serde", + "termcolor", + "unicode-width", +] + +[[package]] +name = "color_quant" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" + +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + +[[package]] +name = "const-oid" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a" + +[[package]] +name = "const-random" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359" +dependencies = [ + "const-random-macro", +] + +[[package]] +name = "const-random-macro" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e" +dependencies = [ + "getrandom 0.2.17", + "once_cell", + "tiny-keccak", +] + +[[package]] +name = "convert_case" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "633458d4ef8c78b72454de2d54fd6ab2e60f9e02be22f3c6104cdc8a4e0fceb9" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "core-graphics" +version = "0.23.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081" +dependencies = [ + "bitflags 1.3.2", + "core-foundation 0.9.4", + "core-graphics-types 0.1.3", + "foreign-types", + "libc", +] + +[[package]] +name = "core-graphics" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa95a34622365fa5bbf40b20b75dba8dfa8c94c734aea8ac9a5ca38af14316f1" +dependencies = [ + "bitflags 2.13.2", + "core-foundation 0.10.1", + "core-graphics-types 0.2.0", + "foreign-types", + "libc", +] + +[[package]] +name = "core-graphics-helmer-fork" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32eb7c354ae9f6d437a6039099ce7ecd049337a8109b23d73e48e8ffba8e9cd5" +dependencies = [ + "bitflags 2.13.2", + "core-foundation 0.9.4", + "core-graphics-types 0.1.3", + "foreign-types", + "libc", +] + +[[package]] +name = "core-graphics-types" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf" +dependencies = [ + "bitflags 1.3.2", + "core-foundation 0.9.4", + "libc", +] + +[[package]] +name = "core-graphics-types" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d44a101f213f6c4cdc1853d4b78aef6db6bdfa3468798cc1d9912f4735013eb" +dependencies = [ + "bitflags 2.13.2", + "core-foundation 0.10.1", + "libc", +] + +[[package]] +name = "core-text" +version = "21.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a593227b66cbd4007b2a050dfdd9e1d1318311409c8d600dc82ba1b15ca9c130" +dependencies = [ + "core-foundation 0.10.1", + "core-graphics 0.24.0", + "foreign-types", + "libc", +] + +[[package]] +name = "core_detect" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" + +[[package]] +name = "core_maths" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77745e017f5edba1a9c1d854f6f3a52dac8a12dd5af5d2f54aecf61e43d80d30" +dependencies = [ + "libm", +] + +[[package]] +name = "cosmic-text" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be17b688510d934ce13f48a2beba700e11583e281e0fda99c22bb256a14eda73" +dependencies = [ + "bitflags 2.13.2", + "fontdb", + "harfrust", + "linebender_resource_handle", + "log", + "rangemap", + "rustc-hash 2.1.3", + "self_cell", + "skrifa 0.40.0", + "smol_str", + "swash", + "sys-locale", + "unicode-bidi", + "unicode-linebreak", + "unicode-script", + "unicode-segmentation", +] + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b929dd2a135771f40afabd1073f85eed59c800b7c61901e5c254270497dad64" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" + +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "ctor" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea691ed9f36489d087419b8b2ba3ef27d89621380b4178c73e2445a379e4e0f4" +dependencies = [ + "link-section", + "linktime-proc-macro", +] + +[[package]] +name = "data-url" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be1e0bca6c3637f992fc1cc7cbc52a78c1ef6db076dbf1059c4323d6a2048376" + +[[package]] +name = "deranged" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e" + +[[package]] +name = "derive_more" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d751e9e49156b02b44f9c1815bcb94b984cdcc4396ecc32521c739452808b134" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799a97264921d8623a957f6c3b9011f3b5492f557bbb7a5a19b7fa6d06ba8dcb" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn 2.0.119", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer 0.10.4", + "crypto-common 0.1.7", + "subtle", +] + +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.1", + "const-oid", + "crypto-common 0.2.2", +] + +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys 0.4.1", +] + +[[package]] +name = "dirs" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e" +dependencies = [ + "dirs-sys 0.5.0", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users 0.4.6", + "windows-sys 0.48.0", +] + +[[package]] +name = "dirs-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" +dependencies = [ + "libc", + "option-ext", + "redox_users 0.5.3", + "windows-sys 0.61.2", +] + +[[package]] +name = "dispatch" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd0c93bb4b0c6d9b77f4435b0ae98c24d17f1c45b2ff844c6151a07256ca923b" + +[[package]] +name = "dispatch2" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0e367e4e7da84520dedcac1901e4da967309406d1e51017ae1abfb97adbd38" +dependencies = [ + "bitflags 2.13.2", + "block2 0.6.2", + "objc2 0.6.5", +] + +[[package]] +name = "displaydoc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "dlib" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab8ecd87370524b461f8557c119c405552c396ed91fc0a8eec68679eab26f94a" +dependencies = [ + "libloading", +] + +[[package]] +name = "document-features" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4b8a88685455ed29a21542a33abd9cb6510b6b129abadabdcef0f4c55bc8f61" +dependencies = [ + "litrs", +] + +[[package]] +name = "downcast-rs" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" + +[[package]] +name = "dunce" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" + +[[package]] +name = "dwrote" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b35532432acc8b19ceed096e35dfa088d3ea037fe4f3c085f1f97f33b4d02" +dependencies = [ + "lazy_static", + "libc", + "winapi", + "wio", +] + +[[package]] +name = "dyn-clone" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" + +[[package]] +name = "either" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" + +[[package]] +name = "embed-resource" +version = "3.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6168487b5e7eb2673e6efe1200b1848016e436a6ca0b8031bf04bd9d493a249f" +dependencies = [ + "cc", + "memchr", + "rustc_version", + "toml 1.2.1+spec-1.1.0", + "vswhom", + "winreg", +] + +[[package]] +name = "encoding_rs" +version = "0.8.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89253175e704ab869b04f9db224d38630e0211a53841a79181deae0889e169f5" +dependencies = [ + "cfg-if", + "core_detect", + "multiversion_no_op", + "rustversion", + "scopeguard", + "simdutf8", +] + +[[package]] +name = "encoding_rs_io" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fba3fe847045ecff794b9c138293a80db914678c453ad63fbf0c6a9eb6e00b22" +dependencies = [ + "encoding_rs", +] + +[[package]] +name = "endi" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099" + +[[package]] +name = "enum-iterator" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4549325971814bda7a44061bf3fe7e487d447cba01e4220a4b454d630d7a016" +dependencies = [ + "enum-iterator-derive", +] + +[[package]] +name = "enum-iterator-derive" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "685adfa4d6f3d765a26bc5dbc936577de9abf756c1feeb3089b01dd395034842" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "enumflags2" +version = "0.7.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1027f7680c853e056ebcec683615fb6fbbc07dbaa13b4d5d9442b146ded4ecef" +dependencies = [ + "enumflags2_derive", + "serde", +] + +[[package]] +name = "enumflags2_derive" +version = "0.7.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67c78a4d8fdf9953a5c9d458f9efe940fd97a0cab0941c075a813ac594733827" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "enumn" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f9ed6b3789237c8a0c1c505af1c7eb2c560df6186f01b098c3a1064ea532f38" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "equator" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4711b213838dfee0117e3be6ac926007d7f433d7bbe33595975d4190cb07e6fc" +dependencies = [ + "equator-macro", +] + +[[package]] +name = "equator-macro" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44f23cf4b44bfce11a86ace86f8a73ffdec849c9fd00a386a53d278bd9e81fb3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "equivalent" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" + +[[package]] +name = "errno" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e35d6e78f4cebb90e51d543db8fe41528851d6deeef6ef21ebd4bfdc1e784776" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "etagere" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc89bf99e5dc15954a60f707c1e09d7540e5cd9af85fa75caa0b510bc08c5342" +dependencies = [ + "euclid", + "svg_fmt", +] + +[[package]] +name = "euclid" +version = "0.22.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1a05365e3b1c6d1650318537c7460c6923f1abdd272ad6842baa2b509957a06" +dependencies = [ + "num-traits", +] + +[[package]] +name = "event-listener" +version = "5.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a23add41df1562121a9393cb065eab5146a1242410f23a644851e90cfd669d2" +dependencies = [ + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be9f3dfaaffdae2972880079a491a1a8bb7cbed0b8dd7a347f668b4150a3b93" +dependencies = [ + "event-listener", + "pin-project-lite", +] + +[[package]] +name = "exr" +version = "1.74.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb29d0be702adf92c645d9b2f93472906ba83d400e59f98adb3770ce5c18c2f6" +dependencies = [ + "bit_field", + "half", + "lebe", + "miniz_oxide 0.8.9", + "multiversion", + "rayon-core", + "smallvec", + "zune-inflate", +] + +[[package]] +name = "fastrand" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" + +[[package]] +name = "fax" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caf1079563223d5d59d83c85886a56e586cfd5c1a26292e971a0fa266531ac5a" + +[[package]] +name = "fdeflate" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c" +dependencies = [ + "simd-adler32", +] + +[[package]] +name = "filedescriptor" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e40758ed24c9b2eeb76c35fb0aebc66c626084edd827e07e1552279814c6682d" +dependencies = [ + "libc", + "thiserror 1.0.69", + "winapi", +] + +[[package]] +name = "filetime" +version = "0.2.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1255e7a9f1902190d4c10a75f2f7f495d3d3ce13e9ba26e6c48683a0f4dbed7" +dependencies = [ + "cfg-if", + "libc", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" + +[[package]] +name = "flate2" +version = "1.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "626d4928326b646e70f65ed36fc7cf9f206bdb68a0019ee7c6b4709da4e084c4" +dependencies = [ + "crc32fast", + "miniz_oxide 0.9.1", + "zlib-rs", +] + +[[package]] +name = "float-cmp" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" + +[[package]] +name = "float-ord" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ce81f49ae8a0482e4c55ea62ebbd7e5a686af544c00b9d090bba3ff9be97b3d" + +[[package]] +name = "float_next_after" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bf7cc16383c4b8d58b9905a8509f02926ce3058053c056376248d958c9df1e8" + +[[package]] +name = "fluent-uri" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17c704e9dbe1ddd863da1e6ff3567795087b1eb201ce80d8fa81162e1516500d" +dependencies = [ + "bitflags 1.3.2", +] + +[[package]] +name = "flume" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e139bc46ca777eb5efaf62df0ab8cc5fd400866427e56c68b22e414e53bd3be" +dependencies = [ + "futures-core", + "futures-sink", + "spin 0.9.9", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + +[[package]] +name = "font-types" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b38ad915f6dadd993ced50848a8291a543bd41ca62bc10740d5e64e2ab4cfd7" +dependencies = [ + "bytemuck", +] + +[[package]] +name = "font-types" +version = "0.12.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "835ae9d781464a79026cde387f9cbc650c9a4909d7cf7e658b127e3c35d2f35d" +dependencies = [ + "bytemuck", +] + +[[package]] +name = "fontconfig-parser" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbc773e24e02d4ddd8395fd30dc147524273a83e54e0f312d986ea30de5f5646" +dependencies = [ + "roxmltree 0.20.0", +] + +[[package]] +name = "fontdb" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "457e789b3d1202543297a350643cf459f836cade38934e7a4cf6a39e7cde2905" +dependencies = [ + "fontconfig-parser", + "log", + "memmap2", + "slotmap", + "tinyvec", + "ttf-parser", +] + +[[package]] +name = "foreign-types" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" +dependencies = [ + "foreign-types-macros", + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-macros" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea5190182e6915eb873ddbc16e23b711b6eb1f9c00a0d0a3a91b5f6228475225" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "foreign-types-shared" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "freetype-sys" +version = "0.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7edc5b9669349acfda99533e9e0bcf26a51862ab43b08ee7745c55d28eb134" +dependencies = [ + "cc", + "libc", + "pkg-config", +] + +[[package]] +name = "futf" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df420e2e84819663797d1ec6544b13c5be84629e7bb00dc960d6917db2987843" +dependencies = [ + "mac", + "new_debug_unreachable", +] + +[[package]] +name = "futures" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a31d2a3fbaaeb2af2368bbdd904aa8e812d3c04a1ee10d3171f52d556e5d0a3" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-executor" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "031b47cf1a3c6cc8bc2fc76cd437f521619387907d469316e7c0bc278f1f5432" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53c0fa8157de1303bfffdaa1cc2a673bfffb60102f76b0ef4441659124373fed" + +[[package]] +name = "futures-lite" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f78e10609fe0e0b3f4157ffab1876319b5b0db102a2c60dc4626306dc46b44ad" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "parking", + "pin-project-lite", +] + +[[package]] +name = "futures-macro" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb9654ba8355388abeb8dcb4fc62f511300867002afc858860463bdd9fe0c44" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "futures-sink" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "gethostname" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bd49230192a3797a9a4d6abe9b3eed6f7fa4c8a8a4947977c6f80025f92cbd8" +dependencies = [ + "rustix", + "windows-link 0.2.1", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi 5.3.0", + "wasip2", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi 6.0.0", + "rand_core 0.10.1", + "wasm-bindgen", +] + +[[package]] +name = "gif" +version = "0.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae047235e33e2829703574b54fdec96bfbad892062d97fed2f76022287de61b" +dependencies = [ + "color_quant", + "weezl", +] + +[[package]] +name = "gif" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee8cfcc411d9adbbaba82fb72661cc1bcca13e8bba98b364e62b2dba8f960159" +dependencies = [ + "color_quant", + "weezl", +] + +[[package]] +name = "gimli" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1033caf0b349c518623b5396bfb2cf0bddf44f0306d543a250e5743297aafd10" + +[[package]] +name = "gl_generator" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a95dfc23a2b4a9a2f5ab41d194f8bfda3cabec42af4e39f08c339eb2a0c124d" +dependencies = [ + "khronos_api", + "log", + "xml-rs", +] + +[[package]] +name = "glob" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b" + +[[package]] +name = "globset" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f98b0d85522dc0a824cfebbc9594ff4a9b289c568bea0370319dfbdf20ccdd2d" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "globwalk" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93e3af942408868f6934a7b85134a3230832b9977cf66125df2f9edcfce4ddcc" +dependencies = [ + "bitflags 1.3.2", + "ignore", + "walkdir", +] + +[[package]] +name = "glow" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29038e1c483364cc6bb3cf78feee1816002e127c331a1eec55a4d202b9e1adb5" +dependencies = [ + "js-sys", + "slotmap", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "glutin_wgl_sys" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c4ee00b289aba7a9e5306d57c2d05499b2e5dc427f84ac708bd2c090212cf3e" +dependencies = [ + "gl_generator", +] + +[[package]] +name = "gpu-allocator" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51255ea7cfaadb6c5f1528d43e92a82acb2b96c43365989a28b2d44ee38f8795" +dependencies = [ + "ash", + "hashbrown 0.16.1", + "log", + "presser", + "thiserror 2.0.21", + "windows 0.62.2", +] + +[[package]] +name = "gpui-base" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f4e5aff50636223b268a687ad5788ec54fa77c8e57ca9e53d158f19e0425d95" +dependencies = [ + "aho-corasick", + "anyhow", + "ashpd", + "async-channel", + "chrono", + "data-url", + "futures", + "gpui-pre", + "gpui-pre-macros", + "gpui-pre-sum-tree", + "html5ever", + "instant", + "itertools 0.13.0", + "lsp-types", + "markdown", + "markup5ever_rcdom", + "num-traits", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-foundation 0.3.2", + "raw-window-handle", + "regex", + "ropey", + "schemars", + "serde", + "serde_json", + "smallvec", + "smol", + "sys-locale", + "tracing", + "unicode-linebreak", + "unicode-segmentation", + "web-time", + "windows 0.58.0", +] + +[[package]] +name = "gpui-component" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "607c90f6985ac2b96e54102338994eaea143aff521375c6f9e7fc7ecb3ba3646" +dependencies = [ + "anyhow", + "chrono", + "core-text", + "enum-iterator", + "gpui-base", + "gpui-component-macros", + "gpui-kit-assets", + "gpui-pre", + "gpui-pre-macros", + "gpui-pre-sum-tree", + "instant", + "itertools 0.13.0", + "log", + "lsp-types", + "markdown", + "notify", + "num-traits", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-foundation 0.3.2", + "once_cell", + "paste", + "raw-window-handle", + "regex", + "resvg 0.45.1", + "ropey", + "rust-i18n", + "schemars", + "serde", + "serde_json", + "serde_repr", + "similar", + "smallvec", + "smol", + "tracing", + "tree-sitter", + "tree-sitter-javascript", + "tree-sitter-json", + "unicode-segmentation", + "unicode-width", + "uuid", + "windows 0.58.0", +] + +[[package]] +name = "gpui-component-macros" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "510a438db463e4fcaec876a0cf76c3bfd900bc407207b0bc8c6076f91c09591b" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "gpui-kit" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "228cc61c0a71c4908af2e903807a81d2deabc03ec0be7f22189d43cf552af098" +dependencies = [ + "gpui-base", + "gpui-component", + "gpui-kit-assets", + "gpui-pre", + "gpui-pre-macros", + "gpui-pre-platform", + "gpui-pre-web", +] + +[[package]] +name = "gpui-kit-assets" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "655474e0a0069db561d7d4029f84de59e41c0f1afff2301b23e00329968d3148" +dependencies = [ + "anyhow", + "gpui-pre", + "gpui-pre-reqwest", + "log", + "rust-embed", + "wasm-bindgen", + "wasm-bindgen-futures", +] + +[[package]] +name = "gpui-pre" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29db89613527079737f53752eab27fb65c9a7dd94979256c65a0e590c9fd34d8" +dependencies = [ + "accesskit", + "anyhow", + "async-channel", + "async-task", + "backtrace", + "bitflags 2.13.2", + "chrono", + "derive_more", + "embed-resource", + "etagere", + "futures", + "getrandom 0.3.4", + "gpui-pre-collections", + "gpui-pre-http-client", + "gpui-pre-macros", + "gpui-pre-refineable", + "gpui-pre-scheduler", + "gpui-pre-shared-string", + "gpui-pre-sum-tree", + "gpui-pre-util", + "heapless", + "icu_segmenter", + "image", + "inventory", + "itertools 0.14.0", + "libc", + "log", + "lyon", + "num_cpus", + "objc2-core-foundation", + "objc2-core-video", + "parking", + "parking_lot", + "pin-project", + "pollster", + "profiling", + "rand 0.9.5", + "raw-window-handle", + "regex", + "resvg 0.46.0", + "schemars", + "seahash", + "serde", + "serde_json", + "slotmap", + "smallvec", + "spin 0.10.1", + "strum", + "taffy", + "thiserror 2.0.21", + "ttf-parser", + "unicode-segmentation", + "url", + "usvg 0.46.0", + "uuid", + "waker-fn", + "windows 0.62.2", + "zed-scap", +] + +[[package]] +name = "gpui-pre-apple" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a460605ec4e8cbcc9b213462a84b1d6cc3efe4ed8f0e18669659f07b2d5c6aec" +dependencies = [ + "anyhow", + "async-task", + "block2 0.6.2", + "cbindgen", + "core-foundation 0.10.1", + "core-foundation-sys", + "core-graphics 0.24.0", + "core-text", + "dispatch2", + "etagere", + "futures", + "gpui-pre", + "gpui-pre-collections", + "gpui-pre-util", + "image", + "libc", + "log", + "mach2", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-core-text", + "objc2-core-video", + "objc2-foundation 0.3.2", + "objc2-metal 0.3.2", + "objc2-quartz-core 0.3.2", + "objc2-security", + "parking_lot", + "pathfinder_geometry", + "smallvec", + "thiserror 2.0.21", + "zed-font-kit", +] + +[[package]] +name = "gpui-pre-collections" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb60ece401b399fd5ec13911915b407b0c6efa1f6c9a9f66201aaac56c7ec39d" +dependencies = [ + "gpui-pre-util", + "indexmap", + "rustc-hash 2.1.3", +] + +[[package]] +name = "gpui-pre-derive-refineable" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aeb2a3f5e6c9f300f432ae7989a81b262953ccfb3f9385be51cdc3303aef8858" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "gpui-pre-http-client" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55245de7e3b6e219596fd98bb29555d6e3fe44c055d025960affeb11b894c048" +dependencies = [ + "anyhow", + "bytes", + "futures", + "http", + "http-body", + "parking_lot", + "serde", + "serde_json", + "serde_urlencoded", + "url", +] + +[[package]] +name = "gpui-pre-ios" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10656f6db4bbedb60bd7fcbb8a70f7c7eeee45bc4582fb8933ca807d2ddf52ad" +dependencies = [ + "anyhow", + "block2 0.6.2", + "futures", + "gpui-pre", + "gpui-pre-apple", + "log", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-foundation 0.3.2", + "objc2-quartz-core 0.3.2", + "objc2-ui-kit", + "parking_lot", + "raw-window-handle", + "uuid", +] + +[[package]] +name = "gpui-pre-linux" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54bab418d0b7c027af9495f278619b46887362b3bd6446e246c87c5b0fa167df" +dependencies = [ + "accesskit", + "accesskit_unix", + "anyhow", + "as-raw-xcb-connection", + "ashpd", + "bytemuck", + "calloop", + "calloop-wayland-source", + "filedescriptor", + "futures", + "gpui-pre", + "gpui-pre-collections", + "gpui-pre-http-client", + "gpui-pre-util", + "gpui-pre-wgpu", + "libc", + "log", + "notify-rust", + "oo7", + "open", + "parking_lot", + "raw-window-handle", + "smallvec", + "smol", + "strum", + "url", + "uuid", + "wayland-backend", + "wayland-client", + "wayland-cursor", + "wayland-protocols", + "wayland-protocols-plasma", + "wayland-protocols-wlr", + "x11rb", + "xkbcommon", + "zed-scap", + "zed-xim", +] + +[[package]] +name = "gpui-pre-macos" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2de7d3f4ad51ac387e822495db363fcc1188d349793365731d054444c4cb06ee" +dependencies = [ + "accesskit", + "accesskit_macos", + "anyhow", + "block2 0.6.2", + "core-foundation 0.10.1", + "core-foundation-sys", + "core-graphics 0.24.0", + "ctor", + "dispatch2", + "foreign-types", + "futures", + "gpui-pre", + "gpui-pre-apple", + "gpui-pre-collections", + "gpui-pre-scheduler", + "gpui-pre-util", + "image", + "itertools 0.14.0", + "log", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-core-graphics", + "objc2-core-video", + "objc2-foundation 0.3.2", + "objc2-quartz-core 0.3.2", + "objc2-uniform-type-identifiers", + "objc2-user-notifications", + "parking_lot", + "raw-window-handle", + "semver", + "smallvec", + "strum", + "uuid", +] + +[[package]] +name = "gpui-pre-macros" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c7f67904eced61ed42ae3320a9b4b388aed893f8547fffe9161ec425ad162eb" +dependencies = [ + "heck 0.5.0", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "gpui-pre-platform" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2b785bdcbd229f762096aebf4f9ae913bdaa9db49794907cabaf516eb55227b" +dependencies = [ + "anyhow", + "console_error_panic_hook", + "gpui-pre", + "gpui-pre-ios", + "gpui-pre-linux", + "gpui-pre-macos", + "gpui-pre-web", + "gpui-pre-windows", +] + +[[package]] +name = "gpui-pre-refineable" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d197569e3b1fd84170f574bb866e95ecbc2625e7ae9443c877150eaf7d53374" +dependencies = [ + "gpui-pre-derive-refineable", +] + +[[package]] +name = "gpui-pre-reqwest" +version = "0.12.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05be23908e707966824f8c51a609904b7f30739e8d915e120a53c1b995ba964c" +dependencies = [ + "base64 0.22.1", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "ipnet", + "js-sys", + "log", + "mime", + "mime_guess", + "once_cell", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-native-certs", + "rustls-pemfile", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "system-configuration", + "tokio", + "tokio-rustls", + "tokio-socks", + "tokio-util", + "tower", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-streams", + "web-sys", + "windows-registry 0.4.0", +] + +[[package]] +name = "gpui-pre-scheduler" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1d3fa97eeb4ea233635b756684affe96db9e29fc2af8cdc269268bd7ff649fc" +dependencies = [ + "async-task", + "backtrace", + "chrono", + "flume", + "futures", + "parking_lot", + "rand 0.9.5", + "wasm_thread", + "web-time", +] + +[[package]] +name = "gpui-pre-shared-string" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63c2ed1e44195079d4461b83a99845aa9a52ce8fcf1516410816670a03d32d94" +dependencies = [ + "schemars", + "serde", + "smol_str", +] + +[[package]] +name = "gpui-pre-sum-tree" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6085e61458366decd21491396b0b90b3adf928f2ec27e170c4d3c9fba455d536" +dependencies = [ + "gpui-pre-ztracing", + "heapless", + "rayon", + "tracing", +] + +[[package]] +name = "gpui-pre-util" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ead57ff96b5a34e71d64758080347066d667e0c4bd4ff71e85f3a50643fa3e" +dependencies = [ + "anyhow", + "log", + "which", +] + +[[package]] +name = "gpui-pre-web" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a85e775c6874257c63f9cf51d99f0aba13b3bc615ed8d47e7d5338e3b5941f6a" +dependencies = [ + "anyhow", + "async-channel", + "console_error_panic_hook", + "futures", + "gpui-pre", + "gpui-pre-http-client", + "gpui-pre-scheduler", + "gpui-pre-wgpu", + "js-sys", + "log", + "parking_lot", + "raw-window-handle", + "smallvec", + "unicode-properties", + "unicode-script", + "unicode-segmentation", + "uuid", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm_thread", + "web-sys", + "web-time", +] + +[[package]] +name = "gpui-pre-wgpu" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc76608d385f6d0dcbad44504b1bf669dac3b7d7ffa2885acdb7c1c358caf852" +dependencies = [ + "anyhow", + "bytemuck", + "cosmic-text", + "etagere", + "gpui-pre", + "gpui-pre-collections", + "gpui-pre-util", + "itertools 0.14.0", + "log", + "parking_lot", + "profiling", + "raw-window-handle", + "smallvec", + "swash", + "unicode-bidi", + "unicode-segmentation", + "web-sys", + "wgpu", + "zed-font-kit", +] + +[[package]] +name = "gpui-pre-windows" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7201762420222d5b5ce5bdf0056996199212a3349bfb2457a3a0238c688681f3" +dependencies = [ + "accesskit", + "accesskit_windows", + "anyhow", + "dunce", + "etagere", + "futures", + "gpui-pre", + "gpui-pre-collections", + "gpui-pre-scheduler", + "gpui-pre-util", + "image", + "itertools 0.14.0", + "log", + "parking_lot", + "rand 0.9.5", + "raw-window-handle", + "smallvec", + "uuid", + "windows 0.62.2", + "windows-core 0.62.2", + "windows-numerics 0.3.1", + "windows-registry 0.6.1", +] + +[[package]] +name = "gpui-pre-ztracing" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1043bbe8d711baf03e774b03bae6521155f017450b866dcaa987a07d978d660a" +dependencies = [ + "gpui-pre-ztracing-macro", + "tracing", +] + +[[package]] +name = "gpui-pre-ztracing-macro" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85701631a9bbecf47e6d19f145e85e57767e7c50f7192d5b1bf65b5ef43cb373" + +[[package]] +name = "granit-parser" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e20f99e46474f56bd905c56e817ebddcf377a611f94c53ac4649e4d3fa3c0cd0" +dependencies = [ + "arraydeque", + "smallvec", +] + +[[package]] +name = "h2" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d29020232d6aa3fb1daca64c1127cf662cf97f254ae16c18c05b8ab635fc118" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "half" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" +dependencies = [ + "cfg-if", + "crunchy", + "num-traits", + "zerocopy", +] + +[[package]] +name = "harfrust" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9da2e5ae821f6e96664977bf974d6d6a2d6682f9ccee23e62ec1d134246845f9" +dependencies = [ + "bitflags 2.13.2", + "bytemuck", + "core_maths", + "read-fonts 0.37.0", + "smallvec", +] + +[[package]] +name = "hash32" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" +dependencies = [ + "byteorder", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +dependencies = [ + "foldhash", +] + +[[package]] +name = "heapless" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25ba4bd83f9415b58b4ed8dc5714c76e626a105be4646c02630ad730ad3b5aa4" +dependencies = [ + "hash32", + "stable_deref_trait", +] + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hkdf" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" +dependencies = [ + "hmac", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest 0.10.7", +] + +[[package]] +name = "html5ever" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c13771afe0e6e846f1e67d038d4cb29998a6779f93c809212e4e9c32efd244d4" +dependencies = [ + "log", + "mac", + "markup5ever", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "http" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23169fe34a5fbcdd3f3862e78fb9b6fccd5f02a6dc6f732547005d45631ce71c" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "hybrid-array" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" +dependencies = [ + "typenum", +] + +[[package]] +name = "hyper" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c3e324da4c95177d6291d4c8730197c0d1822f8a9766814a4a44fa5ab797c9c" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "h2", + "http", + "http-body", + "httparse", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfa8e654703247911e29c23fbeaa261834bd9bb74efba2f9acddc37bfb127f53" +dependencies = [ + "http", + "hyper", + "hyper-util", + "rustls", + "rustls-native-certs", + "tokio", + "tokio-rustls", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddc03d96684f9226b8a787cdb71488417b53ab5ea8fdb1dac946cb9431cc8bff" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "httparse", + "hyper", + "libc", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core 0.62.2", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "icu_collections" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe090a42a0d3140fed8c63263ed1fecc02422cb2f4bfe55cd8de2de1b8e105dd" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25d29b57c17c6528d0dc4c2141cfa2d08200465acb537a3909c8b9b8a4a984c0" +dependencies = [ + "displaydoc", + "litemap", + "serde", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_locale_fallback" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "406e9ae5b95d8aa37db6293405931b766a6172d1848daee22d70eb44aa78a43c" +dependencies = [ + "icu_locale_core", + "icu_locale_fallback_data", + "icu_provider", + "potential_utf", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_locale_fallback_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3632ebbd0c5c3637ba1a40bdf8288f2cb9d2268e73441a69d5fc73d7cfbaa87c" + +[[package]] +name = "icu_normalizer" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "404a3b8c486f205c87fe7f23ab6894d84a4ad76d6e4671ae1981ba7def87c599" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "utf16_iter", + "utf8_iter", + "write16", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20f0088bfb7441f7a37fa0c9e20cb1e9cba1cac6b9ce8cd2a6dc44294f8e8cbc" + +[[package]] +name = "icu_properties" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ea83d991c56158f7cf898bf86436c1dfe7c07ee82fe88951f2695c9f37a379f" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98f29af82d02b163fe50161388350e743bb3107cc8e8c2f7314cb505faddfcf" + +[[package]] +name = "icu_provider" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413099315636c588ee8f011190b329b971a09a8fae9d157e01dcae8379468514" +dependencies = [ + "displaydoc", + "icu_locale_core", + "serde", + "stable_deref_trait", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_segmenter" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61b195dc605f81aa181bec9b3bd14e26cc71b903e4644f6af578ca9772c7a710" +dependencies = [ + "icu_collections", + "icu_locale_fallback", + "icu_provider", + "icu_segmenter_data", + "potential_utf", + "smallvec", + "utf8_iter", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_segmenter_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "913a7d294ad056474749152107309fdee3ec17d2482d1ab70fdc61a81c64b9a3" + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "ignore" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "484be5657551d7868d67587df967fb4fcbe9d266a761cc855aaa148fda0b3cc7" +dependencies = [ + "crossbeam-deque", + "globset", + "log", + "memchr", + "regex-automata", + "same-file", + "walkdir", + "winapi-util", +] + +[[package]] +name = "image" +version = "0.25.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85ab80394333c02fe689eaf900ab500fbd0c2213da414687ebf995a65d5a6104" +dependencies = [ + "bytemuck", + "byteorder-lite", + "color_quant", + "exr", + "gif 0.14.2", + "image-webp", + "moxcms", + "num-traits", + "png 0.18.1", + "qoi", + "ravif", + "rayon", + "tiff", + "zune-core 0.5.3", + "zune-jpeg 0.5.15", +] + +[[package]] +name = "image-webp" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525e9ff3e1a4be2fbea1fdf0e98686a6d98b4d8f937e1bf7402245af1909e8c3" +dependencies = [ + "byteorder-lite", + "quick-error", +] + +[[package]] +name = "imagesize" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edcd27d72f2f071c64249075f42e205ff93c9a4c5f6c6da53e79ed9f9832c285" + +[[package]] +name = "imagesize" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e54e57b4c48b40f7aec75635392b12b3421fa26fe8b4332e63138ed278459c" + +[[package]] +name = "imgref" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e44b0a4eaa4c82f441d50a963f2d5f05a787240aeee097597033e72accfd22f" + +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", + "serde", + "serde_core", +] + +[[package]] +name = "inotify" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cc00ea907cab49550b7da656f80ebb97be1b997d931fbcd28d39734e17ce592" +dependencies = [ + "bitflags 2.13.2", + "inotify-sys", + "libc", +] + +[[package]] +name = "inotify-sys" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c033f80b2c113cdf91ab7a33faa9cbc014726dcad99880c8609af2a370edf37d" +dependencies = [ + "libc", +] + +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "block-padding", + "generic-array", +] + +[[package]] +name = "instant" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "interpolate_name" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34819042dc3d3971c46c2190835914dfbe0c3c13f61449b2997f4e9722dfa60" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "inventory" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6928282826c822ad91bf1c9a1cb90a30ba1c26770749929b4656cd6be829cd7c" +dependencies = [ + "rustversion", +] + +[[package]] +name = "ipnet" +version = "2.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" + +[[package]] +name = "is-docker" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "928bae27f42bc99b60d9ac7334e3a21d10ad8f1835a4e12ec3ec0464765ed1b3" +dependencies = [ + "once_cell", +] + +[[package]] +name = "is-wsl" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "173609498df190136aa7dea1a91db051746d339e18476eed5ca40521f02d7aa5" +dependencies = [ + "is-docker", + "once_cell", +] + +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" + +[[package]] +name = "jni-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41a652e1f9b6e0275df1f15b32661cf0d4b78d4d87ddec5e0c3c20f097433258" +dependencies = [ + "jni-sys 0.4.1", +] + +[[package]] +name = "jni-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6377a88cb3910bee9b0fa88d4f42e1d2da8e79915598f65fb0c7ee14c878af2" +dependencies = [ + "jni-sys-macros", +] + +[[package]] +name = "jni-sys-macros" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38c0b942f458fe50cdac086d2f946512305e5631e720728f2a61aabcd47a6264" +dependencies = [ + "quote", + "syn 2.0.119", +] + +[[package]] +name = "jobserver" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3" +dependencies = [ + "getrandom 0.4.3", + "libc", +] + +[[package]] +name = "js-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "khronos-egl" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6aae1df220ece3c0ada96b8153459b67eebe9ae9212258bb0134ae60416fdf76" +dependencies = [ + "libc", + "libloading", + "pkg-config", +] + +[[package]] +name = "khronos_api" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" + +[[package]] +name = "kqueue" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d763e5b24120b4ddf50de6c92308156765aabfbbccebf401da7cff2d70a41ea" +dependencies = [ + "kqueue-sys", + "libc", +] + +[[package]] +name = "kqueue-sys" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07293a4e297ac234359b510362495713f75ea345d5307140414f20c69ffeb087" +dependencies = [ + "bitflags 2.13.2", + "libc", +] + +[[package]] +name = "kurbo" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62026ae44756f8a599ba21140f350303d4f08dcdcc71b5ad9c9bb8128c13c62" +dependencies = [ + "arrayvec", + "euclid", + "smallvec", +] + +[[package]] +name = "kurbo" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b60dfc32f652b926df6192e55525b16d186c69d47876c3ead4da5cc9f8450e2" +dependencies = [ + "arrayvec", + "euclid", + "polycool", + "smallvec", +] + +[[package]] +name = "lazy_static" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" + +[[package]] +name = "lebe" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8" + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "libfuzzer-sys" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9fd2f41a1cba099f79a0b6b6c35656cf7c03351a7bae8ff0f28f25270f929d2" +dependencies = [ + "arbitrary", + "cc", +] + +[[package]] +name = "libloading" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" +dependencies = [ + "cfg-if", + "windows-link 0.2.1", +] + +[[package]] +name = "libm" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" + +[[package]] +name = "libredox" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ff90caf6077a803a240f62fdbe88645a890bbca49ef8174c3cb0404362171d" +dependencies = [ + "libc", +] + +[[package]] +name = "linebender_resource_handle" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4a5ff6bcca6c4867b1c4fd4ef63e4db7436ef363e0ad7531d1558856bae64f4" + +[[package]] +name = "link-section" +version = "0.19.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d9aae57673c1a235149777a74d152f3210ebd3e3b13346d2cdb6809e566bd3c" + +[[package]] +name = "linktime-proc-macro" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e57c38c1e860fd37c604281cdfb1dd2216977fd76a50f85ba2f388ef3219616" + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "litemap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" + +[[package]] +name = "litrs" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4744e383959f0db86ede514b809b1c53251889093803c05267acc7d4e7030d70" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" +dependencies = [ + "serde_core", +] + +[[package]] +name = "loop9" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fae87c125b03c1d2c0150c90365d7d6bcc53fb73a9acaef207d2d065860f062" +dependencies = [ + "imgref", +] + +[[package]] +name = "lru-slab" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4050469837a6ff301cd14c1f8f24f88549e6d548f24f64e2148eb0f72cebc51f" + +[[package]] +name = "lsp-types" +version = "0.97.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53353550a17c04ac46c585feb189c2db82154fc84b79c7a66c96c2c644f66071" +dependencies = [ + "bitflags 1.3.2", + "fluent-uri", + "serde", + "serde_json", + "serde_repr", +] + +[[package]] +name = "lyon" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd0578bdecb7d6d88987b8b2b1e3a4e2f81df9d0ece1078623324a567904e7b7" +dependencies = [ + "lyon_algorithms", + "lyon_tessellation", +] + +[[package]] +name = "lyon_algorithms" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3a6b9d4e8b48acd531fbf1241ad9b270df6e60b6b92bdea1469f4f40899d805" +dependencies = [ + "lyon_path", + "num-traits", +] + +[[package]] +name = "lyon_geom" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d82dd26aa4dc0b5b70b9115de59824842bcb43cf4a28e1b1e6d0ad148c93f93e" +dependencies = [ + "arrayvec", + "euclid", + "num-traits", +] + +[[package]] +name = "lyon_path" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c463f9c428b7fc5ec885dcd39ce4aa61e29111d0e33483f6f98c74e89d8621e" +dependencies = [ + "lyon_geom", + "num-traits", +] + +[[package]] +name = "lyon_tessellation" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd4872650e70dc907dd4f7ebc449d7d657a0d2658e423b2e724970870bbbf75b" +dependencies = [ + "float_next_after", + "lyon_path", + "num-traits", +] + +[[package]] +name = "mac" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4" + +[[package]] +name = "mac-notification-sys" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd604973958ddcc11b561193c0fb96ba146506ef2f231ef2e7c35fd2cbc9beca" +dependencies = [ + "cc", + "log", + "objc2 0.6.5", + "objc2-foundation 0.3.2", + "time", + "uuid", +] + +[[package]] +name = "mach2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dae608c151f68243f2b000364e1f7b186d9c29845f7d2d85bd31b9ad77ad552b" + +[[package]] +name = "malloc_buf" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" +dependencies = [ + "libc", +] + +[[package]] +name = "markdown" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5cab8f2cadc416a82d2e783a1946388b31654d391d1c7d92cc1f03e295b1deb" +dependencies = [ + "serde", + "unicode-id", +] + +[[package]] +name = "markup5ever" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16ce3abbeba692c8b8441d036ef91aea6df8da2c6b6e21c7e14d3c18e526be45" +dependencies = [ + "log", + "phf 0.11.3", + "phf_codegen", + "string_cache", + "string_cache_codegen", + "tendril", +] + +[[package]] +name = "markup5ever_rcdom" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edaa21ab3701bfee5099ade5f7e1f84553fd19228cf332f13cd6e964bf59be18" +dependencies = [ + "html5ever", + "markup5ever", + "tendril", + "xml5ever", +] + +[[package]] +name = "maybe-rayon" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519" +dependencies = [ + "cfg-if", + "rayon", +] + +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest 0.10.7", +] + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "memmap2" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b0ca1155a9ecc25aa12e8313a119f32edf0255fea2f44839c4ac0a12b2ac157" +dependencies = [ + "libc", +] + +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + +[[package]] +name = "mime" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5848f5a9e8117cf94f2f90faac24a87ff81600ff8628c1e3deaab572e76bfa1" + +[[package]] +name = "mime_guess" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" +dependencies = [ + "mime", + "unicase", +] + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "miniz_oxide" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "mio" +version = "1.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20521a903f02b0a573cd6840e9d0a5a0cc043d361fe924092cd70f8556cc9c4a" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "moxcms" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb85c154ba489f01b25c0d36ae69a87e4a1c73a72631fc6c0eb6dde34a73e44b" +dependencies = [ + "num-traits", + "pxfm", +] + +[[package]] +name = "multiversion" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb8a68be21d0755f83bfe47e79b896cc732687cf1903f0e50585e8d6ecc53076" +dependencies = [ + "multiversion-macros", + "target-features", +] + +[[package]] +name = "multiversion-macros" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a05e0c3c3e4edaa21d87c645852ed0b0c3663752bcc6ec43353bd6189dce35e1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "target-features", +] + +[[package]] +name = "multiversion_no_op" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d" + +[[package]] +name = "naga" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd0dd362bf26c0ffdd143df6da66a25d287e196eff482f117771010b207ad163" +dependencies = [ + "arrayvec", + "bit-set", + "bitflags 2.13.2", + "cfg-if", + "cfg_aliases", + "codespan-reporting", + "half", + "hashbrown 0.17.1", + "indexmap", + "libm", + "log", + "naga-types", + "num-traits", + "once_cell", + "rustc-hash 1.1.0", + "spirv", + "thiserror 2.0.21", + "unicode-ident", +] + +[[package]] +name = "naga-types" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14474170feb673cfb18b3565111a9869797cc1d290fb9ec969c53f5e1e231c6d" +dependencies = [ + "hashbrown 0.17.1", + "indexmap", + "rustc-hash 1.1.0", + "thiserror 2.0.21", +] + +[[package]] +name = "ndk-sys" +version = "0.6.0+11769913" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee6cda3051665f1fb8d9e08fc35c96d5a244fb1be711a03b71118828afc9a873" +dependencies = [ + "jni-sys 0.3.1", +] + +[[package]] +name = "new_debug_unreachable" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" + +[[package]] +name = "no_std_io2" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418abd1b6d34fbf6cae440dc874771b0525a604428704c76e48b29a5e67b8003" +dependencies = [ + "memchr", +] + +[[package]] +name = "nohash-hasher" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "nom" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405" +dependencies = [ + "memchr", +] + +[[package]] +name = "noop_proc_macro" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" + +[[package]] +name = "normpath" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b11ce00d2594068e8a27c9146fdc5cf9f3ac38eb42c7cd34d05fea618873ac9f" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "notify" +version = "8.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c41a04a5f59df9322cc657286dd2d79f90816119b22c5f847607c850110205ab" +dependencies = [ + "bitflags 2.13.2", + "inotify", + "kqueue", + "libc", + "log", + "mio", + "notify-types", + "objc2-core-foundation", + "objc2-core-services", + "walkdir", + "windows-sys 0.61.2", +] + +[[package]] +name = "notify-rust" +version = "4.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57da5b5d3b7809bf71858da5a2818c3404ca4688c02c8abecdda0fcdec235df3" +dependencies = [ + "futures-lite", + "log", + "mac-notification-sys", + "serde", + "tauri-winrt-notification", + "zbus 6.3.0", +] + +[[package]] +name = "notify-types" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbd044e85df3d939d8e32a1c35a3b737c7d0a4eeb7acd737bd207644e00f4e5f" +dependencies = [ + "bitflags 2.13.2", +] + +[[package]] +name = "ntapi" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3b335231dfd352ffb0f8017f3b6027a4917f7df785ea2143d8af2adc66980ae" +dependencies = [ + "winapi", +] + +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-bigint-dig" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7f9a86e097b0d187ad0e65667c2f58b9254671e86e7dbb78036b16692eae099" +dependencies = [ + "libm", + "num-integer", + "num-iter", + "num-traits", + "once_cell", + "rand 0.9.5", + "serde", + "smallvec", + "zeroize", +] + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-conv" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" + +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "num-integer" +version = "0.1.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c92800bd69a1eac91786bcfe9da64a897eb72911b8dc3095decbd07429e8048b" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", + "libm", +] + +[[package]] +name = "num_cpus" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "objc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" +dependencies = [ + "malloc_buf", + "objc_exception", +] + +[[package]] +name = "objc-foundation" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9" +dependencies = [ + "block", + "objc", + "objc_id", +] + +[[package]] +name = "objc-sys" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb91bdd390c7ce1a8607f35f3ca7151b65afc0ff5ff3b34fa350f7d7c7e4310" + +[[package]] +name = "objc2" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19d5490aaf8f1d7cf7688dfa9b0ce07900e168852c45cd2c03f534dfd27cfd0b" +dependencies = [ + "objc-sys", + "objc2-encode", +] + +[[package]] +name = "objc2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08849bbd4767dfae9457696856ae1c84fe4e0281bbe4a7abff2d0e06fb7981f8" +dependencies = [ + "objc2-encode", +] + +[[package]] +name = "objc2-app-kit" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4e89ad9e3d7d297152b17d39ed92cd50ca8063a89a9fa569046d41568891eff" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "libc", + "objc2 0.5.3", + "objc2-core-data 0.2.2", + "objc2-core-image 0.2.2", + "objc2-foundation 0.2.2", + "objc2-quartz-core 0.2.2", +] + +[[package]] +name = "objc2-app-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d49e936b501e5c5bf01fda3a9452ff86dc3ea98ad5f283e1455153142d97518c" +dependencies = [ + "bitflags 2.13.2", + "block2 0.6.2", + "libc", + "objc2 0.6.5", + "objc2-cloud-kit", + "objc2-core-data 0.3.2", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-core-image 0.3.2", + "objc2-core-text", + "objc2-core-video", + "objc2-foundation 0.3.2", + "objc2-quartz-core 0.3.2", + "objc2-uniform-type-identifiers", +] + +[[package]] +name = "objc2-cloud-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73ad74d880bb43877038da939b7427bba67e9dd42004a18b809ba7d87cee241c" +dependencies = [ + "bitflags 2.13.2", + "objc2 0.6.5", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-core-data" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "617fbf49e071c178c0b24c080767db52958f716d9eabdf0890523aeae54773ef" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "objc2 0.5.3", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "objc2-core-data" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b402a653efbb5e82ce4df10683b6b28027616a2715e90009947d50b8dd298fa" +dependencies = [ + "bitflags 2.13.2", + "objc2 0.6.5", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-core-foundation" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536" +dependencies = [ + "bitflags 2.13.2", + "block2 0.6.2", + "dispatch2", + "libc", + "objc2 0.6.5", +] + +[[package]] +name = "objc2-core-graphics" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e022c9d066895efa1345f8e33e584b9f958da2fd4cd116792e15e07e4720a807" +dependencies = [ + "bitflags 2.13.2", + "dispatch2", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-io-surface", +] + +[[package]] +name = "objc2-core-image" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55260963a527c99f1819c4f8e3b47fe04f9650694ef348ffd2227e8196d34c80" +dependencies = [ + "block2 0.5.1", + "objc2 0.5.3", + "objc2-foundation 0.2.2", + "objc2-metal 0.2.2", +] + +[[package]] +name = "objc2-core-image" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5d563b38d2b97209f8e861173de434bd0214cf020e3423a52624cd1d989f006" +dependencies = [ + "objc2 0.6.5", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-core-services" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "583300ad934cba24ff5292aee751ecc070f7ca6b39a574cc21b7b5e588e06a0b" +dependencies = [ + "libc", + "objc2-core-foundation", +] + +[[package]] +name = "objc2-core-text" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cde0dfb48d25d2b4862161a4d5fcc0e3c24367869ad306b0c9ec0073bfed92d" +dependencies = [ + "bitflags 2.13.2", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-core-graphics", +] + +[[package]] +name = "objc2-core-video" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d425caf1df73233f29fd8a5c3e5edbc30d2d4307870f802d18f00d83dc5141a6" +dependencies = [ + "bitflags 2.13.2", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-io-surface", + "objc2-metal 0.3.2", +] + +[[package]] +name = "objc2-encode" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33" + +[[package]] +name = "objc2-foundation" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee638a5da3799329310ad4cfa62fbf045d5f56e3ef5ba4149e7452dcf89d5a8" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "libc", + "objc2 0.5.3", +] + +[[package]] +name = "objc2-foundation" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3e0adef53c21f888deb4fa59fc59f7eb17404926ee8a6f59f5df0fd7f9f3272" +dependencies = [ + "bitflags 2.13.2", + "block2 0.6.2", + "libc", + "objc2 0.6.5", + "objc2-core-foundation", +] + +[[package]] +name = "objc2-io-surface" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180788110936d59bab6bd83b6060ffdfffb3b922ba1396b312ae795e1de9d81d" +dependencies = [ + "bitflags 2.13.2", + "objc2 0.6.5", + "objc2-core-foundation", +] + +[[package]] +name = "objc2-metal" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0cba1276f6023976a406a14ffa85e1fdd19df6b0f737b063b95f6c8c7aadd6" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "objc2 0.5.3", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "objc2-metal" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0125f776a10d00af4152d74616409f0d4a2053a6f57fa5b7d6aa2854ac04794" +dependencies = [ + "bitflags 2.13.2", + "block2 0.6.2", + "dispatch2", + "objc2 0.6.5", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-quartz-core" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e42bee7bff906b14b167da2bac5efe6b6a07e6f7c0a21a7308d40c960242dc7a" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "objc2 0.5.3", + "objc2-foundation 0.2.2", + "objc2-metal 0.2.2", +] + +[[package]] +name = "objc2-quartz-core" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96c1358452b371bf9f104e21ec536d37a650eb10f7ee379fff67d2e08d537f1f" +dependencies = [ + "bitflags 2.13.2", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-foundation 0.3.2", + "objc2-metal 0.3.2", +] + +[[package]] +name = "objc2-security" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "709fe137109bd1e8b5a99390f77a7d8b2961dafc1a1c5db8f2e60329ad6d895a" +dependencies = [ + "objc2-core-foundation", +] + +[[package]] +name = "objc2-ui-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d87d638e33c06f577498cbcc50491496a3ed4246998a7fbba7ccb98b1e7eab22" +dependencies = [ + "bitflags 2.13.2", + "block2 0.6.2", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-foundation 0.3.2", + "objc2-quartz-core 0.3.2", +] + +[[package]] +name = "objc2-uniform-type-identifiers" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7902ac02859fc1f7045f8b598c63f1ae0cc7efeaa06a9bc9f3d9a3c955974fa4" +dependencies = [ + "objc2 0.6.5", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-user-notifications" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9df9128cbbfef73cda168416ccf7f837b62737d748333bfe9ab71c245d76613e" +dependencies = [ + "bitflags 2.13.2", + "block2 0.6.2", + "objc2 0.6.5", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc_exception" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad970fb455818ad6cba4c122ad012fae53ae8b4795f86378bce65e4f6bab2ca4" +dependencies = [ + "cc", +] + +[[package]] +name = "objc_id" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b" +dependencies = [ + "objc", +] + +[[package]] +name = "object" +version = "0.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd229a0361b9d0d4396176e02d65897f487eebeab7caa6d443855ee152ca0b9c" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "oo7" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78f2bfed90f1618b4b48dcad9307f25e14ae894e2949642c87c351601d62cebd" +dependencies = [ + "aes", + "ashpd", + "async-fs", + "async-io", + "async-lock", + "blocking", + "cbc", + "cipher", + "digest 0.10.7", + "endi", + "futures-lite", + "futures-util", + "getrandom 0.4.3", + "hkdf", + "hmac", + "md-5", + "num", + "num-bigint-dig", + "pbkdf2", + "serde", + "serde_bytes", + "sha2 0.10.9", + "subtle", + "zbus 5.19.0", + "zbus_macros 5.19.0", + "zeroize", + "zvariant", +] + +[[package]] +name = "open" +version = "5.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d8c8b8e725ad26404d6896eb2f064294a0a7b4f1d9120210d85544684c4ca95" +dependencies = [ + "is-wsl", + "libc", +] + +[[package]] +name = "openssl-probe" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e036b411754132fd6b74e94b74e9a13c5cf606fa93d1885c35860d3060820b1" + +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + +[[package]] +name = "ordered-float" +version = "5.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f59006ce1e204c30956638535b74d317bca99477b9356dfaec54c37f6f600ab" +dependencies = [ + "num-traits", +] + +[[package]] +name = "ordered-stream" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aa2b01e1d916879f73a53d01d1d6cee68adbb31d6d9177a8cfce093cced1d50" +dependencies = [ + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link 0.2.1", +] + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pastey" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35fb2e5f958ec131621fdd531e9fc186ed768cbe395337403ae56c17a74c68ec" + +[[package]] +name = "pathfinder_geometry" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b7e7b4ea703700ce73ebf128e1450eb69c3a8329199ffbfb9b2a0418e5ad3" +dependencies = [ + "log", + "pathfinder_simd", +] + +[[package]] +name = "pathfinder_simd" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4500030c302e4af1d423f36f3b958d1aecb6c04184356ed5a833bf6b60435777" +dependencies = [ + "rustc_version", +] + +[[package]] +name = "pbkdf2" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" +dependencies = [ + "digest 0.10.7", + "hmac", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "phf" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078" +dependencies = [ + "phf_shared 0.11.3", +] + +[[package]] +name = "phf" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf" +dependencies = [ + "phf_macros", + "phf_shared 0.13.1", + "serde", +] + +[[package]] +name = "phf_codegen" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aef8048c789fa5e851558d709946d6d79a8ff88c0440c587967f8e94bfb1216a" +dependencies = [ + "phf_generator 0.11.3", + "phf_shared 0.11.3", +] + +[[package]] +name = "phf_generator" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d" +dependencies = [ + "phf_shared 0.11.3", + "rand 0.8.8", +] + +[[package]] +name = "phf_generator" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "135ace3a761e564ec88c03a77317a7c6b80bb7f7135ef2544dbe054243b89737" +dependencies = [ + "fastrand", + "phf_shared 0.13.1", +] + +[[package]] +name = "phf_macros" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812f032b54b1e759ccd5f8b6677695d5268c588701effba24601f6932f8269ef" +dependencies = [ + "phf_generator 0.13.1", + "phf_shared 0.13.1", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "phf_shared" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5" +dependencies = [ + "siphasher", +] + +[[package]] +name = "phf_shared" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266" +dependencies = [ + "siphasher", +] + +[[package]] +name = "pico-args" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" + +[[package]] +name = "pin-project" +version = "1.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2466b2336ed02bcdca6b294417127b90ec92038d1d5c4fbeac971a922e0e0924" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c96395f0a926bc13b1c17622aaddda1ecb55d49c8f1bf9777e4d877800a43f8b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "piper" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c835479a4443ded371d6c535cbfd8d31ad92c5d23ae9770a61bc155e4992a3c1" +dependencies = [ + "atomic-waker", + "fastrand", + "futures-io", +] + +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "png" +version = "0.17.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82151a2fc869e011c153adc57cf2789ccb8d9906ce52c0b39a6b5697749d7526" +dependencies = [ + "bitflags 1.3.2", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide 0.8.9", +] + +[[package]] +name = "png" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60769b8b31b2a9f263dae2776c37b1b28ae246943cf719eb6946a1db05128a61" +dependencies = [ + "bitflags 2.13.2", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide 0.8.9", +] + +[[package]] +name = "polling" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d0e4f59085d47d8241c88ead0f274e8a0cb551f3625263c05eb8dd897c34218" +dependencies = [ + "cfg-if", + "concurrent-queue", + "hermit-abi", + "pin-project-lite", + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "pollster" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f3a9f18d041e6d0e102a0a46750538147e5e8992d3b4873aaafee2520b00ce3" + +[[package]] +name = "polycool" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50596ddc09eb5ad5f75cacd40209568e66df71baf86e1499a0e99c4cff12a5a6" +dependencies = [ + "arrayvec", +] + +[[package]] +name = "portable-atomic" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85" + +[[package]] +name = "portable-atomic-util" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10ab3eb7f3becc3a1cbc4f2c6f20267996cfc1a6467a873763411b136a122715" +dependencies = [ + "portable-atomic", +] + +[[package]] +name = "potential_utf" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661" +dependencies = [ + "serde_core", + "writeable", + "zerovec", +] + +[[package]] +name = "powerfmt" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "precomputed-hash" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" + +[[package]] +name = "presser" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8cf8e6a8aa66ce33f63993ffc4ea4271eb5b0530a9002db8455ea6050c77bfa" + +[[package]] +name = "prettyplease" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bfe0f4c752e450fc2faf62654f1c134747922825d5b04ca717b8874f41a40c0" +dependencies = [ + "proc-macro2", + "syn 3.0.10", +] + +[[package]] +name = "proc-macro-crate" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e67ba7e9b2b56446f1d419b1d807906278ffa1a658a8a5d8a39dcb1f5a78614f" +dependencies = [ + "toml_edit 0.25.19+spec-1.1.0", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "profiling" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5" +dependencies = [ + "profiling-procmacros", +] + +[[package]] +name = "profiling-procmacros" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4488a4a36b9a4ba6b9334a32a39971f77c1436ec82c38707bce707699cc3bbcb" +dependencies = [ + "quote", + "syn 2.0.119", +] + +[[package]] +name = "pxfm" +version = "0.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea" + +[[package]] +name = "qoi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001" +dependencies = [ + "bytemuck", +] + +[[package]] +name = "quick-error" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" + +[[package]] +name = "quick-xml" +version = "0.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e660451e55124f798a69a5af3f49ccfbefbd41910eefd25caf2393e1f3473ec1" +dependencies = [ + "memchr", +] + +[[package]] +name = "quinn" +version = "0.11.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4051e23e9185c255a7e33ef59cdbca87a22d359052eecd22fc6b901fb37d9d11" +dependencies = [ + "bytes", + "cfg_aliases", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash 2.1.3", + "rustls", + "socket2", + "thiserror 2.0.21", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e750cca55fe4f0439a15d0bb529da9651e79993e8e72c61a899a36d462befbe" +dependencies = [ + "bytes", + "getrandom 0.4.3", + "lru-slab", + "rand 0.10.4", + "rand_pcg", + "ring", + "rustc-hash 2.1.3", + "rustls", + "rustls-pki-types", + "slab", + "thiserror 2.0.21", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af66907df18639dcf4db56ca65490cabc4b27a97dbadd96f2926cca73298f016" +dependencies = [ + "cfg_aliases", + "libc", + "once_cell", + "socket2", + "tracing", + "windows-sys 0.61.2", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.5", +] + +[[package]] +name = "rand" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f6fa277c67e6ea590466f04c3fe82cb509f0864fe470643e5570de9514a9b11" +dependencies = [ + "chacha20", + "getrandom 0.4.3", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "rand_pcg" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caa0f4137e1c0a72f4c651489402276c8e8e1cf081f3b0ba156d2cbeef09e86a" +dependencies = [ + "rand_core 0.10.1", +] + +[[package]] +name = "range-alloc" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca45419789ae5a7899559e9512e58ca889e41f04f1f2445e9f4b290ceccd1d08" + +[[package]] +name = "rangemap" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a611d15b50743feb4c76b7d03edcb0e64f399c26961e4efe6975bc398be6aa3d" + +[[package]] +name = "rav1e" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b6dd56e85d9483277cde964fd1bdb0428de4fec5ebba7540995639a21cb32b" +dependencies = [ + "aligned-vec", + "arbitrary", + "arg_enum_proc_macro", + "arrayvec", + "av-scenechange", + "av1-grain", + "bitstream-io", + "built", + "cfg-if", + "interpolate_name", + "itertools 0.14.0", + "libc", + "libfuzzer-sys", + "log", + "maybe-rayon", + "new_debug_unreachable", + "noop_proc_macro", + "num-derive", + "num-traits", + "paste", + "profiling", + "rand 0.9.5", + "rand_chacha 0.9.0", + "simd_helpers", + "thiserror 2.0.21", + "v_frame", + "wasm-bindgen", +] + +[[package]] +name = "ravif" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e52310197d971b0f5be7fe6b57530dcd27beb35c1b013f29d66c1ad73fbbcc45" +dependencies = [ + "avif-serialize", + "imgref", + "loop9", + "quick-error", + "rav1e", + "rayon", + "rgb", +] + +[[package]] +name = "raw-window-handle" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539" + +[[package]] +name = "raw-window-metal" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40d213455a5f1dc59214213c7330e074ddf8114c9a42411eb890c767357ce135" +dependencies = [ + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-foundation 0.3.2", + "objc2-quartz-core 0.3.2", +] + +[[package]] +name = "rayon" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "read-fonts" +version = "0.37.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b634fabf032fab15307ffd272149b622260f55974d9fad689292a5d33df02e5" +dependencies = [ + "bytemuck", + "core_maths", + "font-types 0.11.3", +] + +[[package]] +name = "read-fonts" +version = "0.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "046a7d674daf459825b32f5062056d6882db0d2f5a479fbd76ccfc870ac18709" +dependencies = [ + "bytemuck", + "font-types 0.12.7", + "once_cell", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags 2.13.2", +] + +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom 0.2.17", + "libredox", + "thiserror 1.0.69", +] + +[[package]] +name = "redox_users" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60dc65c0ff1a7ae1294b0c67b9f14baf70b644404010370171787bfac1038fc0" +dependencies = [ + "libredox", + "thiserror 2.0.21", +] + +[[package]] +name = "ref-cast" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e440fb4e4b4147295338efb76001ab9e4efc0e5839df2c47fc5ac2381d365c3" +dependencies = [ + "ref-cast-impl", +] + +[[package]] +name = "ref-cast-impl" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ecd8964f8453721699a1ed72037b0db49ce2f5a5138486ee89bed6f67cdf3a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "regex" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9807af0e638c12e753aa1b4e601791bf2a8f71274023c8bbeec12fc149950857" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d60ef07df457506a0a1a2073112eb4aae5733dbd2813c3da77d1b0fd441158" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d902046e6a28a1e4c3cab97258a0ce11e994903f01c2d97a9447a0b3df85f4f6" + +[[package]] +name = "renderdoc-sys" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832" + +[[package]] +name = "resvg" +version = "0.45.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8928798c0a55e03c9ca6c4c6846f76377427d2c1e1f7e6de3c06ae57942df43" +dependencies = [ + "gif 0.13.3", + "image-webp", + "log", + "pico-args", + "rgb", + "svgtypes 0.15.3", + "tiny-skia", + "usvg 0.45.1", + "zune-jpeg 0.4.21", +] + +[[package]] +name = "resvg" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b563218631706d614e23059436526d005b50ab5f2d506b55a17eb65c5eb83419" +dependencies = [ + "gif 0.14.2", + "image-webp", + "log", + "pico-args", + "rgb", + "svgtypes 0.16.2", + "tiny-skia", + "usvg 0.46.0", + "zune-jpeg 0.5.15", +] + +[[package]] +name = "rgb" +version = "0.8.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b34b781b31e5d73e9fbc8689c70551fd1ade9a19e3e28cfec8580a79290cc4" +dependencies = [ + "bytemuck", +] + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "ropey" +version = "2.0.0-beta.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4045a00dc327d084a2bbf126976e14125b54f23bd30511d45b842eba76c52d74" +dependencies = [ + "str_indices", +] + +[[package]] +name = "roxmltree" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97" + +[[package]] +name = "roxmltree" +version = "0.21.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9da7f59328b09405b181fee1018925fad50f157afb43c0c0f5c522c256dd5907" +dependencies = [ + "memchr", +] + +[[package]] +name = "rust-embed" +version = "8.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e98f580cf7a2863aeb6827800283f52cbe6fb3f3bb7c1843783f903c16dcb8ad" +dependencies = [ + "rust-embed-impl", + "rust-embed-utils", + "walkdir", +] + +[[package]] +name = "rust-embed-impl" +version = "8.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a93e2f69cf224a6c7df7509aa5ba4a23f56828ee41b040e92ac8762371eb9ba7" +dependencies = [ + "mime_guess", + "proc-macro2", + "quote", + "rust-embed-utils", + "shellexpand", + "syn 2.0.119", + "walkdir", +] + +[[package]] +name = "rust-embed-utils" +version = "8.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "915e33dccbe7deb2b8a342dc4c517503fe85e0230ddbae40a4fb0220493dad9b" +dependencies = [ + "globset", + "sha2 0.11.1", + "walkdir", +] + +[[package]] +name = "rust-i18n" +version = "4.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8502e73e6461c738659455c5d2cf95bc132760a6f203c717e66ee72b78906d7f" +dependencies = [ + "globwalk", + "regex", + "rust-i18n-macro", + "rust-i18n-support", + "smallvec", +] + +[[package]] +name = "rust-i18n-macro" +version = "4.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54a28dd9d46108d56b9f0c85b220908844233e561c03087890325104f90f2c6" +dependencies = [ + "glob", + "proc-macro2", + "quote", + "rust-i18n-support", + "serde", + "serde_json", + "syn 2.0.119", +] + +[[package]] +name = "rust-i18n-support" +version = "4.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e806c2a9701cbd6043992cd935c30cb1fdb128fa15e7b506f3b46f97045737c0" +dependencies = [ + "arc-swap", + "base62", + "foldhash", + "globwalk", + "itertools 0.11.0", + "normpath", + "serde", + "serde-saphyr", + "serde_json", + "siphasher", + "toml 0.8.23", + "triomphe", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b74b56ffa8bb2830709a538c2cbcae9aa062db0d2a42563bfb09bdaae44020eb" + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" +dependencies = [ + "bitflags 2.13.2", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls" +version = "0.23.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" +dependencies = [ + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-native-certs" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a073f5dc7469f984c52ad2752b63b0807745133b6de880b7b64c1ac4c48aec4" +dependencies = [ + "openssl-probe", + "rustls-pki-types", + "schannel", + "security-framework", +] + +[[package]] +name = "rustls-pemfile" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "rustls-pki-types" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "102f35f7de7b06d8c61f1cff6592f5253e0dea431c74867aad5dfd70f44663fb" +dependencies = [ + "web-time", + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a0760a887c14823fca5a1c970b01d47c328c696c0c64829d1df50e8f853fd5" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "rustybuzz" +version = "0.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3c7c96f8a08ee34eff8857b11b49b07d71d1c3f4e88f8a88d4c9e9f90b1702" +dependencies = [ + "bitflags 2.13.2", + "bytemuck", + "core_maths", + "log", + "smallvec", + "ttf-parser", + "unicode-bidi-mirroring", + "unicode-ccc", + "unicode-properties", + "unicode-script", +] + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "schannel" +version = "0.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cb23c424d8ee841bb4d813677ca237e2a610067d594fec130f87d6093495ab5" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "schemars" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab508826f74a77ca9d5aba6ff19b522583ee3eaf28a19384ff3d0e5835fadf6e" +dependencies = [ + "dyn-clone", + "indexmap", + "ref-cast", + "schemars_derive", + "serde", + "serde_json", +] + +[[package]] +name = "schemars_derive" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1c3a92094fa7d61aa124645844facb6b554dfc797136d0f5fd1f890e2bffc69" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals", + "syn 3.0.10", +] + +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "screencapturekit" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a5eeeb57ac94960cfe5ff4c402be6585ae4c8d29a2cf41b276048c2e849d64e" +dependencies = [ + "screencapturekit-sys", +] + +[[package]] +name = "screencapturekit-sys" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22411b57f7d49e7fe08025198813ee6fd65e1ee5eff4ebc7880c12c82bde4c60" +dependencies = [ + "block", + "dispatch", + "objc", + "objc-foundation", + "objc_id", + "once_cell", +] + +[[package]] +name = "seahash" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" + +[[package]] +name = "security-framework" +version = "3.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" +dependencies = [ + "bitflags 2.13.2", + "core-foundation 0.10.1", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "self_cell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab42ca02749e120097e328d91d415325bdf43b1c72c4c8badf37375fe40a813" + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" +dependencies = [ + "serde", + "serde_core", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde-saphyr" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8050abb251097357e24aff63ba2c52a6309ecb7d23a5474023df960a02694d8" +dependencies = [ + "annotate-snippets", + "base64 0.23.1", + "encoding_rs_io", + "granit-parser", + "nohash-hasher", + "num-traits", + "serde_core", + "smallvec", + "zmij", +] + +[[package]] +name = "serde_bytes" +version = "0.11.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5d440709e79d88e51ac01c4b72fc6cb7314017bb7da9eeff678aa94c10e3ea8" +dependencies = [ + "serde", + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "serde_derive_internals" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f852137cce035d6a4df67ccce505ff6b3e9fd3a10e3e52b24dc71e650bb1a9bd" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "foldhash", + "indexmap", + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_repr" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d3b1629de253c70a0508c3899572da79ca359fdab27c7920ff00406df418906" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "serde_spanned" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_spanned" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c187e0a369471c550af028cba56d4f4fa2a489ad28151ae95a4d340a2a8b782" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1_smol" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d" + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest 0.10.7", +] + +[[package]] +name = "sha2" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", +] + +[[package]] +name = "shellexpand" +version = "3.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32824fab5e16e6c4d86dc1ba84489390419a39f97699852b66480bb87d297ed8" +dependencies = [ + "dirs 6.0.0", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "simd-adler32" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" + +[[package]] +name = "simd_helpers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95890f873bec569a0362c235787f3aca6e1e887302ba4840839bcc6459c42da6" +dependencies = [ + "quote", +] + +[[package]] +name = "simdutf8" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" + +[[package]] +name = "similar" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa" +dependencies = [ + "bstr", + "unicode-segmentation", +] + +[[package]] +name = "simplecss" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a9c6883ca9c3c7c90e888de77b7a5c849c779d25d74a1269b0218b14e8b136c" +dependencies = [ + "log", +] + +[[package]] +name = "siphasher" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" + +[[package]] +name = "skia-bindings" +version = "0.157.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3930c7f5e8d813364d74c3b17b227f3bb3a84504f93bb1f8c222d8fd5a7d9c6f" +dependencies = [ + "bindgen", + "cc", + "flate2", + "heck 0.5.0", + "pkg-config", + "regex", + "serde_json", + "tar", + "toml 1.2.1+spec-1.1.0", +] + +[[package]] +name = "skia-safe" +version = "0.157.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1c946dbc1a10b6b1ace7e368ea71f96c81448bbf55effcad0a9a3a392358eb" +dependencies = [ + "bitflags 2.13.2", + "skia-bindings", +] + +[[package]] +name = "skrifa" +version = "0.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fbdfe3d2475fbd7ddd1f3e5cf8288a30eb3e5f95832829570cd88115a7434ac" +dependencies = [ + "bytemuck", + "read-fonts 0.37.0", +] + +[[package]] +name = "skrifa" +version = "0.44.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819ab7d62b1d3e72d9d9dea5650bac30424f9111364bb94928dbf5ecad1baa68" +dependencies = [ + "bytemuck", + "read-fonts 0.41.0", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "slotmap" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdd58c3c93c3d278ca835519292445cb4b0d4dc59ccfdf7ceadaab3f8aeb4038" +dependencies = [ + "version_check", +] + +[[package]] +name = "smallvec" +version = "1.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "149309498a5f7334b12ddae0cad8aba81ce785fe91755068867e017cf98fee46" + +[[package]] +name = "smol" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33bd3e260892199c3ccfc487c88b2da2265080acb316cd920da72fdfd7c599f" +dependencies = [ + "async-channel", + "async-executor", + "async-fs", + "async-io", + "async-lock", + "async-net", + "async-process", + "blocking", + "futures-lite", +] + +[[package]] +name = "smol_str" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eeb7109d23f319619d4cc0de5464613c8b7b1a09db2774a58101459cf6be2f10" +dependencies = [ + "serde_core", +] + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "spin" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e" +dependencies = [ + "lock_api", +] + +[[package]] +name = "spin" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "023a211cb3138dbc438680b32560ad89f699977624c9f8dbb95a47d5b4c07dd3" +dependencies = [ + "lock_api", +] + +[[package]] +name = "spirv" +version = "0.4.0+sdk-1.4.341.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9571ea910ebd84c86af4b3ed27f9dbdc6ad06f17c5f96146b2b671e2976744f" +dependencies = [ + "bitflags 2.13.2", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "str_indices" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d08889ec5408683408db66ad89e0e1f93dff55c73a4ccc71c427d5b277ee47e6" + +[[package]] +name = "streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2231b7c3057d5e4ad0156fb3dc807d900806020c5ffa3ee6ff2c8c76fb8520" + +[[package]] +name = "strict-num" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" +dependencies = [ + "float-cmp", +] + +[[package]] +name = "string_cache" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf776ba3fa74f83bf4b63c3dcbbf82173db2632ed8452cb2d891d33f459de70f" +dependencies = [ + "new_debug_unreachable", + "parking_lot", + "phf_shared 0.11.3", + "precomputed-hash", + "serde", +] + +[[package]] +name = "string_cache_codegen" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c711928715f1fe0fe509c53b43e993a9a557babc2d0a3567d0a3006f1ac931a0" +dependencies = [ + "phf_generator 0.11.3", + "phf_shared 0.11.3", + "proc-macro2", + "quote", +] + +[[package]] +name = "strum" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9628de9b8791db39ceda2b119bbe13134770b56c138ec1d3af810d045c04f9bd" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab85eea0270ee17587ed4156089e10b9e6880ee688791d45a905f5b1ca36f664" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "svg_fmt" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0193cc4331cfd2f3d2011ef287590868599a2f33c3e69bc22c1a3d3acf9e02fb" + +[[package]] +name = "svgtypes" +version = "0.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68c7541fff44b35860c1a7a47a7cadf3e4a304c457b58f9870d9706ece028afc" +dependencies = [ + "kurbo 0.11.3", + "siphasher", +] + +[[package]] +name = "svgtypes" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f12b12995e3638c70618c1facb1ce65302f6a6f706b675f07c58a5d7391120e" +dependencies = [ + "kurbo 0.13.1", + "siphasher", +] + +[[package]] +name = "sw-cli" +version = "0.1.0" +dependencies = [ + "flate2", + "getrandom 0.3.4", + "libc", + "serde", + "serde_json", + "sha2 0.10.9", + "sw-core", + "sw-host", + "sw-render", +] + +[[package]] +name = "sw-core" +version = "0.1.0" +dependencies = [ + "cc", + "flate2", + "getrandom 0.3.4", + "httparse", + "icu_normalizer", + "libc", + "pkg-config", + "rustix", + "rustls", + "rustls-native-certs", + "serde", + "serde_json", + "sha2 0.10.9", + "sw-quickjs", + "unicode-normalization", + "unicode-properties", + "unicode-segmentation", + "url", +] + +[[package]] +name = "sw-host" +version = "0.1.0" +dependencies = [ + "getrandom 0.3.4", + "libc", + "serde", + "serde_json", + "sha2 0.10.9", + "sw-core", + "sw-render", + "url", +] + +[[package]] +name = "sw-quickjs" +version = "0.1.0" +dependencies = [ + "cc", +] + +[[package]] +name = "sw-render" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "skia-safe", + "sw-core", + "sw-quickjs", +] + +[[package]] +name = "sw-studio" +version = "0.1.0" +dependencies = [ + "futures", + "getrandom 0.3.4", + "gpui-kit", + "image", + "libc", + "lsp-types", + "serde_json", + "smallvec", + "sw-core", + "sw-render", +] + +[[package]] +name = "swash" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c2499c2d826531388872b2268718aed907a39bd785ab0dcfe57fab26283f92e" +dependencies = [ + "skrifa 0.44.0", + "yazi", + "zeno", +] + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "sys-locale" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eab9a99a024a169fe8a903cf9d4a3b3601109bcc13bd9e3c6fff259138626c4" +dependencies = [ + "libc", +] + +[[package]] +name = "sysinfo" +version = "0.31.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "355dbe4f8799b304b05e1b0f05fc59b2a18d36645cf169607da45bde2f69a1be" +dependencies = [ + "core-foundation-sys", + "libc", + "memchr", + "ntapi", + "rayon", + "windows 0.57.0", +] + +[[package]] +name = "system-configuration" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" +dependencies = [ + "bitflags 2.13.2", + "core-foundation 0.9.4", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "taffy" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31f7bd2d97198df05186f93a3a3c306845f40e8b3c22bf9279cdd7e5cf5fea7d" +dependencies = [ + "serde", + "slotmap", + "smallvec", +] + +[[package]] +name = "tao-core-video-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "271450eb289cb4d8d0720c6ce70c72c8c858c93dd61fc625881616752e6b98f6" +dependencies = [ + "cfg-if", + "core-foundation-sys", + "libc", + "objc", +] + +[[package]] +name = "tar" +version = "0.4.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6221d9a6003c78398e3b239969f352578258df48c8eb051caadae0015bc840" +dependencies = [ + "filetime", + "libc", + "xattr", +] + +[[package]] +name = "target-features" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1bbb9f3c5c463a01705937a24fdabc5047929ac764b2d5b9cf681c1f5041ed5" + +[[package]] +name = "tauri-winrt-notification" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f37a6c354fd28fc9e322ed9bd47e3959576dad28c9d58ea1cf888cce1c7ccb36" +dependencies = [ + "thiserror 2.0.21", + "windows 0.62.2", + "windows-version", +] + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.3.4", + "once_cell", + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "tendril" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d24a120c5fc464a3458240ee02c299ebcb9d67b5249c8848b09d639dca8d7bb0" +dependencies = [ + "futf", + "mac", + "utf-8", +] + +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl 2.0.21", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "tiff" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63feaf3343d35b6ca4d50483f94843803b0f51634937cc2ec519fc32232bc52" +dependencies = [ + "fax", + "flate2", + "half", + "quick-error", + "weezl", + "zune-jpeg 0.5.15", +] + +[[package]] +name = "time" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134" +dependencies = [ + "deranged", + "num-conv", + "powerfmt", + "serde_core", + "time-core", +] + +[[package]] +name = "time-core" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109" + +[[package]] +name = "tiny-keccak" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" +dependencies = [ + "crunchy", +] + +[[package]] +name = "tiny-skia" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83d13394d44dae3207b52a326c0c85a8bf87f1541f23b0d143811088497b09ab" +dependencies = [ + "arrayref", + "arrayvec", + "bytemuck", + "cfg-if", + "log", + "png 0.17.16", + "tiny-skia-path", +] + +[[package]] +name = "tiny-skia-path" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c9e7fc0c2e86a30b117d0462aa261b72b7a99b7ebd7deb3a14ceda95c5bdc93" +dependencies = [ + "arrayref", + "bytemuck", + "strict-num", +] + +[[package]] +name = "tinystr" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abc840c238154d58f9e86b2792261edfde565a17696e1cda1a449de27979801a" +dependencies = [ + "displaydoc", + "serde_core", + "zerovec", +] + +[[package]] +name = "tinyvec" +version = "1.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee" + +[[package]] +name = "tokio" +version = "1.54.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3" +dependencies = [ + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9cc2678c2cdd569ef8215e2afd7954ada2ae20b4fdd2c5fe6139a3b02d105db" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-socks" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7e2948f60dbe26b35f2c7fb74ac2854c1fddded0fe9d7548fcc674a246f7615" +dependencies = [ + "either", + "futures-util", + "thiserror 1.0.69", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e464cf451ba96ebfc6f9b6542f17ee8b8956e33f1e40d9690624e59d7a7f8a4b" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "libc", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "toml" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" +dependencies = [ + "serde", + "serde_spanned 0.6.9", + "toml_datetime 0.6.11", + "toml_edit 0.22.27", +] + +[[package]] +name = "toml" +version = "1.2.1+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5755c51863cdac711f1956baf8eb9588e060755f7d3c4c979e70ea1e1f32cd7" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned 1.1.3", + "toml_datetime 1.1.3+spec-1.1.0", + "toml_parser", + "toml_writer", + "winnow 1.0.5", +] + +[[package]] +name = "toml_datetime" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_datetime" +version = "1.1.3+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "173f25c883cd2176a2d8212155638c0a60d484a9d84546d270866f40f56b6e49" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_edit" +version = "0.22.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" +dependencies = [ + "indexmap", + "serde", + "serde_spanned 0.6.9", + "toml_datetime 0.6.11", + "toml_write", + "winnow 0.7.15", +] + +[[package]] +name = "toml_edit" +version = "0.25.19+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd43688c4858d7264ae1cf9a9f874a67c3feb74a883b1cd7bebd6a4ea8307218" +dependencies = [ + "indexmap", + "toml_datetime 1.1.3+spec-1.1.0", + "toml_parser", + "winnow 1.0.5", +] + +[[package]] +name = "toml_parser" +version = "1.1.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c" +dependencies = [ + "winnow 1.0.5", +] + +[[package]] +name = "toml_write" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" + +[[package]] +name = "toml_writer" +version = "1.2.0+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "009dbb7700f11c55ef83aebbe4c0e9cf1f1ac0844bd8d6f00dc38f302026e13a" + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "tree-sitter" +version = "0.26.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17ebdd3a5a7e28a1890b876fdbd0c3c0fe0a6336cffaa104f11b9f720c9daa29" +dependencies = [ + "cc", + "regex", + "regex-syntax", + "serde_json", + "streaming-iterator", + "tree-sitter-language", +] + +[[package]] +name = "tree-sitter-javascript" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf40bf599e0416c16c125c3cec10ee5ddc7d1bb8b0c60fa5c4de249ad34dc1b1" +dependencies = [ + "cc", + "tree-sitter-language", +] + +[[package]] +name = "tree-sitter-json" +version = "0.24.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d727acca406c0020cffc6cf35516764f36c8e3dc4408e5ebe2cb35a947ec471" +dependencies = [ + "cc", + "tree-sitter-language", +] + +[[package]] +name = "tree-sitter-language" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0af592be68c579aa78a16846bd19422978c3c52e438523d45ff5d1bff1f9d4a" + +[[package]] +name = "triomphe" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d15f4f5898bcfc5d8a8e45ed30b2f108d710c1a2e5489dbf80e078f707181bc" +dependencies = [ + "arc-swap", + "serde", + "stable_deref_trait", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "ttf-parser" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31" +dependencies = [ + "core_maths", +] + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "uds_windows" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" +dependencies = [ + "memoffset", + "tempfile", + "windows-sys 0.61.2", +] + +[[package]] +name = "unicase" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357cc3acc6a036009fd6c973ed009037c732d60d0b4f6c673e9041497482a28f" + +[[package]] +name = "unicode-bidi" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" + +[[package]] +name = "unicode-bidi-mirroring" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfa6e8c60bb66d49db113e0125ee8711b7647b5579dc7f5f19c42357ed039fe" + +[[package]] +name = "unicode-ccc" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce61d488bcdc9bc8b5d1772c404828b17fc481c0a582b5581e95fb233aef503e" + +[[package]] +name = "unicode-id" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65dd75ab2d9d379dbe86d4059e91d8b5a6e7054865eaba1ddbfc7a817ff96392" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "unicode-linebreak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" + +[[package]] +name = "unicode-normalization" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75cd3fb03d5288b3ba9a1ee103ac7e9cbb59f0b03d64f8ddfcc04dd42758a85d" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833" + +[[package]] +name = "unicode-script" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa04c955db05429d464c74d495c1ee677e1866e783a4c3747f9c0cef7756cabf" + +[[package]] +name = "unicode-segmentation" +version = "1.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" + +[[package]] +name = "unicode-vo" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1d386ff53b415b7fe27b50bb44679e2cc4660272694b7b6f3326d8480823a94" + +[[package]] +name = "unicode-width" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca718471876992059f251e33aafc334346ada08042b7661dd1fe313a9bced557" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "usvg" +version = "0.45.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80be9b06fbae3b8b303400ab20778c80bbaf338f563afe567cf3c9eea17b47ef" +dependencies = [ + "base64 0.22.1", + "data-url", + "flate2", + "fontdb", + "imagesize 0.13.0", + "kurbo 0.11.3", + "log", + "pico-args", + "roxmltree 0.20.0", + "rustybuzz", + "simplecss", + "siphasher", + "strict-num", + "svgtypes 0.15.3", + "tiny-skia-path", + "unicode-bidi", + "unicode-script", + "unicode-vo", + "xmlwriter", +] + +[[package]] +name = "usvg" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e419dff010bb12512b0ae9e3d2f318dfbdf0167fde7eb05465134d4e8756076f" +dependencies = [ + "base64 0.22.1", + "data-url", + "flate2", + "fontdb", + "imagesize 0.14.0", + "kurbo 0.13.1", + "log", + "pico-args", + "roxmltree 0.21.2", + "rustybuzz", + "simplecss", + "siphasher", + "strict-num", + "svgtypes 0.16.2", + "tiny-skia-path", + "unicode-bidi", + "unicode-script", + "unicode-vo", + "xmlwriter", +] + +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + +[[package]] +name = "utf16_iter" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c7de35d9d8757dbc1f5125dc8b51438c68b4430546a735c3a9b91aaaaf9a18a" +dependencies = [ + "utf_types", +] + +[[package]] +name = "utf8_iter" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af3bd9d3e571c26fcce9d033b87ea5ab061b8d2ffe9b7a5675d810d68f03edf0" +dependencies = [ + "utf_types", +] + +[[package]] +name = "utf_types" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a3b425286980a367278453f3b6d28cdcb134b574e5a35824a6ff5eebad50bb6" + +[[package]] +name = "uuid" +version = "1.29.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c14b15dffa2f4bd712103e857140aa04a4f2ff6e955c77d7eb8e53ee02a3b64" +dependencies = [ + "getrandom 0.4.3", + "js-sys", + "serde_core", + "sha1_smol", + "wasm-bindgen", +] + +[[package]] +name = "v_frame" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "666b7727c8875d6ab5db9533418d7c764233ac9c0cff1d469aec8fa127597be2" +dependencies = [ + "aligned-vec", + "num-traits", + "wasm-bindgen", +] + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "vswhom" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be979b7f07507105799e854203b470ff7c78a1639e330a58f183b5fea574608b" +dependencies = [ + "libc", + "vswhom-sys", +] + +[[package]] +name = "vswhom-sys" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb067e4cbd1ff067d1df46c9194b5de0e98efd2810bbc95c5d5e5f25a3231150" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "waker-fn" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317211a0dc0ceedd78fb2ca9a44aed3d7b9b26f81870d485c07122b4350673b7" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "want" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec4cdd0dd910afe868b7ef477227d8d538b46b3075031afee8a9f2acb0a2ed0b" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0181c14ddd6c7b8b0b0132a5f1ea2f32be0d893cd3ea97b6e3b28886b8a3d1c" +dependencies = [ + "cfg-if", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e14e0327cb5368ad60ccb22d04a9827a693c95a926d8d125e9fa39e0baec501" +dependencies = [ + "js-sys", + "tokio", + "wasm-bindgen", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e931a02a39abdbf66966fc35902b5ed58344750a70885bed4ebb3d8eaf28ed9" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ca4b54280e455bd831e33a6b8e734baf432fe63fbc35c42ac14ce67a043a53a" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 3.0.10", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b1f9c3798725016c6ee5f2f57869ec0831e8bbc0ab0310a86e787e97f41389e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wasm-streams" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + +[[package]] +name = "wasm_thread" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7516db7f32decdadb1c3b8deb1b7d78b9df7606c5cc2f6241737c2ab3a0258e" +dependencies = [ + "futures", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wayland-backend" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38a91b4eaddff87b1cd1074985e3713da4af2c49742d1b356b2c01670a67a078" +dependencies = [ + "cc", + "downcast-rs", + "log", + "rustix", + "scoped-tls", + "smallvec", + "wayland-sys", +] + +[[package]] +name = "wayland-client" +version = "0.31.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3c36a0f861ad76d0901f2800b46321410d9f73f2ea88aac0650d86c32688073" +dependencies = [ + "bitflags 2.13.2", + "rustix", + "wayland-backend", + "wayland-scanner", +] + +[[package]] +name = "wayland-cursor" +version = "0.31.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a52d18780be9b1314328a3de5f930b73d2200112e3849ca6cb11822793fb34d" +dependencies = [ + "rustix", + "wayland-client", + "xcursor", +] + +[[package]] +name = "wayland-protocols" +version = "0.32.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d0c813de3daa2ed6520af85a3bd49b0e722a3078506899aa9686fea58dc4b6" +dependencies = [ + "bitflags 2.13.2", + "wayland-backend", + "wayland-client", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-plasma" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b6d8cf1eb2c1c31ed1f5643c88a6e53538129d4af80030c8cabd1f9fa884d91" +dependencies = [ + "bitflags 2.13.2", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-wlr" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb04e52f7836d7c7976c78ca0250d61e33873c34156a2a1fc9474828ec268234" +dependencies = [ + "bitflags 2.13.2", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-scanner" +version = "0.31.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "338e30461b3a2b67d70eb30a6d89f8e0c93a833e07d2ae89085cd070c4a00ac0" +dependencies = [ + "proc-macro2", + "quick-xml", + "quote", +] + +[[package]] +name = "wayland-sys" +version = "0.31.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8eab23fefc9e41f8e841df4a9c707e8a8c4ed26e944ef69297184de2785e3be" +dependencies = [ + "dlib", + "log", + "once_cell", + "pkg-config", +] + +[[package]] +name = "web-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7d179596ff5ec0f6de4db64d54011bde6289cc49603d3a6e4ce3f329207ab88" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "weezl" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28ac98ddc8b9274cb41bb4d9d4d5c425b6020c50c46f25559911905610b4a88" + +[[package]] +name = "wgpu" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146fc3d654250e6207921a66c87534c621bc886c70df83dcf466c1d66813810e" +dependencies = [ + "arrayvec", + "bitflags 2.13.2", + "bytemuck", + "cfg-if", + "cfg_aliases", + "document-features", + "hashbrown 0.17.1", + "js-sys", + "log", + "naga", + "parking_lot", + "portable-atomic", + "profiling", + "raw-window-handle", + "smallvec", + "static_assertions", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "wgpu-core", + "wgpu-hal", + "wgpu-types", +] + +[[package]] +name = "wgpu-core" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad661da2ee99f208c30090b585284b2ab5b8bf2358ddf2d2215592808ec62f1a" +dependencies = [ + "arrayvec", + "bit-set", + "bit-vec", + "bitflags 2.13.2", + "bytemuck", + "cfg_aliases", + "document-features", + "hashbrown 0.17.1", + "indexmap", + "log", + "naga", + "naga-types", + "once_cell", + "parking_lot", + "portable-atomic", + "profiling", + "raw-window-handle", + "rustc-hash 1.1.0", + "smallvec", + "thiserror 2.0.21", + "wgpu-core-deps-apple", + "wgpu-core-deps-emscripten", + "wgpu-core-deps-wasm", + "wgpu-core-deps-windows-linux-android", + "wgpu-hal", + "wgpu-naga-bridge", + "wgpu-types", +] + +[[package]] +name = "wgpu-core-deps-apple" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d3f2f24aee039ffe9ef527300daf6b7428c58edeefdaa1700f4fa0fd754df16" +dependencies = [ + "wgpu-hal", +] + +[[package]] +name = "wgpu-core-deps-emscripten" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55ccf093bd215ae9dc4f96df293e675513ac1f2569f8293a2911cc34ca3505d1" +dependencies = [ + "wgpu-hal", +] + +[[package]] +name = "wgpu-core-deps-wasm" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b20f43aeb9962d05e61e3ccb717e2e27ba1b04e979d4ca7e361384f8ee03db56" +dependencies = [ + "wgpu-hal", +] + +[[package]] +name = "wgpu-core-deps-windows-linux-android" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1331b08fa2362c5d454eccc4448e6e4f01eccfea569a00e310adbfc4415f2bdc" +dependencies = [ + "wgpu-hal", +] + +[[package]] +name = "wgpu-hal" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a57452fe453658e141aafdb09de1028afa498805fab518238ddf372a518813ff" +dependencies = [ + "android_system_properties", + "arrayvec", + "ash", + "bit-set", + "bitflags 2.13.2", + "block2 0.6.2", + "bytemuck", + "cfg-if", + "cfg_aliases", + "glow", + "glutin_wgl_sys", + "gpu-allocator", + "hashbrown 0.17.1", + "js-sys", + "khronos-egl", + "libc", + "libloading", + "log", + "naga", + "naga-types", + "ndk-sys", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-foundation 0.3.2", + "objc2-metal 0.3.2", + "objc2-quartz-core 0.3.2", + "once_cell", + "ordered-float", + "parking_lot", + "portable-atomic", + "portable-atomic-util", + "profiling", + "range-alloc", + "raw-window-handle", + "raw-window-metal", + "renderdoc-sys", + "smallvec", + "static_assertions", + "thiserror 2.0.21", + "wasm-bindgen", + "wayland-sys", + "web-sys", + "wgpu-naga-bridge", + "wgpu-types", + "windows 0.62.2", + "windows-core 0.62.2", + "windows-result 0.4.1", +] + +[[package]] +name = "wgpu-naga-bridge" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44a62ead68aad7cd40ac9e2cd2b8ec64f32dfbad2765f429b9fb74b8b735869f" +dependencies = [ + "naga", + "wgpu-types", +] + +[[package]] +name = "wgpu-types" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9f6ee0bd50f5ab79fb8c25e3f33afb27126f034af4ba5909c7a0967058659e6" +dependencies = [ + "bitflags 2.13.2", + "bytemuck", + "js-sys", + "log", + "naga-types", + "raw-window-handle", + "static_assertions", + "web-sys", +] + +[[package]] +name = "which" +version = "8.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bae2f2b2b816647a1cab1acc91f5bd20812d53cb344382635ec2181940c8034f" +dependencies = [ + "libc", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143" +dependencies = [ + "windows-core 0.57.0", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" +dependencies = [ + "windows-core 0.58.0", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows" +version = "0.61.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9babd3a767a4c1aef6900409f85f5d53ce2544ccdfaa86dad48c91782c6d6893" +dependencies = [ + "windows-collections 0.2.0", + "windows-core 0.61.2", + "windows-future 0.2.1", + "windows-link 0.1.3", + "windows-numerics 0.2.0", +] + +[[package]] +name = "windows" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580" +dependencies = [ + "windows-collections 0.3.2", + "windows-core 0.62.2", + "windows-future 0.3.2", + "windows-numerics 0.3.1", +] + +[[package]] +name = "windows-capture" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a4df73e95feddb9ec1a7e9c2ca6323b8c97d5eeeff78d28f1eccdf19c882b24" +dependencies = [ + "parking_lot", + "rayon", + "thiserror 2.0.21", + "windows 0.61.3", + "windows-future 0.2.1", +] + +[[package]] +name = "windows-collections" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8" +dependencies = [ + "windows-core 0.61.2", +] + +[[package]] +name = "windows-collections" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610" +dependencies = [ + "windows-core 0.62.2", +] + +[[package]] +name = "windows-core" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d" +dependencies = [ + "windows-implement 0.57.0", + "windows-interface 0.57.0", + "windows-result 0.1.2", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-core" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" +dependencies = [ + "windows-implement 0.58.0", + "windows-interface 0.58.0", + "windows-result 0.2.0", + "windows-strings 0.1.0", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-core" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3" +dependencies = [ + "windows-implement 0.60.2", + "windows-interface 0.59.3", + "windows-link 0.1.3", + "windows-result 0.3.4", + "windows-strings 0.4.2", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement 0.60.2", + "windows-interface 0.59.3", + "windows-link 0.2.1", + "windows-result 0.4.1", + "windows-strings 0.5.1", +] + +[[package]] +name = "windows-future" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e" +dependencies = [ + "windows-core 0.61.2", + "windows-link 0.1.3", + "windows-threading 0.1.0", +] + +[[package]] +name = "windows-future" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb" +dependencies = [ + "windows-core 0.62.2", + "windows-link 0.2.1", + "windows-threading 0.2.1", +] + +[[package]] +name = "windows-implement" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-implement" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-interface" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-interface" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-link" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-numerics" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1" +dependencies = [ + "windows-core 0.61.2", + "windows-link 0.1.3", +] + +[[package]] +name = "windows-numerics" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26" +dependencies = [ + "windows-core 0.62.2", + "windows-link 0.2.1", +] + +[[package]] +name = "windows-registry" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4286ad90ddb45071efd1a66dfa43eb02dd0dfbae1545ad6cc3c51cf34d7e8ba3" +dependencies = [ + "windows-result 0.3.4", + "windows-strings 0.3.1", + "windows-targets 0.53.5", +] + +[[package]] +name = "windows-registry" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02752bf7fbdcce7f2a27a742f798510f3e5ad88dbe84871e5168e2120c3d5720" +dependencies = [ + "windows-link 0.2.1", + "windows-result 0.4.1", + "windows-strings 0.5.1", +] + +[[package]] +name = "windows-result" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-result" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-result" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6" +dependencies = [ + "windows-link 0.1.3", +] + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link 0.2.1", +] + +[[package]] +name = "windows-strings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +dependencies = [ + "windows-result 0.2.0", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-strings" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87fa48cc5d406560701792be122a10132491cff9d0aeb23583cc2dcafc847319" +dependencies = [ + "windows-link 0.1.3", +] + +[[package]] +name = "windows-strings" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57" +dependencies = [ + "windows-link 0.1.3", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link 0.2.1", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link 0.2.1", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm 0.52.6", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link 0.2.1", + "windows_aarch64_gnullvm 0.53.1", + "windows_aarch64_msvc 0.53.1", + "windows_i686_gnu 0.53.1", + "windows_i686_gnullvm 0.53.1", + "windows_i686_msvc 0.53.1", + "windows_x86_64_gnu 0.53.1", + "windows_x86_64_gnullvm 0.53.1", + "windows_x86_64_msvc 0.53.1", +] + +[[package]] +name = "windows-threading" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b66463ad2e0ea3bbf808b7f1d371311c80e115c0b71d60efc142cafbcfb057a6" +dependencies = [ + "windows-link 0.1.3", +] + +[[package]] +name = "windows-threading" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37" +dependencies = [ + "windows-link 0.2.1", +] + +[[package]] +name = "windows-version" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4060a1da109b9d0326b7262c8e12c84df67cc0dbc9e33cf49e01ccc2eb63631" +dependencies = [ + "windows-link 0.2.1", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_i686_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + +[[package]] +name = "winnow" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945" +dependencies = [ + "memchr", +] + +[[package]] +name = "winnow" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c3b93199df0ab874cdf0c6a94f0a0dc3423fe41f1cafb62b8bd7b1deb277aef" +dependencies = [ + "memchr", +] + +[[package]] +name = "winreg" +version = "0.56.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d6f32a0ff4a9f6f01231eb2059cc85479330739333e0e58cadf03b6af2cca10" +dependencies = [ + "cfg-if", + "windows-sys 0.61.2", +] + +[[package]] +name = "wio" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d129932f4644ac2396cb456385cbf9e63b5b30c6e8dc4820bdca4eb082037a5" +dependencies = [ + "winapi", +] + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "write16" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936" + +[[package]] +name = "writeable" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" + +[[package]] +name = "x11" +version = "2.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "502da5464ccd04011667b11c435cb992822c2c0dbde1770c988480d312a0db2e" +dependencies = [ + "libc", + "pkg-config", +] + +[[package]] +name = "x11rb" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9993aa5be5a26815fe2c3eacfc1fde061fc1a1f094bf1ad2a18bf9c495dd7414" +dependencies = [ + "as-raw-xcb-connection", + "gethostname", + "libc", + "rustix", + "x11rb-protocol", + "xcursor", +] + +[[package]] +name = "x11rb-protocol" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea6fc2961e4ef194dcbfe56bb845534d0dc8098940c7e5c012a258bfec6701bd" + +[[package]] +name = "xattr" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "963d6fab92d5504b971dc8bd90ca743c2575e590bc8071ea5bc571598667ae08" +dependencies = [ + "libc", + "rustix", +] + +[[package]] +name = "xcb" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6c2ad15e0e922856ee89afe862b8992334bbe7953adad56cd1199358cb30566" +dependencies = [ + "bitflags 2.13.2", + "libc", + "quick-xml", + "x11", +] + +[[package]] +name = "xcursor" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "163b33ed8786455e2fa5d72f554057ce3f3182425434f756cd39c99839d88e23" + +[[package]] +name = "xim-ctext" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ac61a7062c40f3c37b6e82eeeef835d5cc7824b632a72784a89b3963c33284c" +dependencies = [ + "encoding_rs", +] + +[[package]] +name = "xim-parser" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dcee45f89572d5a65180af3a84e7ddb24f5ea690a6d3aa9de231281544dd7b7" +dependencies = [ + "bitflags 2.13.2", +] + +[[package]] +name = "xkbcommon" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d66ca9352cbd4eecbbc40871d8a11b4ac8107cfc528a6e14d7c19c69d0e1ac9" +dependencies = [ + "as-raw-xcb-connection", + "libc", + "memmap2", + "xkeysym", +] + +[[package]] +name = "xkeysym" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56" + +[[package]] +name = "xml-rs" +version = "0.8.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7" + +[[package]] +name = "xml5ever" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bbb26405d8e919bc1547a5aa9abc95cbfa438f04844f5fdd9dc7596b748bf69" +dependencies = [ + "log", + "mac", + "markup5ever", +] + +[[package]] +name = "xmlwriter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9" + +[[package]] +name = "y4m" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5a4b21e1a62b67a2970e6831bc091d7b87e119e7f9791aef9702e3bef04448" + +[[package]] +name = "yazi" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac366ea532fa8ee6a992cec9dea525b1dc284e69702f36c2ab5fbebadc81ec86" + +[[package]] +name = "yeslogic-fontconfig-sys" +version = "6.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d8b8abf912b9a29ff112e1671c97c33636903d13a69712037190e6805af4f76" +dependencies = [ + "dlib", + "once_cell", + "pkg-config", +] + +[[package]] +name = "yoke" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "417ef3fcf6c300b7dbd25c85531a746931c02ad536f3cf4786ed1e88223340b3" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec8ebde2db3681e8c9980cc27822030e68752690ddfa9473e739aeb4dbde6d71" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", + "synstructure", +] + +[[package]] +name = "zbus" +version = "5.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5db4be7c075cb421e4b7ee645541604239bd243ba7c357511f4ff3a74b555907" +dependencies = [ + "async-broadcast", + "async-executor", + "async-io", + "async-lock", + "async-process", + "async-recursion", + "async-task", + "async-trait", + "blocking", + "enumflags2", + "event-listener", + "futures-core", + "futures-lite", + "hex", + "libc", + "ordered-stream", + "rustix", + "serde", + "serde_repr", + "tracing", + "uds_windows", + "uuid", + "windows-sys 0.61.2", + "winnow 1.0.5", + "zbus_macros 5.19.0", + "zbus_names", + "zvariant", +] + +[[package]] +name = "zbus" +version = "6.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b4aae740b23927ceabed962431abab2cdefb9f7f885e5bbab062da09bf580e4" +dependencies = [ + "async-trait", + "bitflags 2.13.2", + "endi", + "futures-core", + "hex", + "libc", + "ordered-stream", + "rustix", + "serde", + "serde_repr", + "socket2", + "uds_windows", + "uuid", + "windows-sys 0.61.2", + "winnow 1.0.5", + "zbus_macros 6.3.0", + "zbus_utils", + "zcheapstr", + "zruntime", +] + +[[package]] +name = "zbus-lockstep" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6998de05217a084b7578728a9443d04ea4cd80f2a0839b8d78770b76ccd45863" +dependencies = [ + "zbus_xml", + "zvariant", +] + +[[package]] +name = "zbus-lockstep-macros" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10da05367f3a7b7553c8cdf8fa91aee6b64afebe32b51c95177957efc47ca3a0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "zbus-lockstep", + "zbus_xml", + "zvariant", +] + +[[package]] +name = "zbus_macros" +version = "5.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2990635d09ade6df1868f72f8cac69a876a90981e8bd3c40b1be413f8dc88f40" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 3.0.10", + "zbus_names", + "zvariant", + "zvariant_utils", +] + +[[package]] +name = "zbus_macros" +version = "6.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37c9dd4beaf145c16fcb2902862f50da86e54aed13dd368ddf64a1d17e6b8529" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", + "zbus_utils", +] + +[[package]] +name = "zbus_names" +version = "4.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812a12be4ead7a59e18ea8f047d08add0066b73bc62fc1467066a2472ae088dd" +dependencies = [ + "serde", + "winnow 1.0.5", + "zvariant", +] + +[[package]] +name = "zbus_utils" +version = "6.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af55e52da295311b368c45bcae221f020e28e97a2420360170ce389218e76ec7" +dependencies = [ + "proc-macro2", + "quote", + "serde", + "syn 3.0.10", + "winnow 1.0.5", +] + +[[package]] +name = "zbus_xml" +version = "5.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1586c021a01ca0a9216dcd874e546382e156a5cbab5fab6cb5f10087e22682a" +dependencies = [ + "serde", + "winnow 1.0.5", + "zbus_names", + "zvariant", +] + +[[package]] +name = "zcheapstr" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "939f186d9ecebe044315a5cb6d6f78568bbc21152adc954d5a0c2f58bdbc9920" +dependencies = [ + "serde", +] + +[[package]] +name = "zed-font-kit" +version = "0.14.1-zed" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3898e450f36f852edda72e3f985c34426042c4951790b23b107f93394f9bff5" +dependencies = [ + "bitflags 2.13.2", + "byteorder", + "core-foundation 0.10.1", + "core-graphics 0.24.0", + "core-text", + "dirs 5.0.1", + "dwrote", + "float-ord", + "freetype-sys", + "lazy_static", + "libc", + "log", + "pathfinder_geometry", + "pathfinder_simd", + "walkdir", + "winapi", + "yeslogic-fontconfig-sys", +] + +[[package]] +name = "zed-scap" +version = "0.0.8-zed" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b338d705ae33a43ca00287c11129303a7a0aa57b101b72a1c08c863f698ac8" +dependencies = [ + "anyhow", + "cocoa", + "core-graphics-helmer-fork", + "log", + "objc", + "rand 0.8.8", + "screencapturekit", + "screencapturekit-sys", + "sysinfo", + "tao-core-video-sys", + "windows 0.61.3", + "windows-capture", + "x11", + "xcb", +] + +[[package]] +name = "zed-xim" +version = "0.4.0-zed" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c0b46ed118eba34d9ba53d94ddc0b665e0e06a2cf874cfa2dd5dec278148642" +dependencies = [ + "ahash", + "hashbrown 0.14.5", + "log", + "x11rb", + "xim-ctext", + "xim-parser", +] + +[[package]] +name = "zeno" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6df3dc4292935e51816d896edcd52aa30bc297907c26167fec31e2b0c6a32524" + +[[package]] +name = "zerocopy" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "zerofrom" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f75b4683f6c7f45248d4d64056a24298c6281e0993356d7d1b4a1a962ef10d4a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85ccff8a4805aba1eaa4cd4afcd49f7557dd2cf5fd79ffc37f8959466effc5aa" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "zerotrie" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "zerovec" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f58a2799cfe9a1670c7ea8edbd52ef8edb93cb37a7f83bf31ba9e35d9dae6a40" +dependencies = [ + "serde", + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3743ddbf47738d929cd1ae2e199d40d0162979dfe31aef8ae646d2bf00bf97a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "zlib-rs" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" + +[[package]] +name = "zruntime" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6a55d4f521760475a30f7b96779508fe8c40dc834478709454dd1d5f0ebc72" +dependencies = [ + "futures-core", + "futures-io", + "rustix", + "socket2", + "windows-sys 0.61.2", +] + +[[package]] +name = "zune-core" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f423a2c17029964870cfaabb1f13dfab7d092a62a29a89264f4d36990ca414a" + +[[package]] +name = "zune-core" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b" + +[[package]] +name = "zune-inflate" +version = "0.2.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02" +dependencies = [ + "simd-adler32", +] + +[[package]] +name = "zune-jpeg" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29ce2c8a9384ad323cf564b67da86e21d3cfdff87908bc1223ed5c99bc792713" +dependencies = [ + "zune-core 0.4.12", +] + +[[package]] +name = "zune-jpeg" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27bc9d5b815bc103f142aa054f561d9187d191692ec7c2d1e2b4737f8dbd7296" +dependencies = [ + "zune-core 0.5.3", +] + +[[package]] +name = "zvariant" +version = "5.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80bf6d6cd6a4b601acb64cfb8284cdc643fcfa75ef26ae3c46ec8aa4e8a1fbb7" +dependencies = [ + "endi", + "enumflags2", + "serde", + "serde_bytes", + "winnow 1.0.5", + "zcheapstr", + "zvariant_derive", + "zvariant_utils", +] + +[[package]] +name = "zvariant_derive" +version = "5.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef2672456ba1cb54b5eaec136e63135b3c7495fb938e53b28a62e633496d486d" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 3.0.10", + "zvariant_utils", +] + +[[package]] +name = "zvariant_utils" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bad0294361a320b694a328460dc73add56c306150f5cb6bfafc44446120008a3" +dependencies = [ + "proc-macro2", + "quote", + "serde", + "syn 3.0.10", + "winnow 1.0.5", +] diff --git a/Linux/Cargo.toml b/Linux/Cargo.toml new file mode 100644 index 00000000..52b84e20 --- /dev/null +++ b/Linux/Cargo.toml @@ -0,0 +1,71 @@ +[workspace] +resolver = "3" +members = ["crates/sw-quickjs", "crates/sw-core", "crates/sw-render", "crates/sw-cli", "crates/sw-host", "crates/sw-studio"] + +[workspace.package] +version = "0.1.0" +edition = "2024" +license = "Apache-2.0" +publish = false + +[workspace.dependencies] +sw-quickjs = { path = "crates/sw-quickjs" } +cc = "1" +pkg-config = "0.3" +serde_json = { version = "1", features = ["preserve_order"] } +sha2 = "0.10" +getrandom = "0.3" +url = "2" +# The package layer (crates/sw-core/src/packages). Each one is small, widely +# used and maintained, and adds no C code: +# - rustix: openat2 with RESOLVE_BENEATH, openat, fstat and renameat2 as safe +# functions, so the package layer has no `unsafe` of its own. `libc` would +# need hand-written unsafe wrappers for the same calls. +# - flate2 with only the pure-Rust miniz_oxide backend: inflate and deflate. +# The ZIP container itself is read and written by the package layer: the +# central directory has to be checked byte by byte before anything is +# extracted anyway (preflight), and extracting through that same reading +# leaves no second parser that could disagree with it about an entry. +# - unicode-normalization, unicode-segmentation, unicode-properties (all from +# the unicode-rs project): NFC for name collisions, grapheme clusters for +# the package name limit, and the general category for control and format +# characters. The standard library has none of the three. +# - serde: already a dependency of serde_json; named here because the strict +# manifest reader (duplicate keys are an error) implements its visitor. +rustix = { version = "1", default-features = false, features = ["std", "fs"] } +flate2 = { version = "1", default-features = false, features = ["rust_backend"] } +unicode-normalization = "0.1" +unicode-segmentation = "1" +unicode-properties = { version = "0.1", default-features = false, features = ["general-category"] } +serde = "1" +libc = "0.2" +httparse = "1" +icu_normalizer = "2" +# The HTTP stack of fetch, $http, $weather and Data Source Plugins is a small +# HTTP/1.1 client in sw-core (api/http.rs) over std sockets and rustls, not a +# client library. Reasons: the request must go to the very address that was +# resolved and checked (never a second lookup inside a library), TLS must still +# be verified against the host name, redirects are followed by hand so every +# hop is checked again, each wait has its own timeout inside a total deadline, +# and the executor is synchronous, so a blocking client on worker threads fits. +# Client libraries that allow all of this (own resolver, no redirects, no +# proxy) do so through unstable hooks or bring an async runtime and a larger +# dependency tree. rustls with the ring provider needs only a C compiler (no +# OpenSSL, and no cmake as aws-lc-rs does); certificates are checked against +# the system's certificate store (rustls-native-certs), as a distribution +# package is expected to. httparse reads response heads; flate2 (pure Rust +# backend) decodes gzip and deflate bodies and reads and writes .swplugin +# archives. +rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] } +rustls-native-certs = "0.8" +sw-core = { path = "crates/sw-core" } +sw-render = { path = "crates/sw-render" } +sw-host = { path = "crates/sw-host" } +# The version and features the phase 0 check built: a prebuilt Skia is downloaded, nothing is compiled from source. +skia-safe = { version = "0.157.0", features = ["textlayout"] } + +# QuickJS is compiled with -O2 in every profile (see sw-quickjs/build.rs); the +# Rust side of a test build is optimized too, so that the parity run measures +# something close to what ships. +[profile.test] +opt-level = 2 diff --git a/Linux/crates/sw-cli/Cargo.toml b/Linux/crates/sw-cli/Cargo.toml new file mode 100644 index 00000000..6fb47046 --- /dev/null +++ b/Linux/crates/sw-cli/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "sw-cli" +description = "The ScriptWidget command line for Linux: create, check, run, render, pack and install widget packages" +version.workspace = true +edition.workspace = true +license.workspace = true +publish.workspace = true + +[[bin]] +name = "scriptwidget" +path = "src/main.rs" + +[dependencies] +sw-core.workspace = true +sw-render.workspace = true +sw-host.workspace = true +# The element tree of a report nests deeper than serde_json reads by default. +serde_json = { workspace = true, features = ["unbounded_depth"] } +serde.workspace = true +sha2.workspace = true +getrandom.workspace = true +libc.workspace = true + +[dev-dependencies] +# The hand-written archives of sw-core's package tests (tests/packages_support) are reused here. +flate2.workspace = true +serde_json.workspace = true diff --git a/Linux/crates/sw-cli/src/arguments.rs b/Linux/crates/sw-cli/src/arguments.rs new file mode 100644 index 00000000..318a200b --- /dev/null +++ b/Linux/crates/sw-cli/src/arguments.rs @@ -0,0 +1,156 @@ +//! A small hand-written argument parser: `--name value`, `--name=value`, +//! `--flag`, positional arguments, and `--` to end the options. Each command +//! names the options it takes; anything else is a usage error, so a typing +//! mistake is never silently ignored. + +use std::collections::HashMap; + +use crate::report::Failure; + +/// What one command accepts. +pub struct Shape { + /// Options that take a value, without the dashes. + pub values: &'static [&'static str], + /// Options that take none. + pub flags: &'static [&'static str], + pub minimum_positionals: usize, + pub maximum_positionals: usize, + /// What the positional arguments are, for the message when one is missing or extra. + pub positionals: &'static str, +} + +#[derive(Debug, Default)] +pub struct Arguments { + values: HashMap<&'static str, String>, + flags: Vec<&'static str>, + positionals: Vec, +} + +/// `--format json` is accepted by every command, as the Mac command line +/// takes it; JSON is the only format here. +const FORMAT: &str = "format"; + +impl Arguments { + pub fn parse(arguments: &[String], shape: &Shape) -> Result { + let mut parsed = Self::default(); + let mut options_ended = false; + let mut index = 0; + while index < arguments.len() { + let argument = &arguments[index]; + index += 1; + if options_ended || !argument.starts_with("--") { + // A negative number is a value (`move -20 40`), not an option. + let negative_number = argument.len() > 1 && argument[1..].bytes().all(|byte| byte.is_ascii_digit() || byte == b'.') && argument.as_bytes()[1].is_ascii_digit(); + if !options_ended && argument.len() > 1 && argument.starts_with('-') && !negative_number { + return Err(Failure::usage(format!("Unknown option {argument}"))); + } + parsed.positionals.push(argument.clone()); + continue; + } + if argument == "--" { + options_ended = true; + continue; + } + let (name, inline) = match argument[2..].split_once('=') { + Some((name, value)) => (name, Some(value.to_owned())), + None => (&argument[2..], None), + }; + if let Some(flag) = shape.flags.iter().find(|flag| **flag == name) { + if inline.is_some() { + return Err(Failure::usage(format!("--{name} takes no value"))); + } + if !parsed.flags.contains(flag) { + parsed.flags.push(flag); + } + continue; + } + let Some(known) = shape.values.iter().copied().chain([FORMAT]).find(|value| *value == name) else { + return Err(Failure::usage(format!("Unknown option --{name}"))); + }; + let value = match inline { + Some(value) => value, + None => { + let value = arguments.get(index).ok_or_else(|| Failure::usage(format!("--{name} needs a value")))?; + index += 1; + value.clone() + } + }; + if parsed.values.insert(known, value).is_some() { + return Err(Failure::usage(format!("--{name} was given twice"))); + } + } + if parsed.positionals.len() < shape.minimum_positionals { + return Err(Failure::usage(format!("Missing {}", shape.positionals))); + } + if parsed.positionals.len() > shape.maximum_positionals { + return Err(Failure::usage(if shape.maximum_positionals == 0 { "This command takes no arguments".to_owned() } else { format!("Too many arguments: expected {}", shape.positionals) })); + } + if parsed.value(FORMAT).is_some_and(|format| format != "json") { + return Err(Failure::usage("--format must be json: reports are always JSON")); + } + Ok(parsed) + } + + pub fn value(&self, name: &str) -> Option<&str> { + self.values.get(name).map(String::as_str) + } + + pub fn flag(&self, name: &str) -> bool { + self.flags.contains(&name) + } + + pub fn positional(&self, index: usize) -> Option<&str> { + self.positionals.get(index).map(String::as_str) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const SHAPE: Shape = Shape { values: &["family", "output"], flags: &["dark"], minimum_positionals: 1, maximum_positionals: 2, positionals: "a package folder" }; + + fn parse(arguments: &[&str]) -> Result { + Arguments::parse(&arguments.iter().map(|text| text.to_string()).collect::>(), &SHAPE) + } + + #[test] + fn reads_values_flags_and_positionals_in_any_order() { + let parsed = parse(&["--family", "large", "pkg", "--dark", "--output=a=b.png", "second"]).unwrap(); + assert_eq!(parsed.value("family"), Some("large")); + assert_eq!(parsed.value("output"), Some("a=b.png")); + assert!(parsed.flag("dark")); + assert_eq!((parsed.positional(0), parsed.positional(1), parsed.positional(2)), (Some("pkg"), Some("second"), None)); + } + + #[test] + fn a_double_dash_ends_the_options() { + let parsed = parse(&["--", "--dark", "-x"]).unwrap(); + assert!(!parsed.flag("dark")); + assert_eq!((parsed.positional(0), parsed.positional(1)), (Some("--dark"), Some("-x"))); + } + + #[test] + fn mistakes_are_usage_errors() { + for arguments in [ + &["pkg", "--unknown", "1"][..], + &["pkg", "-f"], + &["pkg", "--family"], + &["pkg", "--family", "a", "--family", "b"], + &["pkg", "--dark=yes"], + &[], + &["a", "b", "c"], + &["pkg", "--format", "text"], + ] { + let failure = parse(arguments).expect_err(&format!("{arguments:?}")); + assert_eq!((failure.exit, failure.code.as_str()), (crate::report::EXIT_USAGE, "usage"), "{arguments:?}"); + } + assert!(parse(&["pkg", "--format", "json"]).is_ok()); + assert_eq!(parse(&["-"]).unwrap().positional(0), Some("-")); + // A negative number is a value. + let parsed = parse(&["-20", "-4.5"]).unwrap(); + assert_eq!((parsed.positional(0), parsed.positional(1)), (Some("-20"), Some("-4.5"))); + assert!(parse(&["-.5"]).is_err()); + assert!(parse(&["-2x"]).is_err()); + } +} diff --git a/Linux/crates/sw-cli/src/commands.rs b/Linux/crates/sw-cli/src/commands.rs new file mode 100644 index 00000000..bf424192 --- /dev/null +++ b/Linux/crates/sw-cli/src/commands.rs @@ -0,0 +1,849 @@ +//! The commands. Each one returns its report, or the failure that stopped it +//! before it had one. Ported from `Windows/src/ScriptWidget.Cli/Program.cs`; +//! the report envelope and the output rules follow the Mac command line +//! (`Tools/ScriptWidgetCLI`). +//! +//! Everything a command is pointed at is untrusted: a package folder is +//! verified as a whole before anything in it is compiled, a present manifest +//! that cannot be read fails the command, and archives go through the +//! package store's preflight and staging. + +use std::cell::RefCell; +use std::collections::HashSet; +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use serde_json::{Value, json}; +use sha2::{Digest, Sha256}; +use sw_core::compiler::PrepareOptions; +use sw_core::json::ObjectExt; +use sw_core::library::{FAMILIES, Library}; +use sw_core::packages::limits::{MAX_FILE_BYTES, MAX_METADATA_BYTES}; +use sw_core::packages::{self, ArchiveLimits, DiskPackageFiles, PackageError, codes, manifests, names, path_rules}; +use sw_core::{DataPaths, MemoryPackageFiles, PackageCompiler, PackageFiles, Resources, RunRequest, RunResult, SharedScripts, TimelinePlanner, WidgetRuntime}; +use sw_render::{Presentation, RenderOutput, WidgetRenderer}; + +use crate::arguments::{Arguments, Shape}; +use crate::output; +use crate::report::{EXIT_FAILED, Failure, Report}; + +pub const USAGE: &str = "\ +Usage: scriptwidget [options] + +Package commands (take a package folder): + new [--template ] [--directory ] + Create a package folder from the starter or a bundled template. + check Check widget.json and compile the package; nothing is executed. + run [options] Run the script and print the element tree, logs and errors. + render --output [opts] Run the script and draw the widget to a PNG file. + timeline [--now ] Print the current timeline slot and the next transition. + pack [--output ] [--force] + Write the package as a .swt archive (default: .swt here). + +Options for run and render: + --family small, medium, large or extraLarge (default medium) + --param value of $getenv(\"widget-param\") + --dark run and draw in the dark theme (also --appearance dark) + --scale pixels per point for render, 0.5 to 8 (default 2) + --now fixed clock for dates in render (milliseconds since 1970) + --data data folder for storage and caches (default: a temporary folder) + --fail-on-warning exit 1 when a warning was reported (check, run, render) + +Library commands (use your data folder, or --data ): + install Install an archive or a package folder into the library. + list List installed packages. + templates List the bundled templates that run on Linux. + doctor Check this installation. + +Desktop commands (need the background service): + add [--family ] Put a widget of an installed package on the desktop. + remove Take a widget off the desktop. + widgets List the widgets on the desktop. + refresh [] [--package ] Run one widget, the widgets of a package, or all of them again. + dev [--family ] [--adopt] + Copy a package folder into the library whenever it changes and + refresh its widgets; prints one JSON line each time. + peek [on|off|toggle] Raise the widgets above the windows, or put them back. + arrange [on|off|toggle] Turn arrange mode on or off. + +For the display (see docs/linux/protocol.md): + watch Print the display events, one JSON object a line. + tap [] Region n of a widget was tapped. + move A widget was dragged to a new place. + screen + A screen's name, scale and logical size. + theme The theme of the desktop. + menu resize-small, resize-medium, resize-large, resize-extraLarge, + edit, remove or refresh. + session The session was locked or unlocked. + +The background service: + host run Run the service in this session until it is told to stop. + Usually started by systemd: systemctl --user start scriptwidget.service + +Every command except watch, dev and host prints one JSON report on standard +output; errors and warnings also go to standard error. Exit codes: 0 passed, +1 the package is at fault, 2 usage or environment, 3 internal error, +69 the background service is not running. +"; + +// What every command needs + +pub(crate) fn resources() -> Result { + Resources::locate().map_err(|error| Failure::environment("tool.resources_missing", error.to_string())) +} + +fn shared_scripts(resources: &Resources) -> Result, Failure> { + SharedScripts::new(&resources.runtime).map(Arc::new).map_err(|error| Failure::environment("tool.resources_missing", error.to_string())) +} + +fn compiler(scripts: &Arc) -> Result { + PackageCompiler::new(scripts.clone()).map_err(|error| Failure::internal(format!("The compiler could not be started: {error}"))) +} + +fn runtime(scripts: &Arc, paths: &DataPaths) -> Result { + WidgetRuntime::new(scripts.clone(), paths).map_err(|error| Failure::internal(format!("The runtime could not be started: {error}"))) +} + +/// The data folder of the user, or the one `--data` names. +pub(crate) fn user_data(arguments: &Arguments) -> Result { + DataPaths::new(arguments.value("data").map(Path::new)).map_err(|error| Failure::environment("tool.data_directory", format!("The data folder cannot be used: {error}"))) +} + +/// Where a run keeps `$storage` and caches: the folder `--data` names, or a private temporary one that is removed afterwards. +struct RunData { + paths: DataPaths, + temporary: Option, +} + +impl RunData { + fn new(arguments: &Arguments) -> Result { + if arguments.value("data").is_some() { + return Ok(Self { paths: user_data(arguments)?, temporary: None }); + } + let unusable = |error: std::io::Error| Failure::environment("tool.data_directory", format!("No temporary data folder could be created: {error}")); + let mut token = [0u8; 12]; + getrandom::fill(&mut token).map_err(|error| Failure::internal(format!("No random source: {error}")))?; + let directory = std::env::temp_dir().join(format!("scriptwidget-cli-{}", hex(&token))); + // Created exclusively and private: nobody else can have put anything at this name. + std::os::unix::fs::DirBuilderExt::mode(&mut std::fs::DirBuilder::new(), 0o700).create(&directory).map_err(unusable)?; + let data = Self { paths: DataPaths::at(&directory).map_err(unusable)?, temporary: Some(directory) }; + DataPaths::new(Some(data.paths.root())).map_err(unusable)?; + Ok(data) + } +} + +impl Drop for RunData { + fn drop(&mut self) { + if let Some(directory) = &self.temporary { + let _ = std::fs::remove_dir_all(directory); + } + } +} + +fn hex(bytes: &[u8]) -> String { + bytes.iter().map(|byte| format!("{byte:02x}")).collect() +} + +fn round(value: f64, digits: i32) -> f64 { + let factor = 10f64.powi(digits); + (value * factor).round() / factor +} + +// The package stage + +/// The absolute path of a package folder the user named. It must be a directory itself, not a link to one. +pub(crate) fn package_directory(text: &str) -> Result { + let directory = std::path::absolute(Path::new(text)).unwrap_or_else(|_| PathBuf::from(text)); + match std::fs::symlink_metadata(&directory) { + Ok(metadata) if metadata.is_dir() => Ok(directory), + _ => Err(Failure::environment("input.directory", format!("Not a package folder: {} (it must be an existing directory, not a symbolic link)", directory.display()))), + } +} + +struct Inspected { + files: Arc, + /// The parsed manifest, when the package has one. + manifest: Option, +} + +/// `ScriptWidgetSource/v1`, the fingerprint the Mac command line and Web Studio report for the same files. +fn source_hash(files: &DiskPackageFiles, paths: &[String]) -> Option { + let mut digest = Sha256::new(); + digest.update(b"ScriptWidgetSource/v1\n"); + digest.update((paths.len() as u64).to_be_bytes()); + for path in paths { + let bytes = files.read_bytes_up_to(path, MAX_FILE_BYTES)?; + digest.update((path.len() as u64).to_be_bytes()); + digest.update(path.as_bytes()); + digest.update((bytes.len() as u64).to_be_bytes()); + digest.update(&bytes); + } + Some(hex(&digest.finalize())) +} + +/// Verifies the folder as a whole and validates its manifest; nothing is compiled or executed. +fn inspect(report: &mut Report, directory: &Path, compiler: &PackageCompiler) -> Result { + report.set("package", json!(directory.display().to_string())); + packages::validate_source_tree(directory, &ArchiveLimits::default())?; + let has_manifest = packages::manifest_present(directory)?; + let files = DiskPackageFiles::new(directory); + let paths = files.list()?; + if let Some(hash) = source_hash(&files, &paths) { + report.set("sourceHash", json!(hash)); + report.set("sourceHashAlgorithm", json!("ScriptWidgetSource/v1")); + } + if !has_manifest { + report.set("manifest", Value::Null); + if !files.exists(manifests::LEGACY_ENTRY) { + return Err(PackageError::new(codes::MISSING_ENTRY, "The folder has neither widget.json nor main.jsx.").into()); + } + return Ok(Inspected { files: Arc::new(files), manifest: None }); + } + // A present manifest is authoritative: unreadable is an error, never "legacy". + let invalid = || Failure::new(EXIT_FAILED, codes::INVALID_MANIFEST, "widget.json is invalid."); + let text = files.read_bytes_up_to(manifests::FILE_NAME, MAX_METADATA_BYTES as u64).and_then(|bytes| String::from_utf8(bytes).ok()).ok_or_else(invalid)?; + let validation = compiler.validate_manifest(&text, &paths).map_err(|error| Failure::internal(format!("The manifest validator failed: {error}")))?; + report.diagnostics(validation.opt_array("issues").cloned().unwrap_or_default()); + let valid = validation.opt_bool("ok", false); + let first_error = validation.opt_array("issues").and_then(|issues| issues.iter().filter_map(Value::as_object).find(|issue| issue.opt_string("severity") == "error").cloned()); + let manifest = validation.opt_object("manifest").cloned(); + // What the decoder said when the text is not a manifest at all (not JSON, unknown fields, a wrong type). + let undecodable = validation.opt_string_or_none("error").filter(|message| !message.is_empty()); + report.set("manifest", Value::Object(validation)); + if !valid { + return Err(match (first_error, undecodable) { + // The issue itself is already among the diagnostics. + (Some(issue), _) => Failure::new(EXIT_FAILED, codes::INVALID_MANIFEST, format!("widget.json is invalid: [{}] {}", issue.opt_string("code"), issue.opt_string("message"))), + (None, Some(message)) => Failure::new(EXIT_FAILED, codes::INVALID_MANIFEST, message), + (None, None) => invalid(), + }); + } + Ok(Inspected { files: Arc::new(files), manifest: Some(manifest.ok_or_else(invalid)?) }) +} + +/// Runs one stage of a package command and records how it went. `None` means the report now carries the failure. +fn stage(report: &mut Report, name: &str, work: impl FnOnce(&mut Report) -> Result) -> Option { + match work(report) { + Ok(value) => { + report.check(name, true); + Some(value) + } + Err(failure) => { + report.check(name, false); + report.absorb(&failure); + None + } + } +} + +fn finish_warnings(report: &mut Report, arguments: &Arguments) { + if arguments.flag("fail-on-warning") { + report.fail_on_warnings(); + } +} + +// check + +pub fn check(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &Shape { values: &[], flags: &["fail-on-warning"], minimum_positionals: 1, maximum_positionals: 1, positionals: "a package folder" })?; + let directory = package_directory(arguments.positional(0).unwrap_or_default())?; + let scripts = shared_scripts(&resources()?)?; + let compiler = compiler(&scripts)?; + let mut report = Report::with_stages(command, "static"); + let Some(inspected) = stage(&mut report, "package", |report| inspect(report, &directory, &compiler)) else { return Ok(report) }; + stage(&mut report, "compile", |_| compiler.prepare(inspected.files.as_ref(), &PrepareOptions::default()).map(|_| ()).map_err(|error| Failure::new(EXIT_FAILED, &error.kind, error.message))); + finish_warnings(&mut report, &arguments); + Ok(report) +} + +// run and render + +const RUN_VALUES: [&str; 4] = ["family", "param", "appearance", "data"]; +const RUN_FLAGS: [&str; 2] = ["dark", "fail-on-warning"]; + +fn run_request(arguments: &Arguments, files: Arc, directory: &Path) -> Result { + let family = arguments.value("family").unwrap_or("medium"); + if !FAMILIES.contains(&family) { + return Err(Failure::usage(format!("Unknown family {family}: use small, medium, large or extraLarge"))); + } + let dark = match arguments.value("appearance") { + None | Some("light") => false, + Some("dark") => true, + Some(other) => return Err(Failure::usage(format!("--appearance must be light or dark, not {other}: one run draws one picture"))), + }; + let mut request = RunRequest::new(files, format!("cli:{}", directory.display())); + request.family = family.to_owned(); + request.parameter = arguments.value("param").unwrap_or_default().to_owned(); + request.dark_mode = dark || arguments.flag("dark"); + Ok(request) +} + +/// The members of a run that `run` and `render` share, and the stage results. +fn describe(report: &mut Report, result: &RunResult) { + let execution = &result.execution; + report.set("elapsedMilliseconds", json!(round(execution.elapsed.as_secs_f64() * 1000.0, 2))); + report.set("logs", execution.logs.iter().map(|entry| json!({ "level": entry.level, "message": entry.message })).collect()); + report.diagnostics(execution.diagnostics.iter().cloned()); + report.set("nextTimelineTransition", json!(result.next_timeline_transition)); + let failed_stage = if result.prepared.is_none() { "compile" } else { "execute" }; + if failed_stage == "execute" { + report.check("compile", true); + } + match &execution.error { + Some(error) => { + report.check(failed_stage, false); + report.fail(EXIT_FAILED, &error.kind, &error.message); + } + None => report.check("execute", true), + } +} + +/// The tree as JSON. It nests deeper than serde_json reads by default; its depth is bounded by the runtime contract. +fn parse_tree(text: &str) -> Value { + let mut deserializer = serde_json::Deserializer::from_str(text); + deserializer.disable_recursion_limit(); + serde::Deserialize::deserialize(&mut deserializer).unwrap_or(Value::Null) +} + +struct Started { + report: Report, + arguments: Arguments, + scripts: Arc, + runtime: WidgetRuntime, + /// `None` when the package stage failed; the report says why. + run: Option<(RunRequest, RunResult)>, + _data: RunData, +} + +fn start(command: &str, arguments: Arguments, before_running: impl FnOnce(&Path) -> Result<(), Failure>) -> Result { + let directory = package_directory(arguments.positional(0).unwrap_or_default())?; + // Reject a wrong family or appearance before anything runs. + run_request(&arguments, Arc::new(DiskPackageFiles::new(&directory)), &directory)?; + before_running(&directory)?; + let scripts = shared_scripts(&resources()?)?; + let data = RunData::new(&arguments)?; + let runtime = runtime(&scripts, &data.paths)?; + let mut report = Report::with_stages(command, "linux-native"); + let inspected = stage(&mut report, "package", |report| inspect(report, &directory, runtime.compiler())); + let run = match inspected { + Some(inspected) => { + let request = run_request(&arguments, inspected.files, &directory)?; + let result = runtime.run(&request); + describe(&mut report, &result); + Some((request, result)) + } + None => None, + }; + Ok(Started { report, arguments, scripts, runtime, run, _data: data }) +} + +pub fn run(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &Shape { values: &RUN_VALUES, flags: &RUN_FLAGS, minimum_positionals: 1, maximum_positionals: 1, positionals: "a package folder" })?; + let mut started = start(command, arguments, |_| Ok(()))?; + if let Some((_, result)) = &started.run { + started.report.set("tree", result.execution.tree.as_deref().map_or(Value::Null, parse_tree)); + } + finish_warnings(&mut started.report, &started.arguments); + Ok(started.report) +} + +/// How much of a bitmap shows something: the pixels that are not fully +/// transparent, and how many different colors there are (counted up to 256). +/// A picture with no visible pixel, or with one color only, is empty. +fn picture(output: &RenderOutput) -> Value { + let mut colors = HashSet::new(); + let mut visible = 0u64; + for pixel in output.pixels.chunks_exact(4) { + if pixel[3] != 0 { + visible += 1; + } + if colors.len() < 256 { + colors.insert([pixel[0], pixel[1], pixel[2], pixel[3]]); + } + } + json!({ "visiblePixels": visible, "colors": colors.len() }) +} + +pub fn render(command: &str, arguments: &[String]) -> Result { + const VALUES: [&str; 7] = ["family", "param", "appearance", "data", "output", "scale", "now"]; + let arguments = Arguments::parse(arguments, &Shape { values: &VALUES, flags: &RUN_FLAGS, minimum_positionals: 1, maximum_positionals: 1, positionals: "a package folder" })?; + let target = output::resolve(arguments.value("output").ok_or_else(|| Failure::usage("render needs --output "))?)?; + let scale = arguments.value("scale").unwrap_or("2").parse::().ok().filter(|scale| (0.5..=8.0).contains(scale)).ok_or_else(|| Failure::usage("--scale must be between 0.5 and 8"))?; + let now = arguments.value("now").map(|text| text.parse::().map_err(|_| Failure::usage("--now must be a whole number of milliseconds since 1970"))).transpose()?; + let mut started = start(command, arguments, |directory| output::require_outside(&target, directory))?; + let Some((request, result)) = &started.run else { return Ok(started.report) }; + let report = &mut started.report; + + let renderer = WidgetRenderer::new(started.scripts.clone()).map_err(|error| Failure::internal(format!("The renderer could not be started: {error}")))?; + let mut presentation = Presentation::for_family(&request.family, scale).ok_or_else(|| Failure::usage(format!("Unknown family {}", request.family)))?; + presentation.now_milliseconds = now; + let drawn = sw_render::render_run(&renderer, &started.runtime, request, result, &presentation).and_then(|rendered| Ok((rendered.output.encode_png()?, rendered))); + let (png, rendered) = match drawn { + Ok(drawn) => drawn, + Err(error) => { + report.check("render", false); + report.absorb(&Failure::internal(format!("The widget could not be drawn: {error}"))); + return Ok(started.report); + } + }; + match &rendered.failure { + // The picture shows a card with the reason instead of the widget. + Some(failure) => { + report.check("render", false); + report.fail(EXIT_FAILED, "internalError", &failure.message); + } + // After a script error the picture is the error card; the widget itself was not drawn. + None if result.execution.error.is_none() => report.check("render", true), + None => {} + } + output::write(&target, &png, true)?; + + let output = &rendered.output; + report.set("output", json!(target.display().to_string())); + report.set("artifacts", json!([{ "path": target.display().to_string(), "sha256": hex(&Sha256::digest(&png)), "bytes": png.len() }])); + report.set("family", json!(request.family)); + report.set("scale", json!(round(f64::from(output.scale), 4))); + report.set("pixelWidth", json!(output.pixel_width)); + report.set("pixelHeight", json!(output.pixel_height)); + report.set("picture", picture(output)); + report.set("layoutMilliseconds", json!(round(output.layout_time.as_secs_f64() * 1000.0, 2))); + report.set("paintMilliseconds", json!(round(output.paint_time.as_secs_f64() * 1000.0, 2))); + report.set("renderDiagnostics", Value::Array(output.diagnostics.clone())); + let regions: Vec = output + .hit_regions + .iter() + .map(|region| { + let bounds = ®ion.bounds; + json!({ + "x": round(f64::from(bounds.left), 1), "y": round(f64::from(bounds.top), 1), + "width": round(f64::from(bounds.width()), 1), "height": round(f64::from(bounds.height()), 1), + "interaction": region.interaction, + }) + }) + .collect(); + report.set("hitRegions", Value::Array(regions)); + report.set("animatedImages", json!(output.gif_slots.len())); + finish_warnings(&mut started.report, &started.arguments); + Ok(started.report) +} + +// timeline + +pub fn timeline(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &Shape { values: &["now"], flags: &[], minimum_positionals: 1, maximum_positionals: 1, positionals: "a package folder" })?; + let directory = package_directory(arguments.positional(0).unwrap_or_default())?; + let now = arguments.value("now").map(|text| text.parse::().map_err(|_| Failure::usage("--now must be a whole number of milliseconds since 1970"))).transpose()?; + let scripts = shared_scripts(&resources()?)?; + let mut report = Report::new(command); + report.set("package", json!(directory.display().to_string())); + let manifest = (|| -> Result, Failure> { + packages::validate_source_tree(&directory, &ArchiveLimits::default())?; + if !packages::manifest_present(&directory)? { + return Ok(None); + } + let text = DiskPackageFiles::new(&directory).read_bytes_up_to(manifests::FILE_NAME, MAX_METADATA_BYTES as u64).and_then(|bytes| String::from_utf8(bytes).ok()); + // A present manifest that cannot be read has no schedule to report; it is an error. + manifests::parse(text.as_deref()).map(Some).ok_or_else(|| Failure::new(EXIT_FAILED, codes::INVALID_MANIFEST, "widget.json is invalid.")) + })(); + let manifest = match manifest { + Ok(manifest) => manifest, + Err(failure) => { + report.absorb(&failure); + return Ok(report); + } + }; + let planner = TimelinePlanner::new(scripts); + let state = match now { + Some(now) => planner.state_at(manifest.as_ref(), now), + None => planner.state(manifest.as_ref()), + }; + report.set("hasSchedule", json!(state.is_some())); + report.set("environments", state.as_ref().map_or(Value::Null, |state| Value::Object(state.environments.iter().map(|(name, value)| (name.clone(), json!(value))).collect()))); + report.set("nextTransition", json!(state.and_then(|state| state.next_transition_milliseconds))); + Ok(report) +} + +// pack + +/// The name of the archive's top-level folder: the folder's own name when it is a package name, else the manifest's. +pub(crate) fn archive_name(directory: &Path, manifest: Option<&sw_core::json::Object>) -> String { + let folder = directory.file_name().and_then(|name| name.to_str()).filter(|name| names::is_valid(name)).map(str::to_owned); + folder.or_else(|| manifest.and_then(|manifest| manifests::string_value(manifest, "name")).filter(|name| names::is_valid(name))).unwrap_or_else(|| "Widget".into()) +} + +pub fn pack(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &Shape { values: &["output"], flags: &["force"], minimum_positionals: 1, maximum_positionals: 2, positionals: "a package folder and, optionally, the .swt file to write" })?; + let directory = package_directory(arguments.positional(0).unwrap_or_default())?; + let named = match (arguments.value("output"), arguments.positional(1)) { + (Some(_), Some(_)) => return Err(Failure::usage("Name the output once: either --output or the second argument")), + (first, second) => first.or(second), + }; + let check_target = |target: &Path| { + if target.extension().is_none_or(|extension| extension != "swt") { + return Err(Failure::usage("The pack output must end in .swt")); + } + output::require_outside(target, &directory)?; + if !arguments.flag("force") && std::fs::symlink_metadata(target).is_ok() { + return Err(Failure::environment("output.exists", format!("{} exists; choose a new file or pass --force.", target.display()))); + } + Ok(()) + }; + let named = named.map(output::resolve).transpose()?; + if let Some(target) = &named { + check_target(target)?; + } + let scripts = shared_scripts(&resources()?)?; + let compiler = compiler(&scripts)?; + let mut report = Report::with_stages(command, "static"); + let Some(inspected) = stage(&mut report, "package", |report| inspect(report, &directory, &compiler)) else { return Ok(report) }; + let compiled = stage(&mut report, "compile", |_| compiler.prepare(inspected.files.as_ref(), &PrepareOptions::default()).map(|_| ()).map_err(|error| Failure::new(EXIT_FAILED, &error.kind, error.message))); + if compiled.is_none() { + return Ok(report); + } + let name = archive_name(&directory, inspected.manifest.as_ref()); + let target = match named { + Some(target) => target, + None => { + let target = output::resolve(&format!("{}.swt", names::archive_root_name(&name)))?; + check_target(&target)?; + target + } + }; + let exported = stage(&mut report, "export", |_| { + let mut archive = Vec::new(); + packages::export_directory(&directory, &name, &mut archive, &ArchiveLimits::default())?; + output::write(&target, &archive, arguments.flag("force"))?; + Ok(archive) + }); + if let Some(archive) = exported { + report.set("name", json!(names::archive_root_name(&name))); + report.set("output", json!(target.display().to_string())); + report.set("artifacts", json!([{ "path": target.display().to_string(), "sha256": hex(&Sha256::digest(&archive)), "bytes": archive.len() }])); + } + Ok(report) +} + +// install, list, templates + +fn library(arguments: &Arguments, resources: Option<&Resources>) -> Result { + Ok(Library::new(&user_data(arguments)?, resources.and_then(|resources| resources.templates.clone()))) +} + +pub fn install(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &Shape { values: &["data"], flags: &[], minimum_positionals: 1, maximum_positionals: 1, positionals: "a .swt file or a package folder" })?; + let source = std::path::absolute(Path::new(arguments.positional(0).unwrap_or_default())).map_err(|_| Failure::usage("Missing a .swt file or a package folder"))?; + let metadata = std::fs::symlink_metadata(&source).map_err(|_| Failure::environment("input.file", format!("Not a .swt file or a package folder: {}", source.display())))?; + let resources = resources()?; + let scripts = shared_scripts(&resources)?; + let paths = user_data(&arguments)?; + let runtime = runtime(&scripts, &paths)?; + let library = Library::new(&paths, resources.templates.clone()); + let mut report = Report::with_stages(command, "static"); + + // A folder is packed in memory and installed like any archive, so both ways pass the same checks and publish atomically. + let (archive, suggested): (Box, Option) = if metadata.is_dir() { + let packed = stage(&mut report, "package", |report| { + let inspected = inspect(report, &source, runtime.compiler())?; + let mut archive = Vec::new(); + packages::export_directory(&source, &archive_name(&source, inspected.manifest.as_ref()), &mut archive, &ArchiveLimits::default())?; + Ok(archive) + }); + let Some(packed) = packed else { return Ok(report) }; + (Box::new(std::io::Cursor::new(packed)), None) + } else { + let file = std::fs::File::open(&source).ok().filter(|file| file.metadata().is_ok_and(|metadata| metadata.is_file())); + let file = file.ok_or_else(|| Failure::environment("input.file", format!("Not a readable .swt file: {}", source.display())))?; + report.set("package", json!(source.display().to_string())); + (Box::new(file), source.file_stem().and_then(|stem| stem.to_str()).map(str::to_owned)) + }; + let mut archive = archive; + + // The staged files are compiled before anything is published; what the compiler said is kept for the report. + let compile_error: RefCell> = RefCell::new(None); + let reached_compile = std::cell::Cell::new(false); + let validate = |files: &dyn PackageFiles| { + reached_compile.set(true); + runtime.validate_package(files).map_err(|error| { + let refused = PackageError::new(codes::INVALID_MANIFEST, error.message.clone()); + *compile_error.borrow_mut() = Some(error); + refused + }) + }; + match library.import_archive(&mut archive, suggested.as_deref(), Some(&validate)) { + Ok(installed) => { + report.check("package", true); + report.check("compile", true); + report.set("name", json!(installed.name)); + report.set("title", json!(installed.display_name())); + report.set("installation", json!(installed.installation)); + report.set("directory", json!(installed.directory.display().to_string())); + } + Err(error) => { + let failure = match compile_error.into_inner() { + Some(error) => Failure::new(EXIT_FAILED, &error.kind, error.message), + None => error.into(), + }; + let failed_stage = if reached_compile.get() { "compile" } else { "package" }; + if failed_stage == "compile" { + report.check("package", true); + } + report.check(failed_stage, false); + report.absorb(&failure); + } + } + Ok(report) +} + +pub fn list(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &Shape { values: &["data"], flags: &[], minimum_positionals: 0, maximum_positionals: 0, positionals: "" })?; + let library = library(&arguments, None)?; + let packages: Vec = library + .list()? + .iter() + .map(|package| json!({ "name": package.name, "title": package.display_name(), "installation": package.installation, "directory": package.directory.display().to_string() })) + .collect(); + let mut report = Report::new(command); + report.set("library", json!(library.root().display().to_string())); + report.set("packages", Value::Array(packages)); + Ok(report) +} + +fn templates_directory(resources: &Resources) -> Result { + resources.templates.clone().filter(|directory| directory.is_dir()).ok_or_else(|| { + Failure::environment("tool.templates_missing", format!("The bundled templates were not found. Install the package, or set {} or {}.", sw_core::resources::ROOT_VARIABLE, sw_core::resources::TEMPLATES_VARIABLE)) + }) +} + +/// The template list needs no data folder: only the shipped templates are read. +fn template_library(resources: &Resources) -> Result { + let directory = templates_directory(resources)?; + let nowhere = DataPaths::at(Path::new("/nonexistent/scriptwidget")).map_err(|error| Failure::internal(error.to_string()))?; + Ok(Library::new(&nowhere, Some(directory))) +} + +pub fn templates(command: &str, arguments: &[String]) -> Result { + Arguments::parse(arguments, &Shape { values: &[], flags: &[], minimum_positionals: 0, maximum_positionals: 0, positionals: "" })?; + let library = template_library(&resources()?)?; + let mut report = Report::new(command); + report.set("templates", library.bundled().iter().map(|template| json!({ "group": template.group, "name": template.name, "network": template.network, "directory": template.directory.display().to_string() })).collect()); + report.set("unavailable", library.unavailable().iter().map(|template| json!({ "group": template.group, "name": template.name, "api": template.api })).collect()); + Ok(report) +} + +// new + +const STARTER: &str = r##"// A ScriptWidget package. Edit this file, then: +// scriptwidget check . +// scriptwidget render . --output /tmp/widget.png + +const size = $getenv("widget-size"); + +$render( + + + + + + + + Your next idea + Start editing main.jsx to make it yours. + +); +"##; + +pub fn new(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &Shape { values: &["template", "directory"], flags: &[], minimum_positionals: 1, maximum_positionals: 1, positionals: "a package name" })?; + let name = arguments.positional(0).unwrap_or_default(); + if !names::is_valid(name) { + return Err(Failure::usage(format!("{name:?} cannot be a package name: no path separators, leading dot or control characters, at most 80 characters"))); + } + let parent = std::path::absolute(Path::new(arguments.value("directory").unwrap_or("."))).map_err(|_| Failure::usage("--directory is not a usable path"))?; + if !parent.is_dir() { + return Err(Failure::environment("output.path", format!("Not a folder: {}", parent.display()))); + } + let (template, files): (String, Vec<(String, Vec)>) = match arguments.value("template") { + None => ("starter".into(), vec![(manifests::LEGACY_ENTRY.into(), STARTER.as_bytes().to_vec()), (manifests::FILE_NAME.into(), manifests::encode(&manifests::new_manifest(name)))]), + Some(reference) => { + let library = template_library(&resources()?)?; + let bundled = library.find_bundled(reference).ok_or_else(|| Failure::usage(format!("No bundled template named {reference}. scriptwidget templates lists them.")))?; + let mut files = library.bundled_files(&bundled); + let (_, manifest) = Library::bundled_sources(name, &files)?; + files.insert(manifests::FILE_NAME.into(), manifest.into_bytes()); + (format!("{}/{}", bundled.group, bundled.name), files.into_iter().collect()) + } + }; + + let target = parent.join(name); + // Created here and now: an existing folder, or anything else at this name, is never written into. + std::fs::create_dir(&target).map_err(|error| match error.kind() { + std::io::ErrorKind::AlreadyExists => Failure::new(EXIT_FAILED, codes::PACKAGE_EXISTS, format!("{} already exists.", target.display())), + _ => Failure::environment("output.write", format!("{}: {error}", target.display())), + })?; + let written = (|| -> std::io::Result> { + let mut paths = Vec::new(); + for (path, bytes) in &files { + let parts = path_rules::visible_components(path).ok_or_else(|| std::io::Error::other(format!("{path} is not a package path")))?; + let file = parts.iter().fold(target.clone(), |file, part| file.join(part)); + if let Some(folder) = file.parent() { + std::fs::create_dir_all(folder)?; + } + std::io::Write::write_all(&mut std::fs::OpenOptions::new().write(true).create_new(true).open(&file)?, bytes)?; + paths.push(path.clone()); + } + Ok(paths) + })(); + let paths = written.map_err(|error| { + let _ = std::fs::remove_dir_all(&target); + Failure::environment("output.write", format!("{}: {error}", target.display())) + })?; + let mut report = Report::new(command); + report.set("name", json!(name)); + report.set("directory", json!(target.display().to_string())); + report.set("template", json!(template)); + report.set("files", json!(paths)); + Ok(report) +} + +// doctor + +fn writable(directory: &Path) -> bool { + // The folder itself, or the nearest one above it that exists and would hold it. + let mut existing = directory.to_owned(); + while !existing.exists() { + if !existing.pop() { + return false; + } + } + std::ffi::CString::new(std::os::unix::ffi::OsStrExt::as_bytes(existing.as_os_str())).is_ok_and(|path| { + // SAFETY: `path` is a valid NUL-terminated string for the duration of the call. + existing.is_dir() && unsafe { libc::access(path.as_ptr(), libc::W_OK | libc::X_OK) } == 0 + }) +} + +pub fn doctor(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &Shape { values: &["data"], flags: &[], minimum_positionals: 0, maximum_positionals: 0, positionals: "" })?; + let mut report = Report::new(command); + let release = sw_core::api::system::os_release(Path::new("/")); + report.set("system", json!({ "name": release.get("PRETTY_NAME").or_else(|| release.get("NAME")), "id": release.get("ID"), "architecture": std::env::consts::ARCH, "icu": sw_core::intl::icu_version(), "locale": sw_core::intl::default_locale_tag(), "timeZone": sw_core::intl::default_time_zone() })); + let set = |name: &str| std::env::var_os(name).is_some_and(|value| !value.is_empty()); + // Nothing here needs a graphical session; this only says what the desktop commands will find. + report.set("session", json!({ "wayland": set("WAYLAND_DISPLAY"), "x11": set("DISPLAY"), "desktop": std::env::var("XDG_CURRENT_DESKTOP").ok() })); + + // Where things are. + let data = match arguments.value("data") { + Some(root) => DataPaths::at(Path::new(root)), + None => DataPaths::locate(|name| std::env::var_os(name)), + }; + let resources = Resources::locate(); + report.set( + "paths", + json!({ + "runtime": resources.as_ref().ok().map(|resources| resources.runtime.display().to_string()), + "templates": resources.as_ref().ok().and_then(|resources| resources.templates.as_ref()).map(|path| path.display().to_string()), + "data": data.as_ref().ok().map(|paths| paths.root().display().to_string()), + "cache": data.as_ref().ok().map(|paths| paths.cache().display().to_string()), + }), + ); + match &data { + Ok(paths) if writable(paths.root()) && writable(paths.cache()) => report.check("dataDirectory", true), + Ok(paths) => { + report.check("dataDirectory", false); + report.fail(EXIT_FAILED, "tool.data_directory", &format!("The data folder {} or the cache folder {} cannot be written.", paths.root().display(), paths.cache().display())); + } + Err(error) => { + report.check("dataDirectory", false); + report.fail(EXIT_FAILED, "tool.data_directory", &format!("No data folder: {error}")); + } + } + + // The runtime files, then what needs them. + let scripts = resources.as_ref().map_err(|error| error.to_string()).and_then(|resources| SharedScripts::new(&resources.runtime).map(Arc::new).map_err(|error| error.to_string())); + for name in ["templates", "engine", "render"] { + report.check_not_run(name); + } + let scripts = match scripts { + Ok(scripts) => { + report.check("resources", true); + scripts + } + Err(message) => { + report.check("resources", false); + report.fail(EXIT_FAILED, "tool.resources_missing", &message); + report.check_not_run("service"); + return Ok(report); + } + }; + let contract = scripts.contract(); + report.set("runtime", json!({ "apiVersion": contract.get("apiVersion"), "compilerFingerprint": contract.get("compilerFingerprint") })); + + match resources.as_ref().ok().and_then(|resources| template_library(resources).ok()) { + Some(library) if !library.bundled().is_empty() => { + report.check("templates", true); + report.set("templateCount", json!({ "available": library.bundled().len(), "unavailable": library.unavailable().len() })); + } + _ => { + report.check("templates", false); + report.fail(EXIT_FAILED, "tool.templates_missing", "The bundled templates were not found."); + } + } + + // One small package through the compiler, the engine and the painter, with its data in a temporary folder. + let scratch = RunData::new(&Arguments::default())?; + let runtime = runtime(&scripts, &scratch.paths)?; + let files: Arc = Arc::new(MemoryPackageFiles::new([("main.jsx".to_owned(), "$render(ScriptWidget 你好);".to_owned())], [])); + let request = RunRequest::new(files, "cli:doctor"); + let result = runtime.run(&request); + match &result.execution.error { + None => report.check("engine", true), + Some(error) => { + report.check("engine", false); + report.fail(EXIT_FAILED, &error.kind, &format!("The self-test package did not run: {}", error.message)); + } + } + if result.execution.error.is_none() { + let drawn = WidgetRenderer::new(scripts.clone()).map_err(|error| error.to_string()).and_then(|renderer| { + let fonts = renderer.fonts().map_err(|error| error.message)?; + let presentation = Presentation::for_family("small", 1.0).ok_or("no small family")?; + let rendered = sw_render::render_run(&renderer, &runtime, &request, &result, &presentation).map_err(|error| error.message)?; + match rendered.failure { + Some(failure) => Err(failure.message), + None => Ok((fonts, picture(&rendered.output))), + } + }); + match drawn { + Ok((fonts, picture)) if picture["visiblePixels"].as_u64() > Some(0) && picture["colors"].as_u64() > Some(1) => { + report.check("render", true); + report.set("fonts", json!({ "bundled": fonts.bundled, "locale": fonts.locale, "cjkFamilies": fonts.cjk_families, "monospace": fonts.monospace, "serif": fonts.serif })); + if fonts.cjk_families.is_empty() { + report.diagnostic("warning", "fonts.no_cjk", "No Chinese, Japanese or Korean font is installed; such text will show as empty boxes."); + } + } + Ok(_) => { + report.check("render", false); + report.fail(EXIT_FAILED, "tool.render", "The self-test picture is empty."); + } + Err(message) => { + report.check("render", false); + report.fail(EXIT_FAILED, "tool.render", &format!("The self-test package could not be drawn: {message}")); + } + } + } + // The background service is optional for everything above: not running is not a fault of the installation. + match crate::desktop::status() { + Ok(Some(status)) => { + report.check("service", true); + report.set("service", json!({ "running": true, "protocol": status["protocol"], "version": status["service"], "widgets": status["widgets"], "watchers": status["watchers"], "locked": status["locked"] })); + } + Ok(None) => { + report.check_not_run("service"); + report.set("service", json!({ "running": false })); + } + Err(failure) => { + report.check("service", false); + report.fail(EXIT_FAILED, &failure.code, &failure.message); + } + } + Ok(report) +} diff --git a/Linux/crates/sw-cli/src/desktop.rs b/Linux/crates/sw-cli/src/desktop.rs new file mode 100644 index 00000000..9eaa60cc --- /dev/null +++ b/Linux/crates/sw-cli/src/desktop.rs @@ -0,0 +1,441 @@ +//! The commands that talk to the background service (`docs/linux/protocol.md`), +//! and the service itself (`scriptwidget host run`). +//! +//! Each command checks its arguments, sends one line to the service's socket +//! and prints the answer as the usual report. Two commands do not print a +//! report, because something reads their output line by line for as long as +//! they run: `watch` prints the display events, and `dev` prints one line for +//! every time it copied the folder. +//! +//! When the service is not running these commands exit with 69 and say how +//! to start it. + +use std::collections::BTreeMap; +use std::io::Write; +use std::os::unix::fs::OpenOptionsExt; +use std::path::{Path, PathBuf}; +use std::time::{Duration, SystemTime}; + +use serde_json::{Value, json}; +use sw_core::library::{InstalledPackage, Library}; +use sw_core::packages::limits::{MAX_FILE_BYTES, MAX_METADATA_BYTES, MAX_PATH_DEPTH}; +use sw_core::packages::{self, ArchiveLimits, DiskPackageFiles, PackageError, codes, manifests, path_rules}; +use sw_host::client::{self, ClientError, WatchEnd}; +use sw_host::protocol::{self, Request, Switch}; +use sw_host::{RuntimePaths, service}; + +use crate::arguments::{Arguments, Shape}; +use crate::commands::{archive_name, package_directory, resources, user_data}; +use crate::report::{EXIT_FAILED, EXIT_NEEDS_SERVICE, EXIT_USAGE, Failure, Report}; + +/// How to start the service; part of every "not running" message. +const HOW_TO_START: &str = "Start it with: systemctl --user start scriptwidget.service (or run: scriptwidget host run)"; + +pub fn needs_service(command: &str) -> Failure { + Failure::new(EXIT_NEEDS_SERVICE, "service.unavailable", format!("scriptwidget {command} needs the background service, which is not running. {HOW_TO_START}")) +} + +/// The service's socket in this session. Without a runtime directory there is no session and so no service. +fn socket(command: &str) -> Result { + RuntimePaths::from_environment().map(|paths| paths.socket()).map_err(|_| needs_service(command)) +} + +fn failure_of(command: &str, error: ClientError) -> Failure { + match error { + ClientError::NotRunning => needs_service(command), + ClientError::Lost => Failure::new(EXIT_NEEDS_SERVICE, "service.lost", format!("The background service did not answer scriptwidget {command}.")), + ClientError::Refused { code, message } => { + // What a package did is the package's fault; everything else is how the command was used. + let exit = if matches!(code.as_str(), "link.refused" | "family.unsupported") { EXIT_FAILED } else { EXIT_USAGE }; + Failure::new(exit, &code, message) + } + } +} + +/// Checks the request here, sends it, and returns the service's answer. +fn ask(command: &str, request: Request) -> Result { + let request = request.checked().map_err(|refusal| Failure::usage(refusal.message))?; + client::request(&socket(command)?, &request).map_err(|error| failure_of(command, error)) +} + +/// The report of a command: the members of the service's answer. +fn report(command: &str, answer: Value) -> Report { + let mut report = Report::new(command); + if let Value::Object(members) = answer { + for (name, value) in members { + if name != "ok" { + report.set(&name, value); + } + } + } + report +} + +fn shape(minimum: usize, maximum: usize, positionals: &'static str) -> Shape { + Shape { values: &[], flags: &[], minimum_positionals: minimum, maximum_positionals: maximum, positionals } +} + +fn identifier(text: &str) -> Result { + if protocol::is_identifier(text) { Ok(text.to_owned()) } else { Err(Failure::usage(format!("{text} is not a widget identifier; scriptwidget widgets lists them"))) } +} + +fn integer(text: &str, what: &str) -> Result { + text.parse::().map_err(|_| Failure::usage(format!("{what} must be a whole number, not {text}"))) +} + +/// A position in logical pixels. A display may compute half a pixel; it is rounded to a whole one. +fn coordinate(text: &str, what: &str) -> Result { + let plain = !text.is_empty() && text.len() <= 16 && text.bytes().enumerate().all(|(index, byte)| byte.is_ascii_digit() || byte == b'.' || (index == 0 && byte == b'-')); + text.parse::().ok().filter(|value| plain && value.is_finite()).map(|value| value.round() as i64).ok_or_else(|| Failure::usage(format!("{what} must be a number, not {text}"))) +} + +pub fn add(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &Shape { values: &["family"], flags: &[], minimum_positionals: 1, maximum_positionals: 1, positionals: "the name of an installed package" })?; + let request = Request::Add { package: arguments.positional(0).unwrap_or_default().to_owned(), family: arguments.value("family").map(str::to_owned) }; + Ok(report(command, ask(command, request)?)) +} + +pub fn remove(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &shape(1, 1, "a widget identifier"))?; + Ok(report(command, ask(command, Request::Remove { id: identifier(arguments.positional(0).unwrap_or_default())? })?)) +} + +pub fn widgets(command: &str, arguments: &[String]) -> Result { + Arguments::parse(arguments, &shape(0, 0, ""))?; + Ok(report(command, ask(command, Request::Widgets)?)) +} + +pub fn refresh(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &Shape { values: &["package"], flags: &[], minimum_positionals: 0, maximum_positionals: 1, positionals: "at most one widget identifier" })?; + let id = arguments.positional(0).map(identifier).transpose()?; + Ok(report(command, ask(command, Request::Refresh { id, package: arguments.value("package").map(str::to_owned) })?)) +} + +pub fn switch(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &shape(0, 1, "on, off or toggle"))?; + let switch = Switch::parse(arguments.positional(0).unwrap_or("toggle")).ok_or_else(|| Failure::usage("Use on, off or toggle"))?; + Ok(report(command, ask(command, if command == "peek" { Request::Peek(switch) } else { Request::Arrange(switch) })?)) +} + +pub fn tap(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &shape(2, 3, "a widget identifier, a region number and, optionally, the version of the picture"))?; + let number = |index: usize, what: &str| integer(arguments.positional(index).unwrap_or_default(), what); + let n = u32::try_from(number(1, "The region number")?).map_err(|_| Failure::usage("The region number must not be negative"))?; + let version = if arguments.positional(2).is_some() { Some(u64::try_from(number(2, "The version")?).map_err(|_| Failure::usage("The version must not be negative"))?) } else { None }; + Ok(report(command, ask(command, Request::Tap { id: identifier(arguments.positional(0).unwrap_or_default())?, n, version })?)) +} + +pub fn move_widget(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &shape(4, 4, "a widget identifier, a screen name, x and y"))?; + let request = Request::Move { + id: identifier(arguments.positional(0).unwrap_or_default())?, + screen: arguments.positional(1).unwrap_or_default().to_owned(), + x: coordinate(arguments.positional(2).unwrap_or_default(), "x")?, + y: coordinate(arguments.positional(3).unwrap_or_default(), "y")?, + }; + Ok(report(command, ask(command, request)?)) +} + +pub fn screen(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &shape(4, 4, "a screen name, its scale, width and height"))?; + let scale = arguments.positional(1).unwrap_or_default(); + let request = Request::Screen { + name: arguments.positional(0).unwrap_or_default().to_owned(), + scale: protocol::scale_thousandths(scale).ok_or_else(|| Failure::usage(format!("The scale must be a number such as 1, 1.5 or 2, not {scale}")))?, + width: integer(arguments.positional(2).unwrap_or_default(), "The width")?, + height: integer(arguments.positional(3).unwrap_or_default(), "The height")?, + }; + Ok(report(command, ask(command, request)?)) +} + +pub fn theme(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &shape(1, 1, "dark or light"))?; + let dark = match arguments.positional(0) { + Some("dark") => true, + Some("light") => false, + _ => return Err(Failure::usage("Use dark or light")), + }; + Ok(report(command, ask(command, Request::Theme { dark })?)) +} + +pub fn menu(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &shape(2, 2, "a widget identifier and a menu word"))?; + let word = arguments.positional(1).unwrap_or_default(); + if !protocol::MENU_WORDS.contains(&word) { + return Err(Failure::usage(format!("The menu word must be one of: {}", protocol::MENU_WORDS.join(", ")))); + } + Ok(report(command, ask(command, Request::Menu { id: identifier(arguments.positional(0).unwrap_or_default())?, word: word.to_owned() })?)) +} + +pub fn session(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &shape(1, 1, "locked or unlocked"))?; + let locked = match arguments.positional(0) { + Some("locked") => true, + Some("unlocked") => false, + _ => return Err(Failure::usage("Use locked or unlocked")), + }; + Ok(report(command, ask(command, Request::Session { locked })?)) +} + +/// What `doctor` says about the service: its status when it runs, `None` when it does not. +pub fn status() -> Result, Failure> { + let Ok(socket) = socket("doctor") else { return Ok(None) }; + match client::request(&socket, &Request::Status) { + Ok(status) => Ok(Some(status)), + Err(ClientError::NotRunning) => Ok(None), + Err(error) => Err(failure_of("doctor", error)), + } +} + +// Commands whose output is read line by line + +fn print_line(line: &str) -> bool { + let mut output = std::io::stdout().lock(); + writeln!(output, "{line}").and_then(|()| output.flush()).is_ok() +} + +fn fail(command: &str, failure: &Failure) -> i32 { + Report::from_failure(command, failure).finish() +} + +/// `watch`, `dev` and `host`: the exit code when `command` is one of them. +pub fn stream(command: &str, arguments: &[String]) -> Option { + match command { + "watch" => Some(watch(command, arguments)), + "dev" => Some(dev(command, arguments).unwrap_or_else(|failure| fail(command, &failure))), + "host" => Some(host(command, arguments)), + _ => None, + } +} + +/// Prints the display events, one JSON object a line, until the service +/// says `bye` or the connection ends. The last line is always a `bye`: the +/// service's own, or one this command writes with the reason `not-running` +/// (there was no service) or `lost` (the connection ended without one). +fn watch(command: &str, arguments: &[String]) -> i32 { + if let Err(failure) = Arguments::parse(arguments, &shape(0, 0, "")) { + return fail(command, &failure); + } + let end = match socket(command) { + Ok(socket) => client::watch(&socket, print_line), + Err(_) => WatchEnd::NotRunning, + }; + match end { + WatchEnd::Bye | WatchEnd::Stopped => 0, + WatchEnd::NotRunning => { + print_line(&protocol::bye("not-running").to_string()); + eprintln!("error: [service.unavailable] {}", needs_service(command).message); + EXIT_NEEDS_SERVICE + } + WatchEnd::Lost => { + print_line(&protocol::bye("lost").to_string()); + eprintln!("error: [service.lost] The connection to the background service ended."); + EXIT_NEEDS_SERVICE + } + } +} + +/// `scriptwidget host run`: the background service. It prints nothing on +/// standard output; what it has to say goes to standard error, for the journal. +fn host(command: &str, arguments: &[String]) -> i32 { + let parsed = match Arguments::parse(arguments, &Shape { values: &["data"], flags: &[], minimum_positionals: 1, maximum_positionals: 1, positionals: "run" }) { + Ok(parsed) => parsed, + Err(failure) => return fail(command, &failure), + }; + if parsed.positional(0) != Some("run") { + return fail(command, &Failure::usage("Use: scriptwidget host run")); + } + service::run_process(parsed.value("data").map(Path::new)) +} + +// dev + +/// The dot-file in an installed package that says which folder `dev` copies into it. Listings and exports skip dot-files. +const DEV_MARKER: &str = ".scriptwidget-dev"; +const POLL: Duration = Duration::from_millis(400); +/// A folder with more entries than this is not watched. +const MAXIMUM_WATCHED: usize = 4000; + +/// Path, modification time and size of every file below `directory` that +/// can be part of a package. Links are not followed. +fn snapshot(directory: &Path) -> BTreeMap, u64)> { + fn walk(directory: &Path, prefix: &str, depth: usize, found: &mut BTreeMap, u64)>) { + let Ok(entries) = std::fs::read_dir(directory) else { return }; + for entry in entries.flatten() { + let Ok(name) = entry.file_name().into_string() else { continue }; + if path_rules::is_excluded(&name) || found.len() >= MAXIMUM_WATCHED { + continue; + } + let Ok(metadata) = std::fs::symlink_metadata(entry.path()) else { continue }; + let path = if prefix.is_empty() { name } else { format!("{prefix}/{name}") }; + if metadata.is_dir() { + if depth < MAX_PATH_DEPTH { + walk(&entry.path(), &path, depth + 1, found); + } + } else { + found.insert(path, (metadata.modified().ok(), metadata.len())); + } + } + } + let mut found = BTreeMap::new(); + walk(directory, "", 1, &mut found); + found +} + +fn read_marker(installed: &InstalledPackage) -> Option { + use std::io::Read; + let file = std::fs::OpenOptions::new().read(true).custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC | libc::O_NONBLOCK).open(installed.directory.join(DEV_MARKER)).ok()?; + let mut text = String::new(); + file.take(8192).read_to_string(&mut text).ok()?; + Some(text) +} + +fn write_marker(installed: &InstalledPackage, source: &Path) { + let marker = installed.directory.join(DEV_MARKER); + let _ = std::fs::remove_file(&marker); + if let Ok(mut file) = std::fs::OpenOptions::new().write(true).create_new(true).custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC).mode(0o600).open(&marker) { + let _ = file.write_all(source.display().to_string().as_bytes()); + } +} + +struct Developed { + library: Library, + source: PathBuf, + /// The name of the installed package the folder is copied into. + name: String, +} + +impl Developed { + /// Makes the installed package hold exactly the files of the folder. + /// The folder is verified as a whole first; every write and removal goes + /// through the package store. Returns the paths written and removed. + fn sync(&self) -> Result<(Vec, Vec), Failure> { + packages::validate_source_tree(&self.source, &ArchiveLimits::default())?; + let source = DiskPackageFiles::new(&self.source); + let paths = source.list()?; + let installed = self.library.find(&self.name).ok_or_else(|| PackageError::new(codes::PACKAGE_NOT_FOUND, "The installed package is gone."))?; + let target = DiskPackageFiles::new(&installed.directory); + let mut written = Vec::new(); + for path in &paths { + let bytes = source.read_bytes_up_to(path, MAX_FILE_BYTES).ok_or_else(|| PackageError::new(codes::FILE_NOT_FOUND, format!("{path} cannot be read.")))?; + if target.read_bytes_up_to(path, MAX_FILE_BYTES).as_deref() != Some(&bytes[..]) { + self.library.write_file(&self.name, path, &bytes)?; + written.push(path.clone()); + } + } + let mut removed = Vec::new(); + for path in target.list()? { + if !paths.contains(&path) { + self.library.delete_file(&self.name, &path)?; + removed.push(path); + } + } + Ok((written, removed)) + } +} + +/// Finds or creates the installed package a folder is developed into. +fn develop(library: Library, source: PathBuf, adopt: bool) -> Result<(Developed, InstalledPackage), Failure> { + packages::validate_source_tree(&source, &ArchiveLimits::default())?; + let files = DiskPackageFiles::new(&source); + // A present manifest is authoritative: unreadable is an error, never "legacy". + let manifest_text = if packages::manifest_present(&source)? { + let invalid = || Failure::new(EXIT_FAILED, codes::INVALID_MANIFEST, "widget.json is invalid."); + let text = files.read_bytes_up_to(manifests::FILE_NAME, MAX_METADATA_BYTES as u64).and_then(|bytes| String::from_utf8(bytes).ok()).ok_or_else(invalid)?; + manifests::parse(Some(&text)).ok_or_else(invalid)?; + Some(text) + } else { + None + }; + let manifest = manifests::parse(manifest_text.as_deref()); + let name = archive_name(&source, manifest.as_ref()); + let canonical = std::fs::canonicalize(&source).unwrap_or_else(|_| source.clone()); + let installed = match library.find(&name) { + Some(installed) => { + let ours = read_marker(&installed).is_some_and(|marker| Path::new(&marker) == canonical); + if !ours && !adopt { + return Err(Failure::new( + EXIT_FAILED, + "dev.package_exists", + format!("A package named {} is already installed and does not come from this folder. Pass --adopt to replace its files with the folder's, or rename the folder.", installed.name), + )); + } + installed + } + None => { + let entry = manifest.as_ref().and_then(|manifest| manifests::string_value(manifest, "entry")).unwrap_or_else(|| manifests::LEGACY_ENTRY.to_owned()); + let main = files.read_bytes_up_to(&entry, MAX_FILE_BYTES).and_then(|bytes| String::from_utf8(bytes).ok()).ok_or_else(|| PackageError::new(codes::MISSING_ENTRY, format!("The folder has no {entry}.")))?; + library.create(&name, &main, manifest_text.as_deref())? + } + }; + write_marker(&installed, &canonical); + Ok((Developed { library, source, name: installed.name.clone() }, installed)) +} + +/// Copies a package folder into the library whenever a file in it changes, +/// and asks the service to run the widgets of that package again. The folder +/// is looked at a few times a second, in this command; the service is not +/// told a path and watches nothing. +fn dev(command: &str, arguments: &[String]) -> Result { + let arguments = Arguments::parse(arguments, &Shape { values: &["family", "data"], flags: &["adopt"], minimum_positionals: 1, maximum_positionals: 1, positionals: "a package folder" })?; + let source = package_directory(arguments.positional(0).unwrap_or_default())?; + let family = arguments.value("family").map(str::to_owned); + if family.as_deref().is_some_and(|family| !protocol::is_family(family)) { + return Err(Failure::usage("Unknown family: use small, medium, large or extraLarge")); + } + // Nothing is copied when there is no service to show it. + ask(command, Request::Status)?; + let library = Library::new(&user_data(&arguments)?, resources().ok().and_then(|resources| resources.templates)); + let (developed, installed) = develop(library, source, arguments.flag("adopt"))?; + developed.sync()?; + + // A widget of this package on the desktop, placed now when there is none. + let placed = |answer: &Value| -> Vec { answer["widgets"].as_array().into_iter().flatten().filter(|widget| widget["installation"] == installed.installation.as_str()).map(|widget| widget["id"].clone()).collect() }; + let mut widgets = placed(&ask(command, Request::Widgets)?); + let added = widgets.is_empty(); + if added { + widgets.push(ask(command, Request::Add { package: developed.name.clone(), family })?["id"].clone()); + } else { + ask(command, Request::Refresh { id: None, package: Some(developed.name.clone()) })?; + } + if !print_line(&json!({ "event": "ready", "package": developed.name, "installation": installed.installation, "widgets": widgets, "added": added }).to_string()) { + return Ok(0); + } + + let mut seen = snapshot(&developed.source); + loop { + std::thread::sleep(POLL); + let now = snapshot(&developed.source); + if now == seen { + continue; + } + // An editor writes a file in several steps: wait until the folder holds still. + let mut settled = now; + loop { + std::thread::sleep(Duration::from_millis(100)); + let again = snapshot(&developed.source); + if again == settled { + break; + } + settled = again; + } + seen = settled; + let line = match developed.sync() { + Ok((written, removed)) if written.is_empty() && removed.is_empty() => continue, + Ok((written, removed)) => match ask(command, Request::Refresh { id: None, package: Some(developed.name.clone()) }) { + Ok(answer) => json!({ "event": "synced", "package": developed.name, "written": written, "removed": removed, "refreshed": answer["refreshed"] }), + Err(failure) if failure.exit == EXIT_NEEDS_SERVICE => { + print_line(&protocol::bye("not-running").to_string()); + eprintln!("error: [{}] {}", failure.code, failure.message); + return Ok(EXIT_NEEDS_SERVICE); + } + Err(failure) => json!({ "event": "error", "code": failure.code, "message": failure.message }), + }, + // The folder is wrong for the moment (a link, a file too large): say so and keep watching. + Err(failure) => json!({ "event": "error", "code": failure.code, "message": failure.message }), + }; + if !print_line(&line.to_string()) { + return Ok(0); + } + } +} diff --git a/Linux/crates/sw-cli/src/main.rs b/Linux/crates/sw-cli/src/main.rs new file mode 100644 index 00000000..80204269 --- /dev/null +++ b/Linux/crates/sw-cli/src/main.rs @@ -0,0 +1,97 @@ +//! `scriptwidget`: the ScriptWidget command line for Linux. It creates, +//! checks, runs, draws, packs and installs widget packages with the same +//! runtime the desktop service uses, and needs no graphical session. +//! +//! Every command prints one JSON report on standard output and exits with one +//! of the codes in [`report`]. See `commands::USAGE`. The exceptions are in +//! [`desktop`]: `watch` and `dev` print one line of JSON at a time, and +//! `host run` is the background service itself. The service is this same +//! program so that one file is installed and the command line can never be a +//! different version from the service it talks to. + +mod arguments; +mod commands; +mod desktop; +mod output; +mod report; + +use std::process::ExitCode; + +use report::{EXIT_INTERNAL, EXIT_USAGE, Failure, Report}; + +/// Room for the JSON of the deepest element tree the runtime contract allows. +const MAIN_STACK: usize = 64 * 1024 * 1024; + +fn dispatch(command: &str, rest: &[String]) -> Result { + match command { + "new" => commands::new(command, rest), + // `validate` is what the Windows command line calls it. + "check" | "validate" => commands::check("check", rest), + "run" => commands::run(command, rest), + "render" => commands::render(command, rest), + "timeline" => commands::timeline(command, rest), + "pack" => commands::pack(command, rest), + "install" => commands::install(command, rest), + "list" => commands::list(command, rest), + "templates" => commands::templates(command, rest), + "doctor" => commands::doctor(command, rest), + "--version" | "version" => { + let mut report = Report::new("--version"); + report.set("version", serde_json::json!(env!("CARGO_PKG_VERSION"))); + Ok(report) + } + "add" => desktop::add(command, rest), + "remove" => desktop::remove(command, rest), + "widgets" => desktop::widgets(command, rest), + "refresh" => desktop::refresh(command, rest), + "peek" | "arrange" => desktop::switch(command, rest), + "tap" => desktop::tap(command, rest), + "move" => desktop::move_widget(command, rest), + "screen" => desktop::screen(command, rest), + "theme" => desktop::theme(command, rest), + "menu" => desktop::menu(command, rest), + "session" => desktop::session(command, rest), + _ => Err(Failure::usage(format!("Unknown command {command}. scriptwidget --help lists the commands."))), + } +} + +fn execute() -> i32 { + // A broken pipe ends the write with an error instead of the process with a signal. + // SAFETY: called once, before any other thread exists; SIG_IGN is a valid disposition. + unsafe { libc::signal(libc::SIGPIPE, libc::SIG_IGN) }; + let mut arguments = Vec::new(); + for argument in std::env::args_os().skip(1) { + match argument.into_string() { + Ok(text) => arguments.push(text), + Err(_) => return Report::from_failure("", &Failure::usage("Arguments must be valid UTF-8")).finish(), + } + } + // Installed under the name the plan gives the service, the program is the service. + if std::env::args_os().next().is_some_and(|program| std::path::Path::new(&program).file_name().is_some_and(|name| name == "scriptwidget-host")) { + arguments.splice(0..0, ["host".to_owned(), "run".to_owned()]); + } + let Some(command) = arguments.first().cloned() else { + eprint!("{}", commands::USAGE); + return EXIT_USAGE; + }; + if matches!(command.as_str(), "--help" | "-h" | "help") { + print!("{}", commands::USAGE); + return 0; + } + // The commands that print lines for as long as they run, and the service itself. + if let Some(code) = desktop::stream(&command, &arguments[1..]) { + return code; + } + let report = dispatch(&command, &arguments[1..]).unwrap_or_else(|failure| Report::from_failure(&command, &failure)); + report.finish() +} + +fn main() -> ExitCode { + let worker = std::thread::Builder::new().name("scriptwidget".into()).stack_size(MAIN_STACK).spawn(execute); + let code = match worker.map(|worker| worker.join()) { + Ok(Ok(code)) => code, + // A panic has already printed its message to standard error. + _ => EXIT_INTERNAL, + }; + ExitCode::from(code as u8) +} diff --git a/Linux/crates/sw-cli/src/output.rs b/Linux/crates/sw-cli/src/output.rs new file mode 100644 index 00000000..5de9db20 --- /dev/null +++ b/Linux/crates/sw-cli/src/output.rs @@ -0,0 +1,93 @@ +//! Writing the one file a command was asked to write: the PNG of `render` +//! and the archive of `pack`. +//! +//! The file is written under a fresh private name in the directory the user +//! named and then moved to its final name. So nothing is ever written +//! *through* what is at that name: a symbolic link there is refused, and even +//! one that appears at the last moment is replaced (or, for a file that must +//! be new, makes the command fail) instead of being followed. The output may +//! not lie inside the package it was made from. + +use std::io::Write; +use std::os::unix::fs::OpenOptionsExt; +use std::path::{Component, Path, PathBuf}; + +use crate::report::Failure; + +/// The absolute path a user-supplied output names, with the directories that +/// already exist resolved, so that it can be compared with the package directory. +pub fn resolve(text: &str) -> Result { + let unusable = || Failure::environment("output.path", format!("{text} is not a usable output path")); + if text.is_empty() || text.contains('\0') { + return Err(unusable()); + } + let absolute = std::path::absolute(Path::new(text)).map_err(|_| unusable())?; + let name = absolute.file_name().ok_or_else(unusable)?.to_owned(); + // The deepest directory that exists, resolved; what follows it must be plain names. + let mut existing = absolute.parent().ok_or_else(unusable)?.to_owned(); + let mut pending = Vec::new(); + let resolved = loop { + match std::fs::canonicalize(&existing) { + Ok(resolved) => break resolved, + Err(_) => { + let Some(Component::Normal(last)) = existing.components().next_back() else { return Err(unusable()) }; + pending.push(last.to_owned()); + if !existing.pop() { + return Err(unusable()); + } + } + } + }; + let mut result = resolved; + result.extend(pending.into_iter().rev()); + result.push(name); + Ok(result) +} + +/// Refuses an output inside the package: it would become part of what is packed or run next. +pub fn require_outside(output: &Path, package: &Path) -> Result<(), Failure> { + let package = std::fs::canonicalize(package).unwrap_or_else(|_| package.to_owned()); + if output.starts_with(&package) { + return Err(Failure::environment("output.inside_package", "The output must be outside the package folder.")); + } + Ok(()) +} + +/// Writes `bytes` to `output` (a path from [`resolve`]). With `replace` an +/// existing regular file is replaced; without it the name must be free. +pub fn write(output: &Path, bytes: &[u8], replace: bool) -> Result<(), Failure> { + let failed = |what: &str, error: std::io::Error| Failure::environment("output.write", format!("{}: {what}: {error}", output.display())); + let (Some(parent), Some(name)) = (output.parent(), output.file_name()) else { + return Err(Failure::environment("output.path", format!("{} is not a usable output path", output.display()))); + }; + std::fs::create_dir_all(parent).map_err(|error| failed("the folder could not be created", error))?; + match std::fs::symlink_metadata(output) { + Ok(metadata) if metadata.file_type().is_symlink() => return Err(Failure::environment("output.symlink", format!("{} is a symbolic link; choose another output.", output.display()))), + Ok(metadata) if !metadata.is_file() => return Err(Failure::environment("output.not_a_file", format!("{} exists and is not a regular file.", output.display()))), + Ok(_) if !replace => return Err(Failure::environment("output.exists", format!("{} exists; choose a new file or pass --force.", output.display()))), + _ => {} + } + let mut token = [0u8; 8]; + getrandom::fill(&mut token).map_err(|error| Failure::internal(format!("No random source: {error}")))?; + let mut temporary_name = std::ffi::OsString::from("."); + temporary_name.push(name); + temporary_name.push(format!(".{}.tmp", token.iter().map(|byte| format!("{byte:02x}")).collect::())); + let temporary = parent.join(temporary_name); + let written = (|| { + let mut file = std::fs::OpenOptions::new().write(true).create_new(true).custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC).mode(0o644).open(&temporary)?; + file.write_all(bytes)?; + file.sync_all()?; + if replace { + std::fs::rename(&temporary, output) + } else { + // A hard link fails when the name is taken, whatever took it in the meantime. + let linked = std::fs::hard_link(&temporary, output); + let _ = std::fs::remove_file(&temporary); + linked + } + })(); + written.map_err(|error| { + let _ = std::fs::remove_file(&temporary); + if error.kind() == std::io::ErrorKind::AlreadyExists { Failure::environment("output.exists", format!("{} exists; choose a new file or pass --force.", output.display())) } else { failed("the file could not be written", error) } + }) +} diff --git a/Linux/crates/sw-cli/src/report.rs b/Linux/crates/sw-cli/src/report.rs new file mode 100644 index 00000000..3d880a19 --- /dev/null +++ b/Linux/crates/sw-cli/src/report.rs @@ -0,0 +1,193 @@ +//! The JSON report every command prints, and the fixed exit codes. +//! +//! The envelope is the one of the Mac command line +//! (`Tools/ScriptWidgetCLI/Resources/report-v1.schema.json`): `reportVersion`, +//! `command`, `status`, `tool`, `diagnostics`, and for package commands +//! `checks`, `backend`, `sourceHash` and `artifacts`. The other members are +//! the ones the Windows `swcli` prints for the same command (`ok`, `error`, +//! `tree`, `logs`, `pixelWidth`, …), under the same names. + +use std::io::{IsTerminal, Write}; + +use serde_json::{Map, Value, json}; +use sw_core::packages::PackageError; + +/// The command succeeded. +pub const EXIT_PASSED: i32 = 0; +/// The package, the archive or the script is at fault: an invalid manifest, a compile or script error, an unsafe archive entry. +pub const EXIT_FAILED: i32 = 1; +/// The command line is wrong, or the environment is: a missing folder, an output that cannot be written, missing runtime files. +pub const EXIT_USAGE: i32 = 2; +/// ScriptWidget itself failed. +pub const EXIT_INTERNAL: i32 = 3; +/// The command needs the background service, which is not running (`EX_UNAVAILABLE` of sysexits.h). +pub const EXIT_NEEDS_SERVICE: i32 = 69; + +/// Why a command stopped before it had a result of its own. +#[derive(Debug)] +pub struct Failure { + pub exit: i32, + pub code: String, + pub message: String, +} + +impl Failure { + pub fn new(exit: i32, code: &str, message: impl Into) -> Self { + Self { exit, code: code.to_owned(), message: message.into() } + } + + pub fn usage(message: impl Into) -> Self { + Self::new(EXIT_USAGE, "usage", message) + } + + /// A problem of the machine or the installation, not of the package. + pub fn environment(code: &str, message: impl Into) -> Self { + Self::new(EXIT_USAGE, code, message) + } + + pub fn internal(message: impl Into) -> Self { + Self::new(EXIT_INTERNAL, "tool.internal", message) + } +} + +impl From for Failure { + /// The package layer's codes are the diagnostics' codes. Only a library that cannot be used is the machine's fault. + fn from(error: PackageError) -> Self { + let exit = if error.code == sw_core::packages::codes::STORAGE_UNAVAILABLE { EXIT_USAGE } else { EXIT_FAILED }; + Self { exit, code: error.code, message: error.message } + } +} + +pub const STAGES: [&str; 5] = ["package", "compile", "execute", "render", "export"]; + +pub struct Report { + fields: Map, + diagnostics: Vec, + checks: Option>, + exit: i32, +} + +impl Report { + pub fn new(command: &str) -> Self { + let mut fields = Map::new(); + fields.insert("reportVersion".into(), json!(1)); + fields.insert("command".into(), json!(command)); + fields.insert("status".into(), Value::Null); + fields.insert("ok".into(), Value::Null); + fields.insert("tool".into(), json!({ "name": "scriptwidget", "version": env!("CARGO_PKG_VERSION"), "platform": "linux", "architecture": std::env::consts::ARCH })); + Self { fields, diagnostics: Vec::new(), checks: None, exit: EXIT_PASSED } + } + + /// A report for a package command: the five stages start as "notRun". + pub fn with_stages(command: &str, backend: &str) -> Self { + let mut report = Self::new(command); + report.set("backend", json!(backend)); + report.checks = Some(STAGES.iter().map(|stage| ((*stage).to_owned(), json!("notRun"))).collect()); + report + } + + pub fn set(&mut self, name: &str, value: Value) { + self.fields.insert(name.to_owned(), value); + } + + pub fn check(&mut self, name: &str, passed: bool) { + self.checks.get_or_insert_with(Map::new).insert(name.to_owned(), json!(if passed { "passed" } else { "failed" })); + } + + pub fn check_not_run(&mut self, name: &str) { + self.checks.get_or_insert_with(Map::new).insert(name.to_owned(), json!("notRun")); + } + + pub fn diagnostic(&mut self, severity: &str, code: &str, message: &str) { + self.diagnostics.push(json!({ "severity": severity, "code": code, "message": message })); + } + + /// Diagnostics as another layer produced them: objects with `severity`, `code` and `message`. + pub fn diagnostics(&mut self, items: impl IntoIterator) { + self.diagnostics.extend(items.into_iter().filter(Value::is_object)); + } + + pub fn has_warning(&self) -> bool { + self.diagnostics.iter().any(|item| item["severity"] == "warning") + } + + /// Marks the command as failed with `error` as its reason: `{kind, message}`, as the Windows reports have it. + pub fn fail(&mut self, exit: i32, kind: &str, message: &str) { + if self.exit == EXIT_PASSED { + self.exit = exit; + self.set("error", json!({ "kind": kind, "message": message })); + self.diagnostic("error", kind, message); + } + } + + /// Fails a report that passed so far, without replacing an earlier reason. + pub fn fail_on_warnings(&mut self) { + if self.exit == EXIT_PASSED && self.has_warning() { + self.exit = EXIT_FAILED; + } + } + + pub fn from_failure(command: &str, failure: &Failure) -> Self { + let mut report = Self::new(command); + report.fail(failure.exit, &failure.code, &failure.message); + report + } + + /// Adds a failure that happened part-way to a report that already has results. + pub fn absorb(&mut self, failure: &Failure) { + self.fail(failure.exit, &failure.code, &failure.message); + } + + /// Prints the report to standard output and its errors and warnings to standard error; returns the exit code. + pub fn finish(mut self) -> i32 { + let status = match self.exit { + EXIT_PASSED => "passed", + EXIT_FAILED => "failed", + _ => "error", + }; + self.fields.insert("status".into(), json!(status)); + self.fields.insert("ok".into(), json!(self.exit == EXIT_PASSED)); + if !self.fields.contains_key("error") { + self.fields.insert("error".into(), Value::Null); + } + if let Some(checks) = self.checks.take() { + self.fields.insert("checks".into(), Value::Object(checks)); + } + print_diagnostics(&self.diagnostics); + self.fields.insert("diagnostics".into(), Value::Array(std::mem::take(&mut self.diagnostics))); + let text = serde_json::to_string_pretty(&Value::Object(self.fields)).unwrap_or_else(|_| "{}".into()); + let mut output = std::io::stdout().lock(); + // A closed pipe is the reader's choice; the exit code still tells the result. + let _ = writeln!(output, "{text}"); + let _ = output.flush(); + self.exit + } +} + +/// True when messages for a person may carry color: both streams are a +/// terminal, and the user did not ask for plain text. Reports are never colored. +fn color() -> bool { + std::io::stdout().is_terminal() + && std::io::stderr().is_terminal() + && std::env::var_os("NO_COLOR").is_none_or(|value| value.is_empty()) + && std::env::var_os("TERM").is_none_or(|term| term != "dumb") +} + +fn print_diagnostics(diagnostics: &[Value]) { + let color = color(); + let mut errors = std::io::stderr().lock(); + for item in diagnostics { + let severity = item["severity"].as_str().unwrap_or("error"); + if severity != "error" && severity != "warning" { + continue; + } + let (start, end) = match (color, severity) { + (false, _) => ("", ""), + (true, "warning") => ("\x1b[33m", "\x1b[0m"), + (true, _) => ("\x1b[31m", "\x1b[0m"), + }; + // Control characters of a message that came from a package never reach the terminal. + let message: String = item["message"].as_str().unwrap_or("").chars().map(|character| if character.is_control() && character != '\n' { ' ' } else { character }).collect(); + let _ = writeln!(errors, "{start}{severity}{end}: [{}] {message}", item["code"].as_str().unwrap_or("")); + } +} diff --git a/Linux/crates/sw-cli/tests/cli.rs b/Linux/crates/sw-cli/tests/cli.rs new file mode 100644 index 00000000..6b490718 --- /dev/null +++ b/Linux/crates/sw-cli/tests/cli.rs @@ -0,0 +1,943 @@ +//! Runs the built `scriptwidget` program against temporary folders: every +//! command that needs no background service, the exit codes, the report +//! shapes, and the archive and path rules as they are reached through the +//! command line. +//! +//! node Linux/scripts/build-shared-js.mjs +//! cargo test -p sw-cli +//! +//! Every run starts from an empty environment: no `DISPLAY`, no +//! `WAYLAND_DISPLAY`, a home folder of its own. + +#[path = "../../sw-core/tests/packages_support/mod.rs"] +mod support; + +use std::path::{Path, PathBuf}; +use std::process::Command; + +use serde_json::Value; +use support::{RawEntry, SYMBOLIC_LINK, TemporaryFolder, raw_zip, read_zip}; + +const EXIT_FAILED: i32 = 1; +const EXIT_USAGE: i32 = 2; +const EXIT_NEEDS_SERVICE: i32 = 69; + +/// 2026-01-01 12:00 UTC. +const FIXED_NOW: &str = "1767268800000"; + +fn repository() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../..") +} + +fn runtime_assets() -> PathBuf { + repository().join("Linux/assets/scriptwidget") +} + +fn bundled_templates() -> PathBuf { + repository().join("Shared/ScriptWidgetRuntime/Resource/Script.bundle") +} + +struct Outcome { + code: i32, + stdout: String, + stderr: String, +} + +impl Outcome { + #[track_caller] + fn report(&self) -> Value { + let report: Value = serde_json::from_str(&self.stdout).unwrap_or_else(|error| panic!("standard output is not one JSON report ({error}):\n{}\n{}", self.stdout, self.stderr)); + assert_eq!(report["reportVersion"], 1); + assert!(!self.stdout.contains('\u{1b}') && !self.stderr.contains('\u{1b}'), "no color codes when the output is not a terminal"); + let status = match self.code { + 0 => "passed", + 1 => "failed", + _ => "error", + }; + assert_eq!(report["status"], status, "status follows the exit code {}:\n{}", self.code, self.stdout); + assert_eq!(report["ok"], self.code == 0); + report + } + + #[track_caller] + fn expect(&self, code: i32) -> Value { + assert_eq!(self.code, code, "exit code\nstdout: {}\nstderr: {}", self.stdout, self.stderr); + self.report() + } +} + +/// One isolated user: a home folder, a data folder and a working folder. +struct Sandbox { + scratch: TemporaryFolder, +} + +impl Sandbox { + fn new() -> Self { + let scratch = TemporaryFolder::new(); + for folder in ["home", "work"] { + scratch.new_folder(folder); + } + Self { scratch } + } + + fn path(&self, relative: &str) -> PathBuf { + self.scratch.path(relative) + } + + fn work(&self) -> PathBuf { + self.path("work") + } + + fn data(&self) -> PathBuf { + self.path("data") + } + + /// A command with nothing inherited: in particular no display of any kind. + fn command(&self) -> Command { + let mut command = Command::new(env!("CARGO_BIN_EXE_scriptwidget")); + command.env_clear().env_remove("DISPLAY").env_remove("WAYLAND_DISPLAY"); + command.env("HOME", self.path("home")).env("SCRIPTWIDGET_DATA", self.data()).env("TZ", "UTC").env("LANG", "en_US.UTF-8"); + command.env("SCRIPTWIDGET_RUNTIME_ASSETS", runtime_assets()).env("SCRIPTWIDGET_TEMPLATES", bundled_templates()); + command.current_dir(self.work()); + command + } + + fn run(&self, arguments: &[&str]) -> Outcome { + finish(self.command().args(arguments)) + } + + /// A package folder in the working folder, with a Package 2.0 manifest. + fn package(&self, name: &str, source: &str) -> PathBuf { + let directory = self.scratch.new_folder(&format!("work/{name}")); + self.scratch.new_file(&format!("work/{name}/main.jsx"), source); + self.scratch.new_file(&format!("work/{name}/widget.json"), &manifest(name, "")); + directory + } +} + +fn finish(command: &mut Command) -> Outcome { + let output = command.output().expect("the program starts"); + Outcome { code: output.status.code().expect("the program exits by itself"), stdout: String::from_utf8(output.stdout).expect("UTF-8 output"), stderr: String::from_utf8(output.stderr).expect("UTF-8 errors") } +} + +fn manifest(name: &str, extra: &str) -> String { + format!( + r#"{{"formatVersion":2,"id":"com.example.cli","name":"{name}","version":"1.0.0","runtimeVersion":"1.0","entry":"main.jsx","supportedFamilies":["systemSmall","systemMedium","systemLarge"],"permissions":[],"networkDomains":[]{extra}}}"# + ) +} + +fn text(path: &Path) -> &str { + path.to_str().expect("UTF-8 path") +} + +/// The authored files of a folder: no folders, no dot-files (an installed package holds its identity in one). +fn package_files(directory: &Path) -> Vec { + support::tree(directory).into_iter().filter(|path| !path.ends_with('/') && !path.split('/').any(|part| part.starts_with('.'))).collect() +} + +fn checks(report: &Value) -> [&str; 5] { + ["package", "compile", "execute", "render", "export"].map(|stage| report["checks"][stage].as_str().unwrap_or("missing")) +} + +fn codes(report: &Value) -> Vec<&str> { + report["diagnostics"].as_array().expect("diagnostics").iter().filter_map(|item| item["code"].as_str()).collect() +} + +/// Width and height from the header of a PNG file. +fn png_size(path: &Path) -> (u32, u32) { + let bytes = std::fs::read(path).unwrap_or_else(|error| panic!("{}: {error}", path.display())); + assert_eq!(&bytes[..8], b"\x89PNG\r\n\x1a\n", "{} is a PNG", path.display()); + assert_eq!(&bytes[12..16], b"IHDR"); + let number = |at: usize| u32::from_be_bytes(bytes[at..at + 4].try_into().unwrap()); + (number(16), number(20)) +} + +#[track_caller] +fn assert_has_a_picture(report: &Value) { + assert!(report["picture"]["visiblePixels"].as_u64() > Some(0), "something is visible: {}", report["picture"]); + assert!(report["picture"]["colors"].as_u64() > Some(1), "more than one color: {}", report["picture"]); +} + +const HELLO: &str = "$render(Hello 你好);"; + +// The command surface + +#[test] +fn help_goes_to_standard_output_and_no_command_is_a_usage_error() { + let sandbox = Sandbox::new(); + for flag in ["--help", "-h", "help"] { + let help = sandbox.run(&[flag]); + assert_eq!(help.code, 0); + assert!(help.stdout.starts_with("Usage: scriptwidget ") && help.stderr.is_empty()); + } + let nothing = sandbox.run(&[]); + assert_eq!(nothing.code, EXIT_USAGE); + assert!(nothing.stdout.is_empty() && nothing.stderr.starts_with("Usage: scriptwidget")); +} + +#[test] +fn the_version_is_a_report() { + let sandbox = Sandbox::new(); + for flag in ["--version", "version"] { + let report = sandbox.run(&[flag]).expect(0); + assert_eq!(report["version"], env!("CARGO_PKG_VERSION")); + assert_eq!(report["tool"]["version"], env!("CARGO_PKG_VERSION")); + assert_eq!(report["command"], "--version"); + } +} + +#[test] +fn mistakes_on_the_command_line_exit_with_2_and_say_why() { + let sandbox = Sandbox::new(); + let package = sandbox.package("Widget", HELLO); + for arguments in [ + &["frobnicate"][..], + &["check"], + &["check", text(&package), "extra"], + &["check", text(&package), "--unknown"], + &["check", text(&package), "--format", "text"], + &["run", text(&package), "--family", "huge"], + &["run", text(&package), "--appearance", "both"], + &["run", text(&package), "--family"], + &["render", text(&package)], + &["render", text(&package), "--output", "x.png", "--scale", "64"], + &["render", text(&package), "--output", "x.png", "--now", "tomorrow"], + &["timeline", text(&package), "--now", "1.5"], + &["list", "extra"], + &["new", "../escape"], + &["new", ".hidden"], + &["pack", text(&package), "a.swt", "--output", "b.swt"], + &["pack", text(&package), "--output", "archive.zip"], + ] { + let outcome = sandbox.run(arguments); + let report = outcome.expect(EXIT_USAGE); + assert_eq!(report["error"]["kind"], "usage", "{arguments:?}"); + assert_eq!(codes(&report), ["usage"], "{arguments:?}"); + assert!(outcome.stderr.starts_with("error: [usage] "), "{arguments:?}: {}", outcome.stderr); + } + assert!(!sandbox.path("work/x.png").exists() && !sandbox.path("escape").exists()); +} + +// The commands that need the background service are tested in `desktop.rs`. +#[test] +fn a_desktop_command_without_a_session_needs_the_service() { + let sandbox = Sandbox::new(); + let outcome = sandbox.run(&["widgets"]); + assert_eq!(outcome.expect(EXIT_NEEDS_SERVICE)["error"]["kind"], "service.unavailable"); +} + +#[test] +fn a_missing_package_folder_is_an_environment_error() { + let sandbox = Sandbox::new(); + for command in ["check", "run", "timeline", "pack"] { + let report = sandbox.run(&[command, "nowhere"]).expect(EXIT_USAGE); + assert_eq!(report["error"]["kind"], "input.directory", "{command}"); + } + assert_eq!(sandbox.run(&["render", "nowhere", "--output", "x.png"]).expect(EXIT_USAGE)["error"]["kind"], "input.directory"); + assert_eq!(sandbox.run(&["install", "nowhere.swt"]).expect(EXIT_USAGE)["error"]["kind"], "input.file"); +} + +// new + +#[test] +fn new_creates_a_package_that_checks_and_renders() { + let sandbox = Sandbox::new(); + let report = sandbox.run(&["new", "My Widget"]).expect(0); + let directory = sandbox.path("work/My Widget"); + assert_eq!(report["directory"], text(&std::fs::canonicalize(&directory).unwrap())); + assert_eq!(report["template"], "starter"); + assert_eq!(report["files"], serde_json::json!(["main.jsx", "widget.json"])); + let written: Value = serde_json::from_str(&std::fs::read_to_string(directory.join("widget.json")).unwrap()).unwrap(); + assert_eq!((written["name"].as_str(), written["formatVersion"].as_i64(), written["entry"].as_str()), (Some("My Widget"), Some(2), Some("main.jsx"))); + assert!(written["id"].as_str().unwrap().starts_with("local.")); + + assert_eq!(checks(&sandbox.run(&["check", "My Widget"]).expect(0)), ["passed", "passed", "notRun", "notRun", "notRun"]); + assert_has_a_picture(&sandbox.run(&["render", "My Widget", "--output", "starter.png"]).expect(0)); + + // The name is taken now: nothing is written into the existing folder. + std::fs::write(directory.join("main.jsx"), "// edited").unwrap(); + let again = sandbox.run(&["new", "My Widget"]).expect(EXIT_FAILED); + assert_eq!(again["error"]["kind"], "package_exists"); + assert_eq!(std::fs::read_to_string(directory.join("main.jsx")).unwrap(), "// edited"); +} + +#[test] +fn new_copies_a_bundled_template_under_the_new_name() { + let sandbox = Sandbox::new(); + let elsewhere = sandbox.scratch.new_folder("elsewhere"); + // A template with a manifest of its own, and one without (it gets a generated one). + for (template, name) in [("template/Layout Starter", "From Manifest"), ("component/text", "From Legacy"), ("Countdown", "By Name")] { + let report = sandbox.run(&["new", name, "--template", template, "--directory", text(&elsewhere)]).expect(0); + assert!(report["template"].as_str().unwrap().ends_with(template), "{}", report["template"]); + let directory = elsewhere.join(name); + let written: Value = serde_json::from_str(&std::fs::read_to_string(directory.join("widget.json")).unwrap()).unwrap(); + assert_eq!(written["name"], name); + assert!(written["id"].as_str().unwrap().starts_with("local."), "a copy never keeps the template's identity: {}", written["id"]); + assert_eq!(checks(&sandbox.run(&["check", text(&directory)]).expect(0)), ["passed", "passed", "notRun", "notRun", "notRun"], "{template}"); + } + assert_eq!(sandbox.run(&["new", "X", "--template", "template/No Such Template"]).expect(EXIT_USAGE)["error"]["kind"], "usage"); + // A template that needs an API Linux lacks is not offered. + assert_eq!(sandbox.run(&["new", "X", "--template", "api/health"]).expect(EXIT_USAGE)["error"]["kind"], "usage"); + assert!(!sandbox.path("work/X").exists()); +} + +// check + +#[test] +fn check_validates_and_compiles_without_executing() { + let sandbox = Sandbox::new(); + let package = sandbox.package("Widget", "throw new Error('check must not execute');"); + let report = sandbox.run(&["check", text(&package), "--format", "json"]).expect(0); + assert_eq!(report["command"], "check"); + assert_eq!(report["backend"], "static"); + assert_eq!(checks(&report), ["passed", "passed", "notRun", "notRun", "notRun"]); + assert_eq!(report["manifest"]["ok"], true); + assert_eq!(report["manifest"]["manifest"]["name"], "Widget"); + assert_eq!(report["sourceHashAlgorithm"], "ScriptWidgetSource/v1"); + assert_eq!(report["sourceHash"].as_str().map(str::len), Some(64)); + assert_eq!(report["error"], Value::Null); + // The Windows name of the command. + assert_eq!(sandbox.run(&["validate", text(&package)]).expect(0)["command"], "check"); +} + +#[test] +fn the_source_hash_is_the_documented_fingerprint() { + use std::fmt::Write; + let sandbox = Sandbox::new(); + let package = sandbox.package("Widget", "$render(跨语言 ✓);"); + sandbox.scratch.new_folder("work/Widget/image"); + std::fs::write(package.join("image/色.png"), [0u8, 255, 42]).unwrap(); + std::fs::write(package.join(".hidden"), "not part of the package").unwrap(); + // ScriptWidgetSource/v1: a count, then path and content of every file in byte order of the path, each behind its length. + let mut files: Vec<(String, Vec)> = ["image/色.png", "main.jsx", "widget.json"].iter().map(|path| (path.to_string(), std::fs::read(package.join(path)).unwrap())).collect(); + files.sort(); + let mut bytes = b"ScriptWidgetSource/v1\n".to_vec(); + bytes.extend((files.len() as u64).to_be_bytes()); + for (path, content) in &files { + bytes.extend((path.len() as u64).to_be_bytes()); + bytes.extend(path.as_bytes()); + bytes.extend((content.len() as u64).to_be_bytes()); + bytes.extend(content); + } + let expected = sha256(&bytes).iter().fold(String::new(), |mut hex, byte| { + write!(hex, "{byte:02x}").unwrap(); + hex + }); + assert_eq!(sandbox.run(&["check", text(&package)]).expect(0)["sourceHash"], expected); +} + +#[test] +fn check_reports_what_is_wrong_with_a_package() { + let sandbox = Sandbox::new(); + + let broken = sandbox.package("Broken", "$render(wrong);"); + let report = sandbox.run(&["check", text(&broken)]).expect(EXIT_FAILED); + assert_eq!(checks(&report), ["passed", "failed", "notRun", "notRun", "notRun"]); + assert_eq!(report["error"]["kind"], "scriptException", "the engine's kind for a source that does not parse"); + assert!(report["error"]["message"].as_str().unwrap().contains("SyntaxError"), "{}", report["error"]); + + let unknown_key = sandbox.package("Unknown", HELLO); + std::fs::write(unknown_key.join("widget.json"), manifest("Unknown", ",\"surprise\":true")).unwrap(); + let outcome = sandbox.run(&["check", text(&unknown_key)]); + let report = outcome.expect(EXIT_FAILED); + assert_eq!(checks(&report), ["failed", "notRun", "notRun", "notRun", "notRun"]); + assert_eq!(report["error"]["kind"], "invalid_manifest"); + assert_eq!(report["manifest"]["ok"], false); + assert!(report["error"]["message"].as_str().unwrap().contains("surprise"), "unknown keys fail closed and are named: {}", report["error"]); + assert!(outcome.stderr.starts_with("error: [invalid_manifest] "), "{}", outcome.stderr); + + // Every issue of the validator is a diagnostic; the first one is the reason. + let issues = sandbox.package("Issues", HELLO); + std::fs::write(issues.join("widget.json"), manifest("Issues", "").replace("\"entry\":\"main.jsx\"", "\"entry\":\"nope.jsx\"").replace("\"permissions\":[]", "\"permissions\":[\"network\"]")).unwrap(); + let report = sandbox.run(&["check", text(&issues)]).expect(EXIT_FAILED); + assert_eq!(codes(&report), ["missing_entry", "missing_network_domains", "invalid_manifest"]); + assert!(report["error"]["message"].as_str().unwrap().contains("[missing_entry]"), "{}", report["error"]); + + let future = sandbox.package("Future", HELLO); + std::fs::write(future.join("widget.json"), manifest("Future", "").replace("\"formatVersion\":2", "\"formatVersion\":99")).unwrap(); + assert_eq!(sandbox.run(&["check", text(&future)]).expect(EXIT_FAILED)["error"]["kind"], "invalid_manifest", "an unsupported version fails closed"); + + let nothing = sandbox.scratch.new_folder("work/Empty"); + assert_eq!(sandbox.run(&["check", text(¬hing)]).expect(EXIT_FAILED)["error"]["kind"], "missing_entry"); +} + +#[test] +fn a_present_manifest_that_cannot_be_read_is_never_treated_as_legacy() { + let sandbox = Sandbox::new(); + for (name, content) in [("Malformed", "{ not json"), ("Array", "[]"), ("Duplicate", r#"{"name":"a","name":"b"}"#), ("Binary", "\u{0}\u{1}")] { + let package = sandbox.package(name, HELLO); + std::fs::write(package.join("widget.json"), content).unwrap(); + for command in [&["check"][..], &["run"], &["pack"], &["install"], &["timeline"]] { + let report = sandbox.run(&[command[0], text(&package)]).expect(EXIT_FAILED); + assert_eq!(report["error"]["kind"], "invalid_manifest", "{name} {command:?}: {report}"); + } + assert_eq!(sandbox.run(&["render", text(&package), "--output", "legacy.png"]).expect(EXIT_FAILED)["error"]["kind"], "invalid_manifest"); + } + assert!(!sandbox.path("work/legacy.png").exists() && !sandbox.path("work/Malformed.swt").exists()); + assert_eq!(sandbox.run(&["list"]).expect(0)["packages"], serde_json::json!([])); + + // Another spelling of the name is the manifest on other platforms: an error here too. + let wrong_case = sandbox.scratch.new_folder("work/WrongCase"); + std::fs::write(wrong_case.join("main.jsx"), HELLO).unwrap(); + std::fs::write(wrong_case.join("Widget.JSON"), manifest("WrongCase", "")).unwrap(); + assert_eq!(sandbox.run(&["check", text(&wrong_case)]).expect(EXIT_FAILED)["error"]["kind"], "invalid_manifest_case"); + + // A legacy package (main.jsx and no manifest) is still accepted. + let legacy = sandbox.scratch.new_folder("work/Legacy"); + std::fs::write(legacy.join("main.jsx"), HELLO).unwrap(); + let report = sandbox.run(&["check", text(&legacy)]).expect(0); + assert_eq!(report["manifest"], Value::Null); +} + +// run + +#[test] +fn run_prints_the_tree_logs_and_the_environment_it_ran_in() { + let sandbox = Sandbox::new(); + let package = sandbox.package( + "Widget", + "console.log('from the script'); $storage.setString('k', 'v');\n$render({$getenv('widget-size')}|{$getenv('widget-param')}|{String($device.isdarkmode())}|{$system.platform()}|{$storage.getString('k')});", + ); + let report = sandbox.run(&["run", text(&package), "--family", "large", "--param", "p q", "--dark"]).expect(0); + assert_eq!(report["backend"], "linux-native"); + assert_eq!(checks(&report), ["passed", "passed", "passed", "notRun", "notRun"]); + assert_eq!(report["tree"]["$element"], "text"); + assert_eq!(report["tree"]["children"], serde_json::json!(["large", "|", "p q", "|", "true", "|", "linux", "|", "v"])); + assert_eq!(report["logs"], serde_json::json!([{ "level": "log", "message": "from the script" }])); + assert!(report["elapsedMilliseconds"].as_f64().unwrap() > 0.0); + assert_eq!(report["nextTimelineTransition"], Value::Null); + + let light = sandbox.run(&["run", text(&package), "--appearance", "light"]).expect(0); + assert_eq!(light["tree"]["children"][0], "medium"); + assert_eq!(light["tree"]["children"][4], "false"); + + // Without --data, storage and caches live in a temporary folder that is gone afterwards. + assert!(!sandbox.data().exists(), "the user's data folder is not touched"); + assert!(std::fs::read_dir(sandbox.path("home")).unwrap().next().is_none(), "nothing is left in the home folder"); + // With --data they are kept there. + let kept = sandbox.path("kept"); + sandbox.run(&["run", text(&package), "--data", text(&kept)]).expect(0); + assert!(kept.join("Storage").is_dir()); +} + +#[test] +fn run_reports_script_errors_with_exit_1() { + let sandbox = Sandbox::new(); + let throwing = sandbox.package("Throwing", "console.log('before'); throw new Error('boom');"); + let outcome = sandbox.run(&["run", text(&throwing)]); + let report = outcome.expect(EXIT_FAILED); + assert_eq!(checks(&report), ["passed", "passed", "failed", "notRun", "notRun"]); + assert_eq!(report["tree"], Value::Null); + assert_eq!(report["error"]["kind"], "scriptException"); + assert!(report["error"]["message"].as_str().unwrap().contains("boom")); + assert_eq!(report["logs"][0]["message"], "before"); + assert!(outcome.stderr.contains("boom")); + + let endless = sandbox.package("Endless", "while (true) {}"); + let report = sandbox.run(&["run", text(&endless)]).expect(EXIT_FAILED); + assert_eq!(report["error"]["kind"], "resourceLimit"); + + let syntax = sandbox.package("Syntax", "$render("); + assert_eq!(checks(&sandbox.run(&["run", text(&syntax)]).expect(EXIT_FAILED)), ["passed", "failed", "notRun", "notRun", "notRun"]); +} + +#[test] +fn a_package_cannot_read_outside_its_folder() { + let sandbox = Sandbox::new(); + sandbox.scratch.new_file("work/secret.txt", "secret"); + let package = sandbox.package("Reader", "$render({JSON.stringify($file.read('../secret.txt'))}|{String($file.exists('../secret.txt'))}|{String($file.exists('/etc/passwd'))}|{String($file.exists('widget.json'))});"); + let report = sandbox.run(&["run", text(&package)]).expect(0); + assert_eq!(report["tree"]["children"], serde_json::json!(["\"\"", "|", "false", "|", "false", "|", "true"])); +} + +#[test] +fn warnings_fail_the_command_only_when_asked() { + let sandbox = Sandbox::new(); + let package = sandbox.package("Promise", "Promise.all([]); $render(ok);"); + let outcome = sandbox.run(&["run", text(&package)]); + let report = outcome.expect(0); + assert!(codes(&report).contains(&"compat.promise"), "{}", report["diagnostics"]); + assert!(outcome.stderr.starts_with("warning: [compat.promise] "), "{}", outcome.stderr); + let strict = sandbox.run(&["run", text(&package), "--fail-on-warning"]).expect(EXIT_FAILED); + assert_eq!(checks(&strict), ["passed", "passed", "passed", "notRun", "notRun"]); + assert_eq!(strict["error"], Value::Null, "the warning is the reason; nothing else failed"); +} + +// render + +#[test] +fn render_draws_a_png_without_a_graphical_session() { + let sandbox = Sandbox::new(); + let package = sandbox.package("Widget", "$render(Hello 你好 🌤️);"); + let mut command = sandbox.command(); + // Said again where it matters: no X11 and no Wayland display. + command.env_remove("DISPLAY").env_remove("WAYLAND_DISPLAY").env_remove("XDG_RUNTIME_DIR"); + let outcome = finish(command.args(["render", text(&package), "--family", "medium", "--scale", "2", "--output", "out/deep/widget.png", "--now", FIXED_NOW])); + let report = outcome.expect(0); + let output = std::fs::canonicalize(sandbox.work()).unwrap().join("out/deep/widget.png"); + assert_eq!(checks(&report), ["passed", "passed", "passed", "passed", "notRun"]); + assert_eq!(report["output"], text(&output)); + assert_eq!((report["pixelWidth"].as_u64(), report["pixelHeight"].as_u64()), (Some(658), Some(340))); + assert_eq!(png_size(&output), (658, 340)); + assert_eq!(report["family"], "medium"); + assert_eq!(report["scale"], 2.0); + assert_has_a_picture(&report); + let artifact = &report["artifacts"][0]; + let png = std::fs::read(&output).unwrap(); + assert_eq!(artifact["path"], text(&output)); + assert_eq!(artifact["bytes"], png.len()); + assert_eq!(artifact["sha256"].as_str().unwrap(), sha256(&png).iter().map(|byte| format!("{byte:02x}")).collect::()); + let region = &report["hitRegions"][0]; + assert_eq!(region["interaction"]["type"], "button"); + assert_eq!(region["interaction"]["action"], "reload"); + assert!(region["width"].as_f64().unwrap() > 10.0 && region["height"].as_f64().unwrap() > 5.0 && region["x"].as_f64().unwrap() > 0.0, "{region}"); + assert!(report["layoutMilliseconds"].is_number() && report["paintMilliseconds"].is_number() && report["renderDiagnostics"].is_array()); + + // Every family at its size in points, times the scale. + for (family, scale, size) in [("small", "1", (170, 170)), ("large", "1", (329, 345)), ("extraLarge", "1.5", (959, 518))] { + let file = format!("{family}.png"); + let report = sandbox.run(&["render", text(&package), "--family", family, "--scale", scale, "--output", &file, "--dark"]).expect(0); + assert_eq!(png_size(&sandbox.work().join(&file)), size, "{family}"); + assert_has_a_picture(&report); + } + // The same input draws the same picture. + let first = sandbox.run(&["render", text(&package), "--output", "a.png", "--now", FIXED_NOW]).expect(0); + let second = sandbox.run(&["render", text(&package), "--output", "b.png", "--now", FIXED_NOW]).expect(0); + assert_eq!(first["artifacts"][0]["sha256"], second["artifacts"][0]["sha256"]); +} + +#[test] +fn a_script_error_is_drawn_as_a_card_and_still_fails() { + let sandbox = Sandbox::new(); + let package = sandbox.package("Throwing", "throw new Error('boom');"); + let report = sandbox.run(&["render", text(&package), "--output", "error.png"]).expect(EXIT_FAILED); + assert_eq!(checks(&report), ["passed", "passed", "failed", "notRun", "notRun"]); + assert_eq!(report["error"]["kind"], "scriptException"); + assert_eq!(png_size(&sandbox.path("work/error.png")), (658, 340)); + assert_has_a_picture(&report); +} + +#[test] +fn render_writes_only_the_file_it_was_asked_for() { + let sandbox = Sandbox::new(); + let package = sandbox.package("Widget", HELLO); + + // Inside the package: refused, also through a link to the folder and with dots in the path. + support::symlink(&sandbox.path("work/alias"), &package); + for output in ["Widget/out.png", "Widget/nested/out.png", "alias/out.png", "elsewhere/../Widget/out.png"] { + let report = sandbox.run(&["render", text(&package), "--output", output]).expect(EXIT_USAGE); + assert_eq!(report["error"]["kind"], if output.starts_with("elsewhere") { "output.path" } else { "output.inside_package" }, "{output}"); + } + assert_eq!(support::tree(&package), ["main.jsx", "widget.json"], "the package is unchanged"); + + // A link at the output name is refused, and what it points to is not written. + let precious = sandbox.scratch.new_file("precious.txt", "precious"); + support::symlink(&sandbox.path("work/link.png"), &precious); + let report = sandbox.run(&["render", text(&package), "--output", "link.png"]).expect(EXIT_USAGE); + assert_eq!(report["error"]["kind"], "output.symlink"); + assert_eq!(std::fs::read_to_string(&precious).unwrap(), "precious"); + // So is a dangling link, which would otherwise create its target. + support::symlink(&sandbox.path("work/dangling.png"), &sandbox.path("created-through-link.png")); + assert_eq!(sandbox.run(&["render", text(&package), "--output", "dangling.png"]).expect(EXIT_USAGE)["error"]["kind"], "output.symlink"); + assert!(!sandbox.path("created-through-link.png").exists()); + // A folder is not replaced. + sandbox.scratch.new_folder("work/folder.png"); + assert_eq!(sandbox.run(&["render", text(&package), "--output", "folder.png"]).expect(EXIT_USAGE)["error"]["kind"], "output.not_a_file"); + + // A regular file is replaced, and nothing else appears next to it. + sandbox.scratch.new_folder("work/pictures"); + sandbox.scratch.new_file("work/pictures/old.png", "old"); + sandbox.run(&["render", text(&package), "--output", "pictures/old.png"]).expect(0); + assert_eq!(png_size(&sandbox.path("work/pictures/old.png")), (658, 340)); + assert_eq!(support::tree(&sandbox.path("work/pictures")), ["old.png"], "no temporary file is left"); +} + +// timeline + +#[test] +fn timeline_reports_the_slot_and_the_next_transition() { + let sandbox = Sandbox::new(); + let plain = sandbox.package("Plain", HELLO); + let report = sandbox.run(&["timeline", text(&plain)]).expect(0); + assert_eq!((report["hasSchedule"].as_bool(), &report["environments"], &report["nextTransition"]), (Some(false), &Value::Null, &Value::Null)); + + let scheduled = sandbox.package("Scheduled", "$render({$getenv('timeline-slot')});"); + std::fs::write(scheduled.join("widget.json"), manifest("Scheduled", ",\"timeline\":{\"dailyTimes\":[\"06:00\",\"18:00\"]}")).unwrap(); + assert_eq!(sandbox.run(&["check", text(&scheduled)]).expect(0)["manifest"]["ok"], true); + // 2026-01-01 12:00 UTC, in a process whose zone is UTC: the 06:00 slot, until 18:00. + let report = sandbox.run(&["timeline", text(&scheduled), "--now", FIXED_NOW]).expect(0); + assert_eq!(report["hasSchedule"], true); + assert_eq!(report["nextTransition"], 1_767_268_800_000i64 + 6 * 3_600_000); + assert!(report["environments"].as_object().is_some_and(|environments| !environments.is_empty()), "{}", report["environments"]); + // run reports the same transition for the host to wake at. + assert!(sandbox.run(&["run", text(&scheduled)]).expect(0)["nextTimelineTransition"].as_i64() > Some(1_767_268_800_000)); +} + +// pack + +#[test] +fn pack_writes_an_archive_that_installs_again() { + let sandbox = Sandbox::new(); + let package = sandbox.package("Widget", "throw new Error('pack must not execute');"); + sandbox.scratch.new_file("work/Widget/lib/util.js", "function util() {}"); + sandbox.scratch.new_file("work/Widget/.secret", "never exported"); + sandbox.scratch.new_file("work/Widget/__Build/prepared-0.json", "{}"); + + let report = sandbox.run(&["pack", text(&package)]).expect(0); + let archive = std::fs::canonicalize(sandbox.work()).unwrap().join("Widget.swt"); + assert_eq!(checks(&report), ["passed", "passed", "notRun", "notRun", "passed"]); + assert_eq!(report["output"], text(&archive)); + assert_eq!(report["name"], "Widget"); + let bytes = std::fs::read(&archive).unwrap(); + assert_eq!(report["artifacts"][0]["bytes"], bytes.len()); + let names: Vec = read_zip(&bytes).into_iter().map(|(name, _)| name).collect(); + assert_eq!(names, ["Widget/lib/util.js", "Widget/main.jsx", "Widget/widget.json"], "dot-files and the build cache stay out"); + + // An existing archive is kept unless --force is given; the same package packs to the same bytes. + let again = sandbox.run(&["pack", text(&package)]).expect(EXIT_USAGE); + assert_eq!(again["error"]["kind"], "output.exists"); + std::fs::write(&archive, "old").unwrap(); + sandbox.run(&["pack", text(&package), "--force"]).expect(0); + assert_eq!(std::fs::read(&archive).unwrap(), bytes); + // The output can be named with --output or, as on Windows, as the second argument. + sandbox.run(&["pack", text(&package), "--output", "named/one.swt"]).expect(0); + sandbox.run(&["pack", text(&package), "two.swt"]).expect(0); + assert_eq!(std::fs::read(sandbox.path("work/named/one.swt")).unwrap(), bytes); + assert_eq!(std::fs::read(sandbox.path("work/two.swt")).unwrap(), bytes); + + // A legacy package gets a manifest in the archive; the folder is not modified. + let legacy = sandbox.scratch.new_folder("work/Old Style"); + std::fs::write(legacy.join("main.jsx"), HELLO).unwrap(); + sandbox.run(&["pack", text(&legacy)]).expect(0); + let entries = read_zip(&std::fs::read(sandbox.path("work/Old Style.swt")).unwrap()); + assert_eq!(entries.iter().map(|(name, _)| name.as_str()).collect::>(), ["Old Style/main.jsx", "Old Style/widget.json"]); + assert_eq!(support::tree(&legacy), ["main.jsx"]); + + let installed = sandbox.run(&["install", text(&archive)]).expect(0); + assert_eq!(installed["name"], "Widget"); +} + +#[test] +fn pack_refuses_what_does_not_compile_and_unsafe_outputs() { + let sandbox = Sandbox::new(); + let broken = sandbox.package("Broken", "$render("); + let report = sandbox.run(&["pack", text(&broken)]).expect(EXIT_FAILED); + assert_eq!(checks(&report), ["passed", "failed", "notRun", "notRun", "notRun"]); + assert!(!sandbox.path("work/Broken.swt").exists()); + + let package = sandbox.package("Widget", HELLO); + assert_eq!(sandbox.run(&["pack", text(&package), "--output", "Widget/inside.swt"]).expect(EXIT_USAGE)["error"]["kind"], "output.inside_package"); + let precious = sandbox.scratch.new_file("precious.txt", "precious"); + support::symlink(&sandbox.path("work/link.swt"), &precious); + for force in [&[][..], &["--force"]] { + let mut arguments = vec!["pack", text(&package), "--output", "link.swt"]; + arguments.extend(force); + let report = sandbox.run(&arguments).expect(EXIT_USAGE); + assert_eq!(report["error"]["kind"], if force.is_empty() { "output.exists" } else { "output.symlink" }); + assert_eq!(std::fs::read_to_string(&precious).unwrap(), "precious"); + } + assert_eq!(support::tree(&package), ["main.jsx", "widget.json"]); +} + +// install and list + +#[test] +fn install_adds_archives_and_folders_to_the_library() { + let sandbox = Sandbox::new(); + assert_eq!(sandbox.run(&["list"]).expect(0)["packages"], serde_json::json!([])); + + let archive = sandbox.scratch.new_folder("downloads").join("Any Name.swt"); + std::fs::write(&archive, support::package_zip("Folder In Archive", "Declared Name", &[("lib/a.js", "var a = 1;")])).unwrap(); + let report = sandbox.run(&["install", text(&archive)]).expect(0); + assert_eq!(checks(&report), ["passed", "passed", "notRun", "notRun", "notRun"]); + assert_eq!(report["name"], "Declared Name", "the manifest names the package, not the file or the archive folder"); + let directory = PathBuf::from(report["directory"].as_str().unwrap()); + assert_eq!(package_files(&directory), ["lib/a.js", "main.jsx", "widget.json"]); + assert!(directory.starts_with(std::fs::canonicalize(sandbox.data()).unwrap().join("Packages"))); + + // The same archive again: a second copy with a numbered name and an identity of its own. + let second = sandbox.run(&["install", text(&archive)]).expect(0); + assert_eq!(second["name"], "Declared Name (1)"); + assert_ne!(second["installation"], report["installation"]); + + // A folder is installed through the same checks. + let folder = sandbox.package("From Folder", HELLO); + sandbox.scratch.new_file("work/From Folder/.git/config", "not part of the package"); + let third = sandbox.run(&["install", text(&folder)]).expect(0); + assert_eq!(third["name"], "From Folder"); + assert_eq!(package_files(&PathBuf::from(third["directory"].as_str().unwrap())), ["main.jsx", "widget.json"]); + assert!(!Path::new(third["directory"].as_str().unwrap()).join(".git").exists(), "dot-files are not installed"); + + let listed = sandbox.run(&["list"]).expect(0); + let names: Vec<&str> = listed["packages"].as_array().unwrap().iter().map(|package| package["name"].as_str().unwrap()).collect(); + assert_eq!(names, ["Declared Name", "Declared Name (1)", "From Folder"]); + let first = &listed["packages"][0]; + assert_eq!((first["title"].as_str(), &first["installation"], first["directory"].as_str()), (Some("Declared Name"), &report["installation"], report["directory"].as_str())); + // An installed package runs from the library like any folder. + assert_eq!(sandbox.run(&["run", report["directory"].as_str().unwrap()]).expect(0)["tree"]["children"][0], "hi"); + + // --data names another library. + let other = sandbox.path("other-data"); + assert_eq!(sandbox.run(&["list", "--data", text(&other)]).expect(0)["packages"], serde_json::json!([])); +} + +#[test] +fn install_refuses_unsafe_archives_and_leaves_nothing_behind() { + let sandbox = Sandbox::new(); + let downloads = sandbox.scratch.new_folder("downloads"); + let good = |path: &str| RawEntry::new(path, if path.ends_with("widget.json") { support::manifest("Widget") } else { "$render(hi);".to_owned() }); + let cases: Vec<(&str, Vec, &str)> = vec![ + ("traversal", vec![good("Widget/widget.json"), good("Widget/main.jsx"), RawEntry::new("Widget/../../escaped.txt", "escaped")], "archive_unsafe_path"), + ("parent", vec![good("widget.json"), good("main.jsx"), RawEntry::new("../escaped.txt", "escaped")], "archive_unsafe_path"), + ("absolute", vec![good("Widget/widget.json"), good("Widget/main.jsx"), RawEntry::new("/tmp/scriptwidget-escaped.txt", "escaped")], "archive_unsafe_path"), + ("backslash", vec![good("Widget/widget.json"), good("Widget/main.jsx"), RawEntry::new("Widget\\..\\escaped.txt", "escaped")], "archive_unsafe_path"), + ("symlink", vec![good("Widget/widget.json"), good("Widget/main.jsx"), RawEntry::new("Widget/link", "/etc/passwd").mode(SYMBOLIC_LINK)], "archive_symlink"), + ("collision", vec![good("Widget/widget.json"), good("Widget/main.jsx"), RawEntry::new("Widget/Main.JSX", "other")], "archive_path_collision"), + ("lying-size", vec![good("Widget/widget.json"), RawEntry::new("Widget/main.jsx", vec![b'a'; 4096]).declared_size(16)], "archive_size_mismatch"), + ]; + for (name, entries, code) in cases { + let archive = downloads.join(format!("{name}.swt")); + std::fs::write(&archive, raw_zip(&entries)).unwrap(); + let outcome = sandbox.run(&["install", text(&archive)]); + let report = outcome.expect(EXIT_FAILED); + assert_eq!(report["error"]["kind"], code, "{name}"); + assert_eq!(checks(&report)[0], "failed", "{name}"); + assert!(outcome.stderr.contains(&format!("error: [{code}]")), "{name}: {}", outcome.stderr); + } + std::fs::write(downloads.join("garbage.swt"), "this is not an archive").unwrap(); + assert!(sandbox.run(&["install", text(&downloads.join("garbage.swt"))]).expect(EXIT_FAILED)["error"]["kind"].as_str().unwrap().starts_with("archive_")); + + // An archive whose package does not compile is refused before it is published. + std::fs::write(downloads.join("broken.swt"), raw_zip(&[good("Widget/widget.json"), RawEntry::new("Widget/main.jsx", "$render(")])).unwrap(); + let report = sandbox.run(&["install", text(&downloads.join("broken.swt"))]).expect(EXIT_FAILED); + assert_eq!(checks(&report), ["passed", "failed", "notRun", "notRun", "notRun"]); + assert!(report["error"]["message"].as_str().unwrap().contains("SyntaxError"), "{}", report["error"]); + // And one with an invalid manifest. + std::fs::write(downloads.join("unknown-key.swt"), raw_zip(&[RawEntry::new("Widget/widget.json", manifest("Widget", ",\"surprise\":1")), good("Widget/main.jsx")])).unwrap(); + sandbox.run(&["install", text(&downloads.join("unknown-key.swt"))]).expect(EXIT_FAILED); + + // Nothing was installed, nothing is left in staging, and nothing was written outside the library. + assert_eq!(sandbox.run(&["list"]).expect(0)["packages"], serde_json::json!([])); + assert_eq!(support::tree(&sandbox.data().join("Packages")), Vec::::new()); + assert!(support::tree(&sandbox.data()).iter().all(|path| !path.contains("escaped")), "{:?}", support::tree(&sandbox.data())); + for escaped in [sandbox.path("escaped.txt"), sandbox.data().join("escaped.txt"), sandbox.data().join("Staging/escaped.txt"), PathBuf::from("/tmp/scriptwidget-escaped.txt")] { + assert!(!escaped.exists(), "{}", escaped.display()); + } + assert_eq!(std::fs::read_dir(sandbox.data().join("Staging")).map(|entries| entries.count()).unwrap_or(0), 0, "staging is clean"); +} + +// Links in a package folder + +#[test] +fn a_link_in_a_package_folder_fails_every_command() { + let sandbox = Sandbox::new(); + let secret = sandbox.scratch.new_file("secret.txt", "secret"); + + let linked_file = sandbox.package("LinkedFile", "$render({String($file.read('data.txt'))});"); + support::symlink(&linked_file.join("data.txt"), &secret); + let linked_folder = sandbox.package("LinkedFolder", HELLO); + support::symlink(&linked_folder.join("lib"), &sandbox.path("work")); + let nested = sandbox.package("Nested", HELLO); + sandbox.scratch.new_folder("work/Nested/a/b"); + support::symlink(&nested.join("a/b/passwd"), Path::new("/etc/passwd")); + let hard_linked = sandbox.package("HardLinked", HELLO); + support::hard_link(&hard_linked.join("data.txt"), &secret); + let linked_entry = sandbox.scratch.new_folder("work/LinkedEntry"); + std::fs::write(linked_entry.join("widget.json"), manifest("LinkedEntry", "")).unwrap(); + support::symlink(&linked_entry.join("main.jsx"), &sandbox.scratch.new_file("outside.jsx", HELLO)); + + for package in [&linked_file, &linked_folder, &nested, &hard_linked, &linked_entry] { + for command in ["check", "run", "pack", "install", "timeline"] { + let outcome = sandbox.run(&[command, text(package)]); + let report = outcome.expect(EXIT_FAILED); + assert_eq!(report["error"]["kind"], "unsafe_file", "{command} {}", package.display()); + if command != "timeline" { + assert_eq!(checks(&report)[0], "failed", "{command}"); + } + } + let report = sandbox.run(&["render", text(package), "--output", "linked.png"]).expect(EXIT_FAILED); + assert_eq!(report["error"]["kind"], "unsafe_file"); + } + assert!(!sandbox.path("work/linked.png").exists()); + assert!(std::fs::read_dir(sandbox.work()).unwrap().all(|entry| entry.unwrap().path().extension().is_none_or(|extension| extension != "swt")), "no archive was written"); + assert_eq!(sandbox.run(&["list"]).expect(0)["packages"], serde_json::json!([])); + + // The package folder itself must not be a link. + let real = sandbox.package("Real", HELLO); + support::symlink(&sandbox.path("work/Alias"), &real); + assert_eq!(sandbox.run(&["check", "Alias"]).expect(EXIT_USAGE)["error"]["kind"], "input.directory"); + + // What is not part of the authored package is not looked at: a linked dot-folder or build cache does not matter, and is not exported. + support::symlink(&real.join(".venv"), &sandbox.path("work")); + support::symlink(&real.join("__Build"), &sandbox.path("work")); + sandbox.run(&["check", text(&real)]).expect(0); + sandbox.run(&["pack", text(&real), "--output", "real.swt"]).expect(0); + let names: Vec = read_zip(&std::fs::read(sandbox.path("work/real.swt")).unwrap()).into_iter().map(|(name, _)| name).collect(); + assert_eq!(names, ["Real/main.jsx", "Real/widget.json"]); +} + +// Templates and the installed layout + +/// The data directory as a package installs it, assembled by the same script. +fn staged_resources(scratch: &TemporaryFolder) -> PathBuf { + let root = scratch.path("prefix/share/scriptwidget"); + let status = Command::new("sh").arg(repository().join("Linux/scripts/stage-resources.sh")).arg(&root).stdout(std::process::Stdio::null()).status().expect("sh runs"); + assert!(status.success(), "stage-resources.sh"); + root +} + +#[test] +fn templates_lists_what_runs_on_linux() { + let sandbox = Sandbox::new(); + let report = sandbox.run(&["templates"]).expect(0); + let templates = report["templates"].as_array().unwrap(); + assert!(templates.len() > 100, "{} templates", templates.len()); + let named = |group: &str, name: &str| templates.iter().find(|template| template["group"] == group && template["name"] == name); + assert_eq!(named("template", "An Empty Widget").unwrap()["network"], false); + assert_eq!(named("template", "Countdown").unwrap()["network"], false); + assert_eq!(named("template", "Daily Quote").unwrap()["network"], true); + assert!(Path::new(named("component", "text").unwrap()["directory"].as_str().unwrap()).join("main.jsx").is_file()); + // The ones that use an API Linux does not implement are left out, with the reason. + assert!(named("api", "health").is_none() && named("template", "Music Playback").is_none()); + let unavailable = report["unavailable"].as_array().unwrap(); + assert!(unavailable.iter().any(|template| template["group"] == "api" && template["name"] == "health" && template["api"] == "$health"), "{unavailable:?}"); + assert_eq!(templates.len() + unavailable.len(), ["template", "component", "api"].iter().map(|group| std::fs::read_dir(bundled_templates().join(group)).unwrap().filter(|entry| entry.as_ref().unwrap().path().join("main.jsx").is_file()).count()).sum::()); + + // Without templates the command says so instead of printing an empty list. + let mut command = sandbox.command(); + command.env_remove("SCRIPTWIDGET_TEMPLATES"); + assert_eq!(finish(command.arg("templates")).expect(EXIT_USAGE)["error"]["kind"], "tool.templates_missing"); +} + +/// The phase 1 criterion: with no graphical session, `scriptwidget render` +/// draws every bundled template that needs no system data, from the installed +/// layout, at the medium size. A template fails on any error and on an empty +/// picture. The templates that need an API Linux lacks are not listed at all; +/// the listed ones that ask the network are named and not drawn, so that the +/// test does not depend on other people's servers. +#[test] +fn every_bundled_template_that_needs_no_network_renders() { + let sandbox = Sandbox::new(); + let resources = staged_resources(&sandbox.scratch); + let command = || { + let mut command = sandbox.command(); + command.env_remove("SCRIPTWIDGET_RUNTIME_ASSETS").env_remove("SCRIPTWIDGET_TEMPLATES").env("SCRIPTWIDGET_RESOURCES", &resources); + command.env_remove("DISPLAY").env_remove("WAYLAND_DISPLAY"); + command + }; + let listed = finish(command().arg("templates")).expect(0); + let templates: Vec<(String, String, bool)> = listed["templates"] + .as_array() + .unwrap() + .iter() + .map(|template| (format!("{}/{}", template["group"].as_str().unwrap(), template["name"].as_str().unwrap()), template["directory"].as_str().unwrap().to_owned(), template["network"].as_bool().unwrap())) + .collect(); + let pictures = sandbox.scratch.new_folder("pictures"); + // Keep the pictures for review with SCRIPTWIDGET_CLI_PICTURES=. + let keep = std::env::var_os("SCRIPTWIDGET_CLI_PICTURES").map(PathBuf::from); + + let work: Vec<&(String, String, bool)> = templates.iter().filter(|(_, _, network)| !network).collect(); + let next = std::sync::atomic::AtomicUsize::new(0); + let failures = std::sync::Mutex::new(Vec::new()); + std::thread::scope(|scope| { + for _ in 0..std::thread::available_parallelism().map_or(2, |count| count.get().min(8)) { + scope.spawn(|| { + loop { + let Some((name, directory, _)) = work.get(next.fetch_add(1, std::sync::atomic::Ordering::Relaxed)) else { break }; + let output = pictures.join(format!("{}.png", name.replace('/', "-"))); + let outcome = finish(command().args(["render", directory, "--family", "medium", "--scale", "2", "--now", FIXED_NOW, "--output", text(&output)])); + let problem = match serde_json::from_str::(&outcome.stdout) { + Err(error) => Some(format!("no report ({error}): {}", outcome.stderr)), + Ok(report) if outcome.code != 0 || report["status"] != "passed" => Some(format!("exit {}: {}", outcome.code, report["error"])), + Ok(report) if report["checks"]["render"] != "passed" => Some(format!("render {}", report["checks"]["render"])), + Ok(report) if !(report["picture"]["visiblePixels"].as_u64() > Some(0) && report["picture"]["colors"].as_u64() > Some(1)) => Some(format!("an empty picture: {}", report["picture"])), + Ok(_) if !output.is_file() || png_size(&output) != (658, 340) => Some("no PNG of 658x340 pixels".to_owned()), + Ok(_) => None, + }; + if let Some(keep) = &keep { + std::fs::create_dir_all(keep).unwrap(); + let _ = std::fs::copy(&output, keep.join(output.file_name().unwrap())); + } + if let Some(problem) = problem { + failures.lock().unwrap().push(format!("{name}: {problem}")); + } + } + }); + } + }); + let mut failures = failures.into_inner().unwrap(); + failures.sort(); + let skipped: Vec<&str> = templates.iter().filter(|(_, _, network)| *network).map(|(name, _, _)| name.as_str()).collect(); + println!("rendered {} of {} listed templates at medium; {} ask the network and were not drawn: {}", work.len() - failures.len(), templates.len(), skipped.len(), skipped.join(", ")); + assert!(work.len() > 100, "only {} templates to draw", work.len()); + assert!(failures.is_empty(), "{} of {} templates failed:\n{}", failures.len(), work.len(), failures.join("\n")); +} + +#[test] +fn the_installed_layout_needs_no_settings_and_no_repository() { + let sandbox = Sandbox::new(); + staged_resources(&sandbox.scratch); + // /bin/scriptwidget finds /share/scriptwidget by itself. + let program = sandbox.scratch.new_folder("prefix/bin").join("scriptwidget"); + std::fs::copy(env!("CARGO_BIN_EXE_scriptwidget"), &program).unwrap(); + let installed = |arguments: &[&str]| { + let mut command = Command::new(&program); + command.env_clear().env("HOME", sandbox.path("home")).current_dir(sandbox.work()).args(arguments); + finish(&mut command) + }; + let prefix = std::fs::canonicalize(sandbox.path("prefix")).unwrap(); + + let doctor = installed(&["doctor"]).expect(0); + assert_eq!(doctor["paths"]["runtime"], text(&prefix.join("share/scriptwidget/runtime"))); + assert_eq!(doctor["paths"]["templates"], text(&prefix.join("share/scriptwidget/templates"))); + assert_eq!(doctor["paths"]["data"], text(&sandbox.path("home/.local/share/scriptwidget"))); + for check in ["resources", "templates", "engine", "render", "dataDirectory"] { + assert_eq!(doctor["checks"][check], "passed", "{check}: {doctor}"); + } + assert_eq!(doctor["checks"]["service"], "notRun"); + assert_eq!(doctor["fonts"]["bundled"], true); + assert_eq!(doctor["session"]["wayland"], false); + assert!(!sandbox.path("home/.local").exists(), "doctor creates nothing"); + + assert!(installed(&["templates"]).expect(0)["templates"].as_array().unwrap().len() > 100); + installed(&["new", "Installed", "--template", "template/Countdown"]).expect(0); + assert_has_a_picture(&installed(&["render", "Installed", "--output", "installed.png"]).expect(0)); + // The user's library follows the XDG directories. + let report = installed(&["install", "Installed"]).expect(0); + assert!(Path::new(report["directory"].as_str().unwrap()).starts_with(sandbox.path("home/.local/share/scriptwidget/Packages")), "{}", report["directory"]); +} + +#[test] +fn missing_runtime_files_are_an_environment_error() { + let sandbox = Sandbox::new(); + let package = sandbox.package("Widget", HELLO); + let bare = |arguments: &[&str]| { + let mut command = Command::new(env!("CARGO_BIN_EXE_scriptwidget")); + command.env_clear().env("HOME", sandbox.path("home")).env("XDG_DATA_DIRS", sandbox.path("no-data-dirs")).current_dir(sandbox.work()).args(arguments); + finish(&mut command) + }; + for arguments in [&["check", text(&package)][..], &["run", text(&package)], &["render", text(&package), "--output", "x.png"], &["templates"], &["timeline", text(&package)], &["pack", text(&package)]] { + let outcome = bare(arguments); + let report = outcome.expect(EXIT_USAGE); + assert_eq!(report["error"]["kind"], "tool.resources_missing", "{arguments:?}"); + assert!(outcome.stderr.contains("SCRIPTWIDGET_RESOURCES"), "{}", outcome.stderr); + } + // doctor reports it as a failed check instead of stopping. + let doctor = bare(&["doctor"]).expect(EXIT_FAILED); + assert_eq!(doctor["checks"]["resources"], "failed"); + assert_eq!(doctor["checks"]["engine"], "notRun"); + // What needs no runtime files still works. + bare(&["new", "Offline"]).expect(0); + bare(&["list"]).expect(0); + + // A resource folder that is named but wrong is not silently replaced by another. + let mut command = sandbox.command(); + command.env_remove("SCRIPTWIDGET_RUNTIME_ASSETS").env("SCRIPTWIDGET_RESOURCES", sandbox.path("nowhere")); + assert_eq!(finish(command.args(["check", text(&package)])).expect(EXIT_USAGE)["error"]["kind"], "tool.resources_missing"); +} + +/// SHA-256, so that the test does not trust the program's own digest. +fn sha256(bytes: &[u8]) -> Vec { + use sha2::Digest; + sha2::Sha256::digest(bytes).to_vec() +} diff --git a/Linux/crates/sw-cli/tests/desktop.rs b/Linux/crates/sw-cli/tests/desktop.rs new file mode 100644 index 00000000..ce494247 --- /dev/null +++ b/Linux/crates/sw-cli/tests/desktop.rs @@ -0,0 +1,506 @@ +//! Runs the built `scriptwidget` program as the background service +//! (`scriptwidget host run`) and as its clients, in a session of its own: a +//! temporary runtime directory, a temporary data directory, and a search +//! path that holds nothing but two recording stand-ins for `xdg-open` and +//! `scriptwidget-studio`. No browser is started, no system bus is asked, and +//! the user's own service is never touched. +//! +//! node Linux/scripts/build-shared-js.mjs +//! cargo test -p sw-cli --test desktop + +use std::io::{BufRead, BufReader}; +use std::os::unix::fs::{DirBuilderExt, PermissionsExt}; +use std::path::{Path, PathBuf}; +use std::process::{Child, Command, Stdio}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::mpsc::{Receiver, channel}; +use std::time::{Duration, Instant}; + +use serde_json::Value; + +const EXIT_FAILED: i32 = 1; +const EXIT_USAGE: i32 = 2; +const EXIT_NEEDS_SERVICE: i32 = 69; +const WAIT: Duration = Duration::from_secs(30); +const ID: &str = "0123456789abcdef"; + +fn repository() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../..") +} + +struct Outcome { + code: i32, + stdout: String, + stderr: String, +} + +impl Outcome { + #[track_caller] + fn expect(&self, code: i32) -> Value { + assert_eq!(self.code, code, "exit code\nstdout: {}\nstderr: {}", self.stdout, self.stderr); + let report: Value = serde_json::from_str(&self.stdout).unwrap_or_else(|error| panic!("standard output is not one JSON report ({error}):\n{}\n{}", self.stdout, self.stderr)); + assert_eq!(report["ok"], code == 0); + report + } +} + +/// The output of a running command, line by line. +struct Lines { + child: Child, + lines: Receiver, +} + +impl Lines { + fn start(command: &mut Command) -> Self { + let mut child = command.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().expect("the program starts"); + let output = child.stdout.take().unwrap(); + let (sender, lines) = channel(); + std::thread::spawn(move || { + for line in BufReader::new(output).lines().map_while(Result::ok) { + if sender.send(line).is_err() { + break; + } + } + }); + Self { child, lines } + } + + #[track_caller] + fn next(&mut self) -> Value { + let line = self.lines.recv_timeout(WAIT).expect("another line"); + serde_json::from_str(&line).unwrap_or_else(|error| panic!("not JSON ({error}): {line}")) + } + + #[track_caller] + fn event(&mut self, wanted: impl Fn(&Value) -> bool) -> Value { + loop { + let event = self.next(); + if wanted(&event) { + return event; + } + } + } + + /// Waits for the command to end; returns its exit code and the lines it had not been asked for yet. + #[track_caller] + fn finish(mut self) -> (i32, Vec) { + let deadline = Instant::now() + WAIT; + let code = loop { + if let Some(status) = self.child.try_wait().unwrap() { + break status.code().expect("the program exits by itself"); + } + assert!(Instant::now() < deadline, "the command ends"); + std::thread::sleep(Duration::from_millis(20)); + }; + let mut rest = Vec::new(); + while let Ok(line) = self.lines.recv_timeout(Duration::from_secs(2)) { + rest.push(serde_json::from_str(&line).unwrap()); + } + (code, rest) + } +} + +impl Drop for Lines { + fn drop(&mut self) { + let _ = self.child.kill(); + let _ = self.child.wait(); + } +} + +/// One user's session with nothing in it but ScriptWidget. +struct Desk { + root: PathBuf, + service: Option, +} + +impl Desk { + fn new() -> Self { + static NEXT: AtomicU64 = AtomicU64::new(0); + // Short: the path of a socket is limited to about a hundred bytes. + let root = PathBuf::from("/tmp").join(format!("swd-{}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::SeqCst))); + let _ = std::fs::remove_dir_all(&root); + for folder in ["", "run", "home", "work", "bin"] { + std::fs::DirBuilder::new().mode(0o700).create(root.join(folder)).unwrap(); + } + // Stand-ins that write their arguments, one a line, and start nothing. + for program in ["xdg-open", "scriptwidget-studio"] { + let path = root.join("bin").join(program); + std::fs::write(&path, format!("#!/bin/sh\nfor argument in \"$@\"; do printf '%s\\n' \"$argument\"; done >> '{}'\n", root.join(format!("{program}.log")).display())).unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); + } + Self { root, service: None } + } + + fn path(&self, relative: &str) -> PathBuf { + self.root.join(relative) + } + + fn socket(&self) -> PathBuf { + self.path("run/scriptwidget/host.sock") + } + + /// A command with nothing inherited: no display, no bus, no programs but the stand-ins. + fn command(&self) -> Command { + let mut command = Command::new(env!("CARGO_BIN_EXE_scriptwidget")); + command.env_clear(); + command.env("HOME", self.path("home")).env("SCRIPTWIDGET_DATA", self.path("data")).env("XDG_RUNTIME_DIR", self.path("run")).env("PATH", self.path("bin")); + command.env("TZ", "UTC").env("LANG", "en_US.UTF-8"); + command.env("SCRIPTWIDGET_RUNTIME_ASSETS", repository().join("Linux/assets/scriptwidget")).env("SCRIPTWIDGET_TEMPLATES", repository().join("Shared/ScriptWidgetRuntime/Resource/Script.bundle")); + command.current_dir(self.path("work")); + command + } + + fn run(&self, arguments: &[&str]) -> Outcome { + let output = self.command().args(arguments).output().expect("the program starts"); + Outcome { code: output.status.code().expect("the program exits by itself"), stdout: String::from_utf8(output.stdout).unwrap(), stderr: String::from_utf8(output.stderr).unwrap() } + } + + #[track_caller] + fn ok(&self, arguments: &[&str]) -> Value { + self.run(arguments).expect(0) + } + + fn start_service(&mut self) { + let log = std::fs::File::create(self.path("service.log")).unwrap(); + let child = self.command().args(["host", "run"]).stdin(Stdio::null()).stdout(Stdio::null()).stderr(log).spawn().expect("the service starts"); + self.service = Some(child); + let deadline = Instant::now() + WAIT; + while self.run(&["widgets"]).code != 0 { + assert!(Instant::now() < deadline, "the service answers: {}", self.service_log()); + std::thread::sleep(Duration::from_millis(30)); + } + } + + fn service_log(&self) -> String { + std::fs::read_to_string(self.path("service.log")).unwrap_or_default() + } + + /// Sends a signal to the service and returns how it ended: its exit code, or `None` when the signal ended it. + fn signal_service(&mut self, signal: i32) -> Option { + let mut child = self.service.take().expect("a running service"); + // SAFETY: the child is ours and has not been waited for, so the identifier is still its own. + unsafe { libc::kill(child.id() as i32, signal) }; + let deadline = Instant::now() + WAIT; + loop { + if let Some(status) = child.try_wait().unwrap() { + return status.code(); + } + assert!(Instant::now() < deadline, "the service ends: {}", self.service_log()); + std::thread::sleep(Duration::from_millis(20)); + } + } + + fn package(&self, name: &str, source: &str, extra: &str) -> PathBuf { + let directory = self.path(&format!("work/{name}")); + std::fs::create_dir_all(&directory).unwrap(); + std::fs::write(directory.join("main.jsx"), source).unwrap(); + std::fs::write( + directory.join("widget.json"), + format!( + r#"{{"formatVersion":2,"id":"com.example.{}","name":"{name}","version":"1.0.0","runtimeVersion":"1.0","entry":"main.jsx","supportedFamilies":["systemSmall","systemMedium","systemLarge"],"permissions":["storage"],"networkDomains":[]{extra}}}"#, + name.to_lowercase() + ), + ) + .unwrap(); + directory + } + + fn widget(&self, id: &str) -> Value { + self.ok(&["widgets"])["widgets"].as_array().unwrap().iter().find(|widget| widget["id"] == id).cloned().unwrap_or_else(|| panic!("no widget {id}")) + } + + #[track_caller] + fn until(&self, id: &str, what: &str, done: impl Fn(&Value) -> bool) -> Value { + let deadline = Instant::now() + WAIT; + loop { + let widget = self.widget(id); + if done(&widget) { + return widget; + } + assert!(Instant::now() < deadline, "timed out waiting for {what}: {widget}"); + std::thread::sleep(Duration::from_millis(30)); + } + } + + fn log(&self, program: &str) -> Vec { + std::fs::read_to_string(self.path(&format!("{program}.log"))).unwrap_or_default().lines().map(str::to_owned).collect() + } +} + +impl Drop for Desk { + fn drop(&mut self) { + if let Some(mut child) = self.service.take() { + let _ = child.kill(); + let _ = child.wait(); + } + let _ = std::fs::remove_dir_all(&self.root); + } +} + +const COUNTER: &str = r##" +function bump() { $storage.setString("count", String(Number($storage.getString("count") || "0") + 1)); } +const count = $storage.getString("count") || "0"; +$render( + + + web + script + +); +"##; + +#[test] +fn without_the_service_desktop_commands_exit_69_and_say_how_to_start_it() { + let desk = Desk::new(); + let commands: [&[&str]; 13] = [ + &["add", "Clock", "--family", "small"], + &["remove", ID], + &["widgets"], + &["refresh"], + &["peek"], + &["arrange", "on"], + &["tap", ID, "0"], + &["move", ID, "eDP-1", "-10", "20"], + &["screen", "eDP-1", "1.5", "1920", "1200"], + &["theme", "dark"], + &["menu", ID, "resize-small"], + &["session", "locked"], + &["dev", "."], + ]; + for arguments in commands { + let outcome = desk.run(arguments); + let report = outcome.expect(EXIT_NEEDS_SERVICE); + assert_eq!(report["command"], arguments[0]); + assert_eq!(report["error"]["kind"], "service.unavailable"); + assert!(outcome.stderr.contains("needs the background service, which is not running. Start it with: systemctl --user start scriptwidget.service"), "{}", outcome.stderr); + } + // `watch` prints events, not a report: one `bye` that says why there is nothing to watch. + let outcome = desk.run(&["watch"]); + assert_eq!(outcome.code, EXIT_NEEDS_SERVICE); + assert_eq!(outcome.stdout, "{\"event\":\"bye\",\"reason\":\"not-running\"}\n"); + assert!(outcome.stderr.contains("[service.unavailable]") && outcome.stderr.contains("systemctl --user start scriptwidget.service"), "{}", outcome.stderr); + // The same without a session at all. + let output = desk.command().env_remove("XDG_RUNTIME_DIR").args(["widgets"]).output().unwrap(); + assert_eq!(output.status.code(), Some(EXIT_NEEDS_SERVICE)); + // Nothing was created by asking. + assert!(!desk.path("run/scriptwidget").exists() && !desk.path("data").exists()); + + // A wrong argument is a usage error, with or without a service. + let wrong: [&[&str]; 14] = [ + &["add"], + &["add", "../etc"], + &["add", "Clock", "--family", "huge"], + &["remove", "not-an-id"], + &["remove", "../../../etc/passwd"], + &["tap", ID, "one"], + &["tap", ID, "-1"], + &["move", ID, "a screen", "1", "2"], + &["move", ID, "eDP-1", "1"], + &["screen", "eDP-1", "big", "1", "1"], + &["screen", "eDP-1", "100", "1920", "1080"], + &["theme", "blue"], + &["menu", ID, "delete-everything"], + &["host", "walk"], + ]; + for arguments in wrong { + let report = desk.run(arguments).expect(EXIT_USAGE); + assert_eq!(report["error"]["kind"], "usage", "{arguments:?}"); + } +} + +#[test] +fn the_command_line_drives_the_service() { + let mut desk = Desk::new(); + desk.package("Counter", COUNTER, ""); + assert_eq!(desk.ok(&["install", "Counter"])["name"], "Counter"); + desk.start_service(); + let doctor = desk.ok(&["doctor"]); + assert_eq!(doctor["checks"]["service"], "passed"); + assert_eq!((doctor["service"]["running"].as_bool(), doctor["service"]["protocol"].as_u64()), (Some(true), Some(1))); + + let mut watch = Lines::start(desk.command().arg("watch")); + assert_eq!(watch.next()["event"], "hello"); + assert_eq!(desk.run(&["add", "Missing"]).expect(EXIT_USAGE)["error"]["kind"], "package.unknown"); + assert_eq!(desk.run(&["add", "Counter", "--family", "extraLarge"]).expect(EXIT_FAILED)["error"]["kind"], "family.unsupported"); + let added = desk.ok(&["add", "Counter", "--family", "large"]); + let id = added["id"].as_str().unwrap().to_owned(); + let first = watch.event(|event| event["event"] == "widget"); + assert_eq!((first["id"].as_str(), first["version"].as_u64(), first["height"].as_u64()), (Some(id.as_str()), Some(1), Some(345))); + assert_eq!(first["regions"].as_array().unwrap().len(), 3); + assert!(Path::new(first["image"].as_str().unwrap()).starts_with(desk.path("run/scriptwidget/frames"))); + + // A tap on the button: the picture changes. + assert_eq!(desk.ok(&["tap", &id, "0"])["action"], "button"); + let second = watch.event(|event| event["event"] == "widget"); + assert_eq!(second["version"], 2); + assert_ne!(second["image"], first["image"]); + assert_eq!(desk.ok(&["tap", &id, "0", "2"])["action"], "button"); + assert_eq!(watch.event(|event| event["event"] == "widget")["version"], 3); + assert_eq!(desk.run(&["tap", &id, "9"]).expect(EXIT_USAGE)["error"]["kind"], "region.unknown"); + + // A link goes to xdg-open as one argument, whatever is in it; a script address goes nowhere. + assert_eq!(desk.ok(&["tap", &id, "1"])["action"], "link"); + let deadline = Instant::now() + WAIT; + while desk.log("xdg-open").is_empty() { + assert!(Instant::now() < deadline, "xdg-open was started"); + std::thread::sleep(Duration::from_millis(20)); + } + assert_eq!(desk.log("xdg-open"), ["https://example.com/?q=a&b=$(id)%20;x"], "one argument, no shell"); + assert_eq!(desk.run(&["tap", &id, "2"]).expect(EXIT_FAILED)["error"]["kind"], "link.refused"); + assert_eq!(desk.log("xdg-open").len(), 1); + + // The words of the menu. + desk.ok(&["menu", &id, "edit"]); + let deadline = Instant::now() + WAIT; + while desk.log("scriptwidget-studio").len() < 2 { + assert!(Instant::now() < deadline, "scriptwidget-studio was started"); + std::thread::sleep(Duration::from_millis(20)); + } + assert_eq!(desk.log("scriptwidget-studio"), ["--edit", "Counter"]); + desk.ok(&["menu", &id, "resize-small"]); + let small = watch.event(|event| event["event"] == "widget" && event["height"] == 170); + assert_eq!((small["width"].as_u64(), small["family"].as_str()), (Some(170), Some("small"))); + + // Where it is, on which screen, at what scale, in which theme. + desk.ok(&["screen", "eDP-1", "2", "1440", "900"]); + watch.event(|event| event["event"] == "widget"); + let moved = desk.ok(&["move", &id, "eDP-1", "-50", "299.6"]); + assert_eq!((moved["x"].as_i64(), moved["y"].as_i64()), (Some(0), Some(300))); + let event = watch.event(|event| event["event"] == "widget" && event["screen"] == "eDP-1"); + assert_eq!((event["x"].as_i64(), event["y"].as_i64()), (Some(0), Some(300))); + assert_eq!(desk.ok(&["theme", "dark"])["dark"], true); + assert_eq!(desk.ok(&["session", "locked"])["locked"], true); + assert_eq!(desk.ok(&["session", "unlocked"])["locked"], false); + let listed = desk.until(&id, "the widget at rest", |widget| widget["busy"] == false && widget["scale"] == 2.0); + assert_eq!((listed["package"].as_str(), listed["family"].as_str(), listed["screen"].as_str(), listed["x"].as_i64()), (Some("Counter"), Some("small"), Some("eDP-1"), Some(0))); + + // Modes. + assert_eq!(desk.ok(&["arrange"])["arrange"], true); + assert_eq!(watch.event(|event| event["event"] == "mode"), serde_json::json!({ "event": "mode", "arrange": true, "peek": true }), "arranging raises the widgets"); + assert_eq!(desk.ok(&["peek", "on"])["peek"], true); + assert_eq!(desk.ok(&["arrange", "off"])["arrange"], false); + assert_eq!(desk.ok(&["refresh"])["refreshed"], 1); + assert_eq!(desk.ok(&["refresh", &id])["refreshed"], 1); + assert_eq!(desk.ok(&["refresh", "--package", "Counter"])["refreshed"], 1); + + // A second service in the same session is refused and disturbs nothing. + let second_service = desk.run(&["host", "run"]); + assert_eq!(second_service.code, EXIT_USAGE); + assert!(second_service.stderr.contains("already running"), "{}", second_service.stderr); + assert_eq!(desk.ok(&["widgets"])["widgets"].as_array().unwrap().len(), 1); + + // SIGTERM: every watcher gets `bye`, the service exits with 0 and leaves no socket and no picture. + assert_eq!(desk.signal_service(libc::SIGTERM), Some(0), "{}", desk.service_log()); + let (code, rest) = watch.finish(); + assert_eq!(code, 0, "watch ends with the service"); + assert_eq!(rest.last(), Some(&serde_json::json!({ "event": "bye", "reason": "shutdown" }))); + assert!(!desk.socket().exists()); + assert_eq!(std::fs::read_dir(desk.path("run/scriptwidget/frames")).unwrap().count(), 0); + assert_eq!(desk.run(&["widgets"]).code, EXIT_NEEDS_SERVICE); + + // Started again, the widget is where it was. + desk.start_service(); + let back = desk.until(&id, "the widget after a restart", |widget| widget["version"].as_u64() >= Some(1)); + assert_eq!((back["family"].as_str(), back["screen"].as_str(), back["y"].as_i64()), (Some("small"), Some("eDP-1"), Some(300))); + desk.ok(&["remove", &id]); + assert!(desk.ok(&["widgets"])["widgets"].as_array().unwrap().is_empty()); + let log = desk.service_log(); + assert!(!log.contains("example.com") && !log.contains("Count "), "the log names no link and no content: {log}"); +} + +#[test] +fn a_service_that_dies_is_noticed_and_can_be_started_again() { + let mut desk = Desk::new(); + desk.package("Counter", COUNTER, ""); + desk.ok(&["install", "Counter"]); + desk.start_service(); + let id = desk.ok(&["add", "Counter"])["id"].as_str().unwrap().to_owned(); + let mut watch = Lines::start(desk.command().arg("watch")); + watch.event(|event| event["event"] == "widget"); + + // Killed without a chance to say anything. + assert_eq!(desk.signal_service(libc::SIGKILL), None); + let (code, rest) = watch.finish(); + assert_eq!(code, EXIT_NEEDS_SERVICE); + assert_eq!(rest.last(), Some(&serde_json::json!({ "event": "bye", "reason": "lost" })), "watch says that the connection was lost"); + assert!(desk.socket().exists(), "the socket file of the killed service is still there"); + assert_eq!(desk.run(&["widgets"]).expect(EXIT_NEEDS_SERVICE)["error"]["kind"], "service.unavailable"); + + // The next service removes what the dead one left and shows the same widget. + desk.start_service(); + let back = desk.until(&id, "the widget after a crash", |widget| widget["version"].as_u64() >= Some(1)); + assert_eq!(back["package"], "Counter"); + let frames: Vec = std::fs::read_dir(desk.path("run/scriptwidget/frames")).unwrap().map(|entry| entry.unwrap().file_name().into_string().unwrap()).collect(); + assert_eq!(frames.len(), 1, "{frames:?}"); + assert_eq!(desk.signal_service(libc::SIGINT), Some(0), "Ctrl-C ends it as cleanly as SIGTERM"); + assert!(!desk.socket().exists()); +} + +#[test] +fn dev_copies_the_folder_and_refreshes_its_widgets() { + let mut desk = Desk::new(); + let folder = desk.package("Live", r##"$render(one);"##, ""); + desk.start_service(); + + let mut dev = Lines::start(desk.command().args(["dev", "Live", "--family", "small"])); + let ready = dev.next(); + assert_eq!((ready["event"].as_str(), ready["package"].as_str(), ready["added"].as_bool()), (Some("ready"), Some("Live"), Some(true))); + let id = ready["widgets"][0].as_str().unwrap().to_owned(); + let first = desk.until(&id, "the first picture", |widget| widget["version"].as_u64() >= Some(1) && widget["busy"] == false); + assert_eq!((first["package"].as_str(), first["family"].as_str()), (Some("Live"), Some("small"))); + let installed = desk.path("data/Packages/Live"); + assert_eq!(std::fs::read(installed.join("main.jsx")).unwrap(), std::fs::read(folder.join("main.jsx")).unwrap()); + + // Saving a file: copied, and the widget shows it. + std::fs::write(folder.join("main.jsx"), r##"$import("lib/color.js"); $render(two);"##).unwrap(); + std::fs::create_dir_all(folder.join("lib")).unwrap(); + std::fs::write(folder.join("lib/color.js"), "var color = \"#16a34a\";").unwrap(); + let synced = dev.event(|event| event["event"] == "synced"); + let written: Vec<&str> = synced["written"].as_array().unwrap().iter().filter_map(Value::as_str).collect(); + assert!(written.contains(&"main.jsx"), "{synced}"); + let second = desk.until(&id, "the new picture", |widget| widget["version"].as_u64() > first["version"].as_u64() && widget["busy"] == false && installed.join("lib/color.js").is_file()); + assert_eq!(second["error"], Value::Null, "{second}"); + + // Removing a file removes it from the installed copy too. + std::fs::write(folder.join("main.jsx"), r##"$render(three);"##).unwrap(); + std::fs::remove_file(folder.join("lib/color.js")).unwrap(); + let synced = dev.event(|event| event["event"] == "synced" && event["removed"].as_array().is_some_and(|removed| !removed.is_empty())); + assert_eq!(synced["removed"], serde_json::json!(["lib/color.js"])); + assert!(!installed.join("lib/color.js").exists()); + desk.until(&id, "the third picture", |widget| widget["version"].as_u64() > second["version"].as_u64()); + + // A link in the folder is refused; nothing of its target is copied, and dev keeps watching. + std::fs::write(desk.path("secret.txt"), "secret").unwrap(); + std::os::unix::fs::symlink(desk.path("secret.txt"), folder.join("leak.txt")).unwrap(); + let error = dev.event(|event| event["event"] == "error"); + assert!(error["code"].is_string(), "{error}"); + assert!(!installed.join("leak.txt").exists()); + std::fs::remove_file(folder.join("leak.txt")).unwrap(); + drop(dev); + + // Run again on the same folder: the widget is found, not added a second time. + let mut dev = Lines::start(desk.command().args(["dev", "Live"])); + let ready = dev.next(); + assert_eq!((ready["added"].as_bool(), ready["widgets"].as_array().map(Vec::len)), (Some(false), Some(1))); + drop(dev); + + // A package of the same name that did not come from this folder is not overwritten. + let other = desk.path("work/elsewhere/Live"); + std::fs::create_dir_all(&other).unwrap(); + std::fs::write(other.join("main.jsx"), "$render(other);").unwrap(); + let refused = desk.run(&["dev", "elsewhere/Live"]).expect(EXIT_FAILED); + assert_eq!(refused["error"]["kind"], "dev.package_exists"); + assert!(std::fs::read_to_string(installed.join("main.jsx")).unwrap().contains("three")); + // With --adopt it is. + let mut dev = Lines::start(desk.command().args(["dev", "elsewhere/Live", "--adopt"])); + assert_eq!(dev.next()["event"], "ready"); + assert!(std::fs::read_to_string(installed.join("main.jsx")).unwrap().contains("other")); + assert!(!installed.join("widget.json").exists(), "the installed copy holds exactly the files of the folder"); + drop(dev); + + // Without the service dev copies nothing. + assert_eq!(desk.signal_service(libc::SIGTERM), Some(0)); + desk.package("Later", "$render(later);", ""); + desk.run(&["dev", "Later"]).expect(EXIT_NEEDS_SERVICE); + assert!(!desk.path("data/Packages/Later").exists()); +} diff --git a/Linux/crates/sw-core/Cargo.toml b/Linux/crates/sw-core/Cargo.toml new file mode 100644 index 00000000..e147dbb7 --- /dev/null +++ b/Linux/crates/sw-core/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "sw-core" +description = "The ScriptWidget runtime for Linux: packages, compiler, executor and native APIs" +version.workspace = true +edition.workspace = true +license.workspace = true +publish.workspace = true +links = "swicu" + +[dependencies] +sw-quickjs.workspace = true +serde_json.workspace = true +sha2.workspace = true +getrandom.workspace = true +url.workspace = true +rustix.workspace = true +flate2.workspace = true +unicode-normalization.workspace = true +unicode-segmentation.workspace = true +unicode-properties.workspace = true +serde.workspace = true +libc.workspace = true +httparse.workspace = true +icu_normalizer.workspace = true +rustls.workspace = true +rustls-native-certs.workspace = true + +[build-dependencies] +cc.workspace = true +pkg-config.workspace = true diff --git a/Linux/crates/sw-core/build.rs b/Linux/crates/sw-core/build.rs new file mode 100644 index 00000000..b0a4ba6d --- /dev/null +++ b/Linux/crates/sw-core/build.rs @@ -0,0 +1,34 @@ +//! Compiles the ICU shim and links the system ICU. +//! +//! ICU's C functions are exported with the major version in their names +//! (`udat_open_78`); only the headers know the suffix. The shim is compiled +//! against the installed headers and re-exports the functions the Intl bridge +//! needs under fixed `sw_` names. + +fn main() { + println!("cargo:rerun-if-changed=native/icu_shim.c"); + let mut build = cc::Build::new(); + build.std("c11").file("native/icu_shim.c").warnings(true); + // The shim is a static library that refers to ICU, so ICU has to come after + // it on the link line: the link flags are printed once the shim is built. + let mut libraries = Vec::new(); + for name in ["icu-i18n", "icu-uc"] { + let library = pkg_config::Config::new() + .cargo_metadata(false) + .probe(name) + .unwrap_or_else(|error| panic!("the ICU development files ({name}.pc) are required: {error}")); + for path in &library.include_paths { + build.include(path); + } + libraries.push(library); + } + build.compile("swicu"); + for library in &libraries { + for path in &library.link_paths { + println!("cargo:rustc-link-search=native={}", path.display()); + } + for name in &library.libs { + println!("cargo:rustc-link-lib={name}"); + } + } +} diff --git a/Linux/crates/sw-core/native/icu_shim.c b/Linux/crates/sw-core/native/icu_shim.c new file mode 100644 index 00000000..f2bd8fcf --- /dev/null +++ b/Linux/crates/sw-core/native/icu_shim.c @@ -0,0 +1,114 @@ +// Fixed names for the ICU C functions the Intl bridge calls. ICU exports +// them with its major version appended (udat_open_78), which only the headers +// resolve, so Rust cannot name them directly. Every function here forwards to +// the ICU function of the same name and nothing else. + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Rust passes UTF-16 as uint16_t; UChar is char16_t or uint16_t depending on the compiler. +_Static_assert(sizeof(UChar) == sizeof(uint16_t), "UChar must be 16 bits"); +_Static_assert(sizeof(UErrorCode) == sizeof(int32_t), "UErrorCode must be 32 bits"); +_Static_assert(sizeof(UBool) == sizeof(int8_t), "UBool must be 8 bits"); + +typedef const uint16_t *Text; +typedef uint16_t *Buffer; +typedef int32_t *Status; + +// Major, minor, patch and build number of the ICU library in use. +void sw_u_getVersion(uint8_t version[4]) { u_getVersion(version); } + +int32_t sw_uloc_forLanguageTag(const char *tag, char *localeID, int32_t capacity, int32_t *parsedLength, Status status) { + return uloc_forLanguageTag(tag, localeID, capacity, parsedLength, (UErrorCode *)status); +} + +int32_t sw_ucal_getCanonicalTimeZoneID(Text id, int32_t length, Buffer result, int32_t capacity, int8_t *isSystemID, Status status) { + return ucal_getCanonicalTimeZoneID((const UChar *)id, length, (UChar *)result, capacity, (UBool *)isSystemID, (UErrorCode *)status); +} + +int32_t sw_ucal_getDefaultTimeZone(Buffer result, int32_t capacity, Status status) { + return ucal_getDefaultTimeZone((UChar *)result, capacity, (UErrorCode *)status); +} + +void *sw_udat_open(int32_t timeStyle, int32_t dateStyle, const char *locale, Text zone, int32_t zoneLength, Text pattern, int32_t patternLength, Status status) { + return udat_open((UDateFormatStyle)timeStyle, (UDateFormatStyle)dateStyle, locale, (const UChar *)zone, zoneLength, (const UChar *)pattern, patternLength, (UErrorCode *)status); +} + +void sw_udat_close(void *format) { udat_close((UDateFormat *)format); } + +int32_t sw_udat_format(const void *format, double date, Buffer result, int32_t capacity, Status status) { + return udat_format((const UDateFormat *)format, date, (UChar *)result, capacity, NULL, (UErrorCode *)status); +} + +int32_t sw_udat_formatForFields(const void *format, double date, Buffer result, int32_t capacity, void *iterator, Status status) { + return udat_formatForFields((const UDateFormat *)format, date, (UChar *)result, capacity, (UFieldPositionIterator *)iterator, (UErrorCode *)status); +} + +void *sw_ufieldpositer_open(Status status) { return ufieldpositer_open((UErrorCode *)status); } +void sw_ufieldpositer_close(void *iterator) { ufieldpositer_close((UFieldPositionIterator *)iterator); } +int32_t sw_ufieldpositer_next(void *iterator, int32_t *begin, int32_t *end) { return ufieldpositer_next((UFieldPositionIterator *)iterator, begin, end); } + +void *sw_udatpg_open(const char *locale, Status status) { return udatpg_open(locale, (UErrorCode *)status); } +void sw_udatpg_close(void *generator) { udatpg_close((UDateTimePatternGenerator *)generator); } +int32_t sw_udatpg_getBestPattern(void *generator, Text skeleton, int32_t length, Buffer pattern, int32_t capacity, Status status) { + return udatpg_getBestPattern((UDateTimePatternGenerator *)generator, (const UChar *)skeleton, length, (UChar *)pattern, capacity, (UErrorCode *)status); +} + +void *sw_unumf_openForSkeletonAndLocale(Text skeleton, int32_t length, const char *locale, Status status) { + return unumf_openForSkeletonAndLocale((const UChar *)skeleton, length, locale, (UErrorCode *)status); +} +void sw_unumf_close(void *formatter) { unumf_close((UNumberFormatter *)formatter); } +void *sw_unumf_openResult(Status status) { return unumf_openResult((UErrorCode *)status); } +void sw_unumf_closeResult(void *result) { unumf_closeResult((UFormattedNumber *)result); } +void sw_unumf_formatDecimal(const void *formatter, const char *value, int32_t length, void *result, Status status) { + unumf_formatDecimal((const UNumberFormatter *)formatter, value, length, (UFormattedNumber *)result, (UErrorCode *)status); +} +int32_t sw_unumf_resultToString(const void *result, Buffer buffer, int32_t capacity, Status status) { + return unumf_resultToString((const UFormattedNumber *)result, (UChar *)buffer, capacity, (UErrorCode *)status); +} +// `currency` is a NUL-terminated ISO 4217 code. +int32_t sw_ucurr_getDefaultFractionDigits(Text currency, Status status) { return ucurr_getDefaultFractionDigits((const UChar *)currency, (UErrorCode *)status); } + +void *sw_uplrules_openForType(const char *locale, int32_t type, Status status) { return uplrules_openForType(locale, (UPluralType)type, (UErrorCode *)status); } +void sw_uplrules_close(void *rules) { uplrules_close((UPluralRules *)rules); } +int32_t sw_uplrules_select(const void *rules, double number, Buffer keyword, int32_t capacity, Status status) { + return uplrules_select((const UPluralRules *)rules, number, (UChar *)keyword, capacity, (UErrorCode *)status); +} + +void *sw_ureldatefmt_open(const char *locale, int32_t width, Status status) { + return ureldatefmt_open(locale, NULL, (UDateRelativeDateTimeFormatterStyle)width, UDISPCTX_CAPITALIZATION_NONE, (UErrorCode *)status); +} +void sw_ureldatefmt_close(void *formatter) { ureldatefmt_close((URelativeDateTimeFormatter *)formatter); } +int32_t sw_ureldatefmt_format(const void *formatter, double offset, int32_t unit, Buffer result, int32_t capacity, Status status) { + return ureldatefmt_format((const URelativeDateTimeFormatter *)formatter, offset, (URelativeDateTimeUnit)unit, (UChar *)result, capacity, (UErrorCode *)status); +} +int32_t sw_ureldatefmt_formatNumeric(const void *formatter, double offset, int32_t unit, Buffer result, int32_t capacity, Status status) { + return ureldatefmt_formatNumeric((const URelativeDateTimeFormatter *)formatter, offset, (URelativeDateTimeUnit)unit, (UChar *)result, capacity, (UErrorCode *)status); +} + +void *sw_ucol_open(const char *locale, Status status) { return ucol_open(locale, (UErrorCode *)status); } +void sw_ucol_close(void *collator) { ucol_close((UCollator *)collator); } +int32_t sw_ucol_strcoll(const void *collator, Text source, int32_t sourceLength, Text target, int32_t targetLength) { + return (int32_t)ucol_strcoll((const UCollator *)collator, (const UChar *)source, sourceLength, (const UChar *)target, targetLength); +} + +void *sw_ulistfmt_openForType(const char *locale, int32_t type, int32_t width, Status status) { + return ulistfmt_openForType(locale, (UListFormatterType)type, (UListFormatterWidth)width, (UErrorCode *)status); +} +void sw_ulistfmt_close(void *formatter) { ulistfmt_close((UListFormatter *)formatter); } +int32_t sw_ulistfmt_format(const void *formatter, const Text *strings, const int32_t *lengths, int32_t count, Buffer result, int32_t capacity, Status status) { + return ulistfmt_format((const UListFormatter *)formatter, (const UChar *const *)strings, lengths, count, (UChar *)result, capacity, (UErrorCode *)status); +} diff --git a/Linux/crates/sw-core/src/api/fetch.rs b/Linux/crates/sw-core/src/api/fetch.rs new file mode 100644 index 00000000..2927be1b --- /dev/null +++ b/Linux/crates/sw-core/src/api/fetch.rs @@ -0,0 +1,582 @@ +//! `fetch` and `$http`. Ported from +//! `Windows/src/ScriptWidget.Core/Api/FetchModule.cs`, which mirrors +//! ScriptWidgetRuntimeFetch: public HTTP(S) only, the package's declared hosts +//! only, bounded request and response sizes, a per-execution request budget, +//! and every redirect re-validated. +//! +//! Beyond the host-name rule, the resolved addresses are checked so a public +//! name cannot point at a private or loopback address, and the connection is +//! opened to the very address that was checked (see [`crate::api::http`]), so +//! a DNS answer that changes between the check and the connect cannot be used. +//! +//! Nothing here logs: URLs, headers and bodies can carry credentials. + +use std::collections::HashMap; +use std::fmt; +use std::io::Read; +use std::net::{IpAddr, ToSocketAddrs}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use serde_json::json; +pub use url::Url; + +use crate::api::http::HttpTransport; +use crate::api::policy::{self, NetworkDeclaration}; +use crate::api::workers::BoundedExecutor; +use crate::executor::{Completion, ExecutionRequest, NativeModule}; +use crate::json::{Object, ObjectExt, quote, stringify}; + +pub const MAXIMUM_RESPONSE_BYTES: usize = 2 * 1024 * 1024; +pub const MAXIMUM_REQUEST_BODY_BYTES: usize = 1024 * 1024; +pub const MAXIMUM_REQUESTS: u32 = 32; +pub const MAXIMUM_CONCURRENT: usize = 8; +pub const MAXIMUM_REDIRECTS: usize = 5; +pub const MAXIMUM_TOTAL_MILLISECONDS: u64 = 10_000; +pub const EXPOSED_HEADERS: [&str; 6] = ["content-type", "cache-control", "expires", "last-modified", "date", "retry-after"]; + +pub const BLOCKED_MESSAGE: &str = "Local and private network hosts are not allowed"; +pub const TIMED_OUT_MESSAGE: &str = "The request timed out."; + +/// Sent when the script sets no User-Agent of its own. No user or device detail is included. +pub const DEFAULT_USER_AGENT: &str = concat!("ScriptWidget-Linux/", env!("CARGO_PKG_VERSION")); + +/// Request headers that carry credentials; they stay with the origin the script addressed. +const CREDENTIAL_HEADERS: [&str; 3] = ["authorization", "proxy-authorization", "cookie"]; +const METHODS: [&str; 8] = ["OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "TRACE"]; + +/// Why a request did not produce a response. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum FetchError { + /// A rule or limit of this module; the message is one of its own sentences and is shown to the script. + Failed(String), + /// A failure below it: the socket, TLS or a malformed response. The text + /// comes from the system; callers that must not forward it replace it. + Transport(String), + /// The execution ended while the request was in flight. + Cancelled, +} + +impl FetchError { + pub fn failed(message: impl Into) -> Self { + Self::Failed(message.into()) + } + + pub fn message(&self) -> &str { + match self { + Self::Failed(message) | Self::Transport(message) => message, + Self::Cancelled => "The operation was canceled.", + } + } + + /// Carries the error through `std::io` interfaces such as decoders. + pub fn into_io(self) -> std::io::Error { + std::io::Error::other(self) + } + + /// The error an I/O failure stands for: the one [`FetchError::into_io`] wrapped, or a transport failure. + pub fn from_io(error: std::io::Error) -> Self { + if error.get_ref().is_some_and(|inner| inner.is::()) { + return *error.into_inner().and_then(|inner| inner.downcast::().ok()).expect("checked above"); + } + Self::Transport(error.to_string()) + } +} + +impl fmt::Display for FetchError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(self.message()) + } +} + +impl std::error::Error for FetchError {} + +/// Turns a host name (or address literal) into addresses. +pub type Resolver = Arc std::io::Result> + Send + Sync>; + +/// True once the work is no longer wanted. +pub type Cancel = Arc bool + Send + Sync>; + +/// The operating system's resolver. An address literal is returned as is. +pub fn system_resolver(host: &str) -> std::io::Result> { + if let Ok(literal) = host.parse::() { + return Ok(vec![literal]); + } + let mut addresses: Vec = Vec::new(); + for address in (host, 0u16).to_socket_addrs()? { + if !addresses.contains(&address.ip()) { + addresses.push(address.ip()); + } + } + Ok(addresses) +} + +/// The addresses of `host`, all of them public, or the refusal. With +/// `check_name` the host is refused by name first, without asking the +/// resolver. One private address among public ones is enough to refuse: the +/// connection could go to it. +pub fn vetted_addresses(resolver: &Resolver, host: &str, check_name: bool) -> Result, FetchError> { + let name = unbracket(host); + if check_name && (name.is_empty() || policy::blocked_host_name(name)) { + return Err(FetchError::failed(BLOCKED_MESSAGE)); + } + let addresses = resolver(name).map_err(|_| FetchError::Failed(format!("Could not resolve {host}")))?; + if addresses.is_empty() || addresses.iter().any(|address| policy::blocked_address(*address)) { + return Err(FetchError::failed(BLOCKED_MESSAGE)); + } + Ok(addresses) +} + +pub fn unbracket(host: &str) -> &str { + host.strip_prefix('[').and_then(|rest| rest.strip_suffix(']')).unwrap_or(host) +} + +/// The waits of one request. Each wait (connect and response head, then every +/// read of the body) gets the request timeout, inside a total deadline, and +/// ends early when the execution does. +#[derive(Clone)] +pub struct Wait(Arc); + +struct WaitState { + deadline: Instant, + timeout: Duration, + until: Mutex, + cancel: Option, +} + +impl Wait { + pub fn new(total: Duration, timeout: Duration, cancel: Option) -> Self { + let now = Instant::now(); + Self(Arc::new(WaitState { deadline: now + total, timeout, until: Mutex::new(now), cancel })) + } + + /// Starts the next wait. Fails when the total deadline has passed. + pub fn arm(&self) -> Result<(), FetchError> { + let now = Instant::now(); + if now >= self.0.deadline { + return Err(FetchError::failed(TIMED_OUT_MESSAGE)); + } + *self.0.until.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) = (now + self.0.timeout).min(self.0.deadline); + Ok(()) + } + + pub fn cancelled(&self) -> bool { + self.0.cancel.as_ref().is_some_and(|cancel| cancel()) + } + + /// Time left in the current wait, or why there is none. + pub fn remaining(&self) -> Result { + if self.cancelled() { + return Err(FetchError::Cancelled); + } + let until = *self.0.until.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + let left = until.saturating_duration_since(Instant::now()); + if left.is_zero() { Err(FetchError::failed(TIMED_OUT_MESSAGE)) } else { Ok(left) } + } + + /// Blocks until the current wait is over and says how it ended: what an + /// exchange that never answers amounts to. + pub fn expire(&self) -> FetchError { + loop { + match self.remaining() { + Ok(left) => std::thread::sleep(left.min(Duration::from_millis(20))), + Err(error) => return error, + } + } + } +} + +/// One request as the transport sends it. Nothing in it is trusted to be safe to resolve again. +pub struct TransportRequest<'a> { + /// Upper-case method. + pub method: &'a str, + pub url: &'a Url, + /// Validated names and values, without `Host` and `Content-Length`. + pub headers: &'a [(String, String)], + pub body: Option<&'a [u8]>, +} + +/// The head of a response and its body, still unread. +pub struct TransportResponse { + pub status: u16, + /// In the order received; names as received. + pub headers: Vec<(String, String)>, + /// Fails with [`FetchError::into_io`] errors for timeouts and cancellation. + pub body: Box, +} + +/// Performs one HTTP exchange and nothing more: no redirects, cookies, +/// credentials or proxy. The production transport connects only to addresses +/// the resolver returned and [`policy::blocked_address`] accepted. +pub trait Transport: Send + Sync { + fn send(&self, request: &TransportRequest<'_>, wait: &Wait) -> Result; +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Response { + pub status: u16, + pub body: Vec, + /// Response headers by lower-case name; the last value wins. + pub headers: HashMap, +} + +struct Client { + resolver: Resolver, + transport: Arc, +} + +pub struct FetchModule { + client: Arc, + pool: BoundedExecutor, +} + +impl Default for FetchModule { + fn default() -> Self { + Self::new() + } +} + +impl FetchModule { + /// The production module: the system resolver and the vetted-address transport. + pub fn new() -> Self { + Self::with_resolver(Arc::new(system_resolver)) + } + + /// The production transport over another resolver. + pub fn with_resolver(resolver: Resolver) -> Self { + let transport = Arc::new(HttpTransport::new(resolver.clone())); + Self::with_parts(resolver, transport) + } + + /// `resolver` turns a host name into addresses; `transport` sends the + /// request and must not follow redirects itself. Only the host + /// application constructs modules: package code cannot reach this. + pub fn with_parts(resolver: Resolver, transport: Arc) -> Self { + Self { client: Arc::new(Client { resolver, transport }), pool: BoundedExecutor::new(MAXIMUM_CONCURRENT) } + } + + /// Performs one request, following up to five redirects that + /// `allow_redirect` accepts. Blocks the calling thread; call it from a worker. + /// + /// Every hop is resolved and its addresses checked before anything is + /// sent. The response body is counted while it is read, never trusting + /// `Content-Length`, and reading stops at the limit. + #[allow(clippy::too_many_arguments)] + pub fn perform( + &self, + method: &str, + start: &Url, + headers: &[(String, String)], + body: Option<&[u8]>, + timeout_milliseconds: u64, + allow_redirect: &dyn Fn(&Url) -> bool, + cancel: Option, + ) -> Result { + self.client.perform(method, start, headers, body, timeout_milliseconds, allow_redirect, cancel) + } +} + +impl Client { + #[allow(clippy::too_many_arguments)] + fn perform( + &self, + method: &str, + start: &Url, + headers: &[(String, String)], + body: Option<&[u8]>, + timeout_milliseconds: u64, + allow_redirect: &dyn Fn(&Url) -> bool, + cancel: Option, + ) -> Result { + let mut current = start.clone(); + let mut current_method = method.to_uppercase(); + let mut current_body = body; + if !METHODS.contains(¤t_method.as_str()) { + return Err(FetchError::Failed(format!("Unknown method '{current_method}'"))); + } + let wait = Wait::new(Duration::from_millis(MAXIMUM_TOTAL_MILLISECONDS), Duration::from_millis(timeout_milliseconds), cancel); + for hop in 0..=MAXIMUM_REDIRECTS { + if wait.cancelled() { + return Err(FetchError::Cancelled); + } + let host = current.host_str().unwrap_or(""); + vetted_addresses(&self.resolver, host, false)?; + wait.arm()?; + + // A redirect to another origin does not get the credentials meant for the first one, as in + // browsers and curl. (The Android transport this was first ported from sends them on.) + let other_origin = start.origin() != current.origin(); + let mut unique: Vec<(String, String)> = Vec::new(); + for (name, value) in headers { + if other_origin && CREDENTIAL_HEADERS.iter().any(|credential| name.eq_ignore_ascii_case(credential)) { + continue; + } + match unique.iter_mut().find(|(known, _)| known.eq_ignore_ascii_case(name)) { + Some(entry) => entry.1 = value.clone(), + None => unique.push((name.clone(), value.clone())), + } + } + let mut sent: Vec<(String, String)> = Vec::new(); + for (name, value) in &unique { + if name.eq_ignore_ascii_case("Host") || name.eq_ignore_ascii_case("Content-Length") { + continue; + } + // A value can never start a new header line, whatever the transport would do with it. + if !valid_header_name(name) || value.chars().any(|character| matches!(character, '\r' | '\n' | '\0')) { + // The name is the script's own; the value is not repeated. + return Err(FetchError::Failed(format!("Request header {} is not valid", printable(name)))); + } + sent.push((name.clone(), value.clone())); + } + // The system network stacks on iPhone and Android name the client by themselves; services + // such as the GitHub API refuse a request without a user agent. A script's own + // User-Agent header wins. + if !unique.iter().any(|(name, _)| name.eq_ignore_ascii_case("User-Agent")) { + sent.push(("User-Agent".into(), DEFAULT_USER_AGENT.into())); + } + let request = TransportRequest { method: ¤t_method, url: ¤t, headers: &sent, body: current_body.filter(|_| current_method != "GET") }; + let mut response = self.transport.send(&request, &wait)?; + + let status = response.status; + if (300..=399).contains(&status) && status != 304 { + let location = last_header(&response.headers, "location").ok_or_else(|| FetchError::failed("Redirect without a location"))?; + let next = current.join(location).ok().filter(|next| hop != MAXIMUM_REDIRECTS && allow_redirect(next)); + let Some(next) = next else { return Err(FetchError::failed("Redirect target is not allowed")) }; + if status == 303 || ((status == 301 || status == 302) && current_method == "POST") { + current_method = "GET".into(); + current_body = None; + } + current = next; + continue; + } + let too_large = || FetchError::Failed(format!("Response exceeds the {MAXIMUM_RESPONSE_BYTES}-byte limit")); + if last_header(&response.headers, "content-length").and_then(|text| text.trim().parse::().ok()).is_some_and(|length| length > MAXIMUM_RESPONSE_BYTES as u64) { + return Err(too_large()); + } + let mut buffer: Vec = Vec::new(); + let mut chunk = vec![0u8; 16 * 1024]; + loop { + wait.arm()?; + let read = response.body.read(&mut chunk).map_err(FetchError::from_io)?; + if read == 0 { + break; + } + if buffer.len() + read > MAXIMUM_RESPONSE_BYTES { + return Err(too_large()); + } + buffer.extend_from_slice(&chunk[..read]); + } + if wait.cancelled() { + return Err(FetchError::Cancelled); + } + let mut response_headers = HashMap::new(); + for (name, value) in response.headers { + response_headers.insert(name.to_ascii_lowercase(), value); + } + return Ok(Response { status, body: buffer, headers: response_headers }); + } + Err(FetchError::failed("Too many redirects")) + } +} + +impl NativeModule for FetchModule { + fn handles(&self, operation: &str) -> bool { + operation == "http.request" + } + + fn start(&self, request: &ExecutionRequest<'_>, _operation: &str, payload: Option<&Object>, done: Completion) { + let job = payload.cloned().unwrap_or_default(); + let fail = |done: Completion, message: &str| done.complete(false, Some(quote(message))); + if policy::is_live_activity(request) { + return fail(done, "[live_activity_unavailable] Live Activity presentations cannot request network data; update their state from the app"); + } + if policy::is_timeline_projection(request) { + return fail(done, "[timeline_read_only] Future entries cannot make live network requests; read a saved snapshot"); + } + let url_text = job.opt_string("url"); + let url = match parse_request_url(&url_text) { + Ok(url) => url, + Err(message) => return fail(done, &message), + }; + if let Some(invalid) = validation_error(&url) { + return fail(done, invalid); + } + let declaration = NetworkDeclaration::of(request); + if !declaration.allows_host(url.host_str().unwrap_or("")) { + return fail(done, "Network host is not declared in widget.json"); + } + if request.session.add_network_request() > MAXIMUM_REQUESTS { + return fail(done, "Network request budget exceeded"); + } + + let client = self.client.clone(); + let session = request.session.clone(); + self.pool.execute(move || { + let outcome = (|| -> Result, FetchError> { + let mut headers: Vec<(String, String)> = Vec::new(); + if let Some(map) = job.opt_object("headers") { + headers.extend(map.keys().map(|name| (name.clone(), map.opt_string(name)))); + } + let body = job.has("body").then(|| job.opt_string("body").into_bytes()); + if body.is_some() && job.opt_bool("jsonBody", false) && !headers.iter().any(|(name, _)| name.eq_ignore_ascii_case("Content-Type")) { + headers.push(("Content-Type".into(), "application/json".into())); + } + if body.as_ref().is_some_and(|body| body.len() > MAXIMUM_REQUEST_BODY_BYTES) { + return Err(FetchError::Failed(format!("Request body exceeds the {MAXIMUM_REQUEST_BODY_BYTES}-byte limit"))); + } + let timeout = normalized_timeout(job.has("timeout").then(|| job.opt_f64("timeout"))); + let method = match job.opt_string("method") { + method if method.is_empty() => "GET".to_owned(), + method => method, + }; + let live = session.clone(); + let allow_redirect = |redirected: &Url| !live.finished() && validation_error(redirected).is_none() && declaration.allows_host(redirected.host_str().unwrap_or("")); + let ended = session.clone(); + let response = client.perform(&method, &url, &headers, body.as_deref(), timeout, &allow_redirect, Some(Arc::new(move || ended.finished())))?; + if session.finished() { + return Ok(None); + } + let text = if job.opt_bool("base64", false) { Some(base64(&response.body)) } else { String::from_utf8(response.body).ok() }; + if job.opt_bool("metadata", false) { + let text = text.ok_or_else(|| FetchError::failed("Response is not HTTP or has invalid UTF-8"))?; + let mut allowed = Object::new(); + for name in EXPOSED_HEADERS { + if let Some(value) = response.headers.get(name).filter(|value| value.len() <= 2048) { + allowed.insert(name.to_owned(), value.clone().into()); + } + } + Ok(Some(stringify(&json!({ "status": response.status, "body": text, "headers": allowed })))) + } else { + let text = text.ok_or_else(|| FetchError::Failed(format!("{url_text} is empty")))?; + Ok(Some(quote(&text))) + } + })(); + match outcome { + Ok(Some(json)) => done.complete(true, Some(json)), + // The execution ended: there is nobody to answer. + Ok(None) => drop(done), + Err(error) => done.complete(false, Some(quote(if error.message().is_empty() { "Request failed" } else { error.message() }))), + } + }); + } +} + +/// The URL a script asked for, or the message it is refused with. A URL that +/// is not absolute, or not HTTP(S), is refused as such; one that has no +/// authority of its own (`https:///x`, `https:x`) is not a URL here, although +/// a browser would repair it. +pub fn parse_request_url(text: &str) -> Result { + const SCHEME: &str = "Only HTTP and HTTPS URLs are allowed"; + let url = match Url::parse(text) { + Ok(url) => url, + Err(url::ParseError::RelativeUrlWithoutBase) => return Err(SCHEME.into()), + Err(_) => return Err(format!("{text} is not url")), + }; + if !matches!(url.scheme(), "http" | "https") { + return Err(SCHEME.into()); + } + let written = text.trim_matches(|character: char| character <= ' '); + let authority = written.split_once(':').map(|(_, rest)| rest).and_then(|rest| rest.strip_prefix("//")); + match authority.and_then(|rest| rest.chars().next()) { + Some(first) if !matches!(first, '/' | '\\' | '?' | '#') => Ok(url), + _ => Err(format!("{text} is not url")), + } +} + +/// HTTP(S), a host, no credentials, not local or private by name. +pub fn validation_error(url: &Url) -> Option<&'static str> { + if !matches!(url.scheme(), "http" | "https") { + return Some("Only HTTP and HTTPS URLs are allowed"); + } + let host = unbracket(url.host_str().unwrap_or("")); + if host.is_empty() { + return Some("URL host is required"); + } + if !url.username().is_empty() || url.password().is_some() { + return Some("URL credentials are not allowed"); + } + policy::blocked_host_name(host).then_some(BLOCKED_MESSAGE) +} + +/// Seconds from the script, clamped to 1...10; 5 by default. Returns milliseconds. +pub fn normalized_timeout(seconds: Option) -> u64 { + let value = match seconds { + Some(given) if !given.is_nan() && given > 0.0 => given.clamp(1.0, 10.0), + _ => 5.0, + }; + (value * 1000.0) as u64 +} + +/// A header name is a token (RFC 9110): visible ASCII without separators. +fn valid_header_name(name: &str) -> bool { + !name.is_empty() && name.bytes().all(|byte| byte.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&byte)) +} + +fn printable(name: &str) -> String { + name.chars().filter(|character| *character > ' ' && *character < '\u{7f}').take(64).collect() +} + +fn last_header<'a>(headers: &'a [(String, String)], name: &str) -> Option<&'a str> { + headers.iter().rev().find(|(known, _)| known.eq_ignore_ascii_case(name)).map(|(_, value)| value.as_str()) +} + +/// Standard base64 with padding. +pub fn base64(bytes: &[u8]) -> String { + const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let mut output = String::with_capacity(bytes.len().div_ceil(3) * 4); + for group in bytes.chunks(3) { + let value = u32::from(group[0]) << 16 | u32::from(*group.get(1).unwrap_or(&0)) << 8 | u32::from(*group.get(2).unwrap_or(&0)); + output.push(ALPHABET[(value >> 18) as usize & 63] as char); + output.push(ALPHABET[(value >> 12) as usize & 63] as char); + output.push(if group.len() > 1 { ALPHABET[(value >> 6) as usize & 63] as char } else { '=' }); + output.push(if group.len() > 2 { ALPHABET[value as usize & 63] as char } else { '=' }); + } + output +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn base64_matches_the_standard_alphabet() { + for (bytes, expected) in [(&b""[..], ""), (b"f", "Zg=="), (b"fo", "Zm8="), (b"foo", "Zm9v"), (b"foob", "Zm9vYg=="), (&[0xFF, 0x00, 0x10], "/wAQ"), (&[0xFB, 0xEF, 0xBE], "++++")] { + assert_eq!(base64(bytes), expected); + } + } + + #[test] + fn timeouts_are_clamped_to_one_to_ten_seconds() { + assert_eq!(normalized_timeout(None), 5000); + assert_eq!(normalized_timeout(Some(f64::NAN)), 5000); + assert_eq!(normalized_timeout(Some(0.0)), 5000); + assert_eq!(normalized_timeout(Some(-3.0)), 5000); + assert_eq!(normalized_timeout(Some(0.2)), 1000); + assert_eq!(normalized_timeout(Some(2.5)), 2500); + assert_eq!(normalized_timeout(Some(60.0)), 10000); + assert_eq!(normalized_timeout(Some(f64::INFINITY)), 10000); + assert_eq!(MAXIMUM_RESPONSE_BYTES, 2 * 1024 * 1024); + assert_eq!(MAXIMUM_REQUESTS, 32); + assert_eq!(MAXIMUM_REDIRECTS, 5); + } + + #[test] + fn request_urls_need_a_scheme_and_an_authority_of_their_own() { + for text in ["https://example.com/", "http://example.com:8080/a?b=c", "HTTPS://EXAMPLE.com", "http://[::1]/", "https://user:pw@example.com/"] { + assert!(parse_request_url(text).is_ok(), "{text}"); + } + for text in ["ftp://example.com/x", "file:///etc/passwd", "example.com/x", "", "/relative", "javascript:alert(1)", "data:text/plain,x"] { + assert_eq!(parse_request_url(text).unwrap_err(), "Only HTTP and HTTPS URLs are allowed", "{text}"); + } + for text in ["https:///status", "https:example.com", "https:/example.com", "http:\\\\example.com", "https://", "http://exa mple.com/", "https://example.com:port/"] { + assert_eq!(parse_request_url(text).unwrap_err(), format!("{text} is not url"), "{text}"); + } + } + + #[test] + fn io_errors_carry_fetch_errors_through() { + assert_eq!(FetchError::from_io(FetchError::failed(TIMED_OUT_MESSAGE).into_io()), FetchError::failed(TIMED_OUT_MESSAGE)); + assert_eq!(FetchError::from_io(FetchError::Cancelled.into_io()), FetchError::Cancelled); + assert!(matches!(FetchError::from_io(std::io::Error::from(std::io::ErrorKind::ConnectionReset)), FetchError::Transport(_))); + } +} diff --git a/Linux/crates/sw-core/src/api/http.rs b/Linux/crates/sw-core/src/api/http.rs new file mode 100644 index 00000000..ee7c827d --- /dev/null +++ b/Linux/crates/sw-core/src/api/http.rs @@ -0,0 +1,502 @@ +//! The production transport of `fetch`: one HTTP/1.1 exchange over a socket +//! this module opened itself. The Linux counterpart of the +//! `SocketsHttpHandler` with a connect callback in +//! `Windows/src/ScriptWidget.Core/Api/FetchModule.cs`. +//! +//! Rules, all of them tested: +//! - the host is refused by name first; then every address it resolves to must +//! be public, and the socket connects to one of exactly those addresses. The +//! name is never resolved a second time by a lower layer; +//! - TLS is verified against the host name of the URL, whatever address the +//! socket is connected to, with the certificate authorities of the system; +//! - no redirects, cookies, credentials or proxy. The proxy variables of the +//! environment are not read: the address a proxy would reach cannot be +//! checked here; +//! - every wait (connecting, the handshake and the response head, then each +//! read of the body) is bounded by the caller's [`Wait`]; +//! - the response head is at most 64 KiB; the body is delimited by +//! `Content-Length`, by chunks, or by the end of the connection, and is +//! handed on undecoded except for gzip and deflate. +//! +//! One connection serves one request (`Connection: close`). Widgets make a +//! handful of requests per refresh, and a connection that is never reused +//! cannot be reused for another host by mistake. + +use std::io::{self, Read, Write}; +use std::net::{SocketAddr, TcpStream}; +use std::sync::{Arc, OnceLock}; +use std::time::Duration; + +use rustls::pki_types::ServerName; +use rustls::{ClientConfig, ClientConnection, RootCertStore}; + +use crate::api::fetch::{FetchError, Resolver, Transport, TransportRequest, TransportResponse, Wait, unbracket, vetted_addresses}; + +/// Largest response head (status line and headers) accepted. +pub const MAXIMUM_HEAD_BYTES: usize = 64 * 1024; +const MAXIMUM_HEADERS: usize = 128; +const MAXIMUM_CHUNK_LINE_BYTES: usize = 1024; +/// How often a blocked socket operation looks at the clock and at cancellation. +const POLL: Duration = Duration::from_millis(100); +/// Headers that describe the connection itself; a script does not get to set them. +const CONNECTION_HEADERS: [&str; 8] = ["connection", "keep-alive", "proxy-connection", "transfer-encoding", "te", "trailer", "upgrade", "expect"]; + +/// Opens the TCP connection to an address that has been checked. +pub trait Dialer: Send + Sync { + fn dial(&self, address: SocketAddr, timeout: Duration) -> io::Result; +} + +/// Connects to the address it is given, directly. +pub struct SystemDialer; + +impl Dialer for SystemDialer { + fn dial(&self, address: SocketAddr, timeout: Duration) -> io::Result { + TcpStream::connect_timeout(&address, timeout) + } +} + +pub struct HttpTransport { + resolver: Resolver, + dialer: Arc, + tls: OnceLock, String>>, +} + +impl HttpTransport { + pub fn new(resolver: Resolver) -> Self { + Self { resolver, dialer: Arc::new(SystemDialer), tls: OnceLock::new() } + } + + /// The transport over another network: tests route the public addresses + /// of their fixtures to listeners on this machine and trust their own + /// certificate authority. The address checks stay in force. Only the host + /// application constructs transports: package code cannot reach this. + pub fn with_parts(resolver: Resolver, dialer: Arc, tls: Option>) -> Self { + let cell = OnceLock::new(); + if let Some(config) = tls { + let _ = cell.set(Ok(config)); + } + Self { resolver, dialer, tls: cell } + } + + /// Opens the TCP connection for one request. The host is refused by name + /// first; then every address it resolves to must be public, and the + /// socket connects to one of exactly those addresses. + pub fn connect(resolver: &Resolver, dialer: &dyn Dialer, host: &str, port: u16, wait: &Wait) -> Result { + let addresses = vetted_addresses(resolver, host, true)?; + let mut last = None; + for address in addresses { + match dialer.dial(SocketAddr::new(address, port), wait.remaining()?) { + Ok(socket) => return Ok(socket), + Err(error) => last = Some(error), + } + } + Err(match last { + Some(error) if error.kind() == io::ErrorKind::TimedOut => wait.remaining().err().unwrap_or_else(|| FetchError::Transport(error.to_string())), + Some(error) => FetchError::Transport(error.to_string()), + None => FetchError::Transport("No address to connect to".into()), + }) + } + + fn tls_config(&self) -> Result, FetchError> { + self.tls.get_or_init(system_tls_config).clone().map_err(FetchError::Transport) + } +} + +/// TLS 1.2 and 1.3 with the certificate authorities the system trusts. +fn system_tls_config() -> Result, String> { + let mut roots = RootCertStore::empty(); + let found = rustls_native_certs::load_native_certs(); + let (added, _ignored) = roots.add_parsable_certificates(found.certs); + if added == 0 { + return Err("No trusted certificate authorities were found on this system".into()); + } + client_config(roots) +} + +/// A client configuration that trusts exactly `roots`. +pub fn client_config(roots: RootCertStore) -> Result, String> { + let provider = Arc::new(rustls::crypto::ring::default_provider()); + let mut config = ClientConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions() + .map_err(|error| error.to_string())? + .with_root_certificates(roots) + .with_no_client_auth(); + config.alpn_protocols = vec![b"http/1.1".to_vec()]; + Ok(Arc::new(config)) +} + +impl Transport for HttpTransport { + fn send(&self, request: &TransportRequest<'_>, wait: &Wait) -> Result { + let url = request.url; + let secure = match url.scheme() { + "https" => true, + "http" => false, + _ => return Err(FetchError::failed("Only HTTP and HTTPS URLs are allowed")), + }; + let host = url.host_str().unwrap_or(""); + let port = url.port_or_known_default().unwrap_or(if secure { 443 } else { 80 }); + // Read before connecting, so a missing certificate store is reported without touching the network. + let tls = if secure { Some(self.tls_config()?) } else { None }; + + let socket = Self::connect(&self.resolver, self.dialer.as_ref(), host, port, wait)?; + let prepare = || -> io::Result<()> { + socket.set_nodelay(true)?; + socket.set_read_timeout(Some(POLL))?; + socket.set_write_timeout(Some(POLL)) + }; + prepare().map_err(FetchError::from_io)?; + let mut channel = Channel { socket, tls: None, wait: wait.clone() }; + if let Some(config) = tls { + // The certificate must be valid for the name in the URL, not for the address. + let name = ServerName::try_from(unbracket(host).to_owned()).map_err(|_| FetchError::Transport("The host name is not valid for TLS".into()))?; + let connection = ClientConnection::new(config, name).map_err(|error| FetchError::Transport(error.to_string()))?; + channel.tls = Some(Box::new(connection)); + channel.handshake().map_err(FetchError::from_io)?; + } + + channel.write_all(&encode_head(request)).map_err(FetchError::from_io)?; + if let Some(body) = request.body { + channel.write_all(body).map_err(FetchError::from_io)?; + } + let mut source = Source { channel, buffer: Vec::new(), position: 0 }; + let (status, headers) = loop { + let (status, headers) = read_head(&mut source)?; + // Interim responses (100 Continue, 103 Early Hints) precede the real one. + if !(100..200).contains(&status) || status == 101 { + break (status, headers); + } + }; + respond(request.method, status, headers, source) + } +} + +/// The request line and headers, ending with the empty line. +fn encode_head(request: &TransportRequest<'_>) -> Vec { + let url = request.url; + let mut head = format!("{} {}", request.method, url.path()); + if let Some(query) = url.query() { + head.push('?'); + head.push_str(query); + } + head.push_str(" HTTP/1.1\r\nHost: "); + head.push_str(url.host_str().unwrap_or("")); + if let Some(port) = url.port() { + head.push_str(&format!(":{port}")); + } + head.push_str("\r\n"); + let mut accepts = false; + for (name, value) in request.headers { + let lower = name.to_ascii_lowercase(); + if lower == "host" || lower == "content-length" || CONNECTION_HEADERS.contains(&lower.as_str()) { + continue; + } + // The caller validated both; this is the last line of defence against a second header line. + if name.is_empty() || name.bytes().any(|byte| byte <= b' ' || byte >= 0x7f || byte == b':') || value.bytes().any(|byte| matches!(byte, b'\r' | b'\n' | 0)) { + continue; + } + accepts |= lower == "accept-encoding"; + head.push_str(&format!("{name}: {value}\r\n")); + } + if !accepts { + head.push_str("Accept-Encoding: gzip, deflate\r\n"); + } + match request.body { + Some(body) => head.push_str(&format!("Content-Length: {}\r\n", body.len())), + None if matches!(request.method, "POST" | "PUT" | "PATCH") => head.push_str("Content-Length: 0\r\n"), + None => {} + } + head.push_str("Connection: close\r\n\r\n"); + head.into_bytes() +} + +fn invalid_response() -> FetchError { + FetchError::Transport("The server sent an invalid response".into()) +} + +fn read_head(source: &mut Source) -> Result<(u16, Vec<(String, String)>), FetchError> { + loop { + let available = &source.buffer[source.position..]; + if !available.is_empty() { + let mut slots = [httparse::EMPTY_HEADER; MAXIMUM_HEADERS]; + let mut response = httparse::Response::new(&mut slots); + match response.parse(available) { + Ok(httparse::Status::Complete(length)) => { + let status = response.code.ok_or_else(invalid_response)?; + let headers = response.headers.iter().map(|header| (header.name.to_owned(), String::from_utf8_lossy(header.value).trim().to_owned())).collect(); + source.position += length; + return Ok((status, headers)); + } + Ok(httparse::Status::Partial) => {} + Err(_) => return Err(invalid_response()), + } + if available.len() > MAXIMUM_HEAD_BYTES { + return Err(FetchError::Transport("The response headers are too large".into())); + } + } + if source.fill().map_err(FetchError::from_io)? == 0 { + return Err(FetchError::Transport("The connection closed before a response arrived".into())); + } + } +} + +fn header<'a>(headers: &'a [(String, String)], name: &str) -> Option<&'a str> { + headers.iter().rev().find(|(known, _)| known.eq_ignore_ascii_case(name)).map(|(_, value)| value.as_str()) +} + +/// Chooses how the body is delimited and decoded. +fn respond(method: &str, status: u16, mut headers: Vec<(String, String)>, source: Source) -> Result { + let chunked = headers + .iter() + .filter(|(name, _)| name.eq_ignore_ascii_case("transfer-encoding")) + .flat_map(|(_, value)| value.split(',')) + .any(|coding| coding.trim().eq_ignore_ascii_case("chunked")); + let mut lengths = headers.iter().filter(|(name, _)| name.eq_ignore_ascii_case("content-length")).map(|(_, value)| value.as_str()); + let length = match lengths.next() { + None => None, + Some(first) => { + // Two different lengths describe two different bodies: neither is believed. + if lengths.any(|other| other != first) || first.is_empty() || !first.bytes().all(|byte| byte.is_ascii_digit()) { + return Err(invalid_response()); + } + Some(first.parse::().map_err(|_| invalid_response())?) + } + }; + let framing = if method == "HEAD" || status == 204 || status == 304 || (100..200).contains(&status) { + Framing::Done + } else if chunked { + Framing::ChunkSize + } else if let Some(length) = length { + if length == 0 { Framing::Done } else { Framing::Length(length) } + } else { + Framing::UntilClose + }; + let empty = matches!(framing, Framing::Done); + let raw = RawBody { source, framing }; + let coding = header(&headers, "content-encoding").map(|value| value.trim().to_ascii_lowercase()).unwrap_or_default(); + let body: Box = match coding.as_str() { + _ if empty => Box::new(raw), + "gzip" | "x-gzip" => Box::new(flate2::read::GzDecoder::new(raw)), + "deflate" => Box::new(flate2::read::ZlibDecoder::new(raw)), + _ => return Ok(TransportResponse { status, headers, body: Box::new(raw) }), + }; + if !empty { + // The body is handed on decoded: its encoding and its encoded length no longer describe it. + headers.retain(|(name, _)| !name.eq_ignore_ascii_case("content-encoding") && !name.eq_ignore_ascii_case("content-length")); + } + Ok(TransportResponse { status, headers, body }) +} + +/// The socket, with TLS when the URL asks for it. Every operation waits no +/// longer than the current wait allows. +struct Channel { + socket: TcpStream, + tls: Option>, + wait: Wait, +} + +/// The socket as a blocking stream that gives up when the wait is over or the execution ended. +struct Patient<'a> { + socket: &'a TcpStream, + wait: &'a Wait, +} + +fn timed_out(error: &io::Error) -> bool { + matches!(error.kind(), io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut | io::ErrorKind::Interrupted) +} + +impl Read for Patient<'_> { + fn read(&mut self, buffer: &mut [u8]) -> io::Result { + loop { + self.wait.remaining().map_err(FetchError::into_io)?; + match self.socket.read(buffer) { + Err(error) if timed_out(&error) => {} + other => return other, + } + } + } +} + +impl Write for Patient<'_> { + fn write(&mut self, buffer: &[u8]) -> io::Result { + loop { + self.wait.remaining().map_err(FetchError::into_io)?; + match self.socket.write(buffer) { + Err(error) if timed_out(&error) => {} + other => return other, + } + } + } + + fn flush(&mut self) -> io::Result<()> { + self.socket.flush() + } +} + +impl Channel { + fn handshake(&mut self) -> io::Result<()> { + let Some(connection) = self.tls.as_mut() else { return Ok(()) }; + let mut patient = Patient { socket: &self.socket, wait: &self.wait }; + while connection.is_handshaking() { + connection.complete_io(&mut patient)?; + } + Ok(()) + } + + fn write_all(&mut self, data: &[u8]) -> io::Result<()> { + let mut patient = Patient { socket: &self.socket, wait: &self.wait }; + match self.tls.as_mut() { + None => patient.write_all(data), + Some(connection) => { + // Encrypt everything first, then send: a write that times out halfway is not repeated. + connection.set_buffer_limit(None); + connection.writer().write_all(data)?; + while connection.wants_write() { + connection.write_tls(&mut patient)?; + } + Ok(()) + } + } + } + + /// Bytes of the response, or 0 at the end of the connection. + fn read(&mut self, buffer: &mut [u8]) -> io::Result { + let mut patient = Patient { socket: &self.socket, wait: &self.wait }; + let Some(connection) = self.tls.as_mut() else { return patient.read(buffer) }; + loop { + match connection.reader().read(buffer) { + Ok(count) => return Ok(count), + Err(error) if error.kind() == io::ErrorKind::WouldBlock => {} + // The peer closed the connection without saying so. Like the Windows transport this + // counts as the end of the data; a body with a declared length that is cut short is + // still an error, because its length is checked. + Err(error) if error.kind() == io::ErrorKind::UnexpectedEof => return Ok(0), + Err(error) => return Err(error), + } + connection.read_tls(&mut patient)?; + connection.process_new_packets().map_err(io::Error::other)?; + while connection.wants_write() { + connection.write_tls(&mut patient)?; + } + } + } +} + +/// The channel with the bytes that were read past the response head. +struct Source { + channel: Channel, + buffer: Vec, + position: usize, +} + +impl Source { + /// Reads more into the buffer; 0 at the end of the connection. + fn fill(&mut self) -> io::Result { + if self.position > 0 && self.position == self.buffer.len() { + self.buffer.clear(); + self.position = 0; + } + let start = self.buffer.len(); + self.buffer.resize(start + 8 * 1024, 0); + let result = self.channel.read(&mut self.buffer[start..]); + self.buffer.truncate(start + *result.as_ref().unwrap_or(&0)); + result + } + + fn read(&mut self, target: &mut [u8]) -> io::Result { + if self.position == self.buffer.len() && self.fill()? == 0 { + return Ok(0); + } + let count = target.len().min(self.buffer.len() - self.position); + target[..count].copy_from_slice(&self.buffer[self.position..self.position + count]); + self.position += count; + Ok(count) + } + + /// One line without its line break, at most `MAXIMUM_CHUNK_LINE_BYTES` long. + fn line(&mut self) -> io::Result { + let mut line = Vec::new(); + loop { + let mut byte = [0u8; 1]; + if self.read(&mut byte)? == 0 { + return Err(ended_early()); + } + if byte[0] == b'\n' { + if line.last() == Some(&b'\r') { + line.pop(); + } + return String::from_utf8(line).map_err(|_| invalid_response().into_io()); + } + line.push(byte[0]); + if line.len() > MAXIMUM_CHUNK_LINE_BYTES { + return Err(invalid_response().into_io()); + } + } + } +} + +fn ended_early() -> io::Error { + FetchError::Transport("The response ended early".into()).into_io() +} + +enum Framing { + Done, + Length(u64), + ChunkSize, + ChunkData(u64), + UntilClose, +} + +/// The body as the server delimited it, before any decoding. +struct RawBody { + source: Source, + framing: Framing, +} + +impl Read for RawBody { + fn read(&mut self, target: &mut [u8]) -> io::Result { + if target.is_empty() { + return Ok(0); + } + loop { + match self.framing { + Framing::Done => return Ok(0), + Framing::UntilClose => return self.source.read(target), + Framing::Length(left) | Framing::ChunkData(left) => { + let limit = target.len().min(usize::try_from(left).unwrap_or(usize::MAX)); + let count = self.source.read(&mut target[..limit])?; + if count == 0 { + return Err(ended_early()); + } + let left = left - count as u64; + self.framing = match self.framing { + Framing::Length(_) if left == 0 => Framing::Done, + Framing::Length(_) => Framing::Length(left), + _ if left == 0 => { + if !self.source.line()?.is_empty() { + return Err(invalid_response().into_io()); + } + Framing::ChunkSize + } + _ => Framing::ChunkData(left), + }; + return Ok(count); + } + Framing::ChunkSize => { + let line = self.source.line()?; + let digits = line.split(';').next().unwrap_or("").trim(); + if digits.is_empty() || digits.len() > 16 || !digits.bytes().all(|byte| byte.is_ascii_hexdigit()) { + return Err(invalid_response().into_io()); + } + let size = u64::from_str_radix(digits, 16).map_err(|_| invalid_response().into_io())?; + if size == 0 { + // Trailers are read and dropped. + while !self.source.line()?.is_empty() {} + self.framing = Framing::Done; + } else { + self.framing = Framing::ChunkData(size); + } + } + } + } + } +} diff --git a/Linux/crates/sw-core/src/api/mod.rs b/Linux/crates/sw-core/src/api/mod.rs new file mode 100644 index 00000000..bd8a95e3 --- /dev/null +++ b/Linux/crates/sw-core/src/api/mod.rs @@ -0,0 +1,19 @@ +//! Native APIs behind the script globals, and the policy they share. +//! +//! Globals that no module here implements (`$calendar`, `$health`, `$music` +//! and the like) are answered by the shared kernel: `isAvailable()` returns +//! false and calls reject with `[unsupported_platform]`. A module names the +//! globals it does implement in [`crate::NativeModule::apis`]. + +pub mod fetch; +pub mod http; +pub mod policy; +pub mod storage; +pub mod support; +pub mod system; +pub mod weather; +pub mod workers; + +pub use fetch::FetchModule; +pub use storage::StorageModule; +pub use weather::WeatherModule; diff --git a/Linux/crates/sw-core/src/api/policy.rs b/Linux/crates/sw-core/src/api/policy.rs new file mode 100644 index 00000000..6afd42d7 --- /dev/null +++ b/Linux/crates/sw-core/src/api/policy.rs @@ -0,0 +1,169 @@ +//! Manifest and presentation rules that every native API checks before doing +//! work. JavaScript-side checks exist only for early, readable errors. +//! Ported from `Windows/src/ScriptWidget.Core/Api/RuntimePolicy.cs`. + +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; + +use crate::executor::ExecutionRequest; +use crate::json::{ObjectExt, as_text}; + +pub fn has_permission(request: &ExecutionRequest<'_>, permission: &str) -> bool { + request + .prepared + .manifest + .as_ref() + .and_then(|manifest| manifest.opt_array("permissions")) + .is_some_and(|permissions| permissions.iter().any(|item| as_text(item).as_deref() == Some(permission))) +} + +/// Future timeline entries are rendered ahead of time and must not read live data or write. +pub fn is_timeline_projection(request: &ExecutionRequest<'_>) -> bool { + const NAME: &str = "timeline-is-projection"; + let value = request.environment(NAME).or_else(|| request.prepared.timeline_environment.iter().rev().find(|(name, _)| name == NAME).map(|(_, value)| value.as_str())); + value == Some("true") +} + +pub fn is_live_activity(request: &ExecutionRequest<'_>) -> bool { + matches!(request.environment("widget-size"), Some("live-activity" | "dynamic-island")) +} + +/// True while a declared action runs; writes to system data require it. +pub fn is_action(request: &ExecutionRequest<'_>) -> bool { + request.prepared.action_id.is_some() +} + +/// Same textual rule as ScriptWidgetFetchPolicy.validationError. +pub fn blocked_host_name(host: &str) -> bool { + let value = host.to_lowercase(); + let parts: Vec<&str> = value.split('.').collect(); + value == "localhost" + || value == "::1" + || value.ends_with(".local") + || value.starts_with("127.") + || value.starts_with("10.") + || value.starts_with("192.168.") + || (parts.len() == 4 && parts[0] == "172" && parts[1].parse::().is_ok_and(|second| (16..=31).contains(&second))) +} + +/// Rejects addresses that are not on the public Internet, whatever name resolved to them. +pub fn blocked_address(address: IpAddr) -> bool { + match address { + IpAddr::V4(address) => blocked_v4(address), + IpAddr::V6(address) => match address.to_ipv4_mapped() { + Some(mapped) => blocked_v4(mapped), + None => blocked_v6(address), + }, + } +} + +fn blocked_v4(address: Ipv4Addr) -> bool { + let [first, second, ..] = address.octets(); + first == 0 // this network + || first == 10 // private + || first == 127 // loopback + || (first == 100 && (64..=127).contains(&second)) // carrier-grade NAT + || (first == 169 && second == 254) // link-local + || (first == 172 && (16..=31).contains(&second)) // private + || (first == 192 && second == 168) // private + || first >= 224 // multicast, reserved and broadcast +} + +fn blocked_v6(address: Ipv6Addr) -> bool { + let bytes = address.octets(); + let segments = address.segments(); + if address.is_loopback() || address.is_unspecified() || address.is_multicast() { + return true; + } + if segments[0] & 0xffc0 == 0xfe80 || segments[0] & 0xffc0 == 0xfec0 { + return true; // link-local fe80::/10 and the retired site-local fec0::/10 + } + if bytes[0] & 0xfe == 0xfc { + return true; // unique local fc00::/7 + } + // NAT64 and 6to4 embed an IPv4 address that the gateway would reach. + if bytes[..4] == [0x00, 0x64, 0xff, 0x9b] { + return blocked_v4(Ipv4Addr::new(bytes[12], bytes[13], bytes[14], bytes[15])); + } + if bytes[..2] == [0x20, 0x02] { + return blocked_v4(Ipv4Addr::new(bytes[2], bytes[3], bytes[4], bytes[5])); + } + false +} + +pub fn host_matches<'a>(host: &str, declarations: impl IntoIterator) -> bool { + let value = host.to_lowercase(); + declarations.into_iter().any(|declaration| { + let domain = declaration.to_lowercase(); + match domain.strip_prefix("*.") { + None => value == domain, + Some(suffix) => value != suffix && value.ends_with(&format!(".{suffix}")), + } + }) +} + +/// Package 2.0: `network` permission and a matching `networkDomains` entry. +/// Legacy packages without a manifest are allowed. +pub fn package_allows_host(request: &ExecutionRequest<'_>, host: &str) -> bool { + NetworkDeclaration::of(request).allows_host(host) +} + +/// What a package declared about the network, detached from the request so +/// that worker threads can apply the same rule to redirect targets. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct NetworkDeclaration { + has_manifest: bool, + /// `None` when the manifest is missing, lacks the `network` permission or has no `networkDomains`. + domains: Option>, +} + +impl NetworkDeclaration { + pub fn of(request: &ExecutionRequest<'_>) -> Self { + let domains = request + .prepared + .manifest + .as_ref() + .filter(|_| has_permission(request, "network")) + .and_then(|manifest| manifest.opt_array("networkDomains")) + .map(|domains| domains.iter().map(|item| as_text(item).unwrap_or_default()).collect()); + Self { has_manifest: request.prepared.has_manifest, domains } + } + + pub fn allows_host(&self, host: &str) -> bool { + if !self.has_manifest { + return true; + } + self.domains.as_ref().is_some_and(|domains| host_matches(host, domains.iter().map(String::as_str))) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn private_and_local_addresses_are_blocked() { + for text in [ + "127.0.0.1", "10.1.2.3", "172.16.0.1", "172.31.255.255", "192.168.1.1", "169.254.169.254", "100.64.0.1", "0.0.0.0", "224.0.0.1", "255.255.255.255", + "::1", "::", "fe80::1", "fec0::1", "fc00::1", "fd12:3456::1", "ff02::1", "::ffff:127.0.0.1", "::ffff:192.168.0.1", "64:ff9b::a00:1", "2002:c0a8:101::1", + ] { + assert!(blocked_address(text.parse().unwrap()), "{text}"); + } + for text in ["8.8.8.8", "172.32.0.1", "100.128.0.1", "2606:4700:4700::1111", "::ffff:8.8.8.8", "64:ff9b::808:808", "2002:808:808::1"] { + assert!(!blocked_address(text.parse().unwrap()), "{text}"); + } + } + + #[test] + fn host_names_are_matched_textually() { + for host in ["localhost", "LOCALHOST", "printer.local", "127.0.0.1", "10.0.0.8", "192.168.0.1", "172.20.1.1", "::1"] { + assert!(blocked_host_name(host), "{host}"); + } + for host in ["example.com", "172.32.1.1", "local.example.com"] { + assert!(!blocked_host_name(host), "{host}"); + } + assert!(host_matches("API.example.com", ["api.example.com"])); + assert!(host_matches("a.b.example.com", ["*.example.com"])); + assert!(!host_matches("example.com", ["*.example.com"])); + assert!(!host_matches("badexample.com", ["*.example.com", "example.org"])); + } +} diff --git a/Linux/crates/sw-core/src/api/storage.rs b/Linux/crates/sw-core/src/api/storage.rs new file mode 100644 index 00000000..33d8c762 --- /dev/null +++ b/Linux/crates/sw-core/src/api/storage.rs @@ -0,0 +1,210 @@ +//! Package-scoped key-value storage behind `$storage`. Ported from +//! `Windows/src/ScriptWidget.Core/Api/StorageModule.cs`. +//! +//! Each package has its own file, named by a hash of the package identity; a +//! package can never name or reach another package's keys. Values are bounded +//! and writes are atomic. Compare-and-set runs under the same lock as plain +//! writes, so app and widget updates do not lose records. Files are readable +//! by the user only. + +use std::collections::HashMap; +use std::io::Write; +use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt}; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex, OnceLock}; + +use serde_json::{Value, json}; +use sha2::{Digest, Sha256}; +use sw_quickjs::HostResult; + +use crate::api::policy; +use crate::executor::{ExecutionRequest, NativeModule}; +use crate::json::{Object, ObjectExt, parse_object, stringify, stringify_object}; + +pub const MAXIMUM_VALUE_BYTES: usize = 256 * 1024; +pub const MAXIMUM_KEYS: usize = 1024; +pub const MAXIMUM_TOTAL_BYTES: usize = 8 * 1024 * 1024; +const MAXIMUM_KEY_BYTES: usize = 256; + +pub struct StorageModule { + directory: PathBuf, +} + +/// One lock per storage file, shared by every module instance of the process. +fn lock_for(file: &Path) -> Arc> { + static LOCKS: OnceLock>>>> = OnceLock::new(); + let mut locks = LOCKS.get_or_init(Default::default).lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + locks.entry(file.to_owned()).or_default().clone() +} + +impl StorageModule { + pub fn new(directory: impl Into) -> Self { + Self { directory: directory.into() } + } + + /// Reads a string value for native features such as toggle state; empty when missing. + pub fn get_string(&self, package_id: &str, key: &str) -> String { + self.locked(package_id, |file| match read(file).opt_object(key) { + Some(entry) if entry.opt_string("t") == "s" => entry.opt_string("v"), + _ => String::new(), + }) + } + + /// Writes a string value on behalf of a native feature. Returns false when a limit rejects it. + pub fn set_string(&self, package_id: &str, key: &str, value: &str) -> bool { + if !valid_key(key) || !valid_value("s", value) { + return false; + } + self.locked(package_id, |file| { + let mut store = read(file); + if !store.has(key) && store.len() >= MAXIMUM_KEYS { + return false; + } + store.insert(key.to_owned(), json!({ "t": "s", "v": value })); + self.write(file, &store) + }) + } + + /// All stored entries of a package, for the records screen and tests. + pub fn snapshot(&self, package_id: &str) -> Object { + self.locked(package_id, read) + } + + pub fn clear(&self, package_id: &str) { + self.locked(package_id, |file| { + let _ = std::fs::remove_file(file); + }) + } + + fn file_for(&self, package_id: &str) -> PathBuf { + let name: String = Sha256::digest(package_id.as_bytes()).iter().map(|byte| format!("{byte:02x}")).collect(); + self.directory.join(format!("{name}.json")) + } + + fn locked(&self, package_id: &str, work: impl FnOnce(&Path) -> T) -> T { + let file = self.file_for(package_id); + let lock = lock_for(&file); + let _guard = lock.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + work(&file) + } + + fn write(&self, file: &Path, store: &Object) -> bool { + let text = stringify_object(store); + if text.len() > MAXIMUM_TOTAL_BYTES { + return false; + } + let temporary = file.with_extension("json.tmp"); + let written = (|| -> std::io::Result<()> { + std::fs::DirBuilder::new().recursive(true).mode(0o700).create(&self.directory)?; + // The temporary name is fixed and guarded by the file's lock; a stale one is replaced. + let _ = std::fs::remove_file(&temporary); + let mut output = std::fs::OpenOptions::new().write(true).create_new(true).mode(0o600).open(&temporary)?; + output.write_all(text.as_bytes())?; + output.sync_all()?; + std::fs::rename(&temporary, file) + })(); + if written.is_err() { + let _ = std::fs::remove_file(&temporary); + } + written.is_ok() + } +} + +impl NativeModule for StorageModule { + fn handles(&self, operation: &str) -> bool { + operation.starts_with("storage.") + } + + fn call(&self, request: &ExecutionRequest<'_>, operation: &str, payload: Option<&str>) -> HostResult { + let arguments = match payload { + Some(payload) => parse_object(payload)?, + None => Object::new(), + }; + let key = arguments.opt_string("key"); + let writes = matches!(operation, "storage.set" | "storage.cas" | "storage.remove" | "storage.clear"); + if writes && policy::is_timeline_projection(request) { + return Ok(Some("false".into())); + } + let flag = |value: bool| Ok(Some(if value { "true" } else { "false" }.to_owned())); + self.locked(&request.package_id, |file| match operation { + "storage.get" => Ok(Some(match read(file).opt_object(&key) { + Some(entry) if valid_key(&key) => stringify_object(entry), + _ => "null".into(), + })), + "storage.keys" => { + let mut keys: Vec = read(file).keys().cloned().collect(); + keys.sort_unstable(); + Ok(Some(stringify(&json!(keys)))) + } + "storage.set" => { + let value = arguments.opt_string("v"); + let kind = arguments.opt_string("t"); + if !valid_key(&key) || !valid_value(&kind, &value) { + return flag(false); + } + let mut store = read(file); + if !store.has(&key) && store.len() >= MAXIMUM_KEYS { + return flag(false); + } + store.insert(key.clone(), json!({ "t": kind, "v": value })); + flag(self.write(file, &store)) + } + "storage.cas" => { + let value = arguments.opt_string("v"); + let kind = arguments.opt_string("t"); + let expected = arguments.opt_string("expected"); + if !valid_key(&key) || !valid_value(&kind, &value) || expected.len() > MAXIMUM_VALUE_BYTES { + return flag(false); + } + let mut store = read(file); + // A missing key matches an empty string or an empty object. + let matches = match store.opt_object(&key) { + None => expected == if kind == "s" { "" } else { "{}" }, + Some(current) => current.opt_string("t") == kind && current.opt_string("v") == expected, + }; + if !matches || (!store.has(&key) && store.len() >= MAXIMUM_KEYS) { + return flag(false); + } + store.insert(key.clone(), json!({ "t": kind, "v": value })); + flag(self.write(file, &store)) + } + "storage.remove" => { + if !valid_key(&key) { + return flag(false); + } + let mut store = read(file); + store.shift_remove(&key); + flag(self.write(file, &store)) + } + "storage.clear" => flag(match std::fs::remove_file(file) { + Ok(()) => true, + Err(error) => error.kind() == std::io::ErrorKind::NotFound, + }), + _ => Err(format!("Unsupported operation {operation}")), + }) + } +} + +fn valid_key(key: &str) -> bool { + !key.is_empty() && key.len() <= MAXIMUM_KEY_BYTES +} + +fn valid_value(kind: &str, value: &str) -> bool { + if value.len() > MAXIMUM_VALUE_BYTES { + return false; + } + match kind { + "s" => true, + "j" => matches!(serde_json::from_str::(value), Ok(Value::Object(_))), + _ => false, + } +} + +/// The stored entries; a missing, oversized or damaged file reads as empty. +fn read(file: &Path) -> Object { + match std::fs::metadata(file) { + Ok(metadata) if metadata.is_file() && metadata.len() <= MAXIMUM_TOTAL_BYTES as u64 => {} + _ => return Object::new(), + } + std::fs::read_to_string(file).ok().and_then(|text| parse_object(&text).ok()).unwrap_or_default() +} diff --git a/Linux/crates/sw-core/src/api/support.rs b/Linux/crates/sw-core/src/api/support.rs new file mode 100644 index 00000000..f77521f6 --- /dev/null +++ b/Linux/crates/sw-core/src/api/support.rs @@ -0,0 +1,663 @@ +//! The shared pieces of the Runtime 1.6 system services that `$weather` +//! needs. Ported from `Windows/src/ScriptWidget.Core/Api/SystemSupport.cs`; +//! the other services (grants, mutation receipts, permission prompts) are +//! added with the APIs that use them. + +use std::collections::{HashMap, HashSet}; +use std::fmt; +use std::io::{Read, Write}; +use std::os::unix::fs::{DirBuilderExt, MetadataExt, OpenOptionsExt}; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex, OnceLock, RwLock}; + +use serde_json::Value; +use sha2::{Digest, Sha256}; + +use crate::api::policy; +use crate::api::workers::BoundedExecutor; +use crate::executor::{Completion, ExecutionRequest}; +use crate::json::{Object, ObjectExt, as_text, quote, stringify_object}; + +/// A rejection whose message is delivered to the script verbatim. It never contains user data. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SystemReject(pub String); + +impl fmt::Display for SystemReject { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(&self.0) + } +} + +impl std::error::Error for SystemReject {} + +/// Error codes shared by the Runtime 1.6 system services. The message is the +/// same sentence the Apple runtime uses, so scripts that match on the +/// bracketed code behave the same on Linux. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum SystemServiceError { + InvalidOptions, + PermissionDenied, + Unavailable, + QueryLimit, + ReadOnly, + ScopeChanged, + LimitExceeded, + OutcomeUnknown, + RequestConflict, +} + +/// Message for a failure that is not one of the known codes. +pub const SYSTEM_FAILED: &str = "[system_failed] System operation failed; review the result before retrying a write."; + +impl SystemServiceError { + pub fn code(self) -> &'static str { + match self { + Self::InvalidOptions => "invalid_options", + Self::PermissionDenied => "permission_denied", + Self::Unavailable => "unavailable", + Self::QueryLimit => "query_limit", + Self::ReadOnly => "read_only", + Self::ScopeChanged => "scope_changed", + Self::LimitExceeded => "limit_exceeded", + Self::OutcomeUnknown => "outcome_unknown", + Self::RequestConflict => "request_conflict", + } + } + + pub fn message(self) -> String { + format!("[{}] System operation could not complete. Existing user data is preserved.", self.code()) + } + + pub fn reject(self) -> SystemReject { + SystemReject(self.message()) + } +} + +type Sink = Arc; + +static LOG_SINK: RwLock> = RwLock::new(None); + +/// One line per native call. Callers pass operation names and error codes only, never user data. +pub mod system_log { + use super::{LOG_SINK, Sink}; + + /// Sets what receives each line: the host's log, or a test. Nothing is + /// written by default. Returns the previous sink. + pub fn set_sink(sink: Option) -> Option { + std::mem::replace(&mut *LOG_SINK.write().unwrap_or_else(|poisoned| poisoned.into_inner()), sink) + } + + pub fn call(operation: &str, outcome: &str) { + let sink = LOG_SINK.read().unwrap_or_else(|poisoned| poisoned.into_inner()).clone(); + if let Some(sink) = sink { + sink(&format!("{operation} -> {outcome}")); + } + } + + /// The bracketed code of a rejection message, or `error` when it has none. The free text is never logged. + pub fn code(message: &str) -> &str { + let Some(rest) = message.strip_prefix('[') else { return "error" }; + let Some(end) = rest.find(']') else { return "error" }; + let code = &rest[..end]; + if (1..=40).contains(&code.len()) && code.bytes().all(|byte| byte.is_ascii_alphabetic() || byte == b'_' || byte == b':') { code } else { "error" } + } +} + +/// Background workers for system queries. +fn system_pool() -> &'static BoundedExecutor { + static POOL: OnceLock = OnceLock::new(); + POOL.get_or_init(|| BoundedExecutor::new(4)) +} + +/// Runs `block` off the JavaScript thread, logs one line, and completes +/// `done` exactly once with JSON or a rejection message. +pub fn run_system(operation: &str, done: Completion, block: impl FnOnce() -> Result + Send + 'static) { + let operation = operation.to_owned(); + system_pool().execute(move || match block() { + Ok(json) => { + system_log::call(&operation, "ok"); + done.complete(true, Some(json)); + } + Err(SystemReject(message)) => { + let message = if message.is_empty() { SYSTEM_FAILED.to_owned() } else { message }; + system_log::call(&operation, system_log::code(&message)); + done.complete(false, Some(quote(&message))); + } + }); +} + +type Probe = Box bool + Send + Sync>; + +/// The facts about one execution that policy decisions need. Built from the +/// [`ExecutionRequest`] in production and directly in tests, so every rule +/// can be exercised without the JavaScript engine. +pub struct CallContext { + /// Installation identity; namespaces grants, receipts and caches. + pub installation: String, + pub has_manifest: bool, + pub permissions: HashSet, + pub network_domains: Vec, + pub minimum_runtime_version: Option, + pub is_action: bool, + pub is_timeline_projection: bool, + pub is_live_activity: bool, + data_query: Option, + network_request: Option, + active: Option, +} + +impl CallContext { + pub const MAXIMUM_DATA_QUERIES: u32 = 8; + pub const MAXIMUM_NETWORK_REQUESTS: u32 = 32; + + /// A context without budgets: every query is granted and the execution counts as running. + pub fn new(installation: &str, has_manifest: bool, permissions: &[&str], network_domains: &[&str], minimum_runtime_version: Option<&str>) -> Self { + Self { + installation: installation.to_owned(), + has_manifest, + permissions: permissions.iter().map(|item| item.to_string()).collect(), + network_domains: network_domains.iter().map(|item| item.to_string()).collect(), + minimum_runtime_version: minimum_runtime_version.map(str::to_owned), + is_action: false, + is_timeline_projection: false, + is_live_activity: false, + data_query: None, + network_request: None, + active: None, + } + } + + /// Replaces the budgets and the liveness check. + pub fn with_probes( + mut self, + data_query: impl Fn() -> bool + Send + Sync + 'static, + network_request: impl Fn() -> bool + Send + Sync + 'static, + active: impl Fn() -> bool + Send + Sync + 'static, + ) -> Self { + self.data_query = Some(Box::new(data_query)); + self.network_request = Some(Box::new(network_request)); + self.active = Some(Box::new(active)); + self + } + + /// An installed owner is a canonical lowercase UUID; previews and unmanaged packages are not owners. + pub fn is_installed(&self) -> bool { + is_owner(&self.installation) + } + + /// True while the execution is still running; results for a finished execution are dropped. + pub fn is_active(&self) -> bool { + self.active.as_ref().is_none_or(|probe| probe()) + } + + /// Takes one of the eight system-data queries of this execution. False when the budget is spent. + pub fn consume_data_query(&self) -> bool { + self.data_query.as_ref().is_none_or(|probe| probe()) + } + + /// Takes one of the 32 network requests of this execution. + pub fn consume_network_request(&self) -> bool { + self.network_request.as_ref().is_none_or(|probe| probe()) + } + + /// Package 2.0 permission check: a missing manifest never declares anything. + pub fn declares(&self, permission: &str) -> bool { + self.has_manifest && self.permissions.contains(permission) + } + + /// `network` plus a matching `networkDomains` entry, the rule `fetch` applies. + pub fn allows_host(&self, host: &str) -> bool { + self.declares("network") && policy::host_matches(host, self.network_domains.iter().map(String::as_str)) + } + + pub fn from_request(request: &ExecutionRequest<'_>) -> Self { + let manifest = request.prepared.manifest.as_ref(); + let strings = |key: &str| -> Vec { + manifest.and_then(|manifest| manifest.opt_array(key)).map(|items| items.iter().map(|item| as_text(item).unwrap_or_default()).collect()).unwrap_or_default() + }; + let runtime = manifest.map(|manifest| manifest.opt_string("minimumRuntimeVersion")).unwrap_or_default(); + let (queries, requests, liveness) = (request.session.clone(), request.session.clone(), request.session.clone()); + Self { + installation: request.package_id.clone(), + // A present manifest that could not be read still counts as present: it declares nothing and is never treated as legacy. + has_manifest: request.prepared.has_manifest, + permissions: strings("permissions").into_iter().collect(), + network_domains: strings("networkDomains"), + minimum_runtime_version: if runtime.is_empty() { None } else { Some(runtime) }, + is_action: policy::is_action(request), + is_timeline_projection: policy::is_timeline_projection(request), + is_live_activity: policy::is_live_activity(request), + data_query: Some(Box::new(move || queries.add_data_query() <= Self::MAXIMUM_DATA_QUERIES)), + network_request: Some(Box::new(move || requests.add_network_request() <= Self::MAXIMUM_NETWORK_REQUESTS)), + active: Some(Box::new(move || !liveness.finished())), + } + } +} + +/// `8-4-4-4-12` lower-case hexadecimal digits. +pub fn is_owner(value: &str) -> bool { + let bytes = value.as_bytes(); + bytes.len() == 36 + && bytes.iter().enumerate().all(|(index, byte)| match index { + 8 | 13 | 18 | 23 => *byte == b'-', + _ => byte.is_ascii_digit() || (b'a'..=b'f').contains(byte), + }) +} + +/// ISO-8601 instants in the forms the Apple runtime accepts and produces. +pub mod iso_dates { + /// Epoch milliseconds of a full date-time with a time zone, or `None`. + /// Dates without a zone or with impossible fields are rejected. + pub fn parse(text: &str) -> Option { + let bytes = text.as_bytes(); + if bytes.len() > 64 { + return None; + } + let mut at = 0usize; + let digits = |count: usize, at: &mut usize| -> Option { + let slice = bytes.get(*at..*at + count)?; + if !slice.iter().all(u8::is_ascii_digit) { + return None; + } + *at += count; + Some(slice.iter().fold(0i64, |value, digit| value * 10 + i64::from(digit - b'0'))) + }; + let literal = |expected: u8, at: &mut usize| -> Option<()> { + if bytes.get(*at) == Some(&expected) { + *at += 1; + Some(()) + } else { + None + } + }; + let year = digits(4, &mut at)?; + literal(b'-', &mut at)?; + let month = digits(2, &mut at)?; + literal(b'-', &mut at)?; + let day = digits(2, &mut at)?; + literal(b'T', &mut at)?; + let hour = digits(2, &mut at)?; + literal(b':', &mut at)?; + let minute = digits(2, &mut at)?; + literal(b':', &mut at)?; + let second = digits(2, &mut at)?; + let mut fraction = 0i64; + if bytes.get(at) == Some(&b'.') { + at += 1; + let start = at; + while at < bytes.len() && bytes[at].is_ascii_digit() { + at += 1; + } + if at == start || at - start > 9 { + return None; + } + // Milliseconds: the first three digits, padded on the right. + for index in 0..3 { + fraction = fraction * 10 + bytes.get(start + index).filter(|_| start + index < at).map_or(0, |digit| i64::from(digit - b'0')); + } + } + let mut offset_minutes = 0i64; + match bytes.get(at) { + Some(b'Z') => at += 1, + Some(sign @ (b'+' | b'-')) => { + at += 1; + let hours = digits(2, &mut at)?; + literal(b':', &mut at)?; + let rest = digits(2, &mut at)?; + if hours > 18 || rest > 59 || (hours == 18 && rest != 0) { + return None; + } + offset_minutes = (if *sign == b'-' { -1 } else { 1 }) * (hours * 60 + rest); + } + _ => return None, + } + if at != bytes.len() { + return None; + } + if year < 1 || !(1..=12).contains(&month) || day < 1 || day > days_in_month(year, month) || hour > 23 || minute > 59 || second > 59 { + return None; + } + let seconds = days_from_civil(year, month, day) * 86_400 + hour * 3600 + minute * 60 + second; + Some(seconds * 1000 - offset_minutes * 60_000 + fraction) + } + + /// `2026-11-01T12:00:00Z`: whole seconds, UTC. + pub fn format(milliseconds: i64) -> String { + let seconds = if milliseconds >= 0 { milliseconds / 1000 } else { -((-milliseconds + 999) / 1000) }; + let days = seconds.div_euclid(86_400); + let time = seconds.rem_euclid(86_400); + let (year, month, day) = civil_from_days(days); + format!("{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}Z", time / 3600, time / 60 % 60, time % 60) + } + + fn days_in_month(year: i64, month: i64) -> i64 { + match month { + 2 if (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 => 29, + 2 => 28, + 4 | 6 | 9 | 11 => 30, + _ => 31, + } + } + + /// Days since 1970-01-01 in the proleptic Gregorian calendar. + fn days_from_civil(year: i64, month: i64, day: i64) -> i64 { + let year = if month <= 2 { year - 1 } else { year }; + let era = year.div_euclid(400); + let year_of_era = year.rem_euclid(400); + let day_of_year = (153 * (if month > 2 { month - 3 } else { month + 9 }) + 2) / 5 + day - 1; + let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year; + era * 146_097 + day_of_era - 719_468 + } + + fn civil_from_days(days: i64) -> (i64, i64, i64) { + let shifted = days + 719_468; + let era = shifted.div_euclid(146_097); + let day_of_era = shifted.rem_euclid(146_097); + let year_of_era = (day_of_era - day_of_era / 1460 + day_of_era / 36_524 - day_of_era / 146_096) / 365; + let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100); + let month_index = (5 * day_of_year + 2) / 153; + let day = day_of_year - (153 * month_index + 2) / 5 + 1; + let month = if month_index < 10 { month_index + 3 } else { month_index - 9 }; + (year_of_era + era * 400 + i64::from(month <= 2), month, day) + } +} + +/// Strict, bounded option parsing shared by the system services. Every failure is `invalid_options`. +pub mod system_options { + use super::*; + + pub const MAXIMUM_OPTION_BYTES: usize = 16384; + + fn invalid() -> SystemReject { + SystemServiceError::InvalidOptions.reject() + } + + /// Rejects unknown fields and option objects above 16 KiB. + pub fn fields(options: &Object, allowed: &[&str]) -> Result<(), SystemReject> { + if options.keys().any(|key| !allowed.contains(&key.as_str())) || stringify_object(options).len() > MAXIMUM_OPTION_BYTES { + return Err(invalid()); + } + Ok(()) + } + + pub fn has_control_characters(text: &str) -> bool { + text.chars().any(is_control_or_format) + } + + /// A finite JSON number inside the range; Booleans and strings are not numbers. + pub fn number(value: Option<&Value>, minimum: f64, maximum: f64) -> Result { + match json_number(value) { + Some(number) if number.is_finite() && number >= minimum && number <= maximum => Ok(number), + _ => Err(invalid()), + } + } + + /// The value of a JSON number; `None` for everything else. + pub fn json_number(value: Option<&Value>) -> Option { + match value { + Some(Value::Number(number)) => number.as_f64(), + _ => None, + } + } + + pub fn integer(value: Option<&Value>, minimum: i32, maximum: i32) -> Result { + let number = number(value, f64::from(minimum), f64::from(maximum))?; + if number.round_ties_even() != number { + return Err(invalid()); + } + Ok(number as i32) + } +} + +/// True for the Unicode categories Cc (control) and Cf (format). +pub fn is_control_or_format(character: char) -> bool { + if character.is_control() { + return true; + } + // General category Cf, Unicode 16. + const FORMAT: &[(u32, u32)] = &[ + (0x00AD, 0x00AD), + (0x0600, 0x0605), + (0x061C, 0x061C), + (0x06DD, 0x06DD), + (0x070F, 0x070F), + (0x0890, 0x0891), + (0x08E2, 0x08E2), + (0x180E, 0x180E), + (0x200B, 0x200F), + (0x202A, 0x202E), + (0x2060, 0x2064), + (0x2066, 0x206F), + (0xFEFF, 0xFEFF), + (0xFFF9, 0xFFFB), + (0x110BD, 0x110BD), + (0x110CD, 0x110CD), + (0x13430, 0x1343F), + (0x1BCA0, 0x1BCA3), + (0x1D173, 0x1D17A), + (0xE0001, 0xE0001), + (0xE0020, 0xE007F), + ]; + let code = character as u32; + FORMAT.iter().any(|(first, last)| (*first..=*last).contains(&code)) +} + +/// Digests for cache identities. +pub mod canonical { + use super::*; + + pub fn sha256(text: &str) -> String { + Sha256::digest(text.as_bytes()).iter().map(|byte| format!("{byte:02x}")).collect() + } + + /// File names for per-installation records: a digest, so an identity can never become a path. + pub fn owner_file_name(installation: &str, suffix: &str) -> String { + format!("{}{suffix}", &sha256(installation)[..40]) + } +} + +/// A present private file that cannot be read; callers fail closed instead of treating it as missing. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Unreadable; + +/// A small private JSON file. Reads are bounded and refuse links and +/// non-regular files; writes replace the file atomically and leave it +/// readable by the user only. +pub struct PrivateFile { + path: PathBuf, + maximum_bytes: usize, +} + +impl PrivateFile { + pub fn new(path: impl Into, maximum_bytes: usize) -> Self { + Self { path: path.into(), maximum_bytes } + } + + pub fn path(&self) -> &Path { + &self.path + } + + /// Lock shared by every reader and writer of this path in the process. + pub fn lock(&self) -> Arc> { + static LOCKS: OnceLock>>>> = OnceLock::new(); + let key = std::path::absolute(&self.path).unwrap_or_else(|_| self.path.clone()); + let mut locks = LOCKS.get_or_init(Default::default).lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + locks.entry(key).or_default().clone() + } + + pub fn exists(&self) -> bool { + std::fs::symlink_metadata(&self.path).is_ok() + } + + /// File text, or `None` when the file does not exist. + pub fn read(&self) -> Result, Unreadable> { + let before = match std::fs::symlink_metadata(&self.path) { + Ok(metadata) => metadata, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(_) => return Err(Unreadable), + }; + if !before.file_type().is_file() { + return Err(Unreadable); + } + let file = std::fs::OpenOptions::new().read(true).custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK).open(&self.path).map_err(|_| Unreadable)?; + let opened = file.metadata().map_err(|_| Unreadable)?; + // The opened file must be the regular file that was looked at. + if !opened.is_file() || opened.dev() != before.dev() || opened.ino() != before.ino() || opened.len() > self.maximum_bytes as u64 { + return Err(Unreadable); + } + let mut bytes = Vec::new(); + file.take(self.maximum_bytes as u64 + 1).read_to_end(&mut bytes).map_err(|_| Unreadable)?; + if bytes.len() > self.maximum_bytes { + return Err(Unreadable); + } + Ok(Some(String::from_utf8_lossy(&bytes).into_owned())) + } + + /// Writes the text through a temporary file and an atomic rename. False + /// when it exceeds the bound or the disk refuses it. + pub fn write(&self, text: &str) -> bool { + if text.len() > self.maximum_bytes { + return false; + } + let mut temporary = self.path.clone().into_os_string(); + temporary.push(".tmp"); + let temporary = PathBuf::from(temporary); + let written = (|| -> std::io::Result<()> { + if let Some(parent) = self.path.parent().filter(|parent| !parent.as_os_str().is_empty()) { + std::fs::DirBuilder::new().recursive(true).mode(0o700).create(parent)?; + } + // The temporary name is fixed and guarded by the file's lock; a stale one is replaced. + let _ = std::fs::remove_file(&temporary); + let mut output = std::fs::OpenOptions::new().write(true).create_new(true).mode(0o600).open(&temporary)?; + output.write_all(text.as_bytes())?; + output.sync_all()?; + std::fs::rename(&temporary, &self.path) + })(); + if written.is_err() { + let _ = std::fs::remove_file(&temporary); + } + written.is_ok() + } + + pub fn delete(&self) { + let _ = std::fs::remove_file(&self.path); + } +} + +/// The shared gate of the Runtime 1.6 system services. The order is the Apple +/// runtime's and decides which code a script sees: +/// 1. `permission_denied`: not an installed owner, no manifest, the permission +/// is not declared, or `minimumRuntimeVersion` is below 1.6; +/// 2. `read_only`: a future timeline entry, a Live Activity presentation, or +/// a write outside a user-initiated action; +/// 3. `query_limit`: the eight-query budget of the execution is spent. +/// +/// Services with a per-widget native grant check it between steps 2 and 3; `$weather` has none. +pub mod system_gate { + use super::*; + + pub fn check(context: &CallContext, permission: &str, writing: bool) -> Result<(), SystemReject> { + validate(context, permission, writing)?; + if !context.consume_data_query() { + return Err(SystemServiceError::QueryLimit.reject()); + } + Ok(()) + } + + /// Steps 1 and 2 only; repeated before a result is delivered so a changed state stops an in-flight call. + pub fn validate(context: &CallContext, permission: &str, writing: bool) -> Result<(), SystemReject> { + if !context.is_installed() || !context.declares(permission) || !supports_runtime("1.6", context.minimum_runtime_version.as_deref()) { + return Err(SystemServiceError::PermissionDenied.reject()); + } + if context.is_timeline_projection || context.is_live_activity || (writing && !context.is_action) { + return Err(SystemServiceError::ReadOnly.reject()); + } + Ok(()) + } + + /// True when `declared` (`major.minor`) is at least `required`. A missing or malformed version is 0.0. + pub fn supports_runtime(required: &str, declared: Option<&str>) -> bool { + fn parts(text: &str) -> Option<(u32, u32)> { + let (major, minor) = text.split_once('.')?; + let field = |text: &str| if (1..=4).contains(&text.len()) && text.bytes().all(|byte| byte.is_ascii_digit()) { text.parse::().ok() } else { None }; + Some((field(major)?, field(minor)?)) + } + let Some(need) = parts(required) else { return false }; + let have = declared.and_then(parts).unwrap_or((0, 0)); + have >= need + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn owners_are_canonical_lowercase_uuids() { + assert!(is_owner("11111111-1111-4111-8111-111111111111")); + assert!(is_owner("abcdef01-2345-6789-abcd-ef0123456789")); + for value in ["", "preview", "11111111-1111-4111-8111-11111111111", "11111111-1111-4111-8111-1111111111111", "ABCDEF01-2345-6789-abcd-ef0123456789", "11111111x1111-4111-8111-111111111111"] { + assert!(!is_owner(value), "{value}"); + } + } + + #[test] + fn runtime_versions_compare_numerically() { + use system_gate::supports_runtime; + assert!(supports_runtime("1.6", Some("1.6"))); + assert!(supports_runtime("1.6", Some("1.10"))); + assert!(supports_runtime("1.6", Some("2.0"))); + for declared in [None, Some("1.5"), Some(""), Some("1"), Some("1.6.0"), Some("v1.6"), Some("1.66666")] { + assert!(!supports_runtime("1.6", declared), "{declared:?}"); + } + assert!(!supports_runtime("latest", Some("9.9"))); + } + + #[test] + fn log_codes_never_carry_free_text() { + assert_eq!(system_log::code("[permission_denied] System operation could not complete."), "permission_denied"); + assert_eq!(system_log::code("[weather:Limit] x"), "weather:Limit"); + for message in ["no code", "[] empty", "[has space] x", "[unterminated", "x [late] y", "[1234] digits"] { + assert_eq!(system_log::code(message), "error", "{message}"); + } + assert_eq!(SystemServiceError::QueryLimit.message(), "[query_limit] System operation could not complete. Existing user data is preserved."); + } + + #[test] + fn control_and_format_characters_are_recognized() { + for character in ['\0', '\n', '\u{7f}', '\u{85}', '\u{ad}', '\u{200b}', '\u{200d}', '\u{202e}', '\u{feff}', '\u{e0041}'] { + assert!(is_control_or_format(character), "{:x}", character as u32); + } + for character in ['a', ' ', '\u{a0}', '\u{2028}', '你', '🌧'] { + assert!(!is_control_or_format(character), "{:x}", character as u32); + } + } + + #[test] + fn private_files_are_bounded_private_and_refuse_links() { + use std::os::unix::fs::PermissionsExt; + let directory = std::env::temp_dir().join(format!("scriptwidget-test-private-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&directory); + let file = PrivateFile::new(directory.join("nested").join("record.json"), 16); + assert_eq!(file.read(), Ok(None)); + assert!(!file.exists()); + assert!(file.write("{\"a\":1}")); + assert_eq!(file.read(), Ok(Some("{\"a\":1}".into()))); + assert_eq!(std::fs::metadata(file.path()).unwrap().permissions().mode() & 0o777, 0o600); + assert_eq!(std::fs::metadata(directory.join("nested")).unwrap().permissions().mode() & 0o777, 0o700); + assert!(!file.write("12345678901234567"), "over the bound"); + assert_eq!(file.read(), Ok(Some("{\"a\":1}".into()))); + // A file that grew behind the bound, a directory and a link are unreadable, not missing. + std::fs::write(file.path(), "12345678901234567").unwrap(); + assert_eq!(file.read(), Err(Unreadable)); + file.delete(); + std::fs::create_dir(file.path()).unwrap(); + assert_eq!(file.read(), Err(Unreadable)); + std::fs::remove_dir(file.path()).unwrap(); + std::fs::write(directory.join("target.json"), "secret").unwrap(); + std::os::unix::fs::symlink(directory.join("target.json"), file.path()).unwrap(); + assert_eq!(file.read(), Err(Unreadable)); + assert!(file.exists()); + let _ = std::fs::remove_dir_all(&directory); + } +} diff --git a/Linux/crates/sw-core/src/api/system.rs b/Linux/crates/sw-core/src/api/system.rs new file mode 100644 index 00000000..0fe9993a --- /dev/null +++ b/Linux/crates/sw-core/src/api/system.rs @@ -0,0 +1,364 @@ +//! Snapshots behind `$device` and `$system`: the Linux counterpart of +//! `Windows/src/ScriptWidget.Core/Api/DeviceInfo.cs`. Values are captured +//! once per execution, like the native iOS runtime. None of them needs a +//! permission or a privilege: everything is read from `/etc/os-release`, +//! `/proc`, `/sys` and the environment of the process. +//! +//! Choices where Linux has no single answer (see `docs/windows/README.md`, +//! "Data APIs", for the Windows values they follow): +//! - `model` is always `"PC"`; the hardware model is not exposed to scripts. +//! - `name` and `hostName` are the host name. +//! - `brightness` is always `1`, `thermalState` always `"nominal"` and +//! `reduceMotionEnabled` always `false`: desktops disagree on where these +//! live, and none is readable everywhere. +//! - a machine without a battery reports `level` 1 and state `"full"`. +//! - `lowPowerMode` is true when the kernel's platform profile is `low-power`. +//! - `screen` is whatever the host passes in: the runtime has no display +//! connection of its own. + +use std::ffi::CString; +use std::path::Path; +use std::time::{SystemTime, UNIX_EPOCH}; + +use serde_json::{Value, json}; + +use crate::intl; +use crate::json::Object; + +/// `$system.platform()` on this host. +pub const PLATFORM: &str = "linux"; + +/// The display a widget is shown on, in points. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Screen { + pub width: f64, + pub height: f64, + pub scale: f64, +} + +impl Default for Screen { + /// What is reported when no display is known, as in a command-line render. + fn default() -> Self { + Self { width: 1920.0, height: 1080.0, scale: 1.0 } + } +} + +/// The `device` member of an execution request. +pub fn device_info(screen: Screen) -> Object { + device_from(Path::new("/"), screen) +} + +/// The `system` member of an execution request. +pub fn system_info() -> Object { + system_from(Path::new("/")) +} + +/// `device_info` with the system files read below `root`, for tests. +pub fn device_from(root: &Path, screen: Screen) -> Object { + let release = os_release(root); + let (level, state) = battery(root); + let (total, free) = disk_space(&data_directory()); + object(json!({ + "screen": { "width": screen.width, "height": screen.height, "scale": screen.scale }, + "language": language(&intl::default_locale_tag()), + "model": "PC", + "name": host_name(root), + "battery": { "level": level, "state": state }, + "systemVersion": release.get("VERSION_ID").cloned().filter(|version| !version.is_empty()).unwrap_or_else(|| kernel_release(root)), + "totalDiskSpace": total, + "freeDiskSpace": free, + })) +} + +/// `system_info` with the system files read below `root`, for tests. +pub fn system_from(root: &Path) -> Object { + let locale = intl::default_locale_tag(); + let now = SystemTime::now().duration_since(UNIX_EPOCH).map_or(0, |elapsed| elapsed.as_secs() as i64); + let offset = utc_offset_seconds(now); + let release = os_release(root); + let pretty = release.get("PRETTY_NAME").or_else(|| release.get("NAME")).cloned().filter(|name| !name.is_empty()).unwrap_or_else(|| "Linux".into()); + let host = host_name(root); + let active = std::thread::available_parallelism().map_or(1, |count| count.get()); + let (first_weekday, minimum_days) = week_rules(region(&locale)); + object(json!({ + "appInfo": { "name": "ScriptWidget", "bundleId": "com.atomsprout.scriptwidget", "version": env!("CARGO_PKG_VERSION"), "build": "0" }, + "locale": locale.replace('-', "_"), + "preferredLanguages": preferred_languages(std::env::var("LANGUAGE").ok().as_deref(), &locale), + "timeZone": { "identifier": intl::default_time_zone(), "abbreviation": abbreviation(offset), "offsetSeconds": offset }, + "is24HourClock": is_24_hour_clock(&locale), + "calendarInfo": { "identifier": "gregorian", "firstWeekday": first_weekday, "minimumDaysInFirstWeek": minimum_days }, + "systemUptime": uptime(root), + "memory": { "physical": physical_memory(root) }, + "thermalState": "nominal", + "lowPowerMode": read(root, "sys/firmware/acpi/platform_profile").is_some_and(|profile| profile.trim() == "low-power"), + "brightness": 1.0, + "reduceMotionEnabled": false, + "platform": PLATFORM, + "hostName": host, + "processName": "ScriptWidget", + "osVersionString": format!("{pretty} (Linux {})", kernel_release(root)), + "processorCount": configured_processors().max(active), + "activeProcessorCount": active, + })) +} + +fn object(value: Value) -> Object { + match value { + Value::Object(object) => object, + _ => Object::new(), + } +} + +/// A small text file below `root`; `None` when it is missing, unreadable or implausibly large. +fn read(root: &Path, relative: &str) -> Option { + let path = root.join(relative); + let metadata = std::fs::metadata(&path).ok()?; + if !metadata.is_file() || metadata.len() > 64 * 1024 { + return None; + } + let text = std::fs::read_to_string(path).ok()?; + (text.len() <= 64 * 1024).then_some(text) +} + +/// `KEY=value` lines of os-release(5), with shell quotes removed. +pub fn os_release(root: &Path) -> std::collections::HashMap { + let text = read(root, "etc/os-release").or_else(|| read(root, "usr/lib/os-release")).unwrap_or_default(); + let mut values = std::collections::HashMap::new(); + for line in text.lines() { + let line = line.trim(); + if line.starts_with('#') { + continue; + } + let Some((key, value)) = line.split_once('=') else { continue }; + let value = value.trim(); + let unquoted = match value.as_bytes() { + [b'"', .., b'"'] | [b'\'', .., b'\''] if value.len() >= 2 => &value[1..value.len() - 1], + _ => value, + }; + let mut plain = String::with_capacity(unquoted.len()); + let mut characters = unquoted.chars(); + while let Some(character) = characters.next() { + // Inside double quotes a backslash protects the next character. + plain.push(if character == '\\' && value.starts_with('"') { characters.next().unwrap_or('\\') } else { character }); + } + values.insert(key.trim().to_owned(), plain); + } + values +} + +fn host_name(root: &Path) -> String { + ["proc/sys/kernel/hostname", "etc/hostname"] + .iter() + .filter_map(|file| read(root, file)) + .map(|text| text.trim().to_owned()) + .find(|name| !name.is_empty()) + .or_else(|| std::env::var("HOSTNAME").ok().filter(|name| !name.is_empty())) + .unwrap_or_else(|| "localhost".into()) +} + +fn kernel_release(root: &Path) -> String { + read(root, "proc/sys/kernel/osrelease").map(|text| text.trim().to_owned()).unwrap_or_default() +} + +/// Level 0…1 and `charging`, `unplugged`, `full` or `unknown`, from the first +/// system battery in `/sys/class/power_supply`. Batteries of mice, keyboards +/// and headsets (`scope` `Device`) do not count. +fn battery(root: &Path) -> (f64, &'static str) { + let directory = root.join("sys/class/power_supply"); + let mut names: Vec = std::fs::read_dir(&directory).into_iter().flatten().filter_map(|entry| entry.ok()?.file_name().into_string().ok()).collect(); + names.sort_unstable(); + let supply = |name: &str, file: &str| read(&directory, &format!("{name}/{file}")).map(|text| text.trim().to_owned()); + let Some(name) = names.iter().find(|name| supply(name, "type").as_deref() == Some("Battery") && supply(name, "scope").as_deref() != Some("Device")) else { + // A desktop without a battery, as on Windows. + return (1.0, "full"); + }; + let percent = supply(name, "capacity").and_then(|text| text.parse::().ok()).filter(|percent| (0.0..=100.0).contains(percent)); + let state = match supply(name, "status").as_deref() { + Some("Charging") => "charging", + Some("Discharging") => "unplugged", + Some("Full") => "full", + // On external power without charging, for example at a charge limit. + Some("Not charging") => { + if percent.is_some_and(|percent| percent >= 100.0) { + "full" + } else { + "charging" + } + } + _ => "unknown", + }; + (percent.map_or(-1.0, |percent| percent / 100.0), state) +} + +fn uptime(root: &Path) -> f64 { + read(root, "proc/uptime").and_then(|text| text.split_whitespace().next()?.parse::().ok()).unwrap_or(0.0) +} + +fn physical_memory(root: &Path) -> u64 { + read(root, "proc/meminfo") + .and_then(|text| { + let line = text.lines().find(|line| line.starts_with("MemTotal:"))?; + line.split_whitespace().nth(1)?.parse::().ok() + }) + .map_or(0, |kilobytes| kilobytes.saturating_mul(1024)) +} + +/// Where ScriptWidget keeps its data: `$XDG_DATA_HOME`, or `~/.local/share`. +fn data_directory() -> std::path::PathBuf { + std::env::var_os("XDG_DATA_HOME") + .filter(|value| Path::new(value).is_absolute()) + .map(std::path::PathBuf::from) + .or_else(|| std::env::var_os("HOME").map(|home| Path::new(&home).join(".local/share"))) + .unwrap_or_else(|| "/".into()) +} + +/// Total and available bytes of the file system that holds `path` or its nearest existing parent. +// The field types differ between C libraries. +#[allow(clippy::unnecessary_cast)] +fn disk_space(path: &Path) -> (u64, u64) { + let mut current = Some(path); + while let Some(candidate) = current { + if let Ok(name) = CString::new(candidate.as_os_str().as_encoded_bytes()) { + // SAFETY: `name` is NUL-terminated and `stats` is a valid place to write the result. + let stats = unsafe { + let mut stats = std::mem::MaybeUninit::::zeroed(); + if libc::statvfs(name.as_ptr(), stats.as_mut_ptr()) == 0 { Some(stats.assume_init()) } else { None } + }; + if let Some(stats) = stats { + let block = stats.f_frsize as u64; + return ((stats.f_blocks as u64).saturating_mul(block), (stats.f_bavail as u64).saturating_mul(block)); + } + } + current = candidate.parent(); + } + (0, 0) +} + +/// Offset of local time from UTC at `seconds`, in the zone of the process. +#[allow(clippy::unnecessary_cast)] +fn utc_offset_seconds(seconds: i64) -> i64 { + // SAFETY: `localtime_r` writes into `local` and reads `time`; both live on this stack. + unsafe { + let time = seconds as libc::time_t; + let mut local = std::mem::MaybeUninit::::zeroed(); + if libc::localtime_r(&time, local.as_mut_ptr()).is_null() { 0 } else { local.assume_init().tm_gmtoff as i64 } + } +} + +/// `GMT+8`, `GMT-3:30`: the form the Windows runtime reports. +fn abbreviation(offset_seconds: i64) -> String { + let minutes = offset_seconds.abs() / 60; + let sign = if offset_seconds < 0 { '-' } else { '+' }; + if minutes % 60 == 0 { format!("GMT{sign}{}", minutes / 60) } else { format!("GMT{sign}{}:{:02}", minutes / 60, minutes % 60) } +} + +fn configured_processors() -> usize { + // SAFETY: `sysconf` only reads its argument. + let count = unsafe { libc::sysconf(libc::_SC_NPROCESSORS_CONF) }; + usize::try_from(count).unwrap_or(1).max(1) +} + +/// The language subtag of a BCP 47 tag. +fn language(tag: &str) -> String { + tag.split(['-', '_']).next().filter(|language| !language.is_empty()).unwrap_or("en").to_ascii_lowercase() +} + +/// The two-letter region of a BCP 47 tag, or "". +fn region(tag: &str) -> &str { + tag.split(['-', '_']).skip(1).find(|part| part.len() == 2 && part.bytes().all(|byte| byte.is_ascii_alphabetic())).unwrap_or("") +} + +/// The gettext priority list in `LANGUAGE` ("zh_CN:en_US") as BCP 47 tags, or the locale alone. +fn preferred_languages(variable: Option<&str>, locale: &str) -> Vec { + let mut languages: Vec = Vec::new(); + for item in variable.unwrap_or("").split(':') { + let name = item.split(['.', '@']).next().unwrap_or(""); + if name.is_empty() || name == "C" || name == "POSIX" || !name.chars().all(|character| character.is_ascii_alphanumeric() || character == '_' || character == '-') { + continue; + } + let tag = name.replace('_', "-"); + if !languages.contains(&tag) { + languages.push(tag); + } + } + if languages.is_empty() { + languages.push(locale.to_owned()); + } + languages +} + +/// Whether the locale writes 13:00 rather than 1 PM, asked of ICU like every other locale question. +fn is_24_hour_clock(locale: &str) -> bool { + // 13:00 UTC on 2 January 2000, written with Latin digits whatever the locale's own are. + let locale = if locale.contains("-u-") { locale.to_owned() } else { format!("{locale}-u-nu-latn") }; + let request = json!({ "locale": locale, "time": 946_818_000_000.0, "options": { "hour": "numeric", "timeZone": "UTC" } }); + match intl::call("intl.date", Some(&crate::json::stringify(&request))) { + Ok(Some(text)) => text.contains("13"), + _ => true, + } +} + +/// First day of the week (Sunday is 1, as `Calendar.firstWeekday` on iOS) and +/// the minimal days in the first week, by region. CLDR 48's week data, as +/// ICU 78 reports it for every ISO country; other regions use Monday and 1. +fn week_rules(region: &str) -> (u8, u8) { + const SUNDAY: &[&str] = &[ + "AG", "AS", "BD", "BR", "BS", "BT", "BW", "BZ", "CA", "CO", "DM", "DO", "ET", "GT", "GU", "HK", "HN", "ID", "IL", "IN", "IS", "JM", "JP", "KE", "KH", "KR", "LA", "MH", "MM", "MO", "MT", "MX", "MZ", "NI", "NP", + "PA", "PE", "PH", "PK", "PR", "PT", "PY", "SA", "SG", "SV", "TH", "TT", "TW", "UM", "US", "VE", "VI", "WS", "YE", "ZA", "ZW", + ]; + const SATURDAY: &[&str] = &["AF", "BH", "DJ", "DZ", "EG", "IQ", "IR", "JO", "KW", "LY", "OM", "QA", "SD", "SY"]; + const FOUR_DAYS: &[&str] = &[ + "AD", "AT", "AX", "BE", "BG", "CH", "CZ", "DE", "DK", "EE", "ES", "FI", "FJ", "FO", "FR", "GB", "GF", "GG", "GI", "GP", "GR", "HU", "IE", "IM", "IS", "IT", "JE", "LI", "LT", "LU", "MC", "MQ", "NL", "NO", "PL", + "PT", "RE", "RU", "SE", "SJ", "SK", "SM", "VA", + ]; + let region = region.to_ascii_uppercase(); + let first = if SUNDAY.contains(®ion.as_str()) { + 1 + } else if region == "MV" { + 6 + } else if SATURDAY.contains(®ion.as_str()) { + 7 + } else { + 2 + }; + (first, if FOUR_DAYS.contains(®ion.as_str()) { 4 } else { 1 }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn offsets_are_written_like_the_other_platforms() { + assert_eq!(abbreviation(0), "GMT+0"); + assert_eq!(abbreviation(8 * 3600), "GMT+8"); + assert_eq!(abbreviation(-5 * 3600), "GMT-5"); + assert_eq!(abbreviation(-(3 * 3600 + 1800)), "GMT-3:30"); + assert_eq!(abbreviation(5 * 3600 + 45 * 60), "GMT+5:45"); + } + + #[test] + fn languages_and_regions_come_from_the_locale() { + assert_eq!(language("zh-Hans-CN"), "zh"); + assert_eq!(language("en_US"), "en"); + assert_eq!(language(""), "en"); + assert_eq!(region("zh-Hans-CN"), "CN"); + assert_eq!(region("en-US"), "US"); + assert_eq!(region("eo"), ""); + assert_eq!(preferred_languages(Some("zh_CN.UTF-8:en_US:zh_CN:C:../x"), "de-DE"), ["zh-CN", "en-US"]); + assert_eq!(preferred_languages(Some(""), "de-DE"), ["de-DE"]); + assert_eq!(preferred_languages(None, "de-DE"), ["de-DE"]); + } + + #[test] + fn weeks_follow_the_region() { + assert_eq!(week_rules("US"), (1, 1)); + assert_eq!(week_rules("de"), (2, 4)); + assert_eq!(week_rules("GB"), (2, 4)); + assert_eq!(week_rules("CN"), (2, 1)); + assert_eq!(week_rules("EG"), (7, 1)); + assert_eq!(week_rules(""), (2, 1)); + assert_eq!(week_rules("MV"), (6, 1)); + assert_eq!(week_rules("PT"), (1, 4)); + } +} diff --git a/Linux/crates/sw-core/src/api/weather.rs b/Linux/crates/sw-core/src/api/weather.rs new file mode 100644 index 00000000..295f884b --- /dev/null +++ b/Linux/crates/sw-core/src/api/weather.rs @@ -0,0 +1,334 @@ +//! `$weather.forecast`: MET Norway's compact forecast for coordinates the +//! script supplies. Ported from +//! `Windows/src/ScriptWidget.Core/Api/WeatherModule.cs`. + +use std::path::PathBuf; +use std::sync::Arc; +use std::time::{SystemTime, UNIX_EPOCH}; + +use serde_json::{Value, json}; + +use crate::api::fetch::{Cancel, FetchError, FetchModule, Url, validation_error}; +use crate::api::support::{CallContext, PrivateFile, SystemReject, SystemServiceError, canonical, iso_dates, run_system, system_gate, system_log, system_options}; +use crate::executor::{Completion, ExecutionRequest, NativeModule}; +use crate::json::{Object, ObjectExt, quote, stringify_object}; +use crate::plugins::strict_json; + +/// `$weather.forecast(options)` input. +/// +/// Rules: closed fields (`latitude`, `longitude`, `hours`); latitude −90…90 +/// and longitude −180…180, rounded to two decimals before anything leaves the +/// device; `hours` an integer 1…48, 24 when absent. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct WeatherOptions { + pub latitude: f64, + pub longitude: f64, + pub hours: usize, +} + +impl WeatherOptions { + pub const HOST: &'static str = "api.met.no"; + + /// The MET Norway compact forecast for the rounded coordinates. + pub fn url(&self) -> String { + format!("https://{}/weatherapi/locationforecast/2.0/compact?lat={}&lon={}", Self::HOST, decimal(self.latitude), decimal(self.longitude)) + } + + /// Rejects with `invalid_options` for any violation of the rules above. + pub fn parse(options: &Object) -> Result { + system_options::fields(options, &["latitude", "longitude", "hours"])?; + let latitude = Self::round2(system_options::number(options.get("latitude"), -90.0, 90.0)?); + let longitude = Self::round2(system_options::number(options.get("longitude"), -180.0, 180.0)?); + let hours = if options.has("hours") { system_options::integer(options.get("hours"), 1, 48)? as usize } else { 24 }; + Ok(Self { latitude, longitude, hours }) + } + + /// Two decimals, halves away from zero, as `rounded()` does in the Apple runtime. + pub fn round2(value: f64) -> f64 { + let scaled = value * 100.0; + (if scaled < 0.0 { -(-scaled + 0.5).floor() } else { (scaled + 0.5).floor() }) / 100.0 + } +} + +/// Shortest decimal text with at least one decimal place (`60.0`), the form every platform puts in the URL. +fn decimal(value: f64) -> String { + let text = value.to_string(); + if text.contains('.') { text } else { format!("{text}.0") } +} + +/// Pure rules of `$weather`: provider normalisation and the private cache. +pub mod weather_rules { + use super::*; + + pub const MAXIMUM_RESPONSE_BYTES: usize = 2 * 1024 * 1024; + pub const MAXIMUM_SERIES: usize = 300; + pub const MAXIMUM_CACHE_BYTES: usize = 128 * 1024; + /// A matching cache younger than this is returned without a request. + pub const FRESH_MILLISECONDS: i64 = 3_600_000; + /// A matching cache up to this age may stand in for a failed request, marked `isStale`. + pub const USABLE_MILLISECONDS: i64 = 6 * 3_600_000; + pub const PROVIDER: &str = "MET Norway"; + pub const ATTRIBUTION_TEXT: &str = "Weather data from MET Norway"; + pub const ATTRIBUTION_URL: &str = "https://www.met.no/en/free-meteorological-data/Download-services"; + + fn text(value: Option<&Value>) -> Option<&str> { + value.and_then(Value::as_str) + } + + /// Turns a MET Norway compact forecast into the documented result. + /// + /// - The body is at most 2 MiB and `properties.timeseries` is an array of + /// at most 300 objects, else `unavailable`. A document with a repeated + /// member name does not say one thing, so it says nothing. + /// - Entries older than one hour before `now_milliseconds`, or without a + /// time and instant details, are skipped. + /// - `air_temperature` must be −150…100 and `wind_speed` 0…200; a present + /// `precipitation_amount` must be 0…1000. A violation rejects the whole + /// payload (`invalid_options`, the Apple runtime's code). + /// - Missing precipitation and condition stay null; a condition is cut to 64 characters. + /// - At most `options.hours` rows; no usable row is `unavailable`. + pub fn normalize(body: &[u8], options: &WeatherOptions, now_milliseconds: i64) -> Result { + let unavailable = || SystemServiceError::Unavailable.reject(); + if body.len() > MAXIMUM_RESPONSE_BYTES { + return Err(unavailable()); + } + let document = strict_json::parse(&String::from_utf8_lossy(body), false).map_err(|_| unavailable())?; + let series = document.as_object().and_then(|root| root.opt_object("properties")).and_then(|properties| properties.opt_array("timeseries")).ok_or_else(unavailable)?; + if series.len() > MAXIMUM_SERIES || series.iter().any(|item| !item.is_object()) { + return Err(unavailable()); + } + let mut rows: Vec = Vec::new(); + for item in series.iter().filter_map(Value::as_object) { + let Some(time) = text(item.get("time")) else { continue }; + let date = if time.trim().is_empty() || system_options::has_control_characters(time) { None } else { iso_dates::parse(time) }; + if !date.is_some_and(|instant| instant >= now_milliseconds - 3_600_000) { + continue; + } + let Some(values) = item.opt_object("data") else { continue }; + let Some(details) = values.opt_object("instant").and_then(|instant| instant.opt_object("details")) else { continue }; + let temperature = system_options::number(details.get("air_temperature"), -150.0, 100.0)?; + let wind = system_options::number(details.get("wind_speed"), 0.0, 200.0)?; + let next = values.opt_object("next_1_hours"); + let precipitation = match next.and_then(|next| next.opt_object("details")) { + Some(next_details) if next_details.has("precipitation_amount") => Some(system_options::number(next_details.get("precipitation_amount"), 0.0, 1000.0)?), + _ => None, + }; + let symbol = text(next.and_then(|next| next.opt_object("summary")).and_then(|summary| summary.get("symbol_code"))); + rows.push(json!({ + "time": time, + "temperatureC": temperature, + "windMetersPerSecond": wind, + "precipitationMM": precipitation, + "condition": symbol.map(|symbol| symbol.chars().take(64).collect::()), + })); + if rows.len() == options.hours { + break; + } + } + if rows.is_empty() { + return Err(unavailable()); + } + let result = json!({ + "provider": PROVIDER, + "attribution": { "text": ATTRIBUTION_TEXT, "url": ATTRIBUTION_URL }, + "latitude": options.latitude, + "longitude": options.longitude, + "fetchedAt": iso_dates::format(now_milliseconds), + "isStale": false, + "hours": rows, + }); + match result { + Value::Object(result) => Ok(result), + _ => Err(unavailable()), + } + } + + /// A cached forecast that may answer `options`, or `None`. + /// + /// It matches only when the rounded coordinates are equal, `fetchedAt` is + /// not in the future and at most six hours old, and it holds at least the + /// requested number of hours. The result carries the first + /// `options.hours` rows and `isStale` (true from one hour of age). + pub fn from_cache(stored: Option<&str>, options: &WeatherOptions, now_milliseconds: i64) -> Option { + let stored = stored.filter(|stored| stored.len() <= MAXIMUM_CACHE_BYTES)?; + let Ok(Value::Object(mut cached)) = strict_json::parse(stored, false) else { return None }; + if system_options::json_number(cached.get("latitude")) != Some(options.latitude) || system_options::json_number(cached.get("longitude")) != Some(options.longitude) { + return None; + } + let fetched = iso_dates::parse(text(cached.get("fetchedAt"))?)?; + let age = now_milliseconds - fetched; + let hours = cached.opt_array("hours")?; + if !(0..=USABLE_MILLISECONDS).contains(&age) || hours.len() < options.hours { + return None; + } + let rows: Vec = hours[..options.hours].to_vec(); + cached.insert("hours".into(), Value::Array(rows)); + cached.insert("isStale".into(), Value::Bool(age >= FRESH_MILLISECONDS)); + Some(cached) + } +} + +/// A provider response: the HTTP status and the bounded body. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct WeatherResponse { + pub status: u16, + pub body: Vec, +} + +/// The transport failed; the reason is not passed on. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct WeatherTransportError; + +/// The network side of `$weather`, replaceable in tests. +pub trait WeatherTransport: Send + Sync { + /// One GET. `allow_redirect` decides for every redirect target; a refused redirect is a failure. + fn get(&self, url: &str, headers: &[(String, String)], timeout_milliseconds: u64, allow_redirect: &dyn Fn(&Url) -> bool, cancel: Option) -> Result; +} + +/// [`WeatherTransport`] over the bounded `fetch` transport: public hosts only, resolved addresses checked, at most 2 MiB. +pub struct FetchWeatherTransport { + fetch: Arc, +} + +impl FetchWeatherTransport { + pub fn new(fetch: Arc) -> Self { + Self { fetch } + } +} + +impl WeatherTransport for FetchWeatherTransport { + fn get(&self, url: &str, headers: &[(String, String)], timeout_milliseconds: u64, allow_redirect: &dyn Fn(&Url) -> bool, cancel: Option) -> Result { + let url = Url::parse(url).map_err(|_| WeatherTransportError)?; + if validation_error(&url).is_some() { + return Err(WeatherTransportError); + } + let allowed = |target: &Url| validation_error(target).is_none() && allow_redirect(target); + let response: Result<_, FetchError> = self.fetch.perform("GET", &url, headers, None, timeout_milliseconds, &allowed, cancel); + response.map(|response| WeatherResponse { status: response.status, body: response.body }).map_err(|_| WeatherTransportError) + } +} + +/// Epoch milliseconds. +pub type Clock = Arc i64 + Send + Sync>; + +/// `$weather.forecast`. The device location is never read here. The order of +/// checks is the Apple runtime's: +/// 1. the system gate (`weather` declared by an installed Runtime 1.6 +/// package, not a future timeline entry or Live Activity, the eight-query +/// budget); +/// 2. `invalid_options` for the options; +/// 3. `permission_denied` unless the package declares `network` and `api.met.no`; +/// 4. a matching cache younger than one hour is returned without a request; +/// 5. `query_limit` when the 32-request network budget of the execution is spent; +/// 6. the request (3 s timeout, redirects only to declared hosts); a failure +/// returns a matching cache up to six hours old with `isStale: true`, else +/// the failure. +/// +/// There is no per-widget native grant for weather: nothing is read from the device. +#[derive(Clone)] +pub struct WeatherModule { + transport: Arc, + directory: PathBuf, + user_agent: String, + clock: Clock, +} + +impl WeatherModule { + pub const PERMISSION: &'static str = "weather"; + pub const TIMEOUT_MILLISECONDS: u64 = 3_000; + + pub fn new(transport: Arc, directory: impl Into, user_agent: impl Into) -> Self { + let clock: Clock = Arc::new(|| SystemTime::now().duration_since(UNIX_EPOCH).map_or(0, |elapsed| elapsed.as_millis() as i64)); + Self::with_clock(transport, directory, user_agent, clock) + } + + pub fn with_clock(transport: Arc, directory: impl Into, user_agent: impl Into, clock: Clock) -> Self { + Self { transport, directory: directory.into(), user_agent: user_agent.into(), clock } + } + + /// MET Norway asks every client to identify itself; no user or device detail is included. + pub fn user_agent(version_name: &str) -> String { + format!("ScriptWidget-Linux/{} https://github.com/everettjf/scriptwidget", if version_name.is_empty() { "0" } else { version_name }) + } + + pub fn forecast(&self, context: &CallContext, options: Option<&Object>) -> Result { + system_gate::check(context, Self::PERMISSION, false)?; + let query = WeatherOptions::parse(options.unwrap_or(&Object::new()))?; + if !context.allows_host(WeatherOptions::HOST) { + return Err(SystemServiceError::PermissionDenied.reject()); + } + let cache = self.cache(&context.installation); + let lock = cache.lock(); + let cached = { + let _guard = lock.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + let stored = cache.read().ok().flatten(); + weather_rules::from_cache(stored.as_deref(), &query, (self.clock)()) + }; + if let Some(fresh) = cached.as_ref().filter(|cached| !cached.opt_bool("isStale", false)) { + return Ok(fresh.clone()); + } + if !context.consume_network_request() { + return Err(SystemServiceError::QueryLimit.reject()); + } + let fetched = (|| -> Result { + let unavailable = || SystemServiceError::Unavailable.reject(); + let headers = [("User-Agent".to_owned(), self.user_agent.clone())]; + let allow_redirect = |target: &Url| context.is_active() && context.allows_host(target.host_str().unwrap_or("")); + let response = self.transport.get(&query.url(), &headers, Self::TIMEOUT_MILLISECONDS, &allow_redirect, None).map_err(|_| unavailable())?; + system_gate::validate(context, Self::PERMISSION, false)?; + if !context.allows_host(WeatherOptions::HOST) || response.status != 200 { + return Err(unavailable()); + } + let normalized = weather_rules::normalize(&response.body, &query, (self.clock)())?; + let encoded = stringify_object(&normalized); + if encoded.len() > weather_rules::MAXIMUM_CACHE_BYTES { + return Err(SystemServiceError::LimitExceeded.reject()); + } + let written = { + let _guard = lock.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + cache.write(&encoded) + }; + if !written { + return Err(unavailable()); + } + Ok(normalized) + })(); + match (fetched, cached) { + (Err(_), Some(stale)) => Ok(stale), + (outcome, _) => outcome, + } + } + + /// Removes the forecast cache of a deleted installation. + pub fn retire(&self, installation: &str) { + if crate::api::support::is_owner(installation) { + self.cache(installation).delete(); + } + system_log::call("weather.retire", "ok"); + } + + /// One forecast per installation, outside every script-reachable path. + fn cache(&self, installation: &str) -> PrivateFile { + PrivateFile::new(self.directory.join(canonical::owner_file_name(installation, ".json")), weather_rules::MAXIMUM_CACHE_BYTES) + } +} + +impl NativeModule for WeatherModule { + fn handles(&self, operation: &str) -> bool { + operation.starts_with("weather.") + } + + fn apis(&self) -> &[&'static str] { + &["$weather"] + } + + fn start(&self, request: &ExecutionRequest<'_>, operation: &str, payload: Option<&Object>, done: Completion) { + if operation != "weather.forecast" { + return done.complete(false, Some(quote(&format!("Unsupported operation {operation}")))); + } + let context = CallContext::from_request(request); + let options = payload.and_then(|payload| payload.opt_object("options")).cloned(); + let module = self.clone(); + run_system(operation, done, move || module.forecast(&context, options.as_ref()).map(|result| stringify_object(&result))); + } +} diff --git a/Linux/crates/sw-core/src/api/workers.rs b/Linux/crates/sw-core/src/api/workers.rs new file mode 100644 index 00000000..d2aabf6e --- /dev/null +++ b/Linux/crates/sw-core/src/api/workers.rs @@ -0,0 +1,95 @@ +//! Worker threads for native operations, so the JavaScript thread never +//! blocks on the network or the disk. The counterpart of `BoundedExecutor` in +//! `Windows/src/ScriptWidget.Core/Api/FetchModule.cs`. + +use std::collections::VecDeque; +use std::panic::{AssertUnwindSafe, catch_unwind}; +use std::sync::{Arc, Mutex}; + +type Work = Box; + +#[derive(Default)] +struct State { + queue: VecDeque, + running: usize, +} + +/// Runs work on background threads, at most a fixed number at a time; the +/// rest waits in order. Threads end when there is nothing left to do. +pub struct BoundedExecutor { + maximum: usize, + state: Arc>, +} + +impl BoundedExecutor { + pub fn new(maximum_concurrent: usize) -> Self { + Self { maximum: maximum_concurrent.max(1), state: Arc::default() } + } + + pub fn execute(&self, work: impl FnOnce() + Send + 'static) { + let mut work: Work = Box::new(work); + { + let mut state = self.state.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + if state.running >= self.maximum { + state.queue.push_back(work); + return; + } + state.running += 1; + } + let shared = self.state.clone(); + let drain = move || { + loop { + // Work reports its own failures; one that still panics must not end the worker. + let _ = catch_unwind(AssertUnwindSafe(work)); + let mut state = shared.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + match state.queue.pop_front() { + Some(next) => work = next, + None => { + state.running -= 1; + return; + } + } + } + }; + if let Err(error) = std::thread::Builder::new().name("scriptwidget-native".into()).spawn(drain) { + // No thread could be started: the work is dropped, which rejects its operation. + drop(error); + let mut state = self.state.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + state.running -= 1; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::mpsc; + use std::time::Duration; + + #[test] + fn runs_everything_with_bounded_concurrency() { + let pool = BoundedExecutor::new(3); + let active = Arc::new(AtomicUsize::new(0)); + let highest = Arc::new(AtomicUsize::new(0)); + let (done, finished) = mpsc::channel(); + for index in 0..20 { + let (active, highest, done) = (active.clone(), highest.clone(), done.clone()); + pool.execute(move || { + let now = active.fetch_add(1, Ordering::SeqCst) + 1; + highest.fetch_max(now, Ordering::SeqCst); + std::thread::sleep(Duration::from_millis(5)); + active.fetch_sub(1, Ordering::SeqCst); + if index == 7 { + done.send(index).unwrap(); + panic!("a failing job does not stop the pool"); + } + done.send(index).unwrap(); + }); + } + let mut seen: Vec = (0..20).map(|_| finished.recv_timeout(Duration::from_secs(10)).expect("job")).collect(); + seen.sort_unstable(); + assert_eq!(seen, (0..20).collect::>()); + assert!(highest.load(Ordering::SeqCst) <= 3); + } +} diff --git a/Linux/crates/sw-core/src/compiler.rs b/Linux/crates/sw-core/src/compiler.rs new file mode 100644 index 00000000..7db0ff05 --- /dev/null +++ b/Linux/crates/sw-core/src/compiler.rs @@ -0,0 +1,338 @@ +//! Validates manifests and transpiles JSX with the same Babel bundle and +//! prepare code as the web preview engine. Ported from +//! `Windows/src/ScriptWidget.Core/Runtime/PackageCompiler.cs`. + +use std::fmt; +use std::sync::Arc; +use std::sync::mpsc::{self, RecvTimeoutError, Sender}; +use std::thread::JoinHandle; +use std::time::Duration; + +use serde_json::{Value, json}; +use sw_quickjs::{Engine, JsError}; + +use crate::intl; +use crate::json::{Object, ObjectExt, as_text, parse_object, stringify}; +use crate::package_files::PackageFiles; +use crate::shared_scripts::SharedScripts; + +/// A failure with the error kind of the shared engine ("transformError", "resourceLimit", …). +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct RuntimeError { + pub kind: String, + pub message: String, +} + +impl RuntimeError { + pub fn new(kind: &str, message: impl Into) -> Self { + Self { kind: kind.to_owned(), message: message.into() } + } + + fn internal(message: impl Into) -> Self { + Self::new("internalError", message) + } +} + +impl fmt::Display for RuntimeError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "[{}] {}", self.kind, self.message) + } +} + +impl std::error::Error for RuntimeError {} + +/// Output of preparing a package: transpiled entry, transpiled imports and the validated manifest. +#[derive(Debug, Clone, Default)] +pub struct PreparedPackage { + pub code: String, + /// Transpiled code, or the compiler's error record, by package path. + pub imports: std::collections::HashMap, + pub manifest: Option, + pub has_manifest: bool, + pub action_id: Option, + /// In the order the compiler produced them. + pub timeline_environment: Vec<(String, String)>, +} + +impl PreparedPackage { + pub fn to_json(&self) -> Value { + let imports: Object = self.imports.iter().map(|(path, code)| (path.clone(), Value::String(code.clone()))).collect(); + let environment: Object = self.timeline_environment.iter().map(|(name, value)| (name.clone(), Value::String(value.clone()))).collect(); + json!({ + "code": self.code, + "imports": imports, + "manifest": self.manifest, + "hasManifest": self.has_manifest, + "actionID": self.action_id, + "timelineEnvironment": environment, + }) + } + + pub fn from_json(json: &Object) -> Result { + let strings = |name: &str| -> Vec<(String, String)> { + json.opt_object(name).map(|map| map.iter().map(|(key, value)| (key.clone(), as_text(value).unwrap_or_default())).collect()).unwrap_or_default() + }; + Ok(Self { + code: json.opt_string_or_none("code").ok_or_else(|| RuntimeError::internal("Compiler returned no code"))?, + imports: strings("imports").into_iter().collect(), + manifest: json.opt_object("manifest").cloned(), + has_manifest: json.opt_bool("hasManifest", false), + action_id: if json.is_null("actionID") { None } else { Some(json.opt_string("actionID")) }, + timeline_environment: strings("timelineEnvironment"), + }) + } +} + +/// What to prepare besides the files themselves. +#[derive(Debug, Clone, Default)] +pub struct PrepareOptions<'a> { + /// Entry file when it is not the manifest's. + pub entry: Option<&'a str>, + /// Trusted code evaluated before the entry; tests use it to fix the clock. + pub prelude: &'a str, + pub action_id: Option<&'a str>, + pub call_function: Option<&'a str>, +} + +const IDLE: Duration = Duration::from_secs(45); +const COMPILE_TIMEOUT_MILLISECONDS: u64 = 30_000; +const COMPILER_MEMORY: usize = 384 * 1024 * 1024; +const COMPILER_JS_STACK: usize = 12 * 1024 * 1024; +const COMPILER_THREAD_STACK: usize = 16 * 1024 * 1024; + +struct Job { + function: &'static str, + arguments: Vec, + reply: Sender, JsError>>, +} + +/// The compiler context. Babel is expensive to load, so the context lives on +/// one dedicated thread and is released after a period of disuse. Package +/// code never executes in this context: it is only parsed and transformed. +pub struct PackageCompiler { + jobs: Option>, + worker: Option>, +} + +impl PackageCompiler { + pub fn new(scripts: Arc) -> std::io::Result { + let (jobs, queue) = mpsc::channel::(); + let worker = std::thread::Builder::new().name("scriptwidget-compiler".into()).stack_size(COMPILER_THREAD_STACK).spawn(move || { + let mut engine: Option> = None; + loop { + let job = if engine.is_some() { + match queue.recv_timeout(IDLE) { + Ok(job) => job, + Err(RecvTimeoutError::Timeout) => { + engine = None; + continue; + } + Err(RecvTimeoutError::Disconnected) => break, + } + } else { + match queue.recv() { + Ok(job) => job, + Err(_) => break, + } + }; + let current = match engine.take() { + Some(current) => current, + None => match create_engine(&scripts) { + Ok(created) => created, + Err(error) => { + let _ = job.reply.send(Err(error)); + continue; + } + }, + }; + let mut current = current; + current.set_deadline(COMPILE_TIMEOUT_MILLISECONDS); + let arguments: Vec> = job.arguments.iter().map(|argument| Some(argument.as_str())).collect(); + let result = current.call(job.function, &arguments); + current.set_deadline(0); + // An interrupted compile can leave Babel's own state half updated: start over next time. + if !matches!(&result, Err(error) if error.interrupted) { + engine = Some(current); + } + let _ = job.reply.send(result); + } + })?; + Ok(Self { jobs: Some(jobs), worker: Some(worker) }) + } + + fn call(&self, function: &'static str, arguments: Vec) -> Result, JsError> { + let stopped = || JsError { message: "The compiler thread has stopped".into(), interrupted: false }; + let (reply, answer) = mpsc::channel(); + self.jobs.as_ref().ok_or_else(stopped)?.send(Job { function, arguments, reply }).map_err(|_| stopped())?; + answer.recv().map_err(|_| stopped())? + } + + /// Validates and transpiles a package. The error carries the engine's error kind. + pub fn prepare(&self, files: &dyn PackageFiles, options: &PrepareOptions<'_>) -> Result { + let mut text = Object::new(); + let mut binaries = Vec::new(); + for path in files.paths() { + match is_text_file(&path).then(|| files.read_text(&path)).flatten() { + Some(content) => { + text.insert(path, Value::String(content)); + } + None => binaries.push(Value::String(path)), + } + } + let mut job = json!({ "files": text, "binaries": binaries, "prelude": options.prelude }); + for (name, value) in [("entry", options.entry), ("actionID", options.action_id), ("callFunction", options.call_function)] { + if let Some(value) = value { + job[name] = Value::String(value.to_owned()); + } + } + let output = self.call("__swPrepare", vec![stringify(&job)]).map_err(|error| { + RuntimeError::new(if error.interrupted { "resourceLimit" } else { "internalError" }, if error.message.is_empty() { "Compiler failed".to_owned() } else { error.message }) + })?; + let prepared = parse_object(&output.ok_or_else(|| RuntimeError::internal("Compiler returned nothing"))?).map_err(RuntimeError::internal)?; + if !prepared.opt_bool("ok", false) { + let error = prepared.opt_object("error"); + return Err(RuntimeError { + kind: error.and_then(|error| error.opt_string_or_none("kind")).unwrap_or_else(|| "internalError".into()), + message: error.and_then(|error| error.opt_string_or_none("message")).unwrap_or_else(|| "Package could not be prepared".into()), + }); + } + PreparedPackage::from_json(&prepared) + } + + /// Returns {ok, manifest?, error?, issues[]} from the shared manifest validator. + pub fn validate_manifest(&self, manifest_text: &str, paths: &[String]) -> Result { + let paths_text = stringify(&Value::Array(paths.iter().cloned().map(Value::String).collect())); + self.object("__swValidateManifest", vec![manifest_text.to_owned(), paths_text]) + } + + /// Timeline plan for a `timeline` manifest object: {entries:[{date, environments}], reloadDate}. + pub fn timeline_plan(&self, schedule: &Object, now_milliseconds: i64, system_time_zone: &str) -> Result { + self.object("__swTimeline", vec![crate::json::stringify_object(schedule), now_milliseconds.to_string(), system_time_zone.to_owned()]) + } + + fn object(&self, function: &'static str, arguments: Vec) -> Result { + let output = self.call(function, arguments).map_err(|error| RuntimeError::new(if error.interrupted { "resourceLimit" } else { "internalError" }, error.message))?; + parse_object(&output.ok_or_else(|| RuntimeError::internal("Compiler returned nothing"))?).map_err(RuntimeError::internal) + } +} + +impl Drop for PackageCompiler { + fn drop(&mut self) { + // Closing the queue ends the worker, which releases the engine on its own thread. + self.jobs = None; + if let Some(worker) = self.worker.take() { + let _ = worker.join(); + } + } +} + +fn create_engine(scripts: &SharedScripts) -> Result, JsError> { + let host = |operation: &str, payload: Option<&str>| match operation { + "url.parse" => Ok(parse_url(payload.unwrap_or(""))), + _ => intl::call(operation, payload), + }; + let mut engine = Engine::with_limits(host, COMPILER_MEMORY, COMPILER_JS_STACK)?; + // The prelude captures the native entry point before the host file removes it. + engine.evaluate(COMPILER_PRELUDE, "compiler-prelude.js")?; + for name in ["host.js", "core.js", "prepare.js", "compiler.js"] { + let text = scripts.text(name).map_err(|error| JsError { message: format!("ScriptWidget runtime asset could not be read: {error}"), interrupted: false })?; + engine.evaluate(&text, name)?; + } + Ok(engine) +} + +/// WHATWG components of the absolute URLs the manifest validator inspects, or `None`. +pub(crate) fn parse_url(text: &str) -> Option { + let trimmed = text.trim(); + let url = url::Url::parse(trimmed).ok()?; + let host = url.host_str().filter(|host| !host.is_empty())?; + if matches!(url.scheme(), "mailto" | "urn" | "data" | "javascript" | "file") { + return None; + } + Some(stringify(&json!({ + "href": trimmed, + "protocol": format!("{}:", url.scheme()), + "hostname": host.to_lowercase().trim_start_matches('[').trim_end_matches(']'), + "port": url.port().map(|port| port.to_string()).unwrap_or_default(), + "pathname": if url.path().is_empty() { "/" } else { url.path() }, + "search": url.query().map(|query| format!("?{query}")).unwrap_or_default(), + "hash": url.fragment().map(|fragment| format!("#{fragment}")).unwrap_or_default(), + "username": url.username(), + "password": url.password().unwrap_or(""), + }))) +} + +/// Text files are handed to the compiler; everything else only has to exist. +/// Same list as the Windows and Android hosts. +pub fn is_text_file(path: &str) -> bool { + const EXTENSIONS: [&str; 14] = ["js", "jsx", "mjs", "json", "txt", "md", "csv", "svg", "xml", "htm", "html", "css", "yml", "yaml"]; + path.rsplit_once('.').is_some_and(|(_, extension)| EXTENSIONS.iter().any(|known| extension.eq_ignore_ascii_case(known))) +} + +/// Globals the trusted compiler code expects. JavaScriptCore contexts have a +/// console object, and the shared manifest validator measures UTF-8 lengths +/// with TextEncoder. Package code never runs here. +const COMPILER_PRELUDE: &str = r#" +globalThis.console = { log() {}, info() {}, warn() {}, error() {}, debug() {}, trace() {} }; +{ + const native = globalThis.__swNative; + globalThis.URL = class URL { + constructor(text) { + const parsed = native("url.parse", String(text)); + if (parsed === null) throw new TypeError("Invalid URL"); + Object.assign(this, JSON.parse(parsed)); + } + toString() { return this.href; } + }; +} +globalThis.TextEncoder = class TextEncoder { + encode(text) { + const bytes = []; + for (const character of String(text ?? "")) { + const code = character.codePointAt(0); + if (code < 0x80) bytes.push(code); + else if (code < 0x800) bytes.push(0xc0 | (code >> 6), 0x80 | (code & 63)); + else if (code < 0x10000) { + const safe = code >= 0xd800 && code <= 0xdfff ? 0xfffd : code; + bytes.push(0xe0 | (safe >> 12), 0x80 | ((safe >> 6) & 63), 0x80 | (safe & 63)); + } else bytes.push(0xf0 | (code >> 18), 0x80 | ((code >> 12) & 63), 0x80 | ((code >> 6) & 63), 0x80 | (code & 63)); + } + return new Uint8Array(bytes); + } +}; +"#; + +#[cfg(test)] +mod tests { + use super::{is_text_file, parse_url}; + + #[test] + fn recognizes_text_files_by_extension() { + for path in ["main.jsx", "lib/util.JS", "data.json", "a.b.yaml", "page.htm", "notes.md"] { + assert!(is_text_file(path), "{path}"); + } + for path in ["image.png", "README", "archive.json.gz", "jsx"] { + assert!(!is_text_file(path), "{path}"); + } + } + + #[test] + fn url_components_match_what_the_validator_reads() { + let parsed: serde_json::Value = serde_json::from_str(&parse_url(" https://User:pw@API.Example.com:8443/v1/items?x=1#top ").unwrap()).unwrap(); + assert_eq!(parsed["href"], "https://User:pw@API.Example.com:8443/v1/items?x=1#top"); + assert_eq!(parsed["protocol"], "https:"); + assert_eq!(parsed["hostname"], "api.example.com"); + assert_eq!(parsed["port"], "8443"); + assert_eq!(parsed["pathname"], "/v1/items"); + assert_eq!(parsed["search"], "?x=1"); + assert_eq!(parsed["hash"], "#top"); + assert_eq!(parsed["username"], "User"); + assert_eq!(parsed["password"], "pw"); + let plain: serde_json::Value = serde_json::from_str(&parse_url("https://example.com").unwrap()).unwrap(); + assert_eq!((plain["port"].as_str(), plain["pathname"].as_str(), plain["search"].as_str()), (Some(""), Some("/"), Some(""))); + assert_eq!(parse_url("http://[::1]/").map(|text| serde_json::from_str::(&text).unwrap()["hostname"].clone()), Some("::1".into())); + for text in ["", "example.com", "/relative", "mailto:a@example.com", "file:///etc/passwd", "javascript:alert(1)", "data:text/plain,hi"] { + assert_eq!(parse_url(text), None, "{text}"); + } + } +} diff --git a/Linux/crates/sw-core/src/data_paths.rs b/Linux/crates/sw-core/src/data_paths.rs new file mode 100644 index 00000000..cf44c326 --- /dev/null +++ b/Linux/crates/sw-core/src/data_paths.rs @@ -0,0 +1,170 @@ +//! Where ScriptWidget keeps its data. The Linux counterpart of +//! `Windows/src/ScriptWidget.Core/Hosting/DataPaths.cs`, following the XDG +//! Base Directory Specification. +//! +//! - Data: `$XDG_DATA_HOME/scriptwidget`, or `~/.local/share/scriptwidget`. +//! - Cache: `$XDG_CACHE_HOME/scriptwidget`, or `~/.cache/scriptwidget`. +//! +//! As the specification requires, a variable that is unset, empty or not an +//! absolute path is ignored. `SCRIPTWIDGET_DATA` names one directory that +//! holds everything, the cache included, as on Windows; it is for tests and +//! for running a second, separate instance. + +use std::ffi::OsString; +use std::io; +use std::os::unix::fs::DirBuilderExt; +use std::path::{Path, PathBuf}; + +const APPLICATION: &str = "scriptwidget"; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct DataPaths { + root: PathBuf, + cache: PathBuf, +} + +impl DataPaths { + /// The directories for this process: `root` when given, otherwise taken + /// from the environment. The data and cache directories are created, + /// readable by the user only, when they do not exist. + pub fn new(root: Option<&Path>) -> io::Result { + let paths = match root { + Some(root) => Self::at(root)?, + None => Self::locate(|name| std::env::var_os(name))?, + }; + for directory in [&paths.root, &paths.cache] { + std::fs::DirBuilder::new().recursive(true).mode(0o700).create(directory)?; + } + Ok(paths) + } + + /// Everything, the cache included, below one directory. + pub fn at(root: &Path) -> io::Result { + let root = std::path::absolute(root)?; + Ok(Self { cache: root.join("Cache"), root }) + } + + /// The directories an environment asks for; nothing is created. + /// `variable` returns the value of an environment variable. + pub fn locate(variable: impl Fn(&str) -> Option) -> io::Result { + if let Some(root) = variable("SCRIPTWIDGET_DATA").filter(|value| !value.is_empty()) { + return Self::at(Path::new(&root)); + } + let absolute = |name: &str| variable(name).map(PathBuf::from).filter(|path| path.is_absolute()); + let home = absolute("HOME"); + let base = |name: &str, fallback: &str| { + absolute(name) + .or_else(|| home.as_ref().map(|home| home.join(fallback))) + .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, format!("neither {name} nor HOME names an absolute directory"))) + }; + Ok(Self { root: base("XDG_DATA_HOME", ".local/share")?.join(APPLICATION), cache: base("XDG_CACHE_HOME", ".cache")?.join(APPLICATION) }) + } + + pub fn root(&self) -> &Path { + &self.root + } + + /// Installed packages, one folder each. + pub fn packages(&self) -> PathBuf { + self.root.join("Packages") + } + + /// Where packages are prepared before they are published into + /// [`DataPaths::packages`] with a rename. Windows keeps this in its cache + /// directory; here it is next to the packages, because `~/.cache` may be + /// another filesystem (a separate subvolume or a tmpfs) and a rename + /// cannot cross filesystems. + pub fn package_staging(&self) -> PathBuf { + self.root.join("Staging") + } + + /// Package-scoped `$storage` files. + pub fn storage(&self) -> PathBuf { + self.root.join("Storage") + } + + pub fn plugins(&self) -> PathBuf { + self.root.join("Plugins") + } + + /// Compiled artifacts, remote images and other data that can be rebuilt. + pub fn cache(&self) -> &Path { + &self.cache + } + + /// The widgets placed on the desktop. + pub fn widgets(&self) -> PathBuf { + self.root.join("widgets.json") + } + + pub fn settings(&self) -> PathBuf { + self.root.join("settings.json") + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn located(variables: &[(&str, &str)]) -> io::Result { + DataPaths::locate(|name| variables.iter().find(|(key, _)| *key == name).map(|(_, value)| OsString::from(value))) + } + + #[test] + fn uses_the_xdg_directories() { + let paths = located(&[("HOME", "/home/u"), ("XDG_DATA_HOME", "/data"), ("XDG_CACHE_HOME", "/cache")]).unwrap(); + assert_eq!(paths.root(), Path::new("/data/scriptwidget")); + assert_eq!(paths.cache(), Path::new("/cache/scriptwidget")); + assert_eq!(paths.packages(), Path::new("/data/scriptwidget/Packages")); + assert_eq!(paths.package_staging(), Path::new("/data/scriptwidget/Staging")); + assert_eq!(paths.storage(), Path::new("/data/scriptwidget/Storage")); + assert_eq!(paths.plugins(), Path::new("/data/scriptwidget/Plugins")); + assert_eq!(paths.widgets(), Path::new("/data/scriptwidget/widgets.json")); + assert_eq!(paths.settings(), Path::new("/data/scriptwidget/settings.json")); + } + + #[test] + fn falls_back_to_the_home_directory() { + let paths = located(&[("HOME", "/home/u")]).unwrap(); + assert_eq!(paths.root(), Path::new("/home/u/.local/share/scriptwidget")); + assert_eq!(paths.cache(), Path::new("/home/u/.cache/scriptwidget")); + } + + #[test] + fn ignores_relative_and_empty_values() { + let paths = located(&[("HOME", "/home/u"), ("XDG_DATA_HOME", "relative/data"), ("XDG_CACHE_HOME", "")]).unwrap(); + assert_eq!(paths.root(), Path::new("/home/u/.local/share/scriptwidget")); + assert_eq!(paths.cache(), Path::new("/home/u/.cache/scriptwidget")); + let mixed = located(&[("HOME", "/home/u"), ("XDG_DATA_HOME", "./data"), ("XDG_CACHE_HOME", "/cache")]).unwrap(); + assert_eq!(mixed.root(), Path::new("/home/u/.local/share/scriptwidget")); + assert_eq!(mixed.cache(), Path::new("/cache/scriptwidget")); + } + + #[test] + fn fails_without_any_absolute_base() { + assert!(located(&[]).is_err()); + assert!(located(&[("HOME", "home/u")]).is_err()); + assert!(located(&[("HOME", ""), ("XDG_DATA_HOME", "/data")]).is_err(), "the cache has no base"); + let paths = located(&[("XDG_DATA_HOME", "/data"), ("XDG_CACHE_HOME", "/cache")]).unwrap(); + assert_eq!(paths.root(), Path::new("/data/scriptwidget")); + } + + #[test] + fn one_directory_can_hold_everything() { + let paths = located(&[("HOME", "/home/u"), ("XDG_DATA_HOME", "/data"), ("SCRIPTWIDGET_DATA", "/srv/sw")]).unwrap(); + assert_eq!(paths.root(), Path::new("/srv/sw")); + assert_eq!(paths.cache(), Path::new("/srv/sw/Cache")); + assert_eq!(located(&[("HOME", "/home/u"), ("SCRIPTWIDGET_DATA", "")]).unwrap().root(), Path::new("/home/u/.local/share/scriptwidget")); + } + + #[test] + fn creates_private_directories() { + use std::os::unix::fs::MetadataExt; + let root = std::env::temp_dir().join(format!("scriptwidget-data-paths-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + let paths = DataPaths::new(Some(&root.join("nested"))).unwrap(); + assert_eq!(std::fs::metadata(paths.root()).unwrap().mode() & 0o777, 0o700); + assert_eq!(std::fs::metadata(paths.cache()).unwrap().mode() & 0o777, 0o700); + std::fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/Linux/crates/sw-core/src/executor.rs b/Linux/crates/sw-core/src/executor.rs new file mode 100644 index 00000000..efddfde2 --- /dev/null +++ b/Linux/crates/sw-core/src/executor.rs @@ -0,0 +1,424 @@ +//! Runs one prepared package in a fresh QuickJS context and returns its +//! element tree. Ported from +//! `Windows/src/ScriptWidget.Core/Runtime/ScriptExecutor.cs`. + +use std::cell::RefCell; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; +use std::sync::mpsc::{self, Sender}; +use std::time::{Duration, Instant}; + +use serde_json::{Value, json}; +use sw_quickjs::{Engine, HostResult, JS_THREAD_STACK, JsError, run_on_js_thread}; + +use crate::compiler::PreparedPackage; +use crate::intl; +use crate::json::{Object, ObjectExt, parse_object, quote, stringify}; +use crate::package_files::{PackageFiles, normalize_package_path}; +use crate::shared_scripts::SharedScripts; + +/// Name the "not supported" messages use for this host. +pub const PLATFORM_NAME: &str = "Linux"; +/// The execution limit of every platform. +pub const DEFAULT_TIMEOUT_MILLISECONDS: u64 = 5000; +const INTERRUPT_GRACE_MILLISECONDS: u64 = 400; +const MAXIMUM_NATIVE_LOGS: usize = 500; + +/// Everything one execution may touch. +pub struct ExecutionRequest<'a> { + pub prepared: &'a PreparedPackage, + pub files: &'a dyn PackageFiles, + /// `$getenv` values, in order; later entries with the same name win. + pub environments: Vec<(String, String)>, + pub dark_mode: bool, + pub device: Option, + pub system: Option, + /// Preview simulations for the shared kernel; production executions leave this empty. + pub fixtures: Object, + pub modules: Vec>, + pub timeout_milliseconds: u64, + /// Package identity for storage namespaces and diagnostics. + pub package_id: String, + /// Budgets and cancellation shared by every native call of this execution. + pub session: Arc, +} + +impl<'a> ExecutionRequest<'a> { + pub fn new(prepared: &'a PreparedPackage, files: &'a dyn PackageFiles) -> Self { + Self { + prepared, + files, + environments: vec![("widget-size".into(), "medium".into()), ("widget-param".into(), String::new())], + dark_mode: false, + device: None, + system: None, + fixtures: Object::new(), + modules: Vec::new(), + timeout_milliseconds: DEFAULT_TIMEOUT_MILLISECONDS, + package_id: String::new(), + session: Arc::new(ExecutionSession::default()), + } + } + + pub fn environment(&self, name: &str) -> Option<&str> { + self.environments.iter().rev().find(|(key, _)| key == name).map(|(_, value)| value.as_str()) + } +} + +#[derive(Debug, Default)] +pub struct ExecutionSession { + network_requests: AtomicU32, + data_queries: AtomicU32, + read_appearance: AtomicBool, + finished: AtomicBool, +} + +impl ExecutionSession { + /// Counts one more network request and returns the new total. + pub fn add_network_request(&self) -> u32 { + self.network_requests.fetch_add(1, Ordering::SeqCst) + 1 + } + + pub fn network_requests(&self) -> u32 { + self.network_requests.load(Ordering::SeqCst) + } + + pub fn add_data_query(&self) -> u32 { + self.data_queries.fetch_add(1, Ordering::SeqCst) + 1 + } + + pub fn data_queries(&self) -> u32 { + self.data_queries.load(Ordering::SeqCst) + } + + /// True once the script asked whether dark mode is on: its output may differ between themes. + pub fn read_appearance(&self) -> bool { + self.read_appearance.load(Ordering::SeqCst) + } + + /// True when the execution ended; in-flight native work should stop. + pub fn finished(&self) -> bool { + self.finished.load(Ordering::SeqCst) + } + + /// Ends the session: results of native work that is still in flight are dropped. + pub fn finish(&self) { + self.finished.store(true, Ordering::SeqCst); + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ExecutionError { + pub kind: String, + pub message: String, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct LogEntry { + pub level: String, + pub message: String, +} + +#[derive(Debug, Clone, Default)] +pub struct ExecutionResult { + /// Canonical tree JSON (see `encodeElement` in the shared kernel), or `None` on error. + pub tree: Option, + pub error: Option, + pub logs: Vec, + pub diagnostics: Vec, + pub elapsed: Duration, +} + +/// Completes an asynchronous native operation, from any thread. Dropping it +/// without an answer rejects the operation, so a script never waits for a +/// result that cannot come. +pub struct Completion { + id: i64, + delivery: Option)>>, + session: Arc, +} + +impl Completion { + /// A completion outside an execution, for hosts and tests that call a + /// module directly: the answer (operation id 0, success, JSON text) + /// arrives on the returned channel, unless `session` has finished. + pub fn detached(session: Arc) -> (Self, mpsc::Receiver<(i64, bool, Option)>) { + let (delivery, answers) = mpsc::channel(); + (Self { id: 0, delivery: Some(delivery), session }, answers) + } + + /// `json` is the JSON text of the value, or of the rejection reason. + pub fn complete(mut self, ok: bool, json: Option) { + self.deliver(ok, json); + } + + fn deliver(&mut self, ok: bool, json: Option) { + if let Some(delivery) = self.delivery.take() + && !self.session.finished() + { + // The receiver is gone once the execution has ended; the answer is then of no use. + let _ = delivery.send((self.id, ok, json)); + } + } +} + +impl Drop for Completion { + fn drop(&mut self) { + self.deliver(false, Some(quote("Native operation ended without a result"))); + } +} + +/// A group of native operations exposed to package code through the host +/// bridge. Policy (manifest permissions, budgets) is enforced here and in the +/// JavaScript installers, never by trusting package code. +pub trait NativeModule: Send + Sync { + fn handles(&self, operation: &str) -> bool; + + /// Script globals this module implements, such as `$weather`; others report "not supported on Linux". + fn apis(&self) -> &[&'static str] { + &[] + } + + /// Synchronous operation. Returns JSON text or `None`; an error is thrown in JavaScript. + fn call(&self, _request: &ExecutionRequest<'_>, operation: &str, _payload: Option<&str>) -> HostResult { + Err(format!("Unsupported operation {operation}")) + } + + /// Asynchronous operation: answer through `done`, now or later from any thread. + fn start(&self, _request: &ExecutionRequest<'_>, operation: &str, _payload: Option<&Object>, done: Completion) { + done.complete(false, Some(quote(&format!("Unsupported operation {operation}")))); + } +} + +/// Runs prepared packages. Every execution gets a fresh engine with the +/// default limits: 64 MB of memory, a bounded stack and the request's deadline. +pub struct ScriptExecutor { + scripts: Arc, +} + +impl ScriptExecutor { + pub fn new(scripts: Arc) -> Self { + Self { scripts } + } + + pub fn execute(&self, request: &ExecutionRequest<'_>) -> ExecutionResult { + let started = Instant::now(); + let mut result = run_on_js_thread("scriptwidget-js", JS_THREAD_STACK, || self.run(request)); + result.elapsed = started.elapsed(); + result + } + + fn run(&self, request: &ExecutionRequest<'_>) -> ExecutionResult { + let (deliver, completions) = mpsc::channel::<(i64, bool, Option)>(); + let finished: RefCell> = RefCell::new(None); + let native_logs: RefCell> = RefCell::new(Vec::new()); + + let host = |operation: &str, payload: Option<&str>| -> HostResult { + match operation { + "finish" => { + *finished.borrow_mut() = payload.map(str::to_owned); + Ok(None) + } + "package.import" => Ok(compiled_import(request, payload)), + "package.read" => Ok(normalize_package_path(payload).and_then(|path| request.files.read_text(&path))), + "package.exists" => Ok(Some(if normalize_package_path(payload).is_some_and(|path| request.files.exists(&path)) { "1" } else { "0" }.into())), + "xml.parse" => Ok(Some(crate::xml::parse(payload.unwrap_or("")))), + "support.script" => Ok(self.scripts.support_script(payload.unwrap_or("")).map(|text| text.to_string())), + "session.appearance" => { + request.session.read_appearance.store(true, Ordering::SeqCst); + Ok(None) + } + "log.log" | "log.error" => { + let mut logs = native_logs.borrow_mut(); + if logs.len() < MAXIMUM_NATIVE_LOGS { + logs.push(LogEntry { level: operation["log.".len()..].to_owned(), message: payload.unwrap_or("").to_owned() }); + } + Ok(None) + } + "async.start" => { + let job = parse_object(payload.ok_or("Missing asynchronous operation")?)?; + let name = job.opt_string("operation"); + let done = Completion { id: job.opt_f64("id") as i64, delivery: Some(deliver.clone()), session: request.session.clone() }; + match request.modules.iter().find(|module| module.handles(&name)) { + Some(module) => module.start(request, &name, job.opt_object("payload"), done), + None => done.complete(false, Some(quote(&format!("Unsupported operation {name}")))), + } + Ok(None) + } + _ if intl::handles(operation) => intl::call(operation, payload), + _ => match request.modules.iter().find(|module| module.handles(operation)) { + Some(module) => module.call(request, operation, payload), + None => Err(format!("Unsupported operation {operation}")), + }, + } + }; + + let deadline = Instant::now() + Duration::from_millis(request.timeout_milliseconds); + let outcome = (|| -> Result<(), JsError> { + let asset = |name: &str| { + self.scripts.text(name).map_err(|error| JsError { message: format!("ScriptWidget runtime asset could not be read: {error}"), interrupted: false }) + }; + let token = random_token()?; + let mut engine = Engine::new(host)?; + // The interrupt deadline is a backstop for synchronous loops; the + // kernel's own timeout reports unresolved asynchronous work first. + engine.set_deadline(request.timeout_milliseconds.saturating_add(INTERRUPT_GRACE_MILLISECONDS)); + for name in self.scripts.host_files() { + engine.evaluate(&asset(name)?, name)?; + } + engine.evaluate(&asset("kernel.js")?, "kernel.js")?; + let mut supported: Vec<&str> = Vec::new(); + for api in request.modules.iter().flat_map(|module| module.apis().iter().copied()) { + if !supported.contains(&api) { + supported.push(api); + } + } + let job = json!({ + "config": stringify(&self.configuration(request)), + "code": request.prepared.code, + "timeoutMilliseconds": request.timeout_milliseconds, + "supportedAPIs": supported, + "platformName": PLATFORM_NAME, + }); + engine.call("__swBoot", &[Some(&token), Some(&stringify(&job))])?; + + while finished.borrow().is_none() { + engine.run_jobs(10_000)?; + if finished.borrow().is_some() { + break; + } + if engine.call(&token, &[Some("pump")])?.as_deref() == Some("1") { + continue; + } + let remaining = deadline.saturating_duration_since(Instant::now()); + let completion = if remaining.is_zero() { None } else { completions.recv_timeout(remaining).ok() }; + match completion { + None => { + engine.call(&token, &[Some("timeout")])?; + engine.run_jobs(10_000)?; + finished.borrow_mut().get_or_insert_with(timeout_failure); + } + Some((id, ok, json)) => { + engine.call(&token, &[Some("resolve"), Some(&id.to_string()), Some(if ok { "1" } else { "0" }), json.as_deref()])?; + } + } + } + Ok(()) + })(); + request.session.finished.store(true, Ordering::SeqCst); + + if let Err(error) = outcome { + finished.borrow_mut().get_or_insert_with(|| { + if error.interrupted { + timeout_failure() + } else if error.message.to_lowercase().contains("out of memory") { + failure("resourceLimit", MEMORY_BUDGET_MESSAGE) + } else { + failure("internalError", if error.message.is_empty() { "Execution failed" } else { &error.message }) + } + }); + } else if finished.borrow().as_deref().is_some_and(reports_exhausted_heap) { + // The kernel caught what the engine threw when the heap ran out, and reported it as the + // script's own exception. Which of the two paths a script takes depends on where the + // failing allocation falls; both are the same failure. + *finished.borrow_mut() = Some(failure("resourceLimit", MEMORY_BUDGET_MESSAGE)); + } + let result_text = finished.into_inner().unwrap_or_else(|| failure("internalError", "Execution failed")); + decode(&result_text, native_logs.into_inner()) + } + + fn configuration(&self, request: &ExecutionRequest<'_>) -> Value { + let prepared = request.prepared; + let mut environments = Object::new(); + for (name, value) in prepared.timeline_environment.iter().chain(&request.environments) { + environments.insert(name.clone(), Value::String(value.clone())); + } + if let Some(action) = &prepared.action_id { + if request.environment("action-id").is_none() { + environments.insert("action-id".into(), Value::String(action.clone())); + } + environments.insert("widget-size".into(), Value::String("action".into())); + } + let mut config = json!({ + "contract": self.scripts.contract(), + "environments": environments, + "actionID": prepared.action_id, + "liveActivityPresentation": matches!(request.environment("widget-size"), Some("live-activity" | "dynamic-island")), + "preview": { "appearance": if request.dark_mode { "dark" } else { "light" } }, + "fixtures": request.fixtures, + "manifest": prepared.manifest, + "hasManifest": prepared.has_manifest, + "packageID": request.package_id, + }); + if let Some(device) = &request.device { + config["device"] = Value::Object(device.clone()); + } + if let Some(system) = &request.system { + config["system"] = Value::Object(system.clone()); + } + config + } +} + +fn compiled_import(request: &ExecutionRequest<'_>, relative_path: Option<&str>) -> Option { + let path = normalize_package_path(relative_path)?; + if !request.files.exists(&path) || request.files.read_text(&path).is_none() { + return None; + } + Some(request.prepared.imports.get(&path).cloned().unwrap_or_else(|| r#"{"ok":false,"kind":"transformError","message":"Not a script file"}"#.into())) +} + +/// Name of the driver function in the execution context. Package code cannot guess it. +fn random_token() -> Result { + let mut bytes = [0u8; 16]; + getrandom::fill(&mut bytes).map_err(|error| JsError { message: format!("No random source: {error}"), interrupted: false })?; + Ok(bytes.iter().fold(String::from("__sw_"), |mut token, byte| { + token.push_str(&format!("{byte:02x}")); + token + })) +} + +const MEMORY_BUDGET_MESSAGE: &str = "Script exceeded the runtime memory budget"; + +/// True for a kernel result whose error is the engine's "out of memory", or +/// the null QuickJS throws when it cannot even allocate that error. A script +/// that throws null itself is indistinguishable from the latter here; telling +/// them apart needs a flag from the engine's allocator. +fn reports_exhausted_heap(result_text: &str) -> bool { + parse_object(result_text).ok().and_then(|result| result.opt_object("error").cloned()).is_some_and(|error| { + let message = error.opt_string("message"); + error.opt_string("kind") == "scriptException" && (message == "null" || message.to_lowercase().contains("out of memory")) + }) +} + +fn timeout_failure() -> String { + failure("resourceLimit", "Script exceeded the 5-second execution limit") +} + +fn failure(kind: &str, message: &str) -> String { + stringify(&json!({ "tree": null, "error": { "kind": kind, "message": message }, "logs": [], "diagnostics": [] })) +} + +fn decode(result_text: &str, native_logs: Vec) -> ExecutionResult { + let json = match parse_object(result_text) { + Ok(json) => json, + Err(message) => { + return ExecutionResult { error: Some(ExecutionError { kind: "internalError".into(), message: format!("Execution result could not be read: {message}") }), logs: native_logs, ..Default::default() }; + } + }; + let error = json.opt_object("error").map(|error| ExecutionError { + kind: error.opt_string_or_none("kind").unwrap_or_else(|| "internalError".into()), + message: error.opt_string("message"), + }); + let logs: Vec = json + .opt_array("logs") + .map(|logs| logs.iter().filter_map(Value::as_object).map(|item| LogEntry { level: item.opt_string("level"), message: item.opt_string("message") }).collect()) + .unwrap_or_default(); + ExecutionResult { + tree: if json.is_null("tree") { None } else { Some(json.opt_string("tree")) }, + error, + logs: if logs.is_empty() { native_logs } else { logs }, + diagnostics: json.opt_array("diagnostics").cloned().unwrap_or_default(), + elapsed: Duration::ZERO, + } +} diff --git a/Linux/crates/sw-core/src/intl.rs b/Linux/crates/sw-core/src/intl.rs new file mode 100644 index 00000000..9389ba70 --- /dev/null +++ b/Linux/crates/sw-core/src/intl.rs @@ -0,0 +1,723 @@ +//! ICU-backed implementation of the Intl subset installed by the QuickJS host +//! file. QuickJS has no Intl; JavaScriptCore on iPhone and the Android and +//! Windows hosts format with ICU as well, so the same skeletons produce +//! matching text for the same locale data. This calls the system ICU through +//! the shim in `native/icu_shim.c`. +//! +//! Ported from `Windows/src/ScriptWidget.Core/Runtime/IntlBridge.cs`. + +use std::collections::{HashMap, VecDeque}; +use std::ffi::{CStr, CString, c_char, c_void}; +use std::ptr; +use std::sync::{Mutex, RwLock}; + +use serde_json::{Value, json}; + +use crate::json::{Object, ObjectExt, as_text, parse_object, stringify, stringify_object}; + +static LOCALE_OVERRIDE: RwLock> = RwLock::new(None); +static TIME_ZONE_OVERRIDE: RwLock> = RwLock::new(None); + +/// Overrides the system locale; used by deterministic tests. +pub fn set_locale_override(tag: Option<&str>) { + *LOCALE_OVERRIDE.write().unwrap_or_else(|poisoned| poisoned.into_inner()) = tag.map(str::to_owned); +} + +/// Overrides the zone Intl formats in and the zone of JavaScript's local +/// time, for the whole process; used by deterministic tests. +/// +/// # Safety +/// Same contract as [`sw_quickjs::set_process_time_zone`], which this calls: +/// no other thread may use the environment or local time meanwhile. +pub unsafe fn set_time_zone_override(iana_name: Option<&str>) { + *TIME_ZONE_OVERRIDE.write().unwrap_or_else(|poisoned| poisoned.into_inner()) = iana_name.map(str::to_owned); + // SAFETY: guaranteed by the caller. + unsafe { sw_quickjs::set_process_time_zone(iana_name) }; +} + +/// BCP 47 tag of the user's locale: the override, or the POSIX locale of the +/// process ("en_US.UTF-8" is "en-US"), or "en-US". +pub fn default_locale_tag() -> String { + if let Some(tag) = LOCALE_OVERRIDE.read().unwrap_or_else(|poisoned| poisoned.into_inner()).clone() { + return tag; + } + ["LC_ALL", "LC_TIME", "LANG"] + .iter() + .filter_map(|name| std::env::var(name).ok()) + .find(|value| !value.is_empty()) + .and_then(|value| posix_locale_tag(&value)) + .unwrap_or_else(|| "en-US".into()) +} + +fn posix_locale_tag(value: &str) -> Option { + let name = value.split(['.', '@']).next().unwrap_or(""); + if name.is_empty() || name == "C" || name == "POSIX" || !name.chars().all(|character| character.is_ascii_alphanumeric() || character == '_') { + return None; + } + Some(name.replace('_', "-")) +} + +/// IANA name of the zone local time is shown in. +pub fn default_time_zone() -> String { + if let Some(zone) = TIME_ZONE_OVERRIDE.read().unwrap_or_else(|poisoned| poisoned.into_inner()).clone() { + return zone; + } + // SAFETY: the buffer and capacity describe the same memory. + text("Time zone", |buffer, capacity, status| unsafe { icu::sw_ucal_getDefaultTimeZone(buffer, capacity, status) }) + .ok() + .map(|zone| String::from_utf16_lossy(&zone)) + .filter(|zone| !zone.is_empty() && zone != "Etc/Unknown") + .unwrap_or_else(|| "UTC".into()) +} + +/// Version of the ICU library in use, such as "78.3". +pub fn icu_version() -> String { + let mut version = [0u8; 4]; + // SAFETY: the function writes four bytes. + unsafe { icu::sw_u_getVersion(version.as_mut_ptr()) }; + format!("{}.{}", version[0], version[1]) +} + +pub fn handles(operation: &str) -> bool { + operation.starts_with("intl.") +} + +/// Formatters are cached and ICU objects are not shared between threads at +/// the same time: every operation runs under this lock. +static STATE: Mutex = Mutex::new(State { formats: None, order: VecDeque::new() }); + +struct State { + /// Building an ICU formatter is far more expensive than using one; planners format thousands of dates. + formats: Option>, + order: VecDeque, +} + +struct DateFormat(*mut c_void); + +// SAFETY: a UDateFormat may be used from any thread, one at a time; `STATE`'s lock guarantees that. +unsafe impl Send for DateFormat {} + +impl Drop for DateFormat { + fn drop(&mut self) { + // SAFETY: opened by udat_open and closed once. + unsafe { icu::sw_udat_close(self.0) } + } +} + +const FORMAT_CACHE_SIZE: usize = 32; + +/// Answers an `intl.*` host operation. `Ok(None)` means the operation is not +/// one of this module's; an error is thrown in JavaScript. +pub fn call(operation: &str, payload: Option<&str>) -> Result, String> { + match operation { + "intl.locale" => return Ok(Some(default_locale_tag())), + "intl.timeZone" => return Ok(Some(default_time_zone())), + "intl.validZone" => return Ok(Some(if is_valid_zone(payload.unwrap_or("")) { "1" } else { "0" }.into())), + _ => {} + } + if !handles(operation) { + return Ok(None); + } + let request = parse_object(payload.ok_or("Missing Intl request")?)?; + let mut state = STATE.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + match operation { + "intl.date" => format_date(&mut state, &request).map(Some), + "intl.dateParts" => date_parts(&mut state, &request).map(|parts| Some(stringify(&parts))), + "intl.number" => format_number(&request).map(Some), + "intl.plural" => plural(&request).map(Some), + "intl.relative" => relative(&request).map(Some), + "intl.compare" => compare(&request).map(Some), + "intl.list" => list(&request).map(Some), + _ => Ok(None), + } +} + +// MARK: - Locales and zones + +/// ICU locale ID ("en_US") for a BCP 47 tag ("en-US"). +fn locale(tag: &str) -> CString { + let fallback = || CString::from(c"en_US"); + let Ok(input) = CString::new(tag.replace('_', "-")) else { return fallback() }; + let mut output = [0 as c_char; 160]; + let mut status = 0; + // SAFETY: `input` is NUL-terminated and the capacity leaves room for the terminator. + let length = unsafe { icu::sw_uloc_forLanguageTag(input.as_ptr(), output.as_mut_ptr(), output.len() as i32 - 1, ptr::null_mut(), &mut status) }; + if status > 0 || length <= 0 { + return fallback(); + } + // SAFETY: the last element is never written, so the buffer is NUL-terminated. + unsafe { CStr::from_ptr(output.as_ptr()) }.to_owned() +} + +fn required_locale(request: &Object) -> Result { + request.opt_string_or_none("locale").ok_or_else(|| "Missing locale".into()) +} + +fn is_valid_zone(id: &str) -> bool { + if id.eq_ignore_ascii_case("UTC") || id.eq_ignore_ascii_case("GMT") { + return true; + } + let units = utf16(id); + if units.is_empty() || units.len() > 100 { + return false; + } + let mut buffer = [0u16; 128]; + let mut status = 0; + let mut system = 0i8; + // SAFETY: lengths and capacities describe the buffers passed with them. + unsafe { icu::sw_ucal_getCanonicalTimeZoneID(units.as_ptr(), units.len() as i32, buffer.as_mut_ptr(), buffer.len() as i32, &mut system, &mut status) }; + status <= 0 && system != 0 +} + +fn check(status: i32, what: &str) -> Result<(), String> { + if status > 0 { Err(format!("{what} failed (ICU error {status})")) } else { Ok(()) } +} + +fn utf16(text: &str) -> Vec { + text.encode_utf16().collect() +} + +/// Runs an ICU call that writes UTF-16 into a buffer, growing the buffer when it is too small. +fn text(what: &str, mut fill: impl FnMut(*mut u16, i32, *mut i32) -> i32) -> Result, String> { + let mut capacity = 256usize; + loop { + let mut buffer = vec![0u16; capacity]; + let mut status = 0; + let length = fill(buffer.as_mut_ptr(), capacity as i32, &mut status); + if status == icu::BUFFER_OVERFLOW && capacity < 1 << 20 { + capacity = (length.max(0) as usize + 1).max(capacity * 2); + continue; + } + check(status, what)?; + buffer.truncate((length.max(0) as usize).min(capacity)); + return Ok(buffer); + } +} + +// MARK: - Dates + +fn empty_options() -> Object { + Object::new() +} + +fn options_of(request: &Object) -> Object { + request.opt_object("options").cloned().unwrap_or_else(empty_options) +} + +/// The cached formatter for the request's locale and options. +fn date_format<'state>(state: &'state mut State, request: &Object) -> Result<&'state DateFormat, String> { + let options = options_of(request); + let key = format!("{}|{}|{}", required_locale(request)?, default_time_zone(), stringify_object(&options)); + let State { formats, order } = state; + let formats = formats.get_or_insert_with(HashMap::new); + if formats.contains_key(&key) { + order.retain(|item| item != &key); + } else { + let created = create_date_format(request, &options)?; + formats.insert(key.clone(), created); + if formats.len() > FORMAT_CACHE_SIZE + && let Some(eldest) = order.pop_front() + { + formats.remove(&eldest); + } + } + order.push_back(key.clone()); + Ok(&formats[&key]) +} + +fn create_date_format(request: &Object, options: &Object) -> Result { + let locale = locale(&required_locale(request)?); + let requested = options.opt_string("timeZone"); + let zone = utf16(&if requested.is_empty() { default_time_zone() } else { requested }); + let date_style = style(&options.opt_string("dateStyle")); + let time_style = style(&options.opt_string("timeStyle")); + let mut status = 0; + let format = if date_style.is_some() || time_style.is_some() { + // SAFETY: every pointer is valid for the length passed with it. + unsafe { + icu::sw_udat_open(time_style.unwrap_or(icu::STYLE_NONE), date_style.unwrap_or(icu::STYLE_NONE), locale.as_ptr(), zone.as_ptr(), zone.len() as i32, ptr::null(), 0, &mut status) + } + } else { + let skeleton = utf16(&skeleton(options)); + // SAFETY: `locale` is NUL-terminated. + let generator = unsafe { icu::sw_udatpg_open(locale.as_ptr(), &mut status) }; + check(status, "Date pattern generator")?; + // SAFETY: the generator is open; lengths describe the buffers. + let pattern = text("Date pattern", |buffer, capacity, error| unsafe { + icu::sw_udatpg_getBestPattern(generator, skeleton.as_ptr(), skeleton.len() as i32, buffer, capacity, error) + }); + // SAFETY: opened above, closed once. + unsafe { icu::sw_udatpg_close(generator) }; + let pattern = apply_hour_width(&pattern?, &options.opt_string("hour")); + // SAFETY: every pointer is valid for the length passed with it. + unsafe { + icu::sw_udat_open(icu::STYLE_PATTERN, icu::STYLE_PATTERN, locale.as_ptr(), zone.as_ptr(), zone.len() as i32, pattern.as_ptr(), pattern.len() as i32, &mut status) + } + }; + if status > 0 || format.is_null() { + if !format.is_null() { + drop(DateFormat(format)); + } + return Err(format!("Date format failed (ICU error {status})")); + } + Ok(DateFormat(format)) +} + +/// ECMA-402 keeps the requested hour width even when the locale pattern for +/// the skeleton uses another one ("2-digit" gives "08:00 PM" in en-US). +fn apply_hour_width(pattern: &[u16], hour: &str) -> Vec { + let width = match hour { + "2-digit" => 2, + "numeric" => 1, + _ => return pattern.to_vec(), + }; + let is_hour = |unit: u16| b"hHkK".iter().any(|letter| unit == u16::from(*letter)); + let mut result = Vec::with_capacity(pattern.len() + 2); + let mut index = 0; + let mut quoted = false; + while index < pattern.len() { + let unit = pattern[index]; + if unit == u16::from(b'\'') { + quoted = !quoted; + } else if !quoted && is_hour(unit) { + while index < pattern.len() && pattern[index] == unit { + index += 1; + } + result.extend(std::iter::repeat_n(unit, width)); + continue; + } + result.push(unit); + index += 1; + } + result +} + +fn style(name: &str) -> Option { + match name { + "full" => Some(0), + "long" => Some(1), + "medium" => Some(2), + "short" => Some(3), + _ => None, + } +} + +/// Maps ECMA-402 date options to an ICU skeleton. +fn skeleton(options: &Object) -> String { + let mut text = String::new(); + let repeat = |text: &mut String, letter: char, count: usize| text.extend(std::iter::repeat_n(letter, count)); + let width = |text: &mut String, name: &str, letter: char| match options.opt_string(name).as_str() { + "narrow" => repeat(text, letter, 5), + "short" => repeat(text, letter, 3), + "long" => repeat(text, letter, 4), + "numeric" => repeat(text, letter, 1), + "2-digit" => repeat(text, letter, 2), + _ => {} + }; + let named = |text: &mut String, name: &str, narrow: &str, short: &str, long: &str| match options.opt_string(name).as_str() { + "narrow" => text.push_str(narrow), + "short" => text.push_str(short), + "long" => text.push_str(long), + _ => {} + }; + named(&mut text, "era", "GGGGG", "G", "GGGG"); + width(&mut text, "year", 'y'); + width(&mut text, "month", 'M'); + named(&mut text, "weekday", "EEEEE", "EEE", "EEEE"); + width(&mut text, "day", 'd'); + let hour = options.opt_string("hour"); + if !hour.is_empty() { + let letter = if options.has("hourCycle") { + match options.opt_string("hourCycle").as_str() { + "h11" => 'K', + "h12" => 'h', + "h23" => 'H', + "h24" => 'k', + _ => 'j', + } + } else if options.has("hour12") { + if options.opt_bool("hour12", false) { 'h' } else { 'H' } + } else { + 'j' + }; + repeat(&mut text, letter, if hour == "2-digit" { 2 } else { 1 }); + } + named(&mut text, "dayPeriod", "BBBBB", "B", "BBBB"); + let minute = options.opt_string("minute"); + if !minute.is_empty() { + text.push_str(if minute == "2-digit" { "mm" } else { "m" }); + } + let second = options.opt_string("second"); + if !second.is_empty() { + text.push_str(if second == "2-digit" { "ss" } else { "s" }); + } + let fraction = options.opt_i32("fractionalSecondDigits", 0); + if (1..=3).contains(&fraction) { + repeat(&mut text, 'S', fraction as usize); + } + text.push_str(match options.opt_string("timeZoneName").as_str() { + "short" => "z", + "long" => "zzzz", + "shortOffset" => "O", + "longOffset" => "OOOO", + "shortGeneric" => "v", + "longGeneric" => "vvvv", + _ => "", + }); + text +} + +fn time(request: &Object) -> Result { + let time = request.opt_f64("time"); + if time.is_nan() { Err("Invalid time value".into()) } else { Ok(time.trunc()) } +} + +fn format_date(state: &mut State, request: &Object) -> Result { + let time = time(request)?; + let format = date_format(state, request)?; + // SAFETY: the formatter is open and used under the state lock. + let formatted = text("Date format", |buffer, capacity, status| unsafe { icu::sw_udat_format(format.0, time, buffer, capacity, status) })?; + Ok(normalize_spaces(&formatted)) +} + +/// ICU 72+ separates the day period with U+202F; JavaScriptCore emits a plain space for compatibility. +fn normalize_spaces(text: &[u16]) -> String { + String::from_utf16_lossy(text).replace('\u{202f}', " ") +} + +/// ECMA-402 part type of a UDateFormatField. +fn part_name(field: i32) -> &'static str { + match field { + 0 => "era", + 1 | 18 | 20 | 30 | 34 => "year", + 2 | 26 => "month", + 3 => "day", + 9 | 19 | 25 => "weekday", + 4 | 5 | 15 | 16 => "hour", + 6 => "minute", + 7 => "second", + 8 => "fractionalSecond", + 14 | 35 | 36 => "dayPeriod", + 17 | 23 | 24 | 29 | 31 | 32 | 33 => "timeZoneName", + _ => "literal", + } +} + +fn date_parts(state: &mut State, request: &Object) -> Result { + let time = time(request)?; + let format = date_format(state, request)?; + let mut status = 0; + // SAFETY: closed below on every path. + let iterator = unsafe { icu::sw_ufieldpositer_open(&mut status) }; + check(status, "Date parts")?; + // SAFETY: formatter and iterator are open. + let formatted = text("Date parts", |buffer, capacity, error| unsafe { icu::sw_udat_formatForFields(format.0, time, buffer, capacity, iterator, error) }); + // The iterator holds the fields of the last successful call. + let mut fields: Vec<(i32, i32, i32)> = Vec::new(); + if formatted.is_ok() { + loop { + let (mut start, mut end) = (0, 0); + // SAFETY: the iterator is open. + let field = unsafe { icu::sw_ufieldpositer_next(iterator, &mut start, &mut end) }; + if field < 0 { + break; + } + fields.push((start, end, field)); + } + } + // SAFETY: opened above, closed once. + unsafe { icu::sw_ufieldpositer_close(iterator) }; + let formatted = formatted?; + fields.sort_by(|left, right| left.0.cmp(&right.0).then(right.1.cmp(&left.1))); + + let mut parts = Vec::new(); + let mut add = |kind: &str, start: usize, end: usize| { + if end > start { + parts.push(json!({ "type": kind, "value": normalize_spaces(&formatted[start..end]) })); + } + }; + let mut index = 0usize; + for (start, end, field) in fields { + if start < 0 || (start as usize) < index || end as usize > formatted.len() || end < start { + continue; + } + let (start, end) = (start as usize, end as usize); + add("literal", index, start); + add(part_name(field), start, end); + index = end; + } + add("literal", index, formatted.len()); + Ok(Value::Array(parts)) +} + +// MARK: - Numbers + +fn format_number(request: &Object) -> Result { + let locale = locale(&required_locale(request)?); + let options = options_of(request); + let value = match request.opt_string("value").as_str() { + "Infinity" => f64::INFINITY, + "-Infinity" => f64::NEG_INFINITY, + // Rust would also read "inf" and "nan"; JavaScript's String(Number) never produces them. + raw if raw.bytes().all(|byte| byte.is_ascii_digit() || matches!(byte, b'+' | b'-' | b'.' | b'e' | b'E')) => raw.parse().unwrap_or(f64::NAN), + _ => f64::NAN, + }; + if value.is_nan() { + return Ok("NaN".into()); + } + if value.is_infinite() { + return Ok(if value > 0.0 { "∞" } else { "-∞" }.into()); + } + + let requested = options.opt_string("style"); + let style = if requested.is_empty() { "decimal" } else { requested.as_str() }; + let mut skeleton = String::new(); + let mut default_minimum = 0; + let mut default_maximum = 3; + match style { + "percent" => { + skeleton.push_str("percent scale/100 "); + default_maximum = 0; + } + "currency" => { + let code = options.opt_string("currency"); + let currency = if code.is_empty() { "USD".to_owned() } else { code.to_uppercase() }; + if currency.len() != 3 || !currency.bytes().all(|byte| byte.is_ascii_alphabetic()) { + return Err(format!("Invalid currency code {currency}")); + } + skeleton.push_str(&format!("currency/{currency} ")); + let mut code_units = utf16(¤cy); + code_units.push(0); + let mut status = 0; + // SAFETY: the code is NUL-terminated. + let digits = unsafe { icu::sw_ucurr_getDefaultFractionDigits(code_units.as_ptr(), &mut status) }; + let digits = if status > 0 { 2 } else { digits }; + default_minimum = digits; + default_maximum = digits; + skeleton.push_str(match options.opt_string("currencyDisplay").as_str() { + "code" => "unit-width-iso-code ", + "name" => "unit-width-full-name ", + "narrowSymbol" => "unit-width-narrow ", + _ => "", + }); + } + _ => {} + } + let notation = options.opt_string("notation"); + match notation.as_str() { + "compact" => skeleton.push_str(if options.opt_string("compactDisplay") == "long" { "compact-long " } else { "compact-short " }), + "scientific" => skeleton.push_str("scientific "), + "engineering" => skeleton.push_str("engineering "), + _ => {} + } + let compact = notation == "compact"; + let repeat = |letter: char, count: i32| letter.to_string().repeat(count.max(0) as usize); + if options.has("minimumSignificantDigits") || options.has("maximumSignificantDigits") { + let minimum = options.opt_i32("minimumSignificantDigits", 1).clamp(1, 21); + let maximum = options.opt_i32("maximumSignificantDigits", 21).clamp(minimum, 21); + skeleton.push_str(&format!("{}{} ", repeat('@', minimum), repeat('#', maximum - minimum))); + } else if options.has("minimumFractionDigits") || options.has("maximumFractionDigits") || !compact { + let minimum = options.opt_i32("minimumFractionDigits", default_minimum.min(options.opt_i32("maximumFractionDigits", default_minimum))).clamp(0, 100); + let maximum = options.opt_i32("maximumFractionDigits", default_maximum.max(minimum)).clamp(minimum, 100); + if maximum == 0 { + skeleton.push_str("precision-integer "); + } else { + skeleton.push_str(&format!(".{}{} ", repeat('0', minimum), repeat('#', maximum - minimum))); + } + } + if options.has("minimumIntegerDigits") { + skeleton.push_str(&format!("integer-width/*{} ", repeat('0', options.opt_i32("minimumIntegerDigits", 1).clamp(1, 21)))); + } + if options.has("useGrouping") && !options.opt_bool("useGrouping", true) { + skeleton.push_str("group-off "); + } + skeleton.push_str(match options.opt_string("signDisplay").as_str() { + "always" => "sign-always ", + "never" => "sign-never ", + "exceptZero" => "sign-except-zero ", + _ => "", + }); + + let skeleton = utf16(skeleton.trim_end()); + let mut error = 0; + // SAFETY: lengths describe the buffers; `locale` is NUL-terminated. + let formatter = unsafe { icu::sw_unumf_openForSkeletonAndLocale(skeleton.as_ptr(), skeleton.len() as i32, locale.as_ptr(), &mut error) }; + if error > 0 || formatter.is_null() { + if !formatter.is_null() { + // SAFETY: opened above. + unsafe { icu::sw_unumf_close(formatter) }; + } + return Err(format!("Number format failed (ICU error {error})")); + } + // SAFETY: closed below. + let result = unsafe { icu::sw_unumf_openResult(&mut error) }; + let formatted = (|| { + check(error, "Number format")?; + // The decimal expansion of the double, like BigDecimal(value) on Android. + let digits = format!("{value:.60e}"); + let mut error = 0; + // SAFETY: formatter and result are open; the length describes `digits`. + unsafe { icu::sw_unumf_formatDecimal(formatter, digits.as_ptr().cast(), digits.len() as i32, result, &mut error) }; + check(error, "Number format")?; + // SAFETY: the result is open. + text("Number format", |buffer, capacity, status| unsafe { icu::sw_unumf_resultToString(result, buffer, capacity, status) }) + })(); + // SAFETY: both were opened above and are closed once. + unsafe { + if !result.is_null() { + icu::sw_unumf_closeResult(result); + } + icu::sw_unumf_close(formatter); + } + Ok(String::from_utf16_lossy(&formatted?)) +} + +fn plural(request: &Object) -> Result { + let locale = locale(&required_locale(request)?); + let mut status = 0; + // SAFETY: `locale` is NUL-terminated. + let rules = unsafe { icu::sw_uplrules_openForType(locale.as_ptr(), if request.opt_string("type") == "ordinal" { 1 } else { 0 }, &mut status) }; + check(status, "Plural rules")?; + let value = request.opt_f64("value"); + // SAFETY: the rules are open. + let keyword = text("Plural rules", |buffer, capacity, error| unsafe { icu::sw_uplrules_select(rules, value, buffer, capacity, error) }); + // SAFETY: opened above, closed once. + unsafe { icu::sw_uplrules_close(rules) }; + Ok(String::from_utf16_lossy(&keyword?)) +} + +fn relative(request: &Object) -> Result { + let options = options_of(request); + let style = match options.opt_string("style").as_str() { + "short" => 1, + "narrow" => 2, + _ => 0, + }; + let name = request.opt_string("unit"); + let unit = match name.strip_suffix('s').unwrap_or(&name) { + "year" => 0, + "quarter" => 1, + "month" => 2, + "week" => 3, + "day" => 4, + "hour" => 5, + "minute" => 6, + "second" => 7, + _ => return Err("Invalid unit argument for format()".into()), + }; + let locale = locale(&required_locale(request)?); + let mut status = 0; + // SAFETY: `locale` is NUL-terminated. + let formatter = unsafe { icu::sw_ureldatefmt_open(locale.as_ptr(), style, &mut status) }; + check(status, "Relative time format")?; + let value = request.opt_f64("value"); + let automatic = options.opt_string("numeric") == "auto"; + // SAFETY: the formatter is open. + let formatted = text("Relative time format", |buffer, capacity, error| unsafe { + if automatic { icu::sw_ureldatefmt_format(formatter, value, unit, buffer, capacity, error) } else { icu::sw_ureldatefmt_formatNumeric(formatter, value, unit, buffer, capacity, error) } + }); + // SAFETY: opened above, closed once. + unsafe { icu::sw_ureldatefmt_close(formatter) }; + Ok(String::from_utf16_lossy(&formatted?)) +} + +fn compare(request: &Object) -> Result { + let locale = locale(&required_locale(request)?); + let left = utf16(&request.opt_string("left")); + let right = utf16(&request.opt_string("right")); + let mut status = 0; + // SAFETY: `locale` is NUL-terminated. + let collator = unsafe { icu::sw_ucol_open(locale.as_ptr(), &mut status) }; + check(status, "Collator")?; + // SAFETY: the collator is open; lengths describe the buffers. Closed once. + let result = unsafe { + let result = icu::sw_ucol_strcoll(collator, left.as_ptr(), left.len() as i32, right.as_ptr(), right.len() as i32); + icu::sw_ucol_close(collator); + result + }; + Ok(result.clamp(-1, 1).to_string()) +} + +fn list(request: &Object) -> Result { + let items: Vec> = request.opt_array("items").map(|items| items.iter().map(|item| utf16(&as_text(item).unwrap_or_default())).collect()).unwrap_or_default(); + let options = options_of(request); + let kind = match options.opt_string("type").as_str() { + "disjunction" => 1, + "unit" => 2, + _ => 0, + }; + let width = match options.opt_string("style").as_str() { + "short" => 1, + "narrow" => 2, + _ => 0, + }; + if items.is_empty() { + return Ok(String::new()); + } + let locale = locale(&required_locale(request)?); + let mut status = 0; + // SAFETY: `locale` is NUL-terminated. + let formatter = unsafe { icu::sw_ulistfmt_openForType(locale.as_ptr(), kind, width, &mut status) }; + check(status, "List format")?; + let pointers: Vec<*const u16> = items.iter().map(|item| item.as_ptr()).collect(); + let lengths: Vec = items.iter().map(|item| item.len() as i32).collect(); + // SAFETY: the formatter is open; both arrays have `items.len()` entries. + let formatted = text("List format", |buffer, capacity, error| unsafe { + icu::sw_ulistfmt_format(formatter, pointers.as_ptr(), lengths.as_ptr(), items.len() as i32, buffer, capacity, error) + }); + // SAFETY: opened above, closed once. + unsafe { icu::sw_ulistfmt_close(formatter) }; + Ok(String::from_utf16_lossy(&formatted?)) +} + +/// The functions of `native/icu_shim.c`. +mod icu { + use std::ffi::{c_char, c_void}; + + pub const BUFFER_OVERFLOW: i32 = 15; + pub const STYLE_NONE: i32 = -1; + pub const STYLE_PATTERN: i32 = -2; + + unsafe extern "C" { + pub fn sw_u_getVersion(version: *mut u8); + pub fn sw_uloc_forLanguageTag(tag: *const c_char, locale_id: *mut c_char, capacity: i32, parsed_length: *mut i32, status: *mut i32) -> i32; + pub fn sw_ucal_getCanonicalTimeZoneID(id: *const u16, length: i32, result: *mut u16, capacity: i32, is_system_id: *mut i8, status: *mut i32) -> i32; + pub fn sw_ucal_getDefaultTimeZone(result: *mut u16, capacity: i32, status: *mut i32) -> i32; + + pub fn sw_udat_open(time_style: i32, date_style: i32, locale: *const c_char, zone: *const u16, zone_length: i32, pattern: *const u16, pattern_length: i32, status: *mut i32) -> *mut c_void; + pub fn sw_udat_close(format: *mut c_void); + pub fn sw_udat_format(format: *const c_void, date: f64, result: *mut u16, capacity: i32, status: *mut i32) -> i32; + pub fn sw_udat_formatForFields(format: *const c_void, date: f64, result: *mut u16, capacity: i32, iterator: *mut c_void, status: *mut i32) -> i32; + pub fn sw_ufieldpositer_open(status: *mut i32) -> *mut c_void; + pub fn sw_ufieldpositer_close(iterator: *mut c_void); + pub fn sw_ufieldpositer_next(iterator: *mut c_void, begin: *mut i32, end: *mut i32) -> i32; + pub fn sw_udatpg_open(locale: *const c_char, status: *mut i32) -> *mut c_void; + pub fn sw_udatpg_close(generator: *mut c_void); + pub fn sw_udatpg_getBestPattern(generator: *mut c_void, skeleton: *const u16, length: i32, pattern: *mut u16, capacity: i32, status: *mut i32) -> i32; + + pub fn sw_unumf_openForSkeletonAndLocale(skeleton: *const u16, length: i32, locale: *const c_char, status: *mut i32) -> *mut c_void; + pub fn sw_unumf_close(formatter: *mut c_void); + pub fn sw_unumf_openResult(status: *mut i32) -> *mut c_void; + pub fn sw_unumf_closeResult(result: *mut c_void); + pub fn sw_unumf_formatDecimal(formatter: *const c_void, value: *const c_char, length: i32, result: *mut c_void, status: *mut i32); + pub fn sw_unumf_resultToString(result: *const c_void, buffer: *mut u16, capacity: i32, status: *mut i32) -> i32; + pub fn sw_ucurr_getDefaultFractionDigits(currency: *const u16, status: *mut i32) -> i32; + + pub fn sw_uplrules_openForType(locale: *const c_char, kind: i32, status: *mut i32) -> *mut c_void; + pub fn sw_uplrules_close(rules: *mut c_void); + pub fn sw_uplrules_select(rules: *const c_void, number: f64, keyword: *mut u16, capacity: i32, status: *mut i32) -> i32; + + pub fn sw_ureldatefmt_open(locale: *const c_char, width: i32, status: *mut i32) -> *mut c_void; + pub fn sw_ureldatefmt_close(formatter: *mut c_void); + pub fn sw_ureldatefmt_format(formatter: *const c_void, offset: f64, unit: i32, result: *mut u16, capacity: i32, status: *mut i32) -> i32; + pub fn sw_ureldatefmt_formatNumeric(formatter: *const c_void, offset: f64, unit: i32, result: *mut u16, capacity: i32, status: *mut i32) -> i32; + + pub fn sw_ucol_open(locale: *const c_char, status: *mut i32) -> *mut c_void; + pub fn sw_ucol_close(collator: *mut c_void); + pub fn sw_ucol_strcoll(collator: *const c_void, source: *const u16, source_length: i32, target: *const u16, target_length: i32) -> i32; + + pub fn sw_ulistfmt_openForType(locale: *const c_char, kind: i32, width: i32, status: *mut i32) -> *mut c_void; + pub fn sw_ulistfmt_close(formatter: *mut c_void); + pub fn sw_ulistfmt_format(formatter: *const c_void, strings: *const *const u16, lengths: *const i32, count: i32, result: *mut u16, capacity: i32, status: *mut i32) -> i32; + } +} diff --git a/Linux/crates/sw-core/src/json.rs b/Linux/crates/sw-core/src/json.rs new file mode 100644 index 00000000..70dce81e --- /dev/null +++ b/Linux/crates/sw-core/src/json.rs @@ -0,0 +1,109 @@ +//! Lenient accessors over `serde_json`, with the behavior of org.json that +//! the Android original and the Windows port rely on. Objects keep insertion +//! order (`preserve_order`). + +use serde_json::{Map, Value}; + +pub type Object = Map; + +/// Parses a JSON object; anything else is an error. +pub fn parse_object(text: &str) -> Result { + match serde_json::from_str::(text) { + Ok(Value::Object(object)) => Ok(object), + Ok(_) => Err("Expected a JSON object".into()), + Err(error) => Err(format!("Invalid JSON: {error}")), + } +} + +pub fn stringify(value: &Value) -> String { + // Serializing a `Value` cannot fail: its keys are strings and its numbers are finite. + serde_json::to_string(value).unwrap_or_else(|_| "null".into()) +} + +pub fn stringify_object(object: &Object) -> String { + serde_json::to_string(object).unwrap_or_else(|_| "{}".into()) +} + +/// A JSON string literal, quotes included. +pub fn quote(text: &str) -> String { + stringify(&Value::String(text.to_owned())) +} + +/// Text of a scalar; `None` for null, objects and arrays. Numbers and booleans are converted. +pub fn as_text(value: &Value) -> Option { + match value { + Value::String(text) => Some(text.clone()), + Value::Bool(flag) => Some(if *flag { "true" } else { "false" }.into()), + Value::Number(number) => Some(number.to_string()), + _ => None, + } +} + +pub fn as_f64(value: &Value) -> Option { + match value { + Value::Number(number) => number.as_f64(), + Value::String(text) => text.trim().parse().ok(), + _ => None, + } +} + +/// The lenient accessors, on an object. +pub trait ObjectExt { + fn has(&self, name: &str) -> bool; + /// True when the member is missing or null. + fn is_null(&self, name: &str) -> bool; + fn opt_object(&self, name: &str) -> Option<&Object>; + fn opt_array(&self, name: &str) -> Option<&Vec>; + /// The member as text, or "" when it is missing, null, an object or an array. + fn opt_string(&self, name: &str) -> String; + fn opt_string_or_none(&self, name: &str) -> Option; + fn opt_bool(&self, name: &str, fallback: bool) -> bool; + /// The member as a number, or NaN. + fn opt_f64(&self, name: &str) -> f64; + fn opt_i32(&self, name: &str, fallback: i32) -> i32; +} + +impl ObjectExt for Object { + fn has(&self, name: &str) -> bool { + self.contains_key(name) + } + + fn is_null(&self, name: &str) -> bool { + matches!(self.get(name), None | Some(Value::Null)) + } + + fn opt_object(&self, name: &str) -> Option<&Object> { + self.get(name).and_then(Value::as_object) + } + + fn opt_array(&self, name: &str) -> Option<&Vec> { + self.get(name).and_then(Value::as_array) + } + + fn opt_string(&self, name: &str) -> String { + self.opt_string_or_none(name).unwrap_or_default() + } + + fn opt_string_or_none(&self, name: &str) -> Option { + self.get(name).and_then(as_text) + } + + fn opt_bool(&self, name: &str, fallback: bool) -> bool { + match self.get(name) { + Some(Value::Bool(flag)) => *flag, + Some(Value::String(text)) if text.eq_ignore_ascii_case("true") => true, + Some(Value::String(text)) if text.eq_ignore_ascii_case("false") => false, + _ => fallback, + } + } + + fn opt_f64(&self, name: &str) -> f64 { + self.get(name).and_then(as_f64).unwrap_or(f64::NAN) + } + + fn opt_i32(&self, name: &str, fallback: i32) -> i32 { + let number = self.opt_f64(name); + // A float-to-integer cast saturates at the bounds. + if number.is_nan() { fallback } else { number as i32 } + } +} diff --git a/Linux/crates/sw-core/src/lib.rs b/Linux/crates/sw-core/src/lib.rs new file mode 100644 index 00000000..f15ca4fe --- /dev/null +++ b/Linux/crates/sw-core/src/lib.rs @@ -0,0 +1,35 @@ +//! The ScriptWidget runtime for Linux, ported from +//! `Windows/src/ScriptWidget.Core` (itself a port of `Android/core`). +//! +//! It runs the same shared JavaScript as Android and Windows (generated by +//! `Linux/scripts/build-shared-js.mjs`) in the QuickJS-NG engine of the +//! `sw-quickjs` crate. Packages, scripts and everything they return are +//! untrusted: package code only ever runs in a fresh execution context that +//! reaches the host through the operations in [`executor`]. + +pub mod api; +pub mod compiler; +pub mod executor; +pub mod intl; +pub mod json; +pub mod library; +pub mod package_files; +pub mod shared_scripts; +pub mod data_paths; +pub mod packages; +pub mod plugins; +pub mod resources; +pub mod runtime; +pub mod timeline; +pub mod xml; + +pub use compiler::{PackageCompiler, PreparedPackage, RuntimeError}; +pub use executor::{Completion, ExecutionError, ExecutionRequest, ExecutionResult, ExecutionSession, LogEntry, NativeModule, ScriptExecutor}; +pub use package_files::{MemoryPackageFiles, PackageFiles, normalize_package_path}; +pub use shared_scripts::SharedScripts; +pub use data_paths::DataPaths; +pub use packages::{ArchiveLimits, DiskPackageFiles, PackageError, PackageInfo, PackageStore}; +pub use library::{BundledPackage, InstalledPackage, Library}; +pub use resources::Resources; +pub use runtime::{RunRequest, RunResult, WidgetRuntime}; +pub use timeline::{TimelinePlanner, TimelineState}; diff --git a/Linux/crates/sw-core/src/library.rs b/Linux/crates/sw-core/src/library.rs new file mode 100644 index 00000000..73d9d048 --- /dev/null +++ b/Linux/crates/sw-core/src/library.rs @@ -0,0 +1,479 @@ +//! The user's widget library under the data folder, plus the read-only +//! templates shipped with the application. Ported from +//! `Windows/src/ScriptWidget.Core/Hosting/Library.cs`. All package I/O goes +//! through the hardened [`PackageStore`]. + +use std::collections::{BTreeMap, BTreeSet}; +use std::io::{Read, Write}; +use std::os::unix::fs::OpenOptionsExt; +use std::path::{Path, PathBuf}; + +use serde_json::Value; + +use crate::data_paths::DataPaths; +use crate::json::{Object, ObjectExt, as_text}; +use crate::packages::limits::{MAX_FILE_BYTES, MAX_FILE_COUNT, MAX_METADATA_BYTES, MAX_PATH_DEPTH}; +use crate::packages::{DiskPackageFiles, ImportValidator, PackageError, PackageInfo, PackageStore, codes, manifests, path_rules}; + +/// The widget sizes a Linux desktop shows, by the name `$getenv("widget-size")` returns. +pub const FAMILIES: [&str; 4] = ["small", "medium", "large", "extraLarge"]; + +/// The short family name for a manifest's WidgetKit name ("systemMedium"), or `None` for a size Linux does not show. +pub fn family_from_manifest(name: &str) -> Option<&'static str> { + match name { + "systemSmall" => Some("small"), + "systemMedium" => Some("medium"), + "systemLarge" => Some("large"), + "systemExtraLarge" => Some("extraLarge"), + _ => None, + } +} + +/// One installed package. `installation` stays the same when the package is renamed. +#[derive(Debug, Clone, PartialEq)] +pub struct InstalledPackage { + pub name: String, + pub directory: PathBuf, + pub installation: String, + /// The manifest as read leniently for display; `None` when absent or unreadable. + pub manifest: Option, + /// True when widget.json exists, readable or not. A present manifest that cannot be read fails closed. + pub manifest_present: bool, +} + +impl InstalledPackage { + pub fn files(&self) -> DiskPackageFiles { + DiskPackageFiles::new(&self.directory) + } + + pub fn build_directory(&self) -> PathBuf { + self.directory.join("__Build") + } + + pub fn display_name(&self) -> String { + self.manifest.as_ref().map(|manifest| manifest.opt_string("name")).filter(|title| !title.is_empty()).unwrap_or_else(|| self.name.clone()) + } + + pub fn description(&self) -> String { + self.manifest.as_ref().map(|manifest| manifest.opt_string("description")).unwrap_or_default() + } + + pub fn permissions(&self) -> Vec { + self.manifest.as_ref().and_then(|manifest| manifest.opt_array("permissions")).map(|items| items.iter().map(|item| as_text(item).unwrap_or_default()).collect()).unwrap_or_default() + } + + /// Widget sizes the manifest lists that Linux can show; every size when it lists none. + pub fn families(&self) -> Vec<&'static str> { + let declared: Vec<&str> = self + .manifest + .as_ref() + .and_then(|manifest| manifest.opt_array("supportedFamilies")) + .map(|items| items.iter().filter_map(|item| family_from_manifest(item.as_str()?)).collect()) + .unwrap_or_default(); + if declared.is_empty() { FAMILIES.to_vec() } else { FAMILIES.iter().copied().filter(|family| declared.contains(family)).collect() } + } +} + +/// A template or example shipped with the application. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct BundledPackage { + /// "template", "component" or "api". + pub group: String, + pub name: String, + pub directory: PathBuf, + /// True when one of its scripts asks the network for data (`fetch`, + /// `$http`, `$weather`, `$dataSource`): its picture depends on a server. + pub network: bool, +} + +/// A shipped template that is not offered because it uses a script global Linux does not implement. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct UnavailableTemplate { + pub group: String, + pub name: String, + /// The first such global in its entry, with the dollar sign. + pub api: String, +} + +/// The name of the dot-file that holds a package's installation identity. Package listings and exports skip dot-files. +pub const INSTALLATION_FILE: &str = ".scriptwidget-installation"; + +const GROUPS: [&str; 3] = ["template", "component", "api"]; + +/// Script globals Linux does not implement in this release (plan 4.9). A +/// template that uses one would only show "not supported", so it is left out +/// of the template list. The same list as on Windows. +const UNSUPPORTED_APIS: [&str; 14] = ["health", "calendar", "reminders", "photos", "contacts", "home", "icloud", "alarms", "maps", "services", "music", "notifications", "location", "documents"]; + +/// What marks a script as asking the network for data. +const NETWORK_TOKENS: [&str; 4] = ["fetch(", "$http.", "$weather.", "$dataSource."]; + +pub struct Library { + root: PathBuf, + templates: Option, + store: PackageStore, +} + +impl Library { + /// `templates` is the folder with the bundled template groups (see [`crate::resources`]); `None` when none are installed. + pub fn new(paths: &DataPaths, templates: Option) -> Self { + let root = paths.packages(); + Self { store: PackageStore::new(&root, paths.package_staging()), root, templates } + } + + pub fn store(&self) -> &PackageStore { + &self.store + } + + pub fn root(&self) -> &Path { + &self.root + } + + /// Installed packages, sorted by name without regard to case. + pub fn list(&self) -> Result, PackageError> { + let mut packages = self.store.list()?; + packages.sort_by_key(|info| (info.name.to_lowercase(), info.name.clone())); + Ok(packages.into_iter().map(describe).collect()) + } + + /// The package with this name, compared without regard to case. + pub fn find(&self, name: &str) -> Option { + let wanted = name.to_lowercase(); + self.store.list().ok()?.into_iter().find(|info| info.name.to_lowercase() == wanted).map(describe) + } + + pub fn find_by_installation(&self, installation: &str) -> Option { + self.list().ok()?.into_iter().find(|package| package.installation == installation) + } + + /// Creates a package; a taken name gets a numbered suffix. + pub fn create(&self, name: &str, source: &str, manifest: Option<&str>) -> Result { + Ok(describe(self.store.create(&self.store.unique_name(name)?, source, manifest)?)) + } + + /// `validate` checks the unpacked files before they are installed; an error refuses the package. + pub fn import_archive(&self, input: &mut dyn Read, suggested_name: Option<&str>, validate: Option>) -> Result { + let info = self.store.import_archive(input, suggested_name, validate)?; + // An archive never carries another installation's identity. + forget_installation(&info.directory); + Ok(describe(info)) + } + + pub fn export_archive(&self, name: &str, output: &mut dyn Write) -> Result<(), PackageError> { + self.store.export_archive(name, output) + } + + pub fn delete(&self, name: &str) -> Result<(), PackageError> { + self.store.delete(name) + } + + pub fn rename(&self, name: &str, new_name: &str) -> Result { + Ok(describe(self.store.rename(name, new_name)?)) + } + + pub fn duplicate(&self, name: &str) -> Result { + let copy = self.store.duplicate(name)?; + forget_installation(©.directory); + Ok(describe(copy)) + } + + pub fn write_file(&self, name: &str, path: &str, bytes: &[u8]) -> Result<(), PackageError> { + self.store.write_file(name, path, bytes)?; + self.clear_build(name); + Ok(()) + } + + /// Removes one file of an installed package; a file that is already gone is not an error. + pub fn delete_file(&self, name: &str, path: &str) -> Result<(), PackageError> { + self.store.delete_file(name, path)?; + self.clear_build(name); + Ok(()) + } + + /// Removes the compiled-artifact cache of a package. + pub fn clear_build(&self, name: &str) { + if let Some(installed) = self.find(name) { + let build = installed.build_directory(); + // Only a real directory: a link with that name is not ours to follow. + if std::fs::symlink_metadata(&build).is_ok_and(|metadata| metadata.is_dir()) { + let _ = std::fs::remove_dir_all(build); + } + } + } + + // Bundled templates + + /// The bundled templates that can run on Linux, grouped as the Apple apps group them. + pub fn bundled(&self) -> Vec { + self.shipped().into_iter().filter_map(|(package, unsupported)| unsupported.is_none().then_some(package)).collect() + } + + /// The shipped templates [`Library::bundled`] leaves out, with the reason. + pub fn unavailable(&self) -> Vec { + self.shipped().into_iter().filter_map(|(package, unsupported)| Some(UnavailableTemplate { group: package.group, name: package.name, api: unsupported? })).collect() + } + + /// A bundled template by "group/name" or by name alone, compared exactly. + pub fn find_bundled(&self, reference: &str) -> Option { + let bundled = self.bundled(); + match reference.split_once('/') { + Some((group, name)) => bundled.into_iter().find(|package| package.group == group && package.name == name), + None => bundled.into_iter().find(|package| package.name == reference), + } + } + + /// Every shipped template with a readable entry, and the unsupported global it uses, if any. + fn shipped(&self) -> Vec<(BundledPackage, Option)> { + let mut result = Vec::new(); + let Some(templates) = &self.templates else { return result }; + for group in GROUPS { + let directory = templates.join(group); + let mut names: Vec = std::fs::read_dir(&directory).into_iter().flatten().filter_map(|entry| entry.ok()?.file_name().into_string().ok()).filter(|name| !path_rules::is_excluded(name)).collect(); + names.sort_by_key(|name| (name.to_lowercase(), name.clone())); + for name in names { + let package = directory.join(&name); + // Read through the package view: a linked or oversized entry is no template. + let Some(main) = DiskPackageFiles::new(&package).read_bytes_up_to(manifests::LEGACY_ENTRY, MAX_FILE_BYTES).and_then(|bytes| String::from_utf8(bytes).ok()) else { continue }; + let unsupported = unsupported_api(&main); + let mut bundled = BundledPackage { group: group.to_owned(), name, directory: package, network: false }; + bundled.network = self.bundled_files(&bundled).iter().any(|(path, content)| { + (path.ends_with(".js") || path.ends_with(".jsx")) && std::str::from_utf8(content).is_ok_and(|source| NETWORK_TOKENS.iter().any(|token| source.contains(token))) + }); + result.push((bundled, unsupported)); + } + } + result + } + + /// Files of a bundled package by relative path. Dot-files, `__Build`, links and oversized files are left out. + pub fn bundled_files(&self, bundled: &BundledPackage) -> BTreeMap> { + let files = DiskPackageFiles::new(&bundled.directory); + let mut result = BTreeMap::new(); + for path in files.list().unwrap_or_default().into_iter().take(MAX_FILE_COUNT) { + if path.split('/').count() <= MAX_PATH_DEPTH + && let Some(bytes) = files.read_bytes_up_to(&path, MAX_FILE_BYTES) + { + result.insert(path, bytes); + } + } + result + } + + /// The entry source and the manifest text a copy of a bundled template + /// named `name` starts with: its own `widget.json` under the new name and + /// a fresh `id`, or the one [`bundled_manifest`] generates. + pub fn bundled_sources(name: &str, files: &BTreeMap>) -> Result<(String, String), PackageError> { + let text = |path: &str| files.get(path).and_then(|bytes| String::from_utf8(bytes.clone()).ok()); + let main = text(manifests::LEGACY_ENTRY).ok_or_else(|| PackageError::new(codes::MISSING_ENTRY, "Template has no main.jsx"))?; + let manifest = match files.get(manifests::FILE_NAME) { + Some(_) => { + let mut manifest = manifests::parse(text(manifests::FILE_NAME).as_deref()).ok_or_else(|| PackageError::new(codes::INVALID_MANIFEST, "widget.json is invalid."))?; + manifest.insert("id".into(), Value::String(manifests::fresh_id())); + manifest.insert("name".into(), Value::String(name.to_owned())); + manifest + } + None => bundled_manifest(name, files), + }; + Ok((main, String::from_utf8(manifests::encode(&manifest)).unwrap_or_default())) + } + + /// Copies a bundled template into the library; a taken name gets a numbered suffix. + pub fn install_bundled(&self, bundled: &BundledPackage) -> Result { + let files = self.bundled_files(bundled); + let name = self.store.unique_name(&bundled.name)?; + let (main, manifest) = Self::bundled_sources(&name, &files)?; + let entry = manifests::parse(Some(&manifest)).and_then(|manifest| manifests::string_value(&manifest, "entry")).unwrap_or_else(|| manifests::LEGACY_ENTRY.to_owned()); + let source = if entry == manifests::LEGACY_ENTRY { main } else { files.get(&entry).and_then(|bytes| String::from_utf8(bytes.clone()).ok()).ok_or_else(|| PackageError::new(codes::MISSING_ENTRY, "Template entry does not exist."))? }; + let created = self.store.create(&name, &source, Some(&manifest))?; + for (path, bytes) in &files { + if *path != entry && path != manifests::FILE_NAME { + self.store.write_file(&created.name, path, bytes).inspect_err(|_| { + // No half-copied template stays in the library. + let _ = self.store.delete(&created.name); + })?; + } + } + Ok(describe(created)) + } +} + +/// The first script global in `source`, with its dollar sign, that Linux does not implement. +pub fn unsupported_api(source: &str) -> Option { + let bytes = source.as_bytes(); + source.match_indices('$').find_map(|(at, _)| { + let rest = &source[at + 1..]; + UNSUPPORTED_APIS.iter().find(|name| rest.starts_with(**name) && !bytes.get(at + 1 + name.len()).is_some_and(|next| next.is_ascii_alphanumeric() || *next == b'_')).map(|name| format!("${name}")) + }) +} + +/// True for a script global without the dollar sign, such as `health`, that Linux does not implement in this release. +pub fn is_unsupported_api(name: &str) -> bool { + UNSUPPORTED_APIS.contains(&name) +} + +/// The manifest for a bundled template that has no `widget.json`. A copy in +/// the library always has a manifest, and a manifest without `network` stops +/// every request, so the hosts the template names in its own source are +/// declared for it. This is done only for the templates shipped with the +/// app, never for an imported package. +pub fn bundled_manifest(name: &str, files: &BTreeMap>) -> Object { + let metadata = files.get(manifests::LEGACY_METADATA).and_then(|bytes| std::str::from_utf8(bytes).ok()); + let mut manifest = manifests::legacy_manifest(name, metadata); + manifest.insert("id".into(), Value::String(manifests::fresh_id())); + let mut hosts = BTreeSet::new(); + for (path, content) in files { + if !path.ends_with(".js") && !path.ends_with(".jsx") { + continue; + } + for line in String::from_utf8_lossy(content).lines().filter(|line| !line.trim_start().starts_with("//")) { + hosts.extend(source_hosts(line).filter(|host| declarable_host(host))); + } + } + if !hosts.is_empty() { + manifest.insert("permissions".into(), Value::Array(vec![Value::String("network".into())])); + manifest.insert("networkDomains".into(), Value::Array(hosts.into_iter().map(Value::String).collect())); + } + manifest +} + +/// The lower-cased host of every `http://` and `https://` in a line: the run of letters, digits, dots and hyphens after it. +fn source_hosts(line: &str) -> impl Iterator + '_ { + ["http://", "https://"].into_iter().flat_map(move |scheme| { + line.match_indices(scheme).map(move |(at, _)| { + let rest = &line[at + scheme.len()..]; + let end = rest.find(|character: char| !(character.is_ascii_alphanumeric() || character == '.' || character == '-')).unwrap_or(rest.len()); + rest[..end].to_ascii_lowercase() + }) + }) +} + +/// A complete public host name: at least two labels of letters, digits and inner hyphens, the last one letters only. +fn declarable_host(host: &str) -> bool { + let labels: Vec<&str> = host.split('.').collect(); + let label = |text: &str| { + let bytes = text.as_bytes(); + !bytes.is_empty() + && bytes.iter().all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || *byte == b'-') + && bytes[0] != b'-' + && bytes[bytes.len() - 1] != b'-' + }; + labels.len() >= 2 && labels.iter().all(|text| label(text)) && labels.last().is_some_and(|last| last.len() >= 2 && last.bytes().all(|byte| byte.is_ascii_lowercase())) +} + +fn describe(info: PackageInfo) -> InstalledPackage { + let manifest = if info.has_manifest { + DiskPackageFiles::new(&info.directory).read_bytes_up_to(manifests::FILE_NAME, MAX_METADATA_BYTES as u64).and_then(|bytes| String::from_utf8(bytes).ok()).and_then(|text| manifests::parse(Some(&text))) + } else { + None + }; + InstalledPackage { installation: installation(&info.directory), name: info.name, manifest, manifest_present: info.has_manifest, directory: info.directory } +} + +fn marker_options() -> std::fs::OpenOptions { + let mut options = std::fs::OpenOptions::new(); + // Never through a link, and private to the user. + options.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC).mode(0o600); + options +} + +/// Stable identity kept in a dot-file, which package listings and exports skip. +fn installation(directory: &Path) -> String { + let marker = directory.join(INSTALLATION_FILE); + let existing = marker_options().read(true).open(&marker).ok().and_then(|file| { + let mut text = String::new(); + file.take(257).read_to_string(&mut text).ok()?; + let text = text.trim().to_owned(); + ((8..=64).contains(&text.len()) && text.bytes().all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')).then_some(text) + }); + if let Some(existing) = existing { + return existing; + } + let created = manifests::fresh_id().trim_start_matches("local.").to_owned(); + // An unreadable or malformed marker is replaced; when that fails the identity lasts for this run only. + let _ = std::fs::remove_file(&marker); + if let Ok(mut file) = marker_options().write(true).create_new(true).open(&marker) { + let _ = file.write_all(created.as_bytes()); + } + created +} + +fn forget_installation(directory: &Path) { + let _ = std::fs::remove_file(directory.join(INSTALLATION_FILE)); +} + +#[cfg(test)] +mod tests { + use super::*; + + fn files(main: &str, meta: Option<&str>) -> BTreeMap> { + let mut files = BTreeMap::from([("main.jsx".to_owned(), main.as_bytes().to_vec())]); + if let Some(meta) = meta { + files.insert("meta.json".to_owned(), meta.as_bytes().to_vec()); + } + files + } + + fn strings(manifest: &Object, key: &str) -> Vec { + manifest.opt_array(key).expect(key).iter().map(|item| item.as_str().unwrap().to_owned()).collect() + } + + // The four tests of Windows/tests/ScriptWidget.Core.Tests/Packages/BundledManifestTests.cs. + + #[test] + fn declares_the_hosts_named_in_the_source() { + let manifest = bundled_manifest( + "Daily Quote", + &files( + "// https://atomsprout.com/scriptwidget\nconst url = \"https://ZenQuotes.io/api/today\";\nconst other = `http://api.example.com/${id}`;\nconst result = await fetch(url);\n", + Some("{\"description\":\"A quote\",\"category\":\"productivity\"}"), + ), + ); + assert_eq!(strings(&manifest, "permissions"), ["network"]); + assert_eq!(strings(&manifest, "networkDomains"), ["api.example.com", "zenquotes.io"]); + assert_eq!(manifest.opt_string("description"), "A quote"); + assert!(manifest.opt_string("id").starts_with("local.")); + } + + #[test] + fn a_template_without_urls_gets_no_network_permission() { + let manifest = bundled_manifest("Clock", &files("// see https://example.com\n$render(hi)\n", None)); + assert!(strings(&manifest, "permissions").is_empty()); + assert!(strings(&manifest, "networkDomains").is_empty()); + } + + #[test] + fn names_the_globals_linux_does_not_implement() { + for name in ["health", "calendar", "location", "music"] { + assert!(is_unsupported_api(name), "{name}"); + } + for name in ["storage", "device", "system", "file", "http", "xml", "weather", "dataSource", "healthy", ""] { + assert!(!is_unsupported_api(name), "{name}"); + } + assert_eq!(unsupported_api("const steps = await $health.steps();").as_deref(), Some("$health")); + assert_eq!(unsupported_api("$storage.get('a'); if ($music) {}").as_deref(), Some("$music")); + assert_eq!(unsupported_api("$healthy(); $locations; health.steps(); $calendar_x"), None); + assert_eq!(unsupported_api("x = $location"), Some("$location".into())); + } + + #[test] + fn hosts_that_cannot_be_declared_are_left_out() { + let manifest = + bundled_manifest("Odd", &files("fetch(`https://api.${region}.example.com/x`); fetch(\"http://localhost/x\"); fetch(\"http://192.168.1.1/x\"); fetch(\"https://-bad.example.com\");\n", None)); + assert!(strings(&manifest, "permissions").is_empty()); + assert!(strings(&manifest, "networkDomains").is_empty()); + } + + #[test] + fn manifest_family_names_map_to_the_short_ones() { + let package = |families: &str| InstalledPackage { + name: "A".into(), + directory: "/nowhere".into(), + installation: "x".into(), + manifest: Some(crate::json::parse_object(&format!("{{\"supportedFamilies\":{families}}}")).unwrap()), + manifest_present: true, + }; + assert_eq!(package("[\"systemLarge\",\"systemSmall\",\"accessoryCircular\"]").families(), ["small", "large"]); + assert_eq!(package("[]").families(), FAMILIES); + assert_eq!(package("[\"accessoryInline\"]").families(), FAMILIES, "as on Windows: nothing Linux can show means every size"); + } +} diff --git a/Linux/crates/sw-core/src/package_files.rs b/Linux/crates/sw-core/src/package_files.rs new file mode 100644 index 00000000..316d78b2 --- /dev/null +++ b/Linux/crates/sw-core/src/package_files.rs @@ -0,0 +1,87 @@ +//! Package-relative paths and the read-only view of a package the runtime works with. + +use std::collections::BTreeMap; + +/// Returns the normalized relative path, or `None` when it could leave the +/// package. Mirrors `normalizePackagePath` in the shared engine. +pub fn normalize_package_path(relative_path: Option<&str>) -> Option { + let path = relative_path?; + if path.is_empty() || path.starts_with('/') || path.contains('\\') || path.contains('\0') { + return None; + } + let mut parts: Vec<&str> = Vec::new(); + for part in path.split('/') { + match part { + "" | "." => {} + ".." => { + parts.pop()?; + } + name => parts.push(name), + } + } + if parts.is_empty() { None } else { Some(parts.join("/")) } +} + +/// Read-only view of one package. Paths are already normalized. +pub trait PackageFiles: Send + Sync { + /// Every file, in byte order of the path. + fn paths(&self) -> Vec; + fn exists(&self, path: &str) -> bool; + /// UTF-8 text of a script or data file, or `None` for missing and binary files. + fn read_text(&self, path: &str) -> Option; + fn read_bytes(&self, path: &str) -> Option>; +} + +/// A package held in memory: tests, previews and packages that are being imported. +#[derive(Debug, Clone, Default)] +pub struct MemoryPackageFiles { + text: BTreeMap, + binary: BTreeMap>, +} + +impl MemoryPackageFiles { + pub fn new(text: impl IntoIterator, binary: impl IntoIterator)>) -> Self { + Self { text: text.into_iter().collect(), binary: binary.into_iter().collect() } + } +} + +impl PackageFiles for MemoryPackageFiles { + fn paths(&self) -> Vec { + let mut paths: Vec = self.text.keys().chain(self.binary.keys()).cloned().collect(); + paths.sort_unstable(); + paths.dedup(); + paths + } + + fn exists(&self, path: &str) -> bool { + self.text.contains_key(path) || self.binary.contains_key(path) + } + + fn read_text(&self, path: &str) -> Option { + self.text.get(path).cloned() + } + + fn read_bytes(&self, path: &str) -> Option> { + self.binary.get(path).cloned().or_else(|| self.text.get(path).map(|text| text.clone().into_bytes())) + } +} + +#[cfg(test)] +mod tests { + use super::normalize_package_path as normalize; + + #[test] + fn keeps_paths_inside_the_package() { + assert_eq!(normalize(Some("main.jsx")).as_deref(), Some("main.jsx")); + assert_eq!(normalize(Some("./lib//util.js")).as_deref(), Some("lib/util.js")); + assert_eq!(normalize(Some("lib/../data/a.json")).as_deref(), Some("data/a.json")); + } + + #[test] + fn rejects_paths_that_could_leave_the_package() { + for path in ["", "/etc/passwd", "../main.jsx", "lib/../../main.jsx", "lib\\main.jsx", "a\0b", ".", "lib/.."] { + assert_eq!(normalize(Some(path)), None, "{path:?}"); + } + assert_eq!(normalize(None), None); + } +} diff --git a/Linux/crates/sw-core/src/packages/archive.rs b/Linux/crates/sw-core/src/packages/archive.rs new file mode 100644 index 00000000..7a76b38c --- /dev/null +++ b/Linux/crates/sw-core/src/packages/archive.rs @@ -0,0 +1,369 @@ +//! `.swt` (ZIP) import and export with the hardening of the Swift +//! implementation (`ScriptPackageArchivePreflight`, +//! `ScriptManager.importScriptPackage`). Ported from `PackageArchive.cs`. +//! +//! Archives are untrusted. The whole central directory is validated before +//! anything is written; extraction happens in a unique directory under a +//! caller-supplied staging directory; and the sizes an archive declares are +//! never trusted, only the bytes actually inflated. +//! +//! The Windows and Android ports hand the bytes to the platform's ZIP reader +//! after the preflight and compare its view of each entry with the +//! preflight's. Here the entries are read through the preflight's own view of +//! the central directory, so there is one reading of the archive and nothing +//! to compare; only inflating is delegated (to `flate2`). + +use std::collections::BTreeMap; +use std::io::{Read, Write}; +use std::os::fd::OwnedFd; +use std::path::{Path, PathBuf}; + +use flate2::Compression; +use flate2::read::DeflateDecoder; +use flate2::write::DeflateEncoder; + +use super::disk_files::DiskPackageFiles; +use super::error::{PackageError, codes}; +use super::limits::ArchiveLimits; +use super::preflight::{self, Entry, u16_at, u32_at}; +use super::{names, path_rules, safe_files, tree}; + +const MACOS_RESOURCE_DIRECTORY: &str = "__MACOSX"; +const LOCAL_SIGNATURE: u64 = 0x0403_4B50; +/// 1980-01-01 00:00:00 as an MS-DOS date; every exported entry carries it. +const FIXED_DATE: u16 = 0x0021; +/// General purpose flag: the name is UTF-8. +const UTF8_FLAG: u16 = 0x0800; + +/// A package extracted from an archive into a private staging directory. +/// Nothing is visible in the library until [`ExtractedPackage::move_to`] +/// succeeds; dropping the value removes whatever is still in staging, so a +/// failed or abandoned import leaves nothing behind. +#[derive(Debug)] +pub struct ExtractedPackage { + directory: PathBuf, + root_name: Option, + work_directory: PathBuf, +} + +impl ExtractedPackage { + /// The extracted package root (inside the staging directory until moved). + pub fn directory(&self) -> &Path { + &self.directory + } + + /// Name of the single top-level archive directory, or `None` when files sat at the archive root. + pub fn root_name(&self) -> Option<&str> { + self.root_name.as_deref() + } + + /// Publishes the package with one atomic rename. `destination` must not + /// exist and must be on the same filesystem as the staging directory. + pub fn move_to(&self, destination: &Path) -> Result<(), PackageError> { + safe_files::publish(&self.directory, &safe_files::root(destination)) + } +} + +impl Drop for ExtractedPackage { + fn drop(&mut self) { + safe_files::delete_tree(&self.work_directory); + } +} + +/// Validates `archive` without extracting it and returns its entry names. +/// Fails with an `archive_*` code for every rule of the preflight. +pub fn preflight(archive: &[u8], limits: &ArchiveLimits) -> Result, PackageError> { + Ok(preflight::validate(archive, limits)?.into_iter().map(|entry| entry.name).collect()) +} + +/// Reads, validates and extracts one package from `input`. +/// +/// Steps, each failing closed: read at most the archive byte limit; preflight +/// the complete central directory; pick the package root (files at the +/// archive root, or exactly one top-level directory); extract only that root +/// into a fresh directory under `staging_directory`; verify the extracted +/// tree. Dot-files, `__MACOSX` and `__Build` (generated cache that must never +/// be accepted from an archive) are not extracted. While inflating, bytes are +/// counted and extraction stops as soon as an entry exceeds its declared size +/// or a limit, so a zip bomb with false size headers cannot expand; the +/// CRC-32 must match as well. Every file is created exclusively, relative to +/// an open descriptor of the fresh directory, and never through a link. +/// +/// On failure the staging directory is already clean. +pub fn extract(input: &mut dyn Read, staging_directory: &Path, limits: &ArchiveLimits) -> Result { + let bytes = read_archive(input, limits)?; + let entries = preflight::validate(&bytes, limits)?; + let selection = select(&entries)?; + let work = safe_files::new_work_directory(&safe_files::root(staging_directory), "import")?; + let package = safe_files::child(&work, "package"); + let extracted = (|| { + let unusable = |error: std::io::Error| PackageError::caused(codes::ARCHIVE_CORRUPT, "The package could not be extracted.", error); + safe_files::create_private_directory(&package).map_err(unusable)?; + let root = safe_files::open_directory(&package).map_err(unusable)?; + let mut total = 0u64; + for (parts, entry) in &selection.files { + let (file_name, directories) = parts.split_last().ok_or_else(|| corrupt(&entry.name, None))?; + let mut directory: Option = None; + for name in directories { + let next = match &directory { + Some(parent) => safe_files::create_directory_at(parent, name), + None => safe_files::create_directory_at(&root, name), + }; + directory = Some(next.map_err(unusable)?); + } + total += copy_entry(&bytes, entry, directory.as_ref().unwrap_or(&root), file_name, total, limits)?; + } + tree::validate_tree(&package, limits) + })(); + match extracted { + Ok(()) => Ok(ExtractedPackage { directory: package, root_name: selection.root_name, work_directory: work }), + Err(error) => { + safe_files::delete_tree(&work); + Err(error) + } + } +} + +/// [`extract`] followed by an atomic move to `destination`; the staging +/// directory is cleaned whether or not the import succeeds. Returns the +/// archive's top-level directory name, if any. +pub fn import_to(input: &mut dyn Read, staging_directory: &Path, destination: &Path, limits: &ArchiveLimits) -> Result, PackageError> { + let extracted = extract(input, staging_directory, limits)?; + extracted.move_to(destination)?; + Ok(extracted.root_name.clone()) +} + +/// Writes the package in `package_directory` to `output` as a `.swt` archive +/// whose entries live under one `root_name/` directory. +/// +/// Dot-files, `__Build`, links, hard-linked and special files are skipped +/// (they are not part of the authored package and must not leak through an +/// export). Entries are sorted and carry a fixed timestamp, so the same +/// package always yields the same bytes. The export obeys the import limits, +/// so every exported archive can be imported again. `extra_files` adds +/// generated files (for example a migrated `widget.json`) that are absent on +/// disk; an extra file takes the place of a disk file of the same path. +/// Nothing is written to `output` unless the whole archive was produced. +pub fn export(package_directory: &Path, root_name: &str, output: &mut dyn Write, extra_files: &BTreeMap>, limits: &ArchiveLimits) -> Result<(), PackageError> { + let root = names::archive_root_name(root_name); + let files = DiskPackageFiles::new(package_directory); + let mut contents: BTreeMap> = files.list()?.into_iter().map(|path| (path, None)).collect(); + for (path, bytes) in extra_files { + if path_rules::visible_components(path).is_none() { + return Err(PackageError::new(codes::INVALID_PATH, "Invalid package path.")); + } + contents.insert(path.clone(), Some(bytes.as_slice())); + } + let count_message = || format!("A package must contain 1–{} files.", limits.maximum_file_count); + if contents.is_empty() { + return Err(PackageError::new(codes::ARCHIVE_NO_PACKAGE, count_message())); + } + if contents.len() > limits.maximum_file_count { + return Err(PackageError::new(codes::TOO_MANY_ENTRIES, count_message())); + } + + let mut archive = ArchiveWriter::default(); + let mut total = 0u64; + for (path, extra) in &contents { + let name = format!("{root}/{path}"); + // The importer would refuse this name, so exporting it would produce a dead archive. + if preflight::entry_parts(&name, limits.maximum_path_depth).is_none() { + return Err(PackageError::new(codes::INVALID_PATH, format!("Package path {path} cannot be exported."))); + } + let read; + let bytes: &[u8] = match extra { + Some(bytes) => bytes, + None => { + read = files.read_bytes_up_to(path, limits.maximum_file_bytes).ok_or_else(|| PackageError::new(codes::FILE_TOO_LARGE, format!("Package file {path} is unreadable or exceeds 25 MiB.")))?; + &read + } + }; + if bytes.len() as u64 > limits.maximum_file_bytes { + return Err(PackageError::new(codes::FILE_TOO_LARGE, format!("Package file {path} exceeds 25 MiB."))); + } + total += bytes.len() as u64; + if total > limits.maximum_expanded_bytes { + return Err(PackageError::new(codes::PACKAGE_TOO_LARGE, "Expanded package exceeds 64 MiB.")); + } + archive.add(&name, bytes).map_err(|error| PackageError::caused(codes::STORAGE_UNAVAILABLE, "The archive could not be written.", error))?; + if archive.finished_length() > limits.maximum_archive_bytes { + return Err(PackageError::new(codes::ARCHIVE_SIZE, "Archive exceeds 32 MiB.")); + } + } + output.write_all(&archive.finish()).and_then(|()| output.flush()).map_err(|error| PackageError::caused(codes::STORAGE_UNAVAILABLE, "The archive could not be written.", error)) +} + +struct Selection<'a> { + /// Each file with its path components below the package root. + files: Vec<(Vec<&'a str>, &'a Entry)>, + root_name: Option, +} + +/// Chooses the package root like `ScriptManager.extractedPackageRoot`: the +/// archive root when `main.jsx` or `widget.json` sits there, otherwise the +/// only top-level directory. +fn select<'a>(entries: &'a [Entry]) -> Result, PackageError> { + let files: Vec<&Entry> = entries.iter().filter(|entry| !entry.is_directory && entry.parts[0] != MACOS_RESOURCE_DIRECTORY && !entry.parts.iter().any(|part| path_rules::is_excluded(part))).collect(); + let below = |entry: &'a Entry, skip: usize| -> (Vec<&'a str>, &'a Entry) { (entry.parts.iter().skip(skip).map(String::as_str).collect(), entry) }; + if files.iter().any(|entry| entry.parts.len() == 1 && matches!(entry.parts[0].as_str(), "main.jsx" | "widget.json")) { + return Ok(Selection { files: files.into_iter().map(|entry| below(entry, 0)).collect(), root_name: None }); + } + let mut roots: Vec<&str> = files.iter().filter(|entry| entry.parts.len() > 1).map(|entry| entry.parts[0].as_str()).collect(); + roots.sort_unstable(); + roots.dedup(); + let [root] = roots[..] else { + return Err(PackageError::new(codes::ARCHIVE_NO_PACKAGE, "The archive must contain one widget package.")); + }; + Ok(Selection { files: files.into_iter().filter(|entry| entry.parts.len() > 1 && entry.parts[0] == root).map(|entry| below(entry, 1)).collect(), root_name: Some(root.to_owned()) }) +} + +fn read_archive(input: &mut dyn Read, limits: &ArchiveLimits) -> Result, PackageError> { + let mut bytes = Vec::new(); + input + .take(limits.maximum_archive_bytes as u64 + 1) + .read_to_end(&mut bytes) + .map_err(|error| PackageError::caused(codes::ARCHIVE_CORRUPT, "Archive could not be read.", error))?; + if bytes.is_empty() || bytes.len() > limits.maximum_archive_bytes { + return Err(PackageError::new(codes::ARCHIVE_SIZE, "Archive must be between 1 byte and 32 MiB.")); + } + Ok(bytes) +} + +/// The compressed bytes of `entry`, located through its local header. +/// +/// The local header must be where the central directory says, carry the same +/// name and method, and must not be marked encrypted; the data must lie +/// inside the archive. A header that needs ZIP64 to state its offset cannot +/// occur in an archive of at most 32 MiB and is treated as corrupt. +fn entry_data<'a>(archive: &'a [u8], entry: &Entry) -> Result<&'a [u8], PackageError> { + let header = usize::try_from(entry.local_header_offset).ok().filter(|&offset| u32_at(archive, offset) == LOCAL_SIGNATURE).ok_or_else(|| corrupt(&entry.name, None))?; + if u16_at(archive, header + 6) & 0x1 != 0 { + return Err(PackageError::new(codes::ARCHIVE_ENCRYPTED, "Encrypted package entries are not supported.")); + } + let name_start = header + 30; + let name_end = name_start + usize::from(u16_at(archive, header + 26)); + let data_start = name_end + usize::from(u16_at(archive, header + 28)); + let data_end = usize::try_from(entry.compressed_size).ok().and_then(|size| data_start.checked_add(size)); + match data_end { + Some(data_end) if data_end <= archive.len() && u16_at(archive, header + 8) == entry.method && archive[name_start..name_end] == *entry.name.as_bytes() => Ok(&archive[data_start..data_end]), + _ => Err(corrupt(&entry.name, None)), + } +} + +/// Inflates one entry into a new file, counting real bytes; returns the +/// number written. Only bytes that were actually produced count, never a +/// length the archive states. +fn copy_entry(archive: &[u8], entry: &Entry, directory: &OwnedFd, file_name: &str, total_before: u64, limits: &ArchiveLimits) -> Result { + let data = entry_data(archive, entry)?; + let mut input: Box = if entry.method == 8 { Box::new(DeflateDecoder::new(data)) } else { Box::new(data) }; + let unusable = |error: std::io::Error| PackageError::caused(codes::ARCHIVE_CORRUPT, "The package could not be extracted.", error); + let mut output = safe_files::create_file_at(directory, file_name).map_err(unusable)?; + let mismatch = || PackageError::new(codes::ARCHIVE_SIZE_MISMATCH, format!("Archive entry {} does not match its declared size.", entry.name)); + let mut checksum = flate2::Crc::new(); + let mut count = 0u64; + let mut buffer = [0u8; 16 * 1024]; + loop { + let read = match input.read(&mut buffer) { + Ok(0) => break, + Ok(read) => read, + Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue, + Err(error) => return Err(corrupt(&entry.name, Some(error))), + }; + count += read as u64; + if count > entry.uncompressed_size || count > limits.maximum_file_bytes { + return Err(mismatch()); + } + if total_before + count > limits.maximum_expanded_bytes { + return Err(PackageError::new(codes::ARCHIVE_EXPANDED_TOO_LARGE, "Expanded package exceeds 64 MiB.")); + } + checksum.update(&buffer[..read]); + output.write_all(&buffer[..read]).map_err(unusable)?; + } + if count != entry.uncompressed_size { + return Err(mismatch()); + } + if checksum.sum() != entry.crc { + return Err(corrupt(&entry.name, None)); + } + Ok(count) +} + +fn corrupt(name: &str, cause: Option) -> PackageError { + PackageError { code: codes::ARCHIVE_CORRUPT.into(), message: format!("Archive entry {name} is corrupt."), cause } +} + +/// CRC-32 (IEEE 802.3, as used by ZIP). +pub fn crc32(bytes: &[u8]) -> u32 { + let mut checksum = flate2::Crc::new(); + checksum.update(bytes); + checksum.sum() +} + +/// A ZIP archive built in memory: deflated entries, UTF-8 names, a fixed +/// timestamp, and the Unix mode of a regular file so that every importer +/// sees plain files. +#[derive(Default)] +struct ArchiveWriter { + body: Vec, + central: Vec, + count: u16, +} + +impl ArchiveWriter { + fn add(&mut self, name: &str, bytes: &[u8]) -> std::io::Result<()> { + let mut encoder = DeflateEncoder::new(Vec::new(), Compression::default()); + encoder.write_all(bytes)?; + let compressed = encoder.finish()?; + let too_large = || std::io::Error::other("entry does not fit a ZIP archive without ZIP64"); + let offset = u32::try_from(self.body.len()).map_err(|_| too_large())?; + let compressed_size = u32::try_from(compressed.len()).map_err(|_| too_large())?; + let size = u32::try_from(bytes.len()).map_err(|_| too_large())?; + let name_length = u16::try_from(name.len()).map_err(|_| too_large())?; + self.count = self.count.checked_add(1).ok_or_else(too_large)?; + + // Version needed 2.0, UTF-8 names, deflate, no modification time, 1980-01-01. + let mut shared = Vec::with_capacity(26); + for field in [20, UTF8_FLAG, 8, 0, FIXED_DATE] { + shared.extend_from_slice(&field.to_le_bytes()); + } + for field in [crc32(bytes), compressed_size, size] { + shared.extend_from_slice(&field.to_le_bytes()); + } + shared.extend_from_slice(&name_length.to_le_bytes()); + shared.extend_from_slice(&0u16.to_le_bytes()); // extra field length + + self.body.extend_from_slice(&(LOCAL_SIGNATURE as u32).to_le_bytes()); + self.body.extend_from_slice(&shared); + self.body.extend_from_slice(name.as_bytes()); + self.body.extend_from_slice(&compressed); + + self.central.extend_from_slice(&0x0201_4B50u32.to_le_bytes()); + self.central.extend_from_slice(&0x0314u16.to_le_bytes()); // made by: Unix, version 2.0 + self.central.extend_from_slice(&shared); + // Comment length, disk number, internal attributes. + self.central.extend_from_slice(&[0; 6]); + self.central.extend_from_slice(&(0o100644u32 << 16).to_le_bytes()); + self.central.extend_from_slice(&offset.to_le_bytes()); + self.central.extend_from_slice(name.as_bytes()); + Ok(()) + } + + /// Length of the archive [`ArchiveWriter::finish`] would return now. + fn finished_length(&self) -> usize { + self.body.len() + self.central.len() + 22 + } + + fn finish(mut self) -> Vec { + let central_offset = self.body.len() as u32; + let central_size = self.central.len() as u32; + self.body.append(&mut self.central); + self.body.extend_from_slice(&0x0605_4B50u32.to_le_bytes()); + self.body.extend_from_slice(&[0; 4]); // disk numbers + self.body.extend_from_slice(&self.count.to_le_bytes()); + self.body.extend_from_slice(&self.count.to_le_bytes()); + self.body.extend_from_slice(¢ral_size.to_le_bytes()); + self.body.extend_from_slice(¢ral_offset.to_le_bytes()); + self.body.extend_from_slice(&[0; 2]); // comment length + self.body + } +} diff --git a/Linux/crates/sw-core/src/packages/disk_files.rs b/Linux/crates/sw-core/src/packages/disk_files.rs new file mode 100644 index 00000000..0b5bb2b7 --- /dev/null +++ b/Linux/crates/sw-core/src/packages/disk_files.rs @@ -0,0 +1,110 @@ +//! Read-only view of one package directory on disk. Ported from +//! `DiskPackageFiles.cs`. This is what the `$file` API of the shared engine +//! reads through (`package.read` and `package.exists` in the executor). + +use std::path::{Path, PathBuf}; + +use super::error::{PackageError, codes}; +use super::limits::{MAX_BINARY_READ_BYTES, MAX_FILE_COUNT, MAX_PATH_DEPTH, MAX_SCRIPT_BYTES, MAX_TEXT_BYTES}; +use super::{path_rules, safe_files}; +use crate::compiler::is_text_file; +use crate::package_files::PackageFiles; + +/// Read-only view of one package directory on disk. +/// +/// Every path is package-relative and must already be normalized. A path is +/// refused (reported as missing) when it is absolute, contains a backslash, +/// NUL, `.`/`..` or empty component, names a dot-file or the generated +/// `__Build` cache, crosses a symbolic link at any component, has a case or +/// Unicode-normalization alias in its directory, is a hard-linked or special +/// file, or would resolve outside the root. Files are opened relative to the +/// package directory without following links (see `safe_files`), and reads +/// are size-bounded while reading. +#[derive(Debug, Clone)] +pub struct DiskPackageFiles { + root: PathBuf, +} + +impl DiskPackageFiles { + pub fn new(root: impl AsRef) -> Self { + Self { root: safe_files::root(root.as_ref()) } + } + + /// Regular files of the package as sorted, normalized relative paths. + /// Dot-files and dot-directories, `__Build`, links, special files, + /// hard-linked files, ambiguous spellings, names that are not UTF-8 and + /// anything deeper than 16 components are left out. + /// + /// Fails with `too_many_entries` when the package holds more than 500 files. + pub fn list(&self) -> Result, PackageError> { + let mut result = Vec::new(); + if safe_files::real_directory(&self.root) { + collect(&self.root, "", 1, &mut result)?; + } + result.sort_unstable(); + Ok(result) + } + + /// Bytes of a file with an explicit bound, for callers with their own limit (manifest, export). + pub fn read_bytes_up_to(&self, path: &str, maximum_bytes: u64) -> Option> { + let parts = path_rules::visible_components(path)?; + safe_files::read_bounded(safe_files::existing_file(&self.root, &parts)?, maximum_bytes) + } +} + +impl PackageFiles for DiskPackageFiles { + /// [`DiskPackageFiles::list`]; a package with too many files lists as + /// empty, so that it cannot be compiled or run. + fn paths(&self) -> Vec { + self.list().unwrap_or_default() + } + + /// True only for a readable regular file inside the package that passes every rule in the type's description. + fn exists(&self, path: &str) -> bool { + path_rules::visible_components(path).is_some_and(|parts| safe_files::existing_file(&self.root, &parts).is_some()) + } + + /// UTF-8 text of a text-like file (per [`is_text_file`]), or `None` when + /// the file is missing, refused, binary, not valid UTF-8, or larger than + /// 512 KiB for scripts and 2 MiB for other text. + fn read_text(&self, path: &str) -> Option { + if !is_text_file(path) { + return None; + } + let limit = if is_script_file(path) { MAX_SCRIPT_BYTES } else { MAX_TEXT_BYTES }; + safe_files::decode_utf8(self.read_bytes_up_to(path, limit as u64)?) + } + + /// Bytes of a file, or `None` when it is missing, refused, or larger than 16 MiB. + fn read_bytes(&self, path: &str) -> Option> { + self.read_bytes_up_to(path, MAX_BINARY_READ_BYTES as u64) + } +} + +/// `.js`, `.jsx` or `.mjs` in any case. +fn is_script_file(path: &str) -> bool { + path.rsplit_once('.').is_some_and(|(_, extension)| ["js", "jsx", "mjs"].iter().any(|known| extension.eq_ignore_ascii_case(known))) +} + +fn collect(directory: &Path, prefix: &str, depth: usize, result: &mut Vec) -> Result<(), PackageError> { + if depth > MAX_PATH_DEPTH { + return Ok(()); + } + let Some(listing) = safe_files::names(directory) else { return Ok(()) }; + for name in &listing.names { + if path_rules::is_excluded(name) || !path_rules::valid_component(name) || !safe_files::unambiguous(&listing.names, name) { + continue; + } + let child = safe_files::child(directory, name); + let Some(entry) = safe_files::attributes(&child) else { continue }; + if entry.is_directory { + collect(&child, &format!("{prefix}{name}/"), depth + 1, result)?; + } else if entry.is_private_regular_file() { + result.push(format!("{prefix}{name}")); + if result.len() > MAX_FILE_COUNT { + return Err(PackageError::new(codes::TOO_MANY_ENTRIES, "Package contains too many entries.")); + } + } + } + Ok(()) +} diff --git a/Linux/crates/sw-core/src/packages/error.rs b/Linux/crates/sw-core/src/packages/error.rs new file mode 100644 index 00000000..efb71a66 --- /dev/null +++ b/Linux/crates/sw-core/src/packages/error.rs @@ -0,0 +1,84 @@ +//! The single failure type of the package layer. Ported from +//! `Windows/src/ScriptWidget.Core/Packages/PackageException.cs`. + +use std::fmt; + +/// A failure of the package layer. `code` is a stable machine-readable +/// identifier (see [`codes`]); `message` is the explanation for a person, +/// with the wording of the Swift implementation where it has one. +#[derive(Debug)] +pub struct PackageError { + pub code: String, + pub message: String, + /// The filesystem error behind the failure, when there was one. + pub cause: Option, +} + +impl PackageError { + pub fn new(code: &str, message: impl Into) -> Self { + Self { code: code.to_owned(), message: message.into(), cause: None } + } + + pub(crate) fn caused(code: &str, message: impl Into, cause: impl Into) -> Self { + Self { code: code.to_owned(), message: message.into(), cause: Some(cause.into()) } + } +} + +impl fmt::Display for PackageError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "{}: {}", self.code, self.message) + } +} + +impl std::error::Error for PackageError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + self.cause.as_ref().map(|cause| cause as _) + } +} + +/// Stable error codes, the same strings as on Windows and Android. The +/// manifest codes (`invalid_name`, `invalid_manifest_case`, `missing_entry`) +/// are the ones `WidgetPackageManifestValidator` and `manifest.js` emit. The +/// Swift archive preflight reports messages only, so the `archive_*` codes +/// were defined by the Android port. +pub mod codes { + /// Archive is empty or larger than the archive byte limit. + pub const ARCHIVE_SIZE: &str = "archive_size"; + /// Missing or garbled central directory, footer, entry header, or ZIP64 metadata. + pub const ARCHIVE_MALFORMED: &str = "archive_malformed"; + pub const ARCHIVE_MULTI_DISK: &str = "archive_multi_disk"; + pub const ARCHIVE_TOO_MANY_ENTRIES: &str = "archive_too_many_entries"; + pub const ARCHIVE_ENCRYPTED: &str = "archive_encrypted"; + pub const ARCHIVE_UNSUPPORTED_COMPRESSION: &str = "archive_unsupported_compression"; + /// Absolute, traversing, backslash, colon, NUL, non-UTF-8, or too deeply nested entry path. + pub const ARCHIVE_UNSAFE_PATH: &str = "archive_unsafe_path"; + /// Two entries that are equal after NFC normalization and case folding, or a file/directory clash. + pub const ARCHIVE_PATH_COLLISION: &str = "archive_path_collision"; + pub const ARCHIVE_SYMLINK: &str = "archive_symlink"; + pub const ARCHIVE_FILE_TOO_LARGE: &str = "archive_file_too_large"; + pub const ARCHIVE_EXPANDED_TOO_LARGE: &str = "archive_expanded_too_large"; + pub const ARCHIVE_INVALID_RATIO: &str = "archive_invalid_ratio"; + /// The inflated byte count differs from the size the central directory declared (zip bomb guard). + pub const ARCHIVE_SIZE_MISMATCH: &str = "archive_size_mismatch"; + /// The entry could not be inflated or its CRC-32 does not match. + pub const ARCHIVE_CORRUPT: &str = "archive_corrupt"; + /// The archive does not contain exactly one widget package. + pub const ARCHIVE_NO_PACKAGE: &str = "archive_no_package"; + + pub const INVALID_NAME: &str = "invalid_name"; + pub const INVALID_MANIFEST: &str = "invalid_manifest"; + pub const INVALID_MANIFEST_CASE: &str = "invalid_manifest_case"; + pub const MISSING_ENTRY: &str = "missing_entry"; + pub const INVALID_ENTRY: &str = "invalid_entry"; + pub const INVALID_PATH: &str = "invalid_path"; + pub const PACKAGE_EXISTS: &str = "package_exists"; + pub const PACKAGE_NOT_FOUND: &str = "package_not_found"; + pub const FILE_NOT_FOUND: &str = "file_not_found"; + pub const FILE_TOO_LARGE: &str = "file_too_large"; + pub const TOO_MANY_ENTRIES: &str = "too_many_entries"; + pub const PACKAGE_TOO_LARGE: &str = "package_too_large"; + /// Symbolic link, hard link, special file, or case alias inside a package tree. + pub const UNSAFE_FILE: &str = "unsafe_file"; + /// The library or staging directory is unusable (missing, a link, or on another filesystem). + pub const STORAGE_UNAVAILABLE: &str = "storage_unavailable"; +} diff --git a/Linux/crates/sw-core/src/packages/file_remover.rs b/Linux/crates/sw-core/src/packages/file_remover.rs new file mode 100644 index 00000000..af2fb3d1 --- /dev/null +++ b/Linux/crates/sw-core/src/packages/file_remover.rs @@ -0,0 +1,40 @@ +//! Removes one file of an installed package, under the same path rules as +//! the package file writer. `scriptwidget dev` uses it to make an installed +//! package follow the folder it is developed in. + +use std::path::Path; + +use rustix::fs::AtFlags; + +use super::error::{PackageError, codes}; +use super::{path_rules, safe_files}; + +/// Removes `relative_path` below the package `root`. +/// +/// The path must pass the package path rules. The directory that holds the +/// file is opened beneath `root` without following a link, and the entry +/// must be a singly linked regular file: a link, a directory or anything +/// else is refused, so nothing outside the package can be removed. A file +/// that is already gone is not an error. Directories are left in place. +pub(crate) fn remove(root: &Path, relative_path: &str) -> Result<(), PackageError> { + let parts = path_rules::visible_components(relative_path).ok_or_else(|| PackageError::new(codes::INVALID_PATH, "Invalid package path."))?; + let (file_name, directories) = parts.split_last().ok_or_else(|| PackageError::new(codes::INVALID_PATH, "Invalid package path."))?; + let unavailable = |error| PackageError::caused(codes::PACKAGE_NOT_FOUND, "Package directory is unavailable.", error); + let root = safe_files::open_directory(root).map_err(unavailable)?; + let directory = if directories.is_empty() { + root + } else { + match safe_files::open_beneath(&root, directories, safe_files::DIRECTORY_FLAGS) { + Ok(directory) => directory, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()), + Err(error) => return Err(PackageError::caused(codes::UNSAFE_FILE, "Package path crosses a link or a file.", error)), + } + }; + match safe_files::attributes_at(&directory, file_name) { + Ok(entry) if entry.is_private_regular_file() => {} + Ok(_) => return Err(PackageError::new(codes::UNSAFE_FILE, "Only regular package files can be removed.")), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()), + Err(error) => return Err(PackageError::caused(codes::UNSAFE_FILE, "Package file could not be verified.", error)), + } + rustix::fs::unlinkat(&directory, *file_name, AtFlags::empty()).map_err(|error| PackageError::caused(codes::STORAGE_UNAVAILABLE, "Package file could not be removed.", error)) +} diff --git a/Linux/crates/sw-core/src/packages/file_writer.rs b/Linux/crates/sw-core/src/packages/file_writer.rs new file mode 100644 index 00000000..2f56902e --- /dev/null +++ b/Linux/crates/sw-core/src/packages/file_writer.rs @@ -0,0 +1,81 @@ +//! Atomic, path-validated file writes inside one package. Ported from +//! `PackageFileWriter.cs`, itself `ScriptWidgetPackageFiles.write` in Swift. + +use std::os::fd::OwnedFd; +use std::path::Path; + +use rustix::fs::AtFlags; +use rustix::io::Errno; + +use super::error::{PackageError, codes}; +use super::limits::{MAX_FILE_BYTES, MAX_METADATA_BYTES}; +use super::{manifests, path_rules, safe_files}; + +/// Writes `bytes` to `relative_path` below the package `root`. +/// +/// The path must pass the package path rules (no traversal, absolute path, +/// backslash, NUL, dot-file or `__Build` component). Every step is made +/// relative to an open descriptor of the directory above it and never follows +/// a link: an existing directory on the way must be a real directory, a +/// missing one is created, and no component may have a case alias, so a write +/// cannot be redirected outside the package. An existing target must be a +/// singly linked regular file. Content is limited to 25 MiB (256 KiB for +/// `widget.json`, whose name must be spelled exactly). The bytes go to a +/// hidden temporary file in the target directory, are flushed, and replace +/// the target with one atomic rename: readers see the old or the new content, +/// never a partial file, and a failed write leaves the old content in place. +pub(crate) fn write(root: &Path, relative_path: &str, bytes: &[u8]) -> Result<(), PackageError> { + let parts = path_rules::visible_components(relative_path).ok_or_else(|| fail(codes::INVALID_PATH, "Invalid package path."))?; + if bytes.len() as u64 > MAX_FILE_BYTES { + return Err(fail(codes::FILE_TOO_LARGE, "Package file exceeds 25 MiB.")); + } + if let [name] = parts[..] + && name.to_lowercase() == manifests::FILE_NAME + { + if name != manifests::FILE_NAME { + return Err(fail(codes::INVALID_MANIFEST_CASE, "Use the exact file name widget.json.")); + } + if bytes.len() > MAX_METADATA_BYTES { + return Err(fail(codes::FILE_TOO_LARGE, "widget.json exceeds 256 KiB.")); + } + } + let (file_name, directories) = parts.split_last().ok_or_else(|| fail(codes::INVALID_PATH, "Invalid package path."))?; + let mut directory = safe_files::open_directory(root).map_err(|error| PackageError::caused(codes::PACKAGE_NOT_FOUND, "Package directory is unavailable.", error))?; + for name in directories { + require_unambiguous(&directory, name)?; + match safe_files::attributes_at(&directory, name) { + Ok(entry) if entry.is_directory => {} + Ok(_) => return Err(fail(codes::UNSAFE_FILE, "Package path crosses a link or a file.")), + Err(error) if error.raw_os_error() == Some(Errno::NOENT.raw_os_error()) => {} + Err(error) => return Err(PackageError::caused(codes::UNSAFE_FILE, "Package file could not be verified.", error)), + } + // Opens the directory itself, creating it first when it is missing; a link that appeared meanwhile is refused. + directory = safe_files::create_directory_at(&directory, name).map_err(|error| PackageError::caused(codes::UNSAFE_FILE, "Package directory could not be created.", error))?; + } + require_unambiguous(&directory, file_name)?; + match safe_files::attributes_at(&directory, file_name) { + Ok(entry) if entry.is_private_regular_file() => {} + Ok(_) => return Err(fail(codes::UNSAFE_FILE, "Only regular package files can be replaced.")), + Err(error) if error.raw_os_error() == Some(Errno::NOENT.raw_os_error()) => {} + Err(error) => return Err(PackageError::caused(codes::UNSAFE_FILE, "Package file could not be verified.", error)), + } + let temporary = format!(".write-{}", safe_files::unique_token()); + let saved = safe_files::write_new_at(&directory, &temporary, bytes).and_then(|()| Ok(rustix::fs::renameat(&directory, temporary.as_str(), &directory, *file_name)?)); + if let Err(error) = saved { + // Hidden temporaries are never listed, read or exported, but none should stay behind. + let _ = rustix::fs::unlinkat(&directory, temporary.as_str(), AtFlags::empty()); + return Err(PackageError::caused(codes::STORAGE_UNAVAILABLE, "Package file could not be saved.", error)); + } + // Makes the rename itself durable; the content already is. + let _ = rustix::fs::fsync(&directory); + Ok(()) +} + +fn require_unambiguous(directory: &OwnedFd, name: &str) -> Result<(), PackageError> { + let siblings = safe_files::names_of(directory).ok_or_else(|| fail(codes::UNSAFE_FILE, "Package directory could not be verified."))?; + if safe_files::unambiguous(&siblings.names, name) { Ok(()) } else { Err(fail(codes::UNSAFE_FILE, "Case-colliding package paths are not allowed.")) } +} + +fn fail(code: &str, message: &str) -> PackageError { + PackageError::new(code, message) +} diff --git a/Linux/crates/sw-core/src/packages/limits.rs b/Linux/crates/sw-core/src/packages/limits.rs new file mode 100644 index 00000000..0f11e45b --- /dev/null +++ b/Linux/crates/sw-core/src/packages/limits.rs @@ -0,0 +1,54 @@ +//! Size and shape limits of the package layer, each taken from the Swift +//! implementation. Ported from `PackageLimits.cs`; the limits that exist only +//! because of NTFS and Win32 (UTF-16 lengths of a name and of a path) are not +//! here, as on Android. + +/// `ScriptPackageArchivePolicy.maximumArchiveBytes`. +pub const MAX_ARCHIVE_BYTES: usize = 32 * 1024 * 1024; +/// `ScriptPackageArchivePolicy.maximumExpandedBytes`. +pub const MAX_EXPANDED_BYTES: u64 = 64 * 1024 * 1024; +/// `ScriptPackageArchivePolicy.maximumFileBytes`; also the bound of a package file read or write. +pub const MAX_FILE_BYTES: u64 = 25 * 1024 * 1024; +/// `ScriptPackageArchivePolicy.maximumFileCount` (files and directories). +pub const MAX_FILE_COUNT: usize = 500; +/// `ScriptPackageArchivePolicy.maximumPathDepth`. +pub const MAX_PATH_DEPTH: usize = 16; +/// Longest path component in UTF-8 bytes (`ScriptWidgetPackageFiles.components`); also the Linux file name limit. +pub const MAX_COMPONENT_BYTES: usize = 255; +/// `ScriptWidgetMetadataFile.maximumBytes`: bound for `widget.json` and `meta.json`. +pub const MAX_METADATA_BYTES: usize = 256 * 1024; +/// `ScriptWidgetRuntimeContract.maximumSourceBytes`: bound for one script source file. +pub const MAX_SCRIPT_BYTES: usize = 512 * 1024; +/// Bound for text reads of non-script files (as on Android). +pub const MAX_TEXT_BYTES: usize = 2 * 1024 * 1024; +/// Bound for binary reads by the runtime (as on Android, below the 25 MiB file limit). +pub const MAX_BINARY_READ_BYTES: usize = 16 * 1024 * 1024; +/// Package names: at most 80 grapheme clusters and 240 UTF-8 bytes (`ScriptWidgetPackageName`). +pub const MAX_NAME_GRAPHEMES: usize = 80; +pub const MAX_NAME_BYTES: usize = 240; +/// Most directory entries listed before a listing fails closed (`ScriptWidgetPackageFiles.names`). +pub const MAX_DIRECTORY_ENTRIES: usize = 100_000; +/// `getValidPackageName` tries the suffixes " (1)" through " (10000)". +pub const MAX_NAME_ATTEMPTS: usize = 10_000; + +/// Archive limits; the defaults are the Swift `ScriptPackageArchivePolicy`. Tests may shrink them. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ArchiveLimits { + pub maximum_archive_bytes: usize, + pub maximum_expanded_bytes: u64, + pub maximum_file_bytes: u64, + pub maximum_file_count: usize, + pub maximum_path_depth: usize, +} + +impl Default for ArchiveLimits { + fn default() -> Self { + Self { + maximum_archive_bytes: MAX_ARCHIVE_BYTES, + maximum_expanded_bytes: MAX_EXPANDED_BYTES, + maximum_file_bytes: MAX_FILE_BYTES, + maximum_file_count: MAX_FILE_COUNT, + maximum_path_depth: MAX_PATH_DEPTH, + } + } +} diff --git a/Linux/crates/sw-core/src/packages/manifests.rs b/Linux/crates/sw-core/src/packages/manifests.rs new file mode 100644 index 00000000..36b33a39 --- /dev/null +++ b/Linux/crates/sw-core/src/packages/manifests.rs @@ -0,0 +1,172 @@ +//! Minimal `widget.json` handling for the library: creating default manifests +//! and reading the display fields. Ported from `PackageManifests.cs`. +//! +//! Semantic validation (unknown keys, permissions, entry, versions) is the +//! shared manifest validator's job ([`crate::PackageCompiler::validate_manifest`]) +//! and is not repeated here. + +use std::fmt; + +use serde::de::{Deserialize, Deserializer, Error as _, MapAccess, SeqAccess, Visitor}; +use serde_json::{Number, Value, json}; + +use super::names; +use super::safe_files::unique_token; +use crate::json::Object; + +pub const FILE_NAME: &str = "widget.json"; +pub const LEGACY_ENTRY: &str = "main.jsx"; +pub const LEGACY_METADATA: &str = "meta.json"; + +const DISCOVERY_KEYS: [&str; 4] = ["description", "category", "icon", "preview"]; + +/// Display fields of a manifest; each is `None` when absent or not a string. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Summary { + pub id: Option, + pub name: Option, + pub version: Option, +} + +/// Parses manifest text as a JSON object, or returns `None`. A present +/// manifest that fails here is invalid, never legacy. +/// +/// The reading is strict: no comments, no trailing commas, nothing after the +/// value, and an object that names a key twice is refused instead of letting +/// the later value silently win. +pub fn parse(text: Option<&str>) -> Option { + match serde_json::from_str::(text?) { + Ok(Strict(Value::Object(manifest))) => Some(manifest), + _ => None, + } +} + +pub fn summarize(manifest: &Object) -> Summary { + Summary { id: string_value(manifest, "id"), name: string_value(manifest, "name"), version: string_value(manifest, "version") } +} + +/// The member when it is a string; numbers and booleans are not converted. +pub fn string_value(manifest: &Object, key: &str) -> Option { + manifest.get(key).and_then(Value::as_str).map(str::to_owned) +} + +/// A fresh local identifier, as `WidgetPackageManifest.newProject` generates. +pub fn fresh_id() -> String { + format!("local.{}", unique_token()) +} + +/// `WidgetPackageManifest.newProject` / `newManifest` in `manifest.js`. +pub fn new_manifest(name: &str) -> Object { + let manifest = json!({ + "formatVersion": 2, + "id": fresh_id(), + "name": name, + "version": "1.0.0", + "runtimeVersion": "1.0", + "entry": LEGACY_ENTRY, + "supportedFamilies": ["systemSmall", "systemMedium", "systemLarge"], + "permissions": [], + "networkDomains": [], + "plugins": [], + "actions": [], + "controls": [], + }); + match manifest { + Value::Object(manifest) => manifest, + _ => Object::new(), + } +} + +/// `WidgetPackageManifest.legacy`: the manifest given to a package that has +/// `main.jsx` and no `widget.json`. Discovery fields are carried over from +/// `meta.json` when it is well formed; a malformed `meta.json` is ignored, as +/// in Swift. +pub fn legacy_manifest(name: &str, metadata_text: Option<&str>) -> Object { + let mut manifest = new_manifest(name); + manifest.insert("id".into(), Value::String(names::legacy_id(name))); + let Some(metadata) = parse(metadata_text) else { return manifest }; + for key in DISCOVERY_KEYS { + if let Some(text) = string_value(&metadata, key) { + manifest.insert(key.into(), Value::String(text)); + } + } + if let Some(Value::Array(tags)) = metadata.get("tags") + && tags.iter().all(Value::is_string) + { + manifest.insert("tags".into(), Value::Array(tags.clone())); + } + manifest +} + +/// The manifest as indented UTF-8 JSON with LF line ends and a final newline. +pub fn encode(manifest: &Object) -> Vec { + let mut text = serde_json::to_string_pretty(manifest).unwrap_or_else(|_| "{}".into()); + text.push('\n'); + text.into_bytes() +} + +/// A JSON value read with duplicate object keys treated as an error. +struct Strict(Value); + +impl<'de> Deserialize<'de> for Strict { + fn deserialize>(deserializer: D) -> Result { + deserializer.deserialize_any(StrictVisitor) + } +} + +struct StrictVisitor; + +impl<'de> Visitor<'de> for StrictVisitor { + type Value = Strict; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a JSON value") + } + + fn visit_bool(self, value: bool) -> Result { + Ok(Strict(Value::Bool(value))) + } + + fn visit_i64(self, value: i64) -> Result { + Ok(Strict(Value::Number(value.into()))) + } + + fn visit_u64(self, value: u64) -> Result { + Ok(Strict(Value::Number(value.into()))) + } + + fn visit_f64(self, value: f64) -> Result { + Ok(Strict(Number::from_f64(value).map_or(Value::Null, Value::Number))) + } + + fn visit_str(self, value: &str) -> Result { + Ok(Strict(Value::String(value.to_owned()))) + } + + fn visit_string(self, value: String) -> Result { + Ok(Strict(Value::String(value))) + } + + fn visit_unit(self) -> Result { + Ok(Strict(Value::Null)) + } + + fn visit_seq>(self, mut sequence: A) -> Result { + let mut items = Vec::new(); + while let Some(Strict(item)) = sequence.next_element()? { + items.push(item); + } + Ok(Strict(Value::Array(items))) + } + + fn visit_map>(self, mut map: A) -> Result { + let mut object = Object::new(); + while let Some(key) = map.next_key::()? { + let Strict(value) = map.next_value()?; + if object.insert(key.clone(), value).is_some() { + return Err(A::Error::custom(format!("duplicate key {key:?}"))); + } + } + Ok(Strict(Value::Object(object))) + } +} diff --git a/Linux/crates/sw-core/src/packages/mod.rs b/Linux/crates/sw-core/src/packages/mod.rs new file mode 100644 index 00000000..ce6cac4d --- /dev/null +++ b/Linux/crates/sw-core/src/packages/mod.rs @@ -0,0 +1,49 @@ +//! Packages on disk: the library of installed packages, `.swt` archives, and +//! the read-only view the runtime compiles and runs from. Ported from +//! `Windows/src/ScriptWidget.Core/Packages` (itself a port of +//! `Android/core/.../pkg`, which ports the Swift `ScriptManager`). +//! +//! Everything here treats package directories and archives as untrusted: +//! +//! - A package-relative path is refused unless it is in normal form with no +//! traversal, absolute path, backslash, NUL or control character +//! ([`path_rules`]). +//! - Nothing is read or written through a symbolic link, and a file with a +//! second hard link is not a package file. Files are opened relative to the +//! package directory with `openat2` and `RESOLVE_BENEATH`, and the link +//! count is checked on the open file before it is read (`safe_files`). +//! - Two names that differ only in case or Unicode normalization are refused +//! wherever they meet, although Linux would keep both ([`names::identity`]). +//! - An archive is validated completely before anything is extracted +//! (entry count, file size, expanded size, paths, entry kinds), extracted +//! into a unique private directory, verified again as a tree, and published +//! with one atomic rename ([`archive`], [`PackageStore`]). +//! - A manifest that is present but malformed is an error; it is never +//! reinterpreted as a legacy package. +//! +//! The rules that exist only for Win32 (reserved device names, alternate data +//! streams, trailing dots and spaces, UTF-16 path lengths) are not applied: +//! Linux has no such names, and the Android port, which runs the same +//! packages, accepts them. A colon in an archive entry name is refused on +//! every platform and so here as well. + +pub mod archive; +mod disk_files; +mod error; +mod file_remover; +mod file_writer; +pub mod limits; +pub mod manifests; +pub mod names; +pub mod path_rules; +mod preflight; +mod safe_files; +mod store; +mod tree; + +pub use archive::ExtractedPackage; +pub use disk_files::DiskPackageFiles; +pub use error::{PackageError, codes}; +pub use limits::ArchiveLimits; +pub use store::{ImportValidator, PackageInfo, PackageStore, export_directory, manifest_present}; +pub use tree::{validate_source_tree, validate_tree}; diff --git a/Linux/crates/sw-core/src/packages/names.rs b/Linux/crates/sw-core/src/packages/names.rs new file mode 100644 index 00000000..e6e42129 --- /dev/null +++ b/Linux/crates/sw-core/src/packages/names.rs @@ -0,0 +1,112 @@ +//! Package (library directory) names, ported from `PackageNames.cs`, itself +//! `validPackageName` in `manifest.js` and `ScriptWidgetPackageName.isValid` +//! in Swift. +//! +//! The rules are those of the Android port. The Windows port also refuses the +//! names Win32 cannot store (a colon, a trailing dot, `CON` and so on); Linux +//! stores them like any other name, and Android accepts them, so they are +//! accepted here too. + +use unicode_normalization::UnicodeNormalization; +use unicode_properties::{GeneralCategory, UnicodeGeneralCategory}; +use unicode_segmentation::UnicodeSegmentation; + +use super::limits::{MAX_NAME_BYTES, MAX_NAME_GRAPHEMES}; + +/// The identity two names share when a case-insensitive or normalizing +/// filesystem would treat them as one entry: NFC, then case folding +/// (`ScriptWidgetPackageFiles.identity`). +/// +/// A Linux filesystem keeps `Main.jsx` and `main.jsx` as two files; the +/// package would then lose one of them on macOS, Windows or an Android SD +/// card, so the two are one identity here as well. The fold goes through +/// upper case first, as the Windows port does because NTFS compares names +/// that way: the long s and the Kelvin sign meet their ASCII letters. Rust's +/// full case mappings fold a little more than .NET's simple ones (`ß` meets +/// `ss`), which only makes more spellings collide. +pub fn identity(name: &str) -> String { + name.nfc().collect::().to_uppercase().to_lowercase() +} + +/// True when `name` may be used as a package name and directory name. +/// +/// Rejects blank names, a leading dot (hidden files, `.`/`..`), the reserved +/// `__Build` cache name in any case, path separators, Windows drive prefixes, +/// and control/format characters other than ZWNJ/ZWJ, so a display name can +/// never become a traversal or a cache path. Length is bounded to 80 grapheme +/// clusters and 240 UTF-8 bytes. +pub fn is_valid(name: &str) -> bool { + if name.trim().is_empty() || name.starts_with('.') { + return false; + } + if identity(name) == "__build" || name.contains('/') || name.contains('\\') || has_drive_prefix(name) { + return false; + } + if has_control_characters(name) { + return false; + } + grapheme_count(name) <= MAX_NAME_GRAPHEMES && name.len() <= MAX_NAME_BYTES +} + +/// `^[A-Za-z]:`, which Windows reads as a drive. +pub(crate) fn has_drive_prefix(text: &str) -> bool { + let bytes = text.as_bytes(); + bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':' +} + +/// Control or format characters, except ZWNJ/ZWJ, which joined emoji and some scripts need. +pub fn has_control_characters(text: &str) -> bool { + text.chars().any(|character| { + !matches!(character, '\u{200C}' | '\u{200D}') && matches!(character.general_category(), GeneralCategory::Control | GeneralCategory::Format) + }) +} + +pub fn grapheme_count(text: &str) -> usize { + text.graphemes(true).count() +} + +/// `name` followed by `suffix`, with the stem shortened by whole grapheme +/// clusters until the result fits the name limits. This is the truncation +/// `getValidPackageName` and `duplicateScript` apply in Swift. +pub fn with_suffix(name: &str, suffix: &str) -> String { + let bounds: Vec = name.grapheme_indices(true).map(|(start, _)| start).chain([name.len()]).collect(); + let mut keep = (bounds.len() - 1).min(MAX_NAME_GRAPHEMES.saturating_sub(grapheme_count(suffix))); + while keep > 0 && bounds[keep] + suffix.len() > MAX_NAME_BYTES { + keep -= 1; + } + format!("{}{suffix}", &name[..bounds[keep]]) +} + +/// `WidgetPackageManifest.legacy`: the identifier of a package that had no `widget.json`. +pub fn legacy_id(name: &str) -> String { + let mut slug = String::new(); + let mut gap = false; + for character in name.to_lowercase().chars() { + if character.is_ascii_lowercase() || character.is_ascii_digit() { + if gap && !slug.is_empty() { + slug.push('-'); + } + gap = false; + slug.push(character); + } else { + gap = true; + } + } + format!("legacy.{}", if slug.is_empty() { "widget" } else { &slug }) +} + +/// `packageDirectoryName` in `archive.js`: a root directory name every +/// platform's importer accepts. At most 80 UTF-16 units, as there. +pub fn archive_root_name(name: &str) -> String { + let replaced: String = name.chars().map(|character| if matches!(character, '\\' | '/' | ':' | '*' | '?' | '"' | '<' | '>' | '|' | '\0') { '-' } else { character }).collect(); + let mut cleaned = String::new(); + let mut units = 0; + for character in replaced.trim_start_matches('.').trim().chars() { + units += character.len_utf16(); + if units > 80 { + break; + } + cleaned.push(character); + } + if cleaned.is_empty() { "Widget".into() } else { cleaned } +} diff --git a/Linux/crates/sw-core/src/packages/path_rules.rs b/Linux/crates/sw-core/src/packages/path_rules.rs new file mode 100644 index 00000000..dd53297a --- /dev/null +++ b/Linux/crates/sw-core/src/packages/path_rules.rs @@ -0,0 +1,39 @@ +//! Package-relative path rules, ported from `PackagePathRules.cs`, itself +//! `ScriptWidgetPackageFiles.components` in Swift. +//! +//! As on Android, the names that only Win32 cannot store (reserved device +//! names, a trailing dot or space, `< > " | ? *`) are ordinary names here. + +use super::limits::{MAX_COMPONENT_BYTES, MAX_PATH_DEPTH}; +use super::names; +use crate::package_files::normalize_package_path; + +/// Splits a package-relative path into components, or returns `None` when it is unsafe. +/// +/// The path must already be in normal form: no leading slash, no empty, `.` +/// or `..` component, no backslash and no NUL (so nothing can name a location +/// outside the package), no Windows drive prefix, at most 16 components of at +/// most 255 UTF-8 bytes each, and no control characters. +pub fn components(relative: &str) -> Option> { + let normalized = normalize_package_path(Some(relative))?; + if normalized != relative || names::has_drive_prefix(relative) { + return None; + } + let parts: Vec<&str> = relative.split('/').collect(); + (parts.len() <= MAX_PATH_DEPTH && parts.iter().all(|part| valid_component(part))).then_some(parts) +} + +/// [`components`], additionally refusing dot-files and the generated +/// `__Build` cache at any depth. Those are never part of the authored +/// package, so they are neither listed, read nor written. +pub fn visible_components(relative: &str) -> Option> { + components(relative).filter(|parts| !parts.iter().any(|part| is_excluded(part))) +} + +pub fn valid_component(part: &str) -> bool { + !part.is_empty() && part != "." && part != ".." && !part.contains('/') && !part.contains('\\') && part.len() <= MAX_COMPONENT_BYTES && !names::has_control_characters(part) +} + +pub fn is_excluded(part: &str) -> bool { + part.starts_with('.') || names::identity(part) == "__build" +} diff --git a/Linux/crates/sw-core/src/packages/preflight.rs b/Linux/crates/sw-core/src/packages/preflight.rs new file mode 100644 index 00000000..3231ccd4 --- /dev/null +++ b/Linux/crates/sw-core/src/packages/preflight.rs @@ -0,0 +1,244 @@ +//! Central-directory validation of an untrusted `.swt` archive, ported from +//! `ArchivePreflight.cs`, itself `ScriptPackageArchivePreflight.validate` in +//! Swift (and `preflightArchive` in `archive.js`). It inspects bytes only and +//! never inflates or writes anything. + +use std::collections::{HashMap, HashSet}; + +use super::error::{PackageError, codes}; +use super::limits::ArchiveLimits; +use super::{names, path_rules}; + +/// One central-directory entry that passed validation. +#[derive(Debug, Clone)] +pub(crate) struct Entry { + pub name: String, + /// The non-empty path components. + pub parts: Vec, + pub is_directory: bool, + pub uncompressed_size: u64, + pub compressed_size: u64, + pub crc: u32, + /// 0 (stored) or 8 (deflate). + pub method: u16, + /// Offset of the entry's local header as the central directory states it; checked when the entry is read. + pub local_header_offset: u64, +} + +const END_SIGNATURE: u64 = 0x0605_4B50; +const CENTRAL_SIGNATURE: u64 = 0x0201_4B50; +const U32_MAX: u64 = 0xFFFF_FFFF; + +/// Validates the whole archive and returns its entries. +/// +/// Rejected: empty or oversized archives; a missing, multi-disk, +/// trailing-garbage or out-of-bounds central directory; more entries than +/// allowed; encrypted entries; compression other than stored/deflate; +/// malformed ZIP64 sizes; entry names that are not UTF-8, are absolute, +/// contain a backslash, colon, NUL, control character, `.` or `..` +/// component, or nest too deeply; duplicate, case-colliding or +/// file-versus-directory clashing paths; symbolic links and other special +/// files; a declared file size or declared total above the limits; and a +/// large file that claims zero compressed bytes. +pub(crate) fn validate(data: &[u8], limits: &ArchiveLimits) -> Result, PackageError> { + if data.is_empty() || data.len() > limits.maximum_archive_bytes { + return Err(fail(codes::ARCHIVE_SIZE, "Archive must be between 1 byte and 32 MiB.")); + } + let end = end_of_central_directory(data).ok_or_else(|| fail(codes::ARCHIVE_MALFORMED, "Archive has no valid ZIP central directory."))?; + if u16_at(data, end + 4) != 0 || u16_at(data, end + 6) != 0 || u16_at(data, end + 8) != u16_at(data, end + 10) { + return Err(fail(codes::ARCHIVE_MULTI_DISK, "Multi-disk ZIP archives are not supported.")); + } + if end + 22 + usize::from(u16_at(data, end + 20)) != data.len() { + return Err(fail(codes::ARCHIVE_MALFORMED, "Archive has a malformed ZIP footer.")); + } + let count = usize::from(u16_at(data, end + 10)); + let central_size = u32_at(data, end + 12); + let central_offset = u32_at(data, end + 16); + if count == 0 || count > limits.maximum_file_count { + return Err(fail(codes::ARCHIVE_TOO_MANY_ENTRIES, format!("Archive must contain 1–{} entries.", limits.maximum_file_count))); + } + if central_offset + central_size > data.len() as u64 { + return Err(fail(codes::ARCHIVE_MALFORMED, "Archive central directory is out of bounds.")); + } + + let mut cursor = central_offset as usize; + let mut expanded = 0u64; + let mut collisions = Collisions::default(); + let mut entries = Vec::with_capacity(count); + for _ in 0..count { + if cursor + 46 > data.len() || u32_at(data, cursor) != CENTRAL_SIGNATURE { + return Err(fail(codes::ARCHIVE_MALFORMED, "Archive central directory contains an invalid entry.")); + } + let flags = u16_at(data, cursor + 8); + let method = u16_at(data, cursor + 10); + let crc = u32_at(data, cursor + 16) as u32; + let name_length = usize::from(u16_at(data, cursor + 28)); + let extra_length = usize::from(u16_at(data, cursor + 30)); + let comment_length = usize::from(u16_at(data, cursor + 32)); + let external = u32_at(data, cursor + 38); + let entry_end = cursor + 46 + name_length + extra_length + comment_length; + if name_length == 0 || entry_end > data.len() { + return Err(fail(codes::ARCHIVE_MALFORMED, "Archive contains a truncated filename.")); + } + let extra_start = cursor + 46 + name_length; + let (compressed, uncompressed) = resolved_sizes(u32_at(data, cursor + 20), u32_at(data, cursor + 24), data, extra_start, extra_start + extra_length) + .ok_or_else(|| fail(codes::ARCHIVE_MALFORMED, "Archive contains malformed ZIP64 size metadata."))?; + if flags & 0x1 != 0 { + return Err(fail(codes::ARCHIVE_ENCRYPTED, "Encrypted package entries are not supported.")); + } + if method != 0 && method != 8 { + return Err(fail(codes::ARCHIVE_UNSUPPORTED_COMPRESSION, format!("Unsupported ZIP compression method {method}."))); + } + let unsafe_path = || fail(codes::ARCHIVE_UNSAFE_PATH, "Archive contains an unsafe or non-UTF-8 path."); + let name = std::str::from_utf8(&data[cursor + 46..cursor + 46 + name_length]).map_err(|_| unsafe_path())?; + let parts = entry_parts(name, limits.maximum_path_depth).ok_or_else(unsafe_path)?; + let is_directory = name.ends_with('/'); + if !collisions.add(name, &parts, is_directory) { + return Err(fail(codes::ARCHIVE_PATH_COLLISION, "Archive contains duplicate or case-colliding paths.")); + } + // Unix mode in the high half: only regular files (0x8000), directories (0x4000) or + // an absent type are accepted; 0xA000 is a symbolic link. + let kind = (external >> 16) & 0xF000; + if kind == 0xA000 { + return Err(fail(codes::ARCHIVE_SYMLINK, "Symbolic links are not allowed in ScriptWidget packages.")); + } + if kind != 0 && kind != 0x8000 && kind != 0x4000 { + return Err(fail(codes::ARCHIVE_SYMLINK, "Only regular files and directories are allowed in ScriptWidget packages.")); + } + if !is_directory { + if uncompressed > limits.maximum_file_bytes { + return Err(fail(codes::ARCHIVE_FILE_TOO_LARGE, format!("Package file {name} exceeds 25 MiB."))); + } + expanded += uncompressed; + if expanded > limits.maximum_expanded_bytes { + return Err(fail(codes::ARCHIVE_EXPANDED_TOO_LARGE, "Expanded package exceeds 64 MiB.")); + } + if uncompressed > 1024 * 1024 && compressed == 0 { + return Err(fail(codes::ARCHIVE_INVALID_RATIO, format!("Archive entry {name} has an invalid compression ratio."))); + } + } + entries.push(Entry { + name: name.to_owned(), + parts: parts.iter().map(|part| part.to_string()).collect(), + is_directory, + uncompressed_size: uncompressed, + compressed_size: compressed, + crc, + method, + local_header_offset: u32_at(data, cursor + 42), + }); + cursor = entry_end; + } + if cursor as u64 != central_offset + central_size { + return Err(fail(codes::ARCHIVE_MALFORMED, "Archive entries do not match the declared central directory.")); + } + Ok(entries) +} + +/// Components of an archive entry name, or `None` when the name is unsafe: a +/// leading slash, any backslash, colon (drive or stream syntax on Windows) or +/// NUL, a `.`/`..` component, a component that is too long or holds control +/// characters, no components, or more than `maximum_depth`. +pub(crate) fn entry_parts(name: &str, maximum_depth: usize) -> Option> { + if name.starts_with('/') || name.contains('\\') || name.contains(':') || name.contains('\0') { + return None; + } + let parts: Vec<&str> = name.split('/').filter(|part| !part.is_empty()).collect(); + (!parts.is_empty() && parts.len() <= maximum_depth && parts.iter().all(|part| path_rules::valid_component(part))).then_some(parts) +} + +/// Tracks every file and directory prefix seen so far. Two spellings of one +/// identity (case or Unicode form), a repeated path, and a path used as both +/// file and directory would overwrite or shadow each other on extraction, so +/// each is a collision. +#[derive(Default)] +struct Collisions { + full_paths: HashSet, + spellings: HashMap, + directories: HashMap, +} + +impl Collisions { + fn add(&mut self, name: &str, parts: &[&str], is_directory: bool) -> bool { + if !self.full_paths.insert(names::identity(name).trim_matches('/').to_owned()) { + return false; + } + let mut path = String::new(); + for (index, part) in parts.iter().enumerate() { + if index > 0 { + path.push('/'); + } + path.push_str(part); + if self.spellings.entry(names::identity(&path)).or_insert_with(|| path.clone()) != &path { + return false; + } + let directory = index + 1 < parts.len() || is_directory; + if *self.directories.entry(path.clone()).or_insert(directory) != directory { + return false; + } + } + true + } +} + +/// The compressed and uncompressed sizes, read from the ZIP64 extra field when a 32-bit field is saturated. +fn resolved_sizes(compressed32: u64, uncompressed32: u64, data: &[u8], extra_start: usize, extra_end: usize) -> Option<(u64, u64)> { + if compressed32 != U32_MAX && uncompressed32 != U32_MAX { + return Some((compressed32, uncompressed32)); + } + let mut compressed = compressed32; + let mut uncompressed = uncompressed32; + let mut cursor = extra_start; + while cursor + 4 <= extra_end { + let identifier = u16_at(data, cursor); + let end = cursor + 4 + usize::from(u16_at(data, cursor + 2)); + if end > extra_end { + return None; + } + if identifier == 0x0001 { + let mut value = cursor + 4; + if uncompressed32 == U32_MAX { + if value + 8 > end { + return None; + } + uncompressed = u64_at(data, value); + value += 8; + } + if compressed32 == U32_MAX { + if value + 8 > end { + return None; + } + compressed = u64_at(data, value); + } + return Some((compressed, uncompressed)); + } + cursor = end; + } + None +} + +/// Offset of the last end-of-central-directory record. +fn end_of_central_directory(data: &[u8]) -> Option { + if data.len() < 22 { + return None; + } + let lower_bound = data.len().saturating_sub(65_557); + (lower_bound..=data.len() - 22).rev().find(|&index| u32_at(data, index) == END_SIGNATURE) +} + +/// Little-endian readers; a read past the end yields 0, which no check accepts as a signature. +pub(crate) fn u16_at(data: &[u8], index: usize) -> u16 { + data.get(index..index + 2).map_or(0, |bytes| u16::from_le_bytes([bytes[0], bytes[1]])) +} + +pub(crate) fn u32_at(data: &[u8], index: usize) -> u64 { + data.get(index..index + 4).map_or(0, |bytes| u64::from(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))) +} + +fn u64_at(data: &[u8], index: usize) -> u64 { + data.get(index..index + 8).map_or(0, |bytes| u64::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]])) +} + +fn fail(code: &str, message: impl Into) -> PackageError { + PackageError::new(code, message) +} diff --git a/Linux/crates/sw-core/src/packages/safe_files.rs b/Linux/crates/sw-core/src/packages/safe_files.rs new file mode 100644 index 00000000..a65d8813 --- /dev/null +++ b/Linux/crates/sw-core/src/packages/safe_files.rs @@ -0,0 +1,570 @@ +//! Link-refusing filesystem primitives shared by the package layer. Ported +//! from `SafeFiles.cs`, with the parts Linux can do better done better. +//! +//! Every lookup looks at the path itself (`lstat`, `O_NOFOLLOW`), so a +//! symbolic link is seen as a link and never as its target. +//! +//! Files and directories inside a package are opened relative to a +//! descriptor of the package directory with `openat2` and +//! `RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS`: the +//! kernel resolves the whole relative path in one call and refuses it when +//! any component is a symbolic link or would leave the directory, so there is +//! no window between a check and the open. The opened descriptor is then +//! examined with `fstat`: only a regular file with a single link is read. +//! +//! Fallback. `openat2` exists since Linux 5.6. Where the call is missing +//! (`ENOSYS`: an older kernel or another Unix such as the Mac used for +//! development) or refused by a sandbox's system call filter (`EPERM`), and +//! when it keeps reporting `EAGAIN` because the tree is being renamed +//! underneath it, the path is walked one component at a time instead: each +//! component is opened relative to the descriptor of its parent with +//! `openat` and `O_NOFOLLOW`. A component is a single name (never `..`, never +//! containing a slash), so the walk can only go down, and no step follows a +//! link; it gives the same guarantee with one system call per component. +//! +//! The Windows port has a canonical-path check (`Inside`) because Win32 +//! rewrites the paths it is given. Here nothing is rewritten and the +//! descriptor-relative opens enforce the boundary, so there is no such check. + +use std::fs::File; +use std::io::{self, Read, Write}; +use std::os::fd::{AsFd, OwnedFd}; +use std::os::unix::fs::{DirBuilderExt, MetadataExt}; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; + +use rustix::fs::{AtFlags, Dir, FileType, Mode, OFlags, Stat}; +use rustix::io::Errno; + +use super::error::{PackageError, codes}; +use super::limits::MAX_DIRECTORY_ENTRIES; +use super::names; + +/// What [`attributes`] reports about a path itself, never about a link target. +#[derive(Debug, Clone, Copy)] +pub(crate) struct FileEntry { + /// A directory that is not a symbolic link. + pub is_directory: bool, + pub is_symbolic_link: bool, + /// A plain file: not a directory, link, device, socket or FIFO. + pub is_regular_file: bool, + /// File size in bytes as the filesystem reports it. + pub size: u64, + /// Number of hard links. + pub links: u64, +} + +impl FileEntry { + #[allow(clippy::unnecessary_cast)] // The field types differ between Linux architectures and macOS. + fn of(stat: &Stat) -> Self { + let kind = FileType::from_raw_mode(stat.st_mode); + Self { + is_directory: kind == FileType::Directory, + is_symbolic_link: kind == FileType::Symlink, + is_regular_file: kind == FileType::RegularFile, + size: u64::try_from(stat.st_size).unwrap_or(u64::MAX), + links: stat.st_nlink as u64, + } + } + + /// A regular file that no second hard link could expose or alter from outside the package. + pub fn is_private_regular_file(&self) -> bool { + self.is_regular_file && self.links <= 1 + } +} + +/// Entry names of a directory. A name that is not UTF-8 cannot be a package +/// path; it is left out of `names` and reported by `foreign`. +#[derive(Debug, Default)] +pub(crate) struct Listing { + pub names: Vec, + pub foreign: bool, +} + +/// Flags for opening a directory itself, never a link to one. +pub(crate) const DIRECTORY_FLAGS: OFlags = OFlags::RDONLY.union(OFlags::DIRECTORY).union(OFlags::NOFOLLOW).union(OFlags::CLOEXEC); +/// Flags for reading a file. `O_NONBLOCK` keeps a FIFO from blocking the open; it has no effect on a regular file. +const READ_FLAGS: OFlags = OFlags::RDONLY.union(OFlags::NOFOLLOW).union(OFlags::CLOEXEC).union(OFlags::NONBLOCK).union(OFlags::NOCTTY); +const DIRECTORY_MODE: u32 = 0o700; +const FILE_MODE: u32 = 0o600; + +/// The absolute form of a caller-supplied root directory. Links are not resolved. +pub(crate) fn root(path: &Path) -> PathBuf { + std::path::absolute(path).unwrap_or_else(|_| path.to_owned()) +} + +/// `name` directly below `directory`. Joined as text: `Path::join` would let a rooted name replace the directory. +pub(crate) fn child(directory: &Path, name: &str) -> PathBuf { + let mut joined = directory.as_os_str().to_owned(); + if !joined.as_encoded_bytes().ends_with(b"/") { + joined.push("/"); + } + joined.push(name); + PathBuf::from(joined) +} + +/// Attributes of `path` itself (never of a link target), or `None` when it is absent or unreadable. +pub(crate) fn attributes(path: &Path) -> Option { + let metadata = std::fs::symlink_metadata(path).ok()?; + let kind = metadata.file_type(); + Some(FileEntry { is_directory: kind.is_dir(), is_symbolic_link: kind.is_symlink(), is_regular_file: kind.is_file(), size: metadata.len(), links: metadata.nlink() }) +} + +/// True when something, including a dangling link, occupies `path`. Present but unreadable still occupies the name. +pub(crate) fn occupied(path: &Path) -> bool { + !matches!(std::fs::symlink_metadata(path), Err(error) if matches!(error.kind(), io::ErrorKind::NotFound | io::ErrorKind::NotADirectory)) +} + +/// True when `path` is a real directory rather than a link to one. +pub(crate) fn real_directory(path: &Path) -> bool { + attributes(path).is_some_and(|entry| entry.is_directory) +} + +/// Opens `path` as a directory; fails when it is a symbolic link or not a directory. +pub(crate) fn open_directory(path: &Path) -> io::Result { + Ok(rustix::fs::open(path, DIRECTORY_FLAGS, Mode::empty())?) +} + +/// Entry names of `directory`, or `None` when it cannot be listed, is a link, or is implausibly large. +pub(crate) fn names(directory: &Path) -> Option { + names_of(&open_directory(directory).ok()?) +} + +/// [`names`] for an open directory. +pub(crate) fn names_of(directory: &impl AsFd) -> Option { + let mut listing = Listing::default(); + let mut count = 0; + for entry in Dir::read_from(directory).ok()? { + let entry = entry.ok()?; + let name = entry.file_name().to_bytes(); + if name == b"." || name == b".." { + continue; + } + count += 1; + if count > MAX_DIRECTORY_ENTRIES { + return None; + } + match std::str::from_utf8(name) { + Ok(name) => listing.names.push(name.to_owned()), + Err(_) => listing.foreign = true, + } + } + Some(listing) +} + +/// False when `names` holds a different spelling of `name` (another case or +/// Unicode form). Such aliases resolve to one file on some filesystems and +/// two on others, so they are refused. +pub(crate) fn unambiguous(names: &[String], name: &str) -> bool { + let identity = names::identity(name); + !names.iter().any(|other| other != name && names::identity(other) == identity) +} + +/// One name of a directory entry: nothing that could address another directory. +fn is_single_name(name: &str) -> bool { + !name.is_empty() && name != "." && name != ".." && !name.contains('/') && !name.contains('\0') +} + +fn invalid_name() -> io::Error { + io::Error::new(io::ErrorKind::InvalidInput, "not a single file name") +} + +/// Opens `parts` below the open directory `root` without following any link +/// and without leaving `root`. See the module description for the two ways +/// this is done. +pub(crate) fn open_beneath(root: &impl AsFd, parts: &[&str], flags: OFlags) -> io::Result { + if parts.is_empty() || !parts.iter().all(|part| is_single_name(part)) { + return Err(invalid_name()); + } + #[cfg(target_os = "linux")] + if !beneath::UNAVAILABLE.load(Ordering::Relaxed) { + match beneath::open(root, parts, flags) { + Err(Errno::NOSYS) => beneath::UNAVAILABLE.store(true, Ordering::Relaxed), + Err(Errno::PERM | Errno::AGAIN) => {} + result => return Ok(result?), + } + } + open_by_walking(root, parts, flags) +} + +#[cfg(target_os = "linux")] +pub(crate) mod beneath { + use std::os::fd::{AsFd, OwnedFd}; + use std::sync::atomic::AtomicBool; + + use rustix::fs::{Mode, OFlags, ResolveFlags}; + use rustix::io::Errno; + + /// Set once the kernel has answered `ENOSYS`, so later opens go straight to the walk. + pub(crate) static UNAVAILABLE: AtomicBool = AtomicBool::new(false); + + /// One `openat2` call for the whole relative path. `EAGAIN` means the + /// kernel could not prove the path stayed beneath `root` while something + /// was being renamed; it is retried a few times. + pub(crate) fn open(root: &impl AsFd, parts: &[&str], flags: OFlags) -> Result { + let path = parts.join("/"); + let resolve = ResolveFlags::BENEATH | ResolveFlags::NO_SYMLINKS | ResolveFlags::NO_MAGICLINKS; + let mut attempts = 0; + loop { + match rustix::fs::openat2(root, path.as_str(), flags | OFlags::NOFOLLOW, Mode::empty(), resolve) { + Err(Errno::AGAIN) if attempts < 3 => attempts += 1, + result => return result, + } + } + } +} + +/// The fallback of [`open_beneath`]: one `openat` with `O_NOFOLLOW` per component. +pub(crate) fn open_by_walking(root: &impl AsFd, parts: &[&str], flags: OFlags) -> io::Result { + let mut current: Option = None; + for (index, name) in parts.iter().enumerate() { + if !is_single_name(name) { + return Err(invalid_name()); + } + let component_flags = if index + 1 == parts.len() { flags | OFlags::NOFOLLOW } else { DIRECTORY_FLAGS }; + let next = match ¤t { + Some(directory) => rustix::fs::openat(directory, *name, component_flags, Mode::empty())?, + None => rustix::fs::openat(root, *name, component_flags, Mode::empty())?, + }; + current = Some(next); + } + current.ok_or_else(invalid_name) +} + +/// Attributes of the entry `name` of an open directory, never of a link target. +pub(crate) fn attributes_at(directory: &impl AsFd, name: &str) -> io::Result { + if !is_single_name(name) { + return Err(invalid_name()); + } + Ok(FileEntry::of(&rustix::fs::statat(directory, name, AtFlags::SYMLINK_NOFOLLOW)?)) +} + +/// Opens the directory `name` of an open directory, creating it when it does not exist. +pub(crate) fn create_directory_at(directory: &impl AsFd, name: &str) -> io::Result { + if !is_single_name(name) { + return Err(invalid_name()); + } + match rustix::fs::mkdirat(directory, name, Mode::from_raw_mode(DIRECTORY_MODE as _)) { + Ok(()) | Err(Errno::EXIST) => {} + Err(error) => return Err(error.into()), + } + open_beneath(directory, &[name], DIRECTORY_FLAGS) +} + +/// Creates the file `name` in an open directory; fails when anything, a link included, already has that name. +pub(crate) fn create_file_at(directory: &impl AsFd, name: &str) -> io::Result { + if !is_single_name(name) { + return Err(invalid_name()); + } + let flags = OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::NOFOLLOW | OFlags::CLOEXEC; + Ok(File::from(rustix::fs::openat(directory, name, flags, Mode::from_raw_mode(FILE_MODE as _))?)) +} + +/// Creates `name` exclusively in an open directory, writes `bytes` and flushes them to storage. +pub(crate) fn write_new_at(directory: &impl AsFd, name: &str, bytes: &[u8]) -> io::Result<()> { + let mut file = create_file_at(directory, name)?; + file.write_all(bytes)?; + file.sync_all() +} + +/// Resolves `parts` below `root` to an existing regular file and opens it for reading, or returns `None`. +/// +/// Each component must be spelled exactly as stored and must not have a case +/// or normalization alias; intermediate components must be directories and +/// the last one a regular file with a single link; none may be a link. The +/// open itself is [`open_beneath`], and the link count and file type are +/// checked again on the open descriptor before anything is read. +pub(crate) fn existing_file(root: &Path, parts: &[&str]) -> Option { + if parts.is_empty() || !real_directory(root) { + return None; + } + let mut directory = root.to_owned(); + for (index, name) in parts.iter().enumerate() { + let siblings = names(&directory)?.names; + if !siblings.iter().any(|sibling| sibling == name) || !unambiguous(&siblings, name) { + return None; + } + let entry_path = child(&directory, name); + let entry = attributes(&entry_path)?; + if entry.is_symbolic_link { + return None; + } + if index + 1 < parts.len() { + if !entry.is_directory { + return None; + } + directory = entry_path; + } else if !entry.is_private_regular_file() { + return None; + } + } + let file = open_beneath(&open_directory(root).ok()?, parts, READ_FLAGS).ok()?; + FileEntry::of(&rustix::fs::fstat(&file).ok()?).is_private_regular_file().then(|| File::from(file)) +} + +/// Reads `file`, or returns `None` when it holds more than `maximum_bytes`. +/// The size is counted while reading, never taken from metadata that may change. +pub(crate) fn read_bounded(file: File, maximum_bytes: u64) -> Option> { + let mut bytes = Vec::new(); + file.take(maximum_bytes.saturating_add(1)).read_to_end(&mut bytes).ok()?; + (bytes.len() as u64 <= maximum_bytes).then_some(bytes) +} + +/// Strict UTF-8 decoding: malformed input yields `None` instead of replacement characters. +pub(crate) fn decode_utf8(bytes: Vec) -> Option { + String::from_utf8(bytes).ok() +} + +/// A random version 4 UUID in its usual text form. +pub(crate) fn unique_token() -> String { + static COUNTER: AtomicU64 = AtomicU64::new(0); + let mut bytes = [0u8; 16]; + if getrandom::fill(&mut bytes).is_err() { + // No entropy source: time, process and a counter still give a name of its own. + let nanoseconds = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |elapsed| elapsed.as_nanos() as u64); + bytes[..8].copy_from_slice(&nanoseconds.to_le_bytes()); + bytes[8..12].copy_from_slice(&std::process::id().to_le_bytes()); + bytes[12..].copy_from_slice(&(COUNTER.fetch_add(1, Ordering::Relaxed) as u32).to_le_bytes()); + } + bytes[6] = (bytes[6] & 0x0F) | 0x40; + bytes[8] = (bytes[8] & 0x3F) | 0x80; + let hex: String = bytes.iter().map(|byte| format!("{byte:02x}")).collect(); + format!("{}-{}-{}-{}-{}", &hex[..8], &hex[8..12], &hex[12..16], &hex[16..20], &hex[20..]) +} + +/// Creates a directory readable by the user only; fails when the name is taken. +pub(crate) fn create_private_directory(path: &Path) -> io::Result<()> { + std::fs::DirBuilder::new().mode(DIRECTORY_MODE).create(path) +} + +/// Creates a uniquely named private working directory below `staging_root`. +pub(crate) fn new_work_directory(staging_root: &Path, prefix: &str) -> Result { + (|| { + std::fs::DirBuilder::new().recursive(true).mode(DIRECTORY_MODE).create(staging_root)?; + if !real_directory(staging_root) { + return Err(io::Error::other("Staging directory is a link")); + } + let work = child(staging_root, &format!("{prefix}-{}", unique_token())); + create_private_directory(&work)?; + Ok(work) + })() + .map_err(|error: io::Error| PackageError::caused(codes::STORAGE_UNAVAILABLE, "The staging directory is unavailable.", error)) +} + +/// Publishes a prepared directory with one atomic rename, so observers see +/// either nothing or the complete package. `destination` must be free. +pub(crate) fn publish(prepared: &Path, destination: &Path) -> Result<(), PackageError> { + let taken = || PackageError::new(codes::PACKAGE_EXISTS, "A package with this name already exists."); + if occupied(destination) { + return Err(taken()); + } + rename_without_replacing(prepared, destination).map_err(|error| match error.kind() { + io::ErrorKind::AlreadyExists => taken(), + io::ErrorKind::CrossesDevices => PackageError::caused(codes::STORAGE_UNAVAILABLE, "Staging and library directories must share a filesystem.", error), + _ => PackageError::caused(codes::STORAGE_UNAVAILABLE, "The package could not be published.", error), + }) +} + +/// Renames `from` to `to`, which must not exist. +/// +/// On Linux this is `renameat2` with `RENAME_NOREPLACE`: the kernel refuses +/// an existing destination in the same step as the rename, so nothing that +/// appears there in the meantime is replaced (a plain `rename` silently +/// replaces an empty directory). Where the filesystem or the kernel does not +/// know the flag, and on other systems, it is a plain rename: callers check +/// the destination first and hold the store lock, as on Android. +pub(crate) fn rename_without_replacing(from: &Path, to: &Path) -> io::Result<()> { + #[cfg(target_os = "linux")] + match rustix::fs::renameat_with(rustix::fs::CWD, from, rustix::fs::CWD, to, rustix::fs::RenameFlags::NOREPLACE) { + Err(Errno::INVAL | Errno::NOSYS) => {} + result => return Ok(result?), + } + std::fs::rename(from, to) +} + +/// Removes `path` and everything below it. Links are removed, never followed. Best effort: +/// leftovers stay inside the private staging directory and are retried by later cleanups. +pub(crate) fn delete_tree(path: &Path) { + let Some(entry) = attributes(path) else { return }; + if !entry.is_directory { + let _ = std::fs::remove_file(path); + return; + } + if std::fs::remove_dir_all(path).is_err() { + // A directory without write or search permission keeps its entries; open it up and try once more. + make_removable(path, 0); + let _ = std::fs::remove_dir_all(path); + } +} + +fn make_removable(directory: &Path, depth: usize) { + // Callers have seen a real directory here (`lstat`); chmod itself would follow a link. + let _ = std::fs::set_permissions(directory, std::os::unix::fs::PermissionsExt::from_mode(DIRECTORY_MODE)); + if depth >= 64 { + return; + } + let Some(listing) = names(directory) else { return }; + for name in listing.names { + let entry_path = child(directory, &name); + if real_directory(&entry_path) { + make_removable(&entry_path, depth + 1); + } + } +} + +#[cfg(test)] +mod tests { + use std::os::unix::fs::symlink; + + use super::*; + + /// A directory of its own under the system temporary directory, removed when the test ends. + struct Scratch(PathBuf); + + impl Scratch { + fn new() -> Self { + let path = std::env::temp_dir().join(format!("scriptwidget-safe-files-{}", unique_token())); + std::fs::create_dir_all(&path).expect("scratch directory"); + Self(path) + } + + /// `root/` with `a.txt`, `sub/b.txt`, a link to a file outside, a link to a directory outside, and `outside/secret.txt` next to it. + fn populated() -> (Self, OwnedFd) { + let scratch = Self::new(); + let root = scratch.0.join("root"); + std::fs::create_dir_all(root.join("sub")).unwrap(); + std::fs::create_dir_all(scratch.0.join("outside")).unwrap(); + std::fs::write(root.join("a.txt"), "a").unwrap(); + std::fs::write(root.join("sub/b.txt"), "b").unwrap(); + std::fs::write(scratch.0.join("outside/secret.txt"), "secret").unwrap(); + symlink(scratch.0.join("outside/secret.txt"), root.join("link.txt")).unwrap(); + symlink(scratch.0.join("outside"), root.join("linkdir")).unwrap(); + symlink("a.txt", root.join("inside.txt")).unwrap(); + symlink("../outside", root.join("sub/up")).unwrap(); + let handle = open_directory(&root).expect("open root"); + (scratch, handle) + } + } + + impl Drop for Scratch { + fn drop(&mut self) { + delete_tree(&self.0); + } + } + + fn text(file: OwnedFd) -> String { + let mut content = String::new(); + File::from(file).read_to_string(&mut content).expect("read"); + content + } + + /// The cases every way of opening must agree on. + fn assert_refuses_links_and_escapes(open: impl Fn(&OwnedFd, &[&str]) -> io::Result, root: &OwnedFd) { + assert_eq!(text(open(root, &["a.txt"]).expect("a.txt")), "a"); + assert_eq!(text(open(root, &["sub", "b.txt"]).expect("sub/b.txt")), "b"); + for parts in [&["link.txt"][..], &["inside.txt"], &["linkdir", "secret.txt"], &["sub", "up", "secret.txt"], &["missing.txt"], &["a.txt", "child"]] { + assert!(open(root, parts).is_err(), "{parts:?} must not open"); + } + } + + #[test] + fn open_beneath_refuses_links_at_every_component() { + let (_scratch, root) = Scratch::populated(); + assert_refuses_links_and_escapes(|root, parts| open_beneath(root, parts, READ_FLAGS), &root); + } + + #[test] + fn the_walking_fallback_refuses_links_at_every_component() { + let (_scratch, root) = Scratch::populated(); + assert_refuses_links_and_escapes(|root, parts| open_by_walking(root, parts, READ_FLAGS), &root); + } + + #[test] + fn names_that_are_not_single_components_never_reach_the_kernel() { + let (_scratch, root) = Scratch::populated(); + for parts in [&[][..], &[""], &["."], &[".."], &["..", "outside", "secret.txt"], &["sub/b.txt"], &["/etc/passwd"], &["sub", "..", "a.txt"], &["a\0.txt"]] { + assert_eq!(open_beneath(&root, parts, READ_FLAGS).map(drop).map_err(|error| error.kind()), Err(io::ErrorKind::InvalidInput), "{parts:?}"); + assert_eq!(open_by_walking(&root, parts, READ_FLAGS).map(drop).map_err(|error| error.kind()), Err(io::ErrorKind::InvalidInput), "{parts:?}"); + } + assert!(attributes_at(&root, "../outside").is_err()); + assert!(create_file_at(&root, "sub/new.txt").is_err()); + assert!(create_directory_at(&root, "..").is_err()); + } + + /// The kernel itself enforces the boundary: this test fails on a kernel + /// without `openat2`, so a run that passes has used it. + #[cfg(target_os = "linux")] + #[test] + fn openat2_is_available_and_resolves_beneath_without_links() { + let (_scratch, root) = Scratch::populated(); + assert_refuses_links_and_escapes(|root, parts| beneath::open(root, parts, READ_FLAGS).map_err(io::Error::from), &root); + assert!(!beneath::UNAVAILABLE.load(Ordering::Relaxed), "openat2 must not have been reported missing"); + // Given raw paths, which the package layer never passes, the kernel still refuses to leave the directory. + let resolve = rustix::fs::ResolveFlags::BENEATH | rustix::fs::ResolveFlags::NO_SYMLINKS | rustix::fs::ResolveFlags::NO_MAGICLINKS; + let raw = |path: &str| rustix::fs::openat2(&root, path, READ_FLAGS, Mode::empty(), resolve).map(drop); + assert_eq!(raw("../outside/secret.txt"), Err(Errno::XDEV)); + assert_eq!(raw("sub/../../outside/secret.txt"), Err(Errno::XDEV)); + assert_eq!(raw("/etc/hostname"), Err(Errno::XDEV)); + assert_eq!(raw("link.txt"), Err(Errno::LOOP)); + assert_eq!(raw("linkdir/secret.txt"), Err(Errno::LOOP)); + assert_eq!(raw("sub/../a.txt"), Ok(())); + } + + #[test] + fn created_files_and_directories_are_private_and_exclusive() { + let (scratch, root) = Scratch::populated(); + let made = create_directory_at(&root, "made").expect("create directory"); + write_new_at(&made, "new.txt", b"new").expect("write"); + assert_eq!(std::fs::read(scratch.0.join("root/made/new.txt")).unwrap(), b"new"); + assert_eq!(std::fs::symlink_metadata(scratch.0.join("root/made")).unwrap().mode() & 0o777, 0o700); + assert_eq!(std::fs::symlink_metadata(scratch.0.join("root/made/new.txt")).unwrap().mode() & 0o777, 0o600); + // An existing name is never replaced or written through, whatever it is. + assert!(write_new_at(&made, "new.txt", b"again").is_err()); + assert!(write_new_at(&root, "link.txt", b"owned").is_err()); + symlink(scratch.0.join("outside/created.txt"), scratch.0.join("root/dangling.txt")).unwrap(); + assert!(write_new_at(&root, "dangling.txt", b"owned").is_err()); + assert!(create_directory_at(&root, "linkdir").is_err()); + assert!(create_directory_at(&root, "a.txt").is_err()); + assert_eq!(std::fs::read(scratch.0.join("outside/secret.txt")).unwrap(), b"secret"); + assert!(!scratch.0.join("outside/created.txt").exists()); + } + + /// Without the flag, `rename` would put the new directory in place of the empty one. + #[cfg(target_os = "linux")] + #[test] + fn the_exclusive_rename_refuses_whatever_is_at_the_destination() { + let scratch = Scratch::new(); + let prepared = scratch.0.join("prepared"); + std::fs::create_dir(&prepared).unwrap(); + std::fs::write(prepared.join("main.jsx"), "new").unwrap(); + let empty = scratch.0.join("empty"); + std::fs::create_dir(&empty).unwrap(); + std::fs::write(scratch.0.join("file"), "mine").unwrap(); + symlink(scratch.0.join("nowhere"), scratch.0.join("dangling")).unwrap(); + for taken in ["empty", "file", "dangling"] { + let error = rename_without_replacing(&prepared, &scratch.0.join(taken)).expect_err(taken); + assert_eq!(error.kind(), io::ErrorKind::AlreadyExists, "{taken}"); + } + assert!(prepared.join("main.jsx").exists()); + assert_eq!(std::fs::read_dir(&empty).unwrap().count(), 0); + assert_eq!(std::fs::read(scratch.0.join("file")).unwrap(), b"mine"); + rename_without_replacing(&prepared, &scratch.0.join("free")).unwrap(); + assert_eq!(std::fs::read(scratch.0.join("free/main.jsx")).unwrap(), b"new"); + assert!(!prepared.exists()); + } + + #[test] + fn unique_tokens_are_version_4_uuids() { + let token = unique_token(); + assert_eq!(token.len(), 36); + assert_eq!(token.as_bytes()[14], b'4'); + assert!(token.bytes().all(|byte| byte == b'-' || byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)), "{token}"); + assert_ne!(token, unique_token()); + } + + #[test] + fn child_never_lets_a_rooted_name_replace_the_directory() { + assert_eq!(child(Path::new("/data/root"), "/etc/passwd"), PathBuf::from("/data/root//etc/passwd")); + assert_eq!(child(Path::new("/data/root/"), "a.js"), PathBuf::from("/data/root/a.js")); + } +} diff --git a/Linux/crates/sw-core/src/packages/store.rs b/Linux/crates/sw-core/src/packages/store.rs new file mode 100644 index 00000000..39b163c7 --- /dev/null +++ b/Linux/crates/sw-core/src/packages/store.rs @@ -0,0 +1,417 @@ +//! The library of installed packages. Ported from `PackageStore.cs`, itself +//! `ScriptManager` in Swift. + +use std::collections::{BTreeMap, HashSet}; +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; +use std::sync::{Mutex, MutexGuard}; + +use serde_json::Value; + +use super::archive::{self, ExtractedPackage}; +use super::disk_files::DiskPackageFiles; +use super::error::{PackageError, codes}; +use super::limits::{ArchiveLimits, MAX_FILE_BYTES, MAX_METADATA_BYTES, MAX_NAME_ATTEMPTS, MAX_SCRIPT_BYTES}; +use super::{file_remover, file_writer, manifests, names, path_rules, safe_files, tree}; +use crate::json::Object; +use crate::package_files::PackageFiles; + +/// One installed package as shown in the library. The manifest fields are +/// display values read leniently from `widget.json`; they are `None` when the +/// manifest is absent, unreadable or the field is not a string. They are not +/// validated. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PackageInfo { + /// The package (directory) name. + pub name: String, + /// Absolute path of the package directory. + pub directory: PathBuf, + /// True when any spelling of `widget.json` is present; such a package is never treated as legacy. + pub has_manifest: bool, + pub manifest_id: Option, + pub manifest_name: Option, + pub manifest_version: Option, +} + +/// A check run on a staged package before it is published; an error vetoes the import. +pub type ImportValidator<'a> = &'a dyn Fn(&dyn PackageFiles) -> Result<(), PackageError>; + +/// The library of installed packages, one directory per package under +/// `library_root//`. +/// +/// Everything that enters the library is prepared in a unique directory under +/// the staging root and published with one atomic rename, so a failed create, +/// import or duplicate leaves no partial package. The staging root must be on +/// the same filesystem as the library root and must not be inside it. Package +/// names follow [`names::is_valid`], and a name is usable only when no other +/// library entry has the same identity after case folding and NFC +/// normalization. +/// +/// Mutations are serialized per store instance; use one instance per library. +#[derive(Debug)] +pub struct PackageStore { + library: PathBuf, + staging: PathBuf, + limits: ArchiveLimits, + gate: Mutex<()>, +} + +impl PackageStore { + pub fn new(library_root: impl AsRef, staging_root: impl AsRef) -> Self { + Self::with_limits(library_root, staging_root, ArchiveLimits::default()) + } + + pub fn with_limits(library_root: impl AsRef, staging_root: impl AsRef, limits: ArchiveLimits) -> Self { + Self { library: safe_files::root(library_root.as_ref()), staging: safe_files::root(staging_root.as_ref()), limits, gate: Mutex::new(()) } + } + + /// Installed packages sorted by name. Entries that are links, files, invalid or ambiguous names are not packages. + pub fn list(&self) -> Result, PackageError> { + let names = self.library_names()?; + let mut sorted: Vec<&String> = names.iter().collect(); + sorted.sort_unstable(); + let mut result = Vec::new(); + for name in sorted { + if !names::is_valid(name) || !safe_files::unambiguous(&names, name) { + continue; + } + let directory = safe_files::child(&self.library, name); + if safe_files::real_directory(&directory) { + result.push(info(name, directory)); + } + } + Ok(result) + } + + /// Read-only, boundary-checked view of an installed package. + pub fn files(&self, name: &str) -> Result { + Ok(DiskPackageFiles::new(self.existing(name)?)) + } + + /// `recommended` if it is free, otherwise the first free `" (n)"` + /// for n in 1…10000, with the stem shortened to keep the name limits + /// (`ScriptManager.getValidPackageName`). + /// + /// Fails with `invalid_name` when `recommended` is invalid or no name is free. + pub fn unique_name(&self, recommended: &str) -> Result { + let _guard = self.lock(); + self.unique_name_locked(recommended) + } + + fn unique_name_locked(&self, recommended: &str) -> Result { + require_valid(recommended)?; + let identities: HashSet = self.library_names()?.iter().map(|name| names::identity(name)).collect(); + let available = |name: &str| names::is_valid(name) && !identities.contains(&names::identity(name)); + if available(recommended) { + return Ok(recommended.to_owned()); + } + (1..=MAX_NAME_ATTEMPTS) + .map(|index| names::with_suffix(recommended, &format!(" ({index})"))) + .find(|candidate| available(candidate)) + .ok_or_else(|| PackageError::new(codes::INVALID_NAME, "No free package name is available.")) + } + + /// Creates a package named exactly `name` from `main_source` and a + /// manifest. Without `manifest_json` a default Package 2.0 manifest is + /// generated. The source is written to the manifest's `entry` (default + /// `main.jsx`). The package becomes visible only after source and + /// manifest are fully written. + /// + /// `manifest_json` must be a JSON object of at most 256 KiB with a safe + /// `.js`/`.jsx` entry path; its semantic validation is the caller's + /// responsibility. + /// + /// Fails with `invalid_name`, `package_exists`, `invalid_manifest`, `invalid_entry` or `file_too_large`. + pub fn create(&self, name: &str, main_source: &str, manifest_json: Option<&str>) -> Result { + require_valid(name)?; + if main_source.len() > MAX_SCRIPT_BYTES { + return Err(PackageError::new(codes::FILE_TOO_LARGE, "Script source exceeds 512 KiB.")); + } + let mut entry = manifests::LEGACY_ENTRY.to_owned(); + let manifest_bytes = match manifest_json { + None => manifests::encode(&manifests::new_manifest(name)), + Some(text) => { + if text.len() > MAX_METADATA_BYTES { + return Err(PackageError::new(codes::FILE_TOO_LARGE, "widget.json exceeds 256 KiB.")); + } + let invalid = || PackageError::new(codes::INVALID_MANIFEST, "widget.json is invalid."); + let manifest = manifests::parse(Some(text)).ok_or_else(invalid)?; + entry = manifests::string_value(&manifest, "entry").ok_or_else(invalid)?; + if path_rules::visible_components(&entry).is_none() || entry == manifests::FILE_NAME || !is_entry_file(&entry) { + return Err(PackageError::new(codes::INVALID_ENTRY, "entry must be a package-relative .js or .jsx file.")); + } + text.as_bytes().to_vec() + } + }; + self.staged("create", |prepared| { + file_writer::write(prepared, &entry, main_source.as_bytes())?; + file_writer::write(prepared, manifests::FILE_NAME, &manifest_bytes)?; + tree::validate_tree(prepared, &self.limits)?; + let _guard = self.lock(); + if !self.free(name)? { + return Err(PackageError::new(codes::PACKAGE_EXISTS, "A package with this name already exists.")); + } + let destination = safe_files::child(&self.library, name); + safe_files::publish(prepared, &destination)?; + Ok(info(name, destination)) + }) + } + + /// Imports a `.swt` archive and returns the installed package. + /// + /// The archive is preflighted and extracted by [`archive::extract`]. The + /// installed directory name comes from the manifest's `name`, never from + /// the archive file name; on a collision it gets the next free `" (n)"` + /// suffix ([`PackageStore::unique_name`]). A package without + /// `widget.json` is legacy: it must contain `main.jsx` and receives a + /// generated manifest named after the archive's top-level directory, or + /// `suggested_name` when the files sit at the archive root. A present + /// manifest is authoritative: a wrongly cased, non-UTF-8, non-object or + /// nameless `widget.json` fails the import instead of being reinterpreted + /// as legacy. + /// + /// `validate` runs on the staged package before publication (plug the + /// shared manifest validator, [`crate::PackageCompiler::validate_manifest`], + /// in here); if it fails, nothing is installed. A rejected import leaves + /// nothing in the library or the staging directory. + pub fn import_archive(&self, input: &mut dyn Read, suggested_name: Option<&str>, validate: Option>) -> Result { + let extracted: ExtractedPackage = archive::extract(input, &self.staging, &self.limits)?; + let prepared = extracted.directory(); + let files = DiskPackageFiles::new(prepared); + let name = if manifest_present(prepared)? { + manifests::string_value(&read_manifest(&files)?, "name").ok_or_else(|| PackageError::new(codes::INVALID_MANIFEST, "widget.json is invalid."))? + } else { + if !files.exists(manifests::LEGACY_ENTRY) { + return Err(PackageError::new(codes::MISSING_ENTRY, "Entry file main.jsx does not exist.")); + } + let name = match (extracted.root_name(), suggested_name) { + (Some(root), _) => root.to_owned(), + (None, Some(suggested)) if names::is_valid(suggested) => suggested.to_owned(), + _ => "Imported Widget".to_owned(), + }; + require_valid(&name)?; + file_writer::write(prepared, manifests::FILE_NAME, &manifests::encode(&legacy_manifest(&name, &files)))?; + name + }; + require_valid(&name)?; + if let Some(validate) = validate { + validate(&files)?; + } + let _guard = self.lock(); + let installed = self.unique_name_locked(&name)?; + let destination = safe_files::child(&self.library, &installed); + extracted.move_to(&destination)?; + Ok(info(&installed, destination)) + } + + /// Exports an installed package as a `.swt` archive under one top-level + /// directory named after the package. `__Build`, dot-files and links are + /// not exported. A legacy package is exported with a generated + /// `widget.json` (the installed package is not modified); a package whose + /// present manifest is unreadable is not exported. + pub fn export_archive(&self, name: &str, output: &mut dyn Write) -> Result<(), PackageError> { + export_directory(&self.existing(name)?, name, output, &self.limits) + } + + /// Copies a package to a new `" Remix"` package (made unique like + /// [`PackageStore::unique_name`]) and returns it. The source tree must be + /// free of links and special files. `__Build` and dot-files are not + /// copied. A Package 2.0 copy gets a fresh `id` and its new name; a + /// legacy package stays legacy (`ScriptManager.duplicateScript`). + pub fn duplicate(&self, name: &str) -> Result { + let source = self.existing(name)?; + tree::validate_tree(&source, &self.limits)?; + let files = DiskPackageFiles::new(&source); + let mut manifest = if manifest_present(&source)? { Some(read_manifest(&files)?) } else { None }; + self.staged("duplicate", |prepared| { + for path in files.list()? { + let bytes = files.read_bytes_up_to(&path, self.limits.maximum_file_bytes).ok_or_else(|| PackageError::new(codes::UNSAFE_FILE, "Package file could not be verified."))?; + file_writer::write(prepared, &path, &bytes)?; + } + let _guard = self.lock(); + let copy = self.unique_name_locked(&names::with_suffix(name, " Remix"))?; + if let Some(manifest) = &mut manifest { + manifest.insert("id".into(), Value::String(manifests::fresh_id())); + manifest.insert("name".into(), Value::String(copy.clone())); + file_writer::write(prepared, manifests::FILE_NAME, &manifests::encode(manifest))?; + } + tree::validate_tree(prepared, &self.limits)?; + let destination = safe_files::child(&self.library, ©); + safe_files::publish(prepared, &destination)?; + Ok(info(©, destination)) + }) + } + + /// Renames a package directory atomically. The manifest is left + /// unchanged, as in Swift. `new_name` must be valid and free; a change of + /// case only is refused as ambiguous. + pub fn rename(&self, name: &str, new_name: &str) -> Result { + let _guard = self.lock(); + let source = self.existing(name)?; + require_valid(new_name)?; + let destination = safe_files::child(&self.library, new_name); + if name != new_name { + if !self.free(new_name)? { + return Err(PackageError::new(codes::PACKAGE_EXISTS, "A package with this name already exists.")); + } + safe_files::publish(&source, &destination)?; + } + Ok(info(new_name, destination)) + } + + /// Deletes a package. The directory first leaves the library with one + /// atomic rename into staging, so it disappears at once; its contents are + /// then removed without following links. + pub fn delete(&self, name: &str) -> Result<(), PackageError> { + let _guard = self.lock(); + let directory = self.existing(name)?; + let moved = safe_files::new_work_directory(&self.staging, "delete").is_ok_and(|work| { + let moved = std::fs::rename(&directory, safe_files::child(&work, "package")).is_ok(); + safe_files::delete_tree(&work); + moved + }); + if !moved { + safe_files::delete_tree(&directory); + } + if safe_files::occupied(&directory) { + return Err(PackageError::new(codes::STORAGE_UNAVAILABLE, "The widget could not be deleted.")); + } + Ok(()) + } + + /// Atomically writes one file of an installed package. See the package + /// file writer for the path, link and size rules. A new file is refused + /// once the package holds 500 files. + pub fn write_file(&self, name: &str, relative_path: &str, bytes: &[u8]) -> Result<(), PackageError> { + let _guard = self.lock(); + let directory = self.existing(name)?; + let files = DiskPackageFiles::new(&directory); + if !files.exists(relative_path) && files.list()?.len() >= self.limits.maximum_file_count { + return Err(PackageError::new(codes::TOO_MANY_ENTRIES, "Package contains too many entries.")); + } + file_writer::write(&directory, relative_path, bytes) + } + + /// Removes one file of an installed package, under the path and link + /// rules of the package file writer. A file that is already gone is not an error. + pub fn delete_file(&self, name: &str, relative_path: &str) -> Result<(), PackageError> { + let _guard = self.lock(); + let directory = self.existing(name)?; + file_remover::remove(&directory, relative_path) + } + + /// Bytes of one file of an installed package, at most 25 MiB, under the rules of [`DiskPackageFiles`]. + /// + /// Fails with `invalid_path` for an unsafe path and `file_not_found` when the file is missing, refused or too large. + pub fn read_file(&self, name: &str, relative_path: &str) -> Result, PackageError> { + let directory = self.existing(name)?; + if path_rules::visible_components(relative_path).is_none() { + return Err(PackageError::new(codes::INVALID_PATH, "Invalid package path.")); + } + DiskPackageFiles::new(directory).read_bytes_up_to(relative_path, MAX_FILE_BYTES).ok_or_else(|| PackageError::new(codes::FILE_NOT_FOUND, format!("File not found: {relative_path}"))) + } + + fn lock(&self) -> MutexGuard<'_, ()> { + self.gate.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) + } + + /// Runs `build` on a fresh staging directory that is removed afterwards unless it was published. + fn staged(&self, prefix: &str, build: impl FnOnce(&Path) -> Result) -> Result { + let work = safe_files::new_work_directory(&self.staging, prefix)?; + let prepared = safe_files::child(&work, "package"); + let result = safe_files::create_private_directory(&prepared).map_err(|error| PackageError::caused(codes::STORAGE_UNAVAILABLE, "The package could not be prepared.", error)).and_then(|()| build(&prepared)); + safe_files::delete_tree(&work); + result + } + + /// Entry names of the library, which is created when it does not exist yet. Names that are not UTF-8 are not packages. + fn library_names(&self) -> Result, PackageError> { + use std::os::unix::fs::DirBuilderExt; + let _ = std::fs::DirBuilder::new().recursive(true).mode(0o700).create(&self.library); + safe_files::names(&self.library).map(|listing| listing.names).ok_or_else(|| PackageError::new(codes::STORAGE_UNAVAILABLE, "The widget library is unavailable.")) + } + + /// True when no library entry shares the identity of `name`. + fn free(&self, name: &str) -> Result { + let identity = names::identity(name); + Ok(!self.library_names()?.iter().any(|other| names::identity(other) == identity)) + } + + /// Directory of an installed package: a valid, exactly spelled, unambiguous name that is a real directory. + fn existing(&self, name: &str) -> Result { + require_valid(name)?; + let names = self.library_names()?; + let directory = safe_files::child(&self.library, name); + if names.iter().any(|other| other == name) && safe_files::unambiguous(&names, name) && safe_files::real_directory(&directory) { + Ok(directory) + } else { + Err(PackageError::new(codes::PACKAGE_NOT_FOUND, "Package not found.")) + } + } +} + +/// Exports the package in `directory` as a `.swt` archive under one top-level +/// directory named `name`, under the rules of [`PackageStore::export_archive`]. +/// The directory need not be in a library: the command line packs a package +/// from where its author keeps it. +pub fn export_directory(directory: &Path, name: &str, output: &mut dyn Write, limits: &ArchiveLimits) -> Result<(), PackageError> { + let files = DiskPackageFiles::new(directory); + let mut extras = BTreeMap::new(); + if manifest_present(directory)? { + read_manifest(&files)?; + } else { + if !files.exists(manifests::LEGACY_ENTRY) { + return Err(PackageError::new(codes::MISSING_ENTRY, "Entry file main.jsx does not exist.")); + } + extras.insert(manifests::FILE_NAME.to_owned(), manifests::encode(&legacy_manifest(name, &files))); + } + archive::export(directory, name, output, &extras, limits) +} + +fn require_valid(name: &str) -> Result<(), PackageError> { + if names::is_valid(name) { Ok(()) } else { Err(PackageError::new(codes::INVALID_NAME, "Invalid package name.")) } +} + +/// `.js` or `.jsx` in any case. +fn is_entry_file(path: &str) -> bool { + path.rsplit_once('.').is_some_and(|(_, extension)| extension.eq_ignore_ascii_case("js") || extension.eq_ignore_ascii_case("jsx")) +} + +/// True when the package has a manifest. Any other spelling of `widget.json` +/// is an error: on a case-insensitive platform it would be the manifest, so +/// it must not be skipped as legacy. +pub fn manifest_present(directory: &Path) -> Result { + let listing = safe_files::names(directory).ok_or_else(|| PackageError::new(codes::UNSAFE_FILE, "Package directory could not be verified."))?; + let mut present = false; + for name in listing.names.iter().filter(|name| name.to_lowercase() == manifests::FILE_NAME) { + if name != manifests::FILE_NAME { + return Err(PackageError::new(codes::INVALID_MANIFEST_CASE, "Use the exact file name widget.json.")); + } + present = true; + } + Ok(present) +} + +fn read_text(files: &DiskPackageFiles, path: &str) -> Option { + safe_files::decode_utf8(files.read_bytes_up_to(path, MAX_METADATA_BYTES as u64)?) +} + +/// The manifest of a package that has one. Unreadable, non-UTF-8 or malformed is an error, never "no manifest". +fn read_manifest(files: &DiskPackageFiles) -> Result { + manifests::parse(read_text(files, manifests::FILE_NAME).as_deref()).ok_or_else(|| PackageError::new(codes::INVALID_MANIFEST, "widget.json is invalid.")) +} + +fn legacy_manifest(name: &str, files: &DiskPackageFiles) -> Object { + manifests::legacy_manifest(name, read_text(files, manifests::LEGACY_METADATA).as_deref()) +} + +fn info(name: &str, directory: PathBuf) -> PackageInfo { + let names = safe_files::names(&directory).map(|listing| listing.names).unwrap_or_default(); + let has_manifest = names.iter().any(|entry| entry.to_lowercase() == manifests::FILE_NAME); + let summary = if names.iter().any(|entry| entry == manifests::FILE_NAME) { + manifests::parse(read_text(&DiskPackageFiles::new(&directory), manifests::FILE_NAME).as_deref()).map(|manifest| manifests::summarize(&manifest)).unwrap_or_default() + } else { + manifests::Summary::default() + }; + PackageInfo { name: name.to_owned(), directory, has_manifest, manifest_id: summary.id, manifest_name: summary.name, manifest_version: summary.version } +} diff --git a/Linux/crates/sw-core/src/packages/tree.rs b/Linux/crates/sw-core/src/packages/tree.rs new file mode 100644 index 00000000..3653b784 --- /dev/null +++ b/Linux/crates/sw-core/src/packages/tree.rs @@ -0,0 +1,95 @@ +//! Whole-tree verification, ported from `PackageTree.cs`, itself +//! `ScriptManager.validateExtractedTree` in Swift. + +use std::path::Path; + +use super::error::{PackageError, codes}; +use super::limits::ArchiveLimits; +use super::{path_rules, safe_files}; + +/// Verifies a package directory before it is published or copied. +/// +/// The tree may hold only directories and singly linked regular files: a +/// symbolic link, hard link or special file (device, socket, FIFO) could +/// reach outside the package. Names must be UTF-8 and valid components with +/// no case or normalization collisions, nesting is limited to 16 components, +/// and the entry count, per-file size and total size limits are those of the +/// archive policy. Sizes are the ones the filesystem reports for files this +/// layer wrote or is about to copy with a bound. +pub fn validate_tree(root: &Path, limits: &ArchiveLimits) -> Result<(), PackageError> { + if !safe_files::real_directory(root) { + return Err(fail(codes::UNSAFE_FILE, "Package directory could not be verified.")); + } + visit(root, 1, &mut State { limits, count: 0, total: 0, authored_only: false }) +} + +/// [`validate_tree`] for a directory an author works in, before it is +/// compiled, run, packed or installed from where it lies. Dot-files and the +/// generated `__Build` cache are not part of the authored package (they are +/// neither listed, read nor exported), so they are not looked at; everything +/// else must pass the same rules, and a link, hard link or special file among +/// the authored files fails the whole package instead of being left out +/// silently. +pub fn validate_source_tree(root: &Path, limits: &ArchiveLimits) -> Result<(), PackageError> { + if !safe_files::real_directory(root) { + return Err(fail(codes::UNSAFE_FILE, "Package directory could not be verified.")); + } + visit(root, 1, &mut State { limits, count: 0, total: 0, authored_only: true }) +} + +struct State<'a> { + limits: &'a ArchiveLimits, + count: usize, + total: u64, + /// Skip what [`path_rules::is_excluded`] names. + authored_only: bool, +} + +fn visit(directory: &Path, depth: usize, state: &mut State<'_>) -> Result<(), PackageError> { + let listing = safe_files::names(directory).ok_or_else(|| fail(codes::UNSAFE_FILE, "Package file could not be verified."))?; + if listing.foreign { + return Err(fail(codes::INVALID_PATH, "Invalid package path.")); + } + for name in &listing.names { + if state.authored_only && path_rules::is_excluded(name) { + continue; + } + if depth > state.limits.maximum_path_depth || !path_rules::valid_component(name) { + return Err(fail(codes::INVALID_PATH, "Invalid package path.")); + } + if !safe_files::unambiguous(&listing.names, name) { + return Err(fail(codes::UNSAFE_FILE, "Case-colliding package paths are not allowed.")); + } + let child = safe_files::child(directory, name); + let entry = safe_files::attributes(&child).ok_or_else(|| fail(codes::UNSAFE_FILE, "Package file could not be verified."))?; + if entry.is_symbolic_link { + return Err(fail(codes::UNSAFE_FILE, "Symbolic links are not allowed.")); + } + if !entry.is_directory && !entry.is_regular_file { + return Err(fail(codes::UNSAFE_FILE, "Only directories and regular files are allowed.")); + } + state.count += 1; + if state.count > state.limits.maximum_file_count { + return Err(fail(codes::TOO_MANY_ENTRIES, "Package contains too many entries.")); + } + if entry.is_directory { + visit(&child, depth + 1, state)?; + continue; + } + if entry.links > 1 { + return Err(fail(codes::UNSAFE_FILE, "Hard-linked files are not allowed.")); + } + if entry.size > state.limits.maximum_file_bytes { + return Err(fail(codes::FILE_TOO_LARGE, "Package file exceeds 25 MiB.")); + } + if state.total + entry.size > state.limits.maximum_expanded_bytes { + return Err(fail(codes::PACKAGE_TOO_LARGE, "Expanded package exceeds 64 MiB.")); + } + state.total += entry.size; + } + Ok(()) +} + +fn fail(code: &str, message: &str) -> PackageError { + PackageError::new(code, message) +} diff --git a/Linux/crates/sw-core/src/plugins/files.rs b/Linux/crates/sw-core/src/plugins/files.rs new file mode 100644 index 00000000..eb7a040e --- /dev/null +++ b/Linux/crates/sw-core/src/plugins/files.rs @@ -0,0 +1,509 @@ +//! Link-refusing filesystem primitives of the plugin store, and the reader +//! and writer of `.swplugin` archives. Ported from +//! `Windows/src/ScriptWidget.Core/Plugins/PluginFiles.cs`. +//! +//! Every lookup looks at the entry itself, so a symbolic link is seen as a +//! link and never as its target. A check and the following open are separate +//! system calls; the opened file is checked again before anything is read. +//! Plugin directories live in the user's private data directory. + +use std::collections::{HashMap, HashSet}; +use std::io::{Read, Write}; +use std::os::unix::fs::{DirBuilderExt, MetadataExt, OpenOptionsExt}; +use std::path::{Path, PathBuf}; + +use crate::api::support::is_control_or_format; +use crate::plugins::manifest::PluginError; + +/// Most directory entries listed before a listing fails closed. +const MAXIMUM_DIRECTORY_ENTRIES: usize = 100_000; + +/// A directory could not be staged or published; the message is safe to show. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct StorageError(pub String); + +/// True when something, including a dangling link, occupies `path`. +pub fn occupied(path: &Path) -> bool { + std::fs::symlink_metadata(path).is_ok() +} + +/// True when `path` is a real directory rather than a link to one. +pub fn real_directory(path: &Path) -> bool { + std::fs::symlink_metadata(path).is_ok_and(|metadata| metadata.file_type().is_dir()) +} + +/// Entry names of `directory`, or `None` when it cannot be listed or is +/// implausibly large. A name that is not UTF-8 is listed in a form that +/// matches no name the store asks for. +pub fn names(directory: &Path) -> Option> { + let mut result = Vec::new(); + for entry in std::fs::read_dir(directory).ok()? { + result.push(entry.ok()?.file_name().to_string_lossy().into_owned()); + if result.len() > MAXIMUM_DIRECTORY_ENTRIES { + return None; + } + } + Some(result) +} + +/// The identity two names share when a case-insensitive or normalizing +/// filesystem would treat them as one entry: NFC, then lower case. +pub fn identity(name: &str) -> String { + icu_normalizer::ComposingNormalizerBorrowed::new_nfc().normalize(name).to_lowercase() +} + +/// Resolves `name` directly below `directory` to an existing regular file, or +/// `None`. The name must be spelled exactly as stored, must not be a link and +/// must not have a case or Unicode-form alias. +pub fn existing_file(directory: &Path, name: &str) -> Option { + if !real_directory(directory) || name.contains('/') { + return None; + } + let siblings = names(directory)?; + let wanted = identity(name); + if !siblings.iter().any(|sibling| sibling == name) || siblings.iter().any(|sibling| sibling != name && identity(sibling) == wanted) { + return None; + } + let child = directory.join(name); + std::fs::symlink_metadata(&child).ok().filter(|metadata| metadata.file_type().is_file()).map(|_| child) +} + +/// Reads `file`, or returns `None` when it holds more than `maximum_bytes`, +/// is not a regular file, is a link, or has more than one hard link: a second +/// link could expose or alter a file outside the plugin. +pub fn read_bounded(file: &Path, maximum_bytes: usize) -> Option> { + // O_NONBLOCK: opening a FIFO that someone put in the way must not hang. + let opened = std::fs::OpenOptions::new().read(true).custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_CLOEXEC).open(file).ok()?; + let metadata = opened.metadata().ok()?; + if !metadata.is_file() || metadata.nlink() > 1 { + return None; + } + // The size is counted while reading, never taken from metadata that may change. + let mut bytes = Vec::new(); + opened.take(maximum_bytes as u64 + 1).read_to_end(&mut bytes).ok()?; + (bytes.len() <= maximum_bytes).then_some(bytes) +} + +/// Creates `file` exclusively, readable by the user only, writes `bytes` and flushes them to storage. +pub fn write_new(file: &Path, bytes: &[u8]) -> std::io::Result<()> { + let mut output = std::fs::OpenOptions::new().write(true).create_new(true).mode(0o600).custom_flags(libc::O_NOFOLLOW).open(file)?; + output.write_all(bytes)?; + output.sync_all() +} + +/// Creates a private directory; the parents must exist. +pub fn create_private_directory(path: &Path) -> std::io::Result<()> { + std::fs::DirBuilder::new().mode(0o700).create(path) +} + +/// Creates a uniquely named private working directory below `staging_root`. +pub fn new_work_directory(staging_root: &Path, prefix: &str) -> Result { + let unavailable = || StorageError("The staging directory is unavailable.".into()); + if !occupied(staging_root) { + create_private_directory(staging_root).map_err(|_| unavailable())?; + } + if !real_directory(staging_root) { + return Err(unavailable()); + } + let mut random = [0u8; 16]; + getrandom::fill(&mut random).map_err(|_| unavailable())?; + let name: String = random.iter().map(|byte| format!("{byte:02x}")).collect(); + let path = staging_root.join(format!("{prefix}-{name}")); + // Fails when the name is taken, by a link or by anything else. + create_private_directory(&path).map_err(|_| unavailable())?; + Ok(path) +} + +/// Publishes a prepared directory with one rename, so observers see either +/// nothing or the complete plugin. `destination` must be free; callers hold +/// the store lock. +pub fn publish(prepared: &Path, destination: &Path) -> Result<(), StorageError> { + if occupied(destination) { + return Err(StorageError("A package with this name already exists.".into())); + } + std::fs::rename(prepared, destination).map_err(|_| StorageError("The package could not be published.".into())) +} + +/// Removes `path` and everything below it. Links are removed, never followed. Best effort. +pub fn delete_tree(path: &Path) { + let Ok(metadata) = std::fs::symlink_metadata(path) else { return }; + // Leftovers stay inside the private staging directory and are retried by later cleanups. + let _ = if metadata.file_type().is_dir() { std::fs::remove_dir_all(path) } else { std::fs::remove_file(path) }; +} + +// MARK: - Archives + +/// One central-directory entry that passed validation. `parts` are the non-empty path components. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ArchiveEntry { + pub name: String, + pub parts: Vec, + pub is_directory: bool, + pub uncompressed_size: u64, + pub crc: u32, + compressed_size: u64, + method: u16, + local_header: u64, + /// Where the central directory starts: no entry's data may reach into it. + data_end: u64, +} + +// ScriptPackageArchivePolicy. +const MAXIMUM_ARCHIVE_BYTES: usize = 32 * 1024 * 1024; +const MAXIMUM_EXPANDED_BYTES: u64 = 64 * 1024 * 1024; +const MAXIMUM_FILE_BYTES: u64 = 25 * 1024 * 1024; +const MAXIMUM_FILE_COUNT: usize = 500; +const MAXIMUM_PATH_DEPTH: usize = 16; +const MAXIMUM_COMPONENT_BYTES: usize = 255; + +const END_SIGNATURE: u64 = 0x0605_4B50; +const CENTRAL_SIGNATURE: u64 = 0x0201_4B50; +const LOCAL_SIGNATURE: u64 = 0x0403_4B50; +const UINT32_MAX: u64 = 0xFFFF_FFFF; + +fn u16_at(data: &[u8], index: usize) -> u64 { + data.get(index..index + 2).map_or(0, |bytes| u64::from(u16::from_le_bytes([bytes[0], bytes[1]]))) +} + +fn u32_at(data: &[u8], index: usize) -> u64 { + data.get(index..index + 4).map_or(0, |bytes| u64::from(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))) +} + +fn u64_at(data: &[u8], index: usize) -> u64 { + u32_at(data, index) | (u32_at(data, index + 4) << 32) +} + +/// Validates the whole archive and returns its entries. +/// +/// Rejected: empty or oversized archives; a missing, multi-disk, +/// trailing-garbage or out-of-bounds central directory; more entries than +/// allowed; encrypted entries; compression other than stored/deflate; +/// malformed ZIP64 sizes; entry names that are not UTF-8, are absolute, +/// contain a backslash, colon, NUL, control character, `.` or `..` component, +/// or nest too deeply; duplicate, case-colliding or file-versus-directory +/// clashing paths; symbolic links and other special files; a declared file +/// size or declared total above the limits; and a large file that claims zero +/// compressed bytes. Validation inspects bytes only and never inflates or +/// writes anything. +pub fn validate_archive(data: &[u8]) -> Result, PluginError> { + let fail = |message: String| PluginError::invalid(message); + if data.is_empty() || data.len() > MAXIMUM_ARCHIVE_BYTES { + return Err(fail("Archive must be between 1 byte and 32 MiB.".into())); + } + let end = end_of_central_directory(data).ok_or_else(|| fail("Archive has no valid ZIP central directory.".into()))?; + if u16_at(data, end + 4) != 0 || u16_at(data, end + 6) != 0 || u16_at(data, end + 8) != u16_at(data, end + 10) { + return Err(fail("Multi-disk ZIP archives are not supported.".into())); + } + if end as u64 + 22 + u16_at(data, end + 20) != data.len() as u64 { + return Err(fail("Archive has a malformed ZIP footer.".into())); + } + let count = u16_at(data, end + 10) as usize; + let central_size = u32_at(data, end + 12); + let central_offset = u32_at(data, end + 16); + if count == 0 || count > MAXIMUM_FILE_COUNT { + return Err(fail(format!("Archive must contain 1–{MAXIMUM_FILE_COUNT} entries."))); + } + if central_offset + central_size > data.len() as u64 { + return Err(fail("Archive central directory is out of bounds.".into())); + } + + let mut cursor = central_offset as usize; + let mut expanded = 0u64; + let mut collisions = Collisions::default(); + let mut entries = Vec::with_capacity(count); + for _ in 0..count { + if cursor + 46 > data.len() || u32_at(data, cursor) != CENTRAL_SIGNATURE { + return Err(fail("Archive central directory contains an invalid entry.".into())); + } + let flags = u16_at(data, cursor + 8); + let method = u16_at(data, cursor + 10); + let crc = u32_at(data, cursor + 16); + let name_length = u16_at(data, cursor + 28) as usize; + let extra_length = u16_at(data, cursor + 30) as usize; + let comment_length = u16_at(data, cursor + 32) as usize; + let external = u32_at(data, cursor + 38); + let local_header = u32_at(data, cursor + 42); + let entry_end = cursor + 46 + name_length + extra_length + comment_length; + if name_length == 0 || entry_end > data.len() { + return Err(fail("Archive contains a truncated filename.".into())); + } + let extra_start = cursor + 46 + name_length; + let Some((compressed, uncompressed)) = resolved_sizes(u32_at(data, cursor + 20), u32_at(data, cursor + 24), data, extra_start, extra_start + extra_length) else { + return Err(fail("Archive contains malformed ZIP64 size metadata.".into())); + }; + if flags & 0x1 != 0 { + return Err(fail("Encrypted package entries are not supported.".into())); + } + if method != 0 && method != 8 { + return Err(fail(format!("Unsupported ZIP compression method {method}."))); + } + let name = std::str::from_utf8(&data[cursor + 46..cursor + 46 + name_length]).ok(); + let parts = name.and_then(|name| entry_parts(name, MAXIMUM_PATH_DEPTH)); + let (Some(name), Some(parts)) = (name, parts) else { return Err(fail("Archive contains an unsafe or non-UTF-8 path.".into())) }; + let is_directory = name.ends_with('/'); + if !collisions.add(name, &parts, is_directory) { + return Err(fail("Archive contains duplicate or case-colliding paths.".into())); + } + // Unix mode in the high half: only regular files (0x8000), directories (0x4000) or + // an absent type are accepted; 0xA000 is a symbolic link. + let kind = (external >> 16) & 0xF000; + if kind == 0xA000 { + return Err(fail("Symbolic links are not allowed in ScriptWidget packages.".into())); + } + if kind != 0 && kind != 0x8000 && kind != 0x4000 { + return Err(fail("Only regular files and directories are allowed in ScriptWidget packages.".into())); + } + if !is_directory { + if uncompressed > MAXIMUM_FILE_BYTES { + return Err(fail(format!("Package file {name} exceeds 25 MiB."))); + } + expanded += uncompressed; + if expanded > MAXIMUM_EXPANDED_BYTES { + return Err(fail("Expanded package exceeds 64 MiB.".into())); + } + if uncompressed > 1024 * 1024 && compressed == 0 { + return Err(fail(format!("Archive entry {name} has an invalid compression ratio."))); + } + } + entries.push(ArchiveEntry { + name: name.to_owned(), + parts, + is_directory, + uncompressed_size: uncompressed, + crc: crc as u32, + compressed_size: compressed, + method: method as u16, + local_header, + data_end: central_offset, + }); + cursor = entry_end; + } + if cursor as u64 != central_offset + central_size { + return Err(fail("Archive entries do not match the declared central directory.".into())); + } + Ok(entries) +} + +/// The archive does not hold what its directory says. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct CorruptArchive; + +/// Inflates one validated entry in memory, counting real bytes so false size +/// headers cannot expand, and checks its CRC-32. Fails when the entry is +/// missing, does not match its declared size or checksum, or exceeds +/// `maximum_bytes`. +pub fn inflate(archive: &[u8], entry: &ArchiveEntry, maximum_bytes: usize) -> Result, CorruptArchive> { + if entry.is_directory || entry.uncompressed_size > maximum_bytes as u64 { + return Err(CorruptArchive); + } + let header = usize::try_from(entry.local_header).map_err(|_| CorruptArchive)?; + if header.checked_add(30).is_none_or(|end| end > archive.len()) || u32_at(archive, header) != LOCAL_SIGNATURE { + return Err(CorruptArchive); + } + let start = header as u64 + 30 + u16_at(archive, header + 26) + u16_at(archive, header + 28); + let end = start.checked_add(entry.compressed_size).filter(|end| *end <= entry.data_end && *end <= archive.len() as u64).ok_or(CorruptArchive)?; + let stored = &archive[start as usize..end as usize]; + let declared = entry.uncompressed_size as usize; + let output = match entry.method { + 0 if stored.len() == declared => stored.to_vec(), + 8 => { + let mut output = Vec::with_capacity(declared.min(64 * 1024)); + // One byte more than declared is enough to see that the declaration is false. + flate2::read::DeflateDecoder::new(stored).take(declared as u64 + 1).read_to_end(&mut output).map_err(|_| CorruptArchive)?; + output + } + _ => return Err(CorruptArchive), + }; + if output.len() != declared || crc32(&output) != entry.crc { + return Err(CorruptArchive); + } + Ok(output) +} + +/// A ZIP archive of deflated regular files with fixed timestamps, so the same input always gives the same bytes. +pub fn write_archive(files: &[(&str, &[u8])]) -> std::io::Result> { + let mut output: Vec = Vec::new(); + let mut central: Vec = Vec::new(); + for (name, bytes) in files { + let mut encoder = flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::best()); + encoder.write_all(bytes)?; + let compressed = encoder.finish()?; + let offset = output.len() as u32; + let crc = crc32(bytes); + // Version 2.0, UTF-8 names, deflate, 1980-01-01 00:00. + let shared: Vec = [20u16.to_le_bytes(), 0x0800u16.to_le_bytes(), 8u16.to_le_bytes(), 0u16.to_le_bytes(), 0x0021u16.to_le_bytes()].concat(); + let sizes: Vec = [crc.to_le_bytes(), (compressed.len() as u32).to_le_bytes(), (bytes.len() as u32).to_le_bytes()].concat(); + output.extend_from_slice(&(LOCAL_SIGNATURE as u32).to_le_bytes()); + output.extend_from_slice(&shared); + output.extend_from_slice(&sizes); + output.extend_from_slice(&(name.len() as u16).to_le_bytes()); + output.extend_from_slice(&0u16.to_le_bytes()); + output.extend_from_slice(name.as_bytes()); + output.extend_from_slice(&compressed); + + central.extend_from_slice(&(CENTRAL_SIGNATURE as u32).to_le_bytes()); + central.extend_from_slice(&0x031Eu16.to_le_bytes()); // made on Unix + central.extend_from_slice(&shared); + central.extend_from_slice(&sizes); + central.extend_from_slice(&(name.len() as u16).to_le_bytes()); + central.extend_from_slice(&[0u8; 8]); // extra, comment, disk, internal attributes + central.extend_from_slice(&(0o100644u32 << 16).to_le_bytes()); + central.extend_from_slice(&offset.to_le_bytes()); + central.extend_from_slice(name.as_bytes()); + } + let central_offset = output.len() as u32; + output.extend_from_slice(¢ral); + output.extend_from_slice(&(END_SIGNATURE as u32).to_le_bytes()); + output.extend_from_slice(&[0u8; 4]); + output.extend_from_slice(&(files.len() as u16).to_le_bytes()); + output.extend_from_slice(&(files.len() as u16).to_le_bytes()); + output.extend_from_slice(&(central.len() as u32).to_le_bytes()); + output.extend_from_slice(¢ral_offset.to_le_bytes()); + output.extend_from_slice(&0u16.to_le_bytes()); + Ok(output) +} + +/// Components of an archive entry name, or `None` when the name is unsafe: a +/// leading slash, any backslash, colon or NUL, a `.`/`..` component, a +/// component that is too long or holds control characters, no components, or +/// more than `maximum_depth`. +pub fn entry_parts(name: &str, maximum_depth: usize) -> Option> { + if name.starts_with('/') || name.contains(['\\', ':', '\0']) { + return None; + } + let parts: Vec<&str> = name.split('/').filter(|part| !part.is_empty()).collect(); + if parts.is_empty() || parts.len() > maximum_depth || !parts.iter().all(|part| valid_component(part)) { + return None; + } + Some(parts.into_iter().map(str::to_owned).collect()) +} + +/// No control or format characters, except ZWNJ and ZWJ, which joined emoji and some scripts need. +fn valid_component(part: &str) -> bool { + !part.is_empty() && part != "." && part != ".." && part.len() <= MAXIMUM_COMPONENT_BYTES && !part.chars().any(|character| !matches!(character, '\u{200c}' | '\u{200d}') && is_control_or_format(character)) +} + +/// Tracks every file and directory prefix seen so far. Two spellings of one +/// identity (case or Unicode form), a repeated path, and a path used as both +/// file and directory would overwrite or shadow each other on extraction, so +/// each is a collision. +#[derive(Default)] +struct Collisions { + full_paths: HashSet, + spellings: HashMap, + directories: HashMap, +} + +impl Collisions { + fn add(&mut self, name: &str, parts: &[String], is_directory: bool) -> bool { + if !self.full_paths.insert(identity(name).trim_matches('/').to_owned()) { + return false; + } + let mut path = String::new(); + for (index, part) in parts.iter().enumerate() { + if index > 0 { + path.push('/'); + } + path.push_str(part); + if self.spellings.entry(identity(&path)).or_insert_with(|| path.clone()) != &path { + return false; + } + let directory = index < parts.len() - 1 || is_directory; + if *self.directories.entry(path.clone()).or_insert(directory) != directory { + return false; + } + } + true + } +} + +fn resolved_sizes(compressed32: u64, uncompressed32: u64, data: &[u8], extra_start: usize, extra_end: usize) -> Option<(u64, u64)> { + if compressed32 != UINT32_MAX && uncompressed32 != UINT32_MAX { + return Some((compressed32, uncompressed32)); + } + let mut compressed = compressed32; + let mut uncompressed = uncompressed32; + let mut cursor = extra_start; + while cursor + 4 <= extra_end { + let identifier = u16_at(data, cursor); + let end = cursor + 4 + u16_at(data, cursor + 2) as usize; + if end > extra_end { + return None; + } + if identifier == 0x0001 { + let mut value = cursor + 4; + if uncompressed32 == UINT32_MAX { + if value + 8 > end { + return None; + } + uncompressed = u64_at(data, value); + value += 8; + } + if compressed32 == UINT32_MAX { + if value + 8 > end { + return None; + } + compressed = u64_at(data, value); + } + return Some((compressed, uncompressed)); + } + cursor = end; + } + None +} + +fn end_of_central_directory(data: &[u8]) -> Option { + if data.len() < 22 { + return None; + } + let lower_bound = data.len().saturating_sub(65_557); + (lower_bound..=data.len() - 22).rev().find(|index| u32_at(data, *index) == END_SIGNATURE) +} + +pub fn crc32(data: &[u8]) -> u32 { + let mut crc = 0xFFFF_FFFFu32; + for byte in data { + crc ^= u32::from(*byte); + for _ in 0..8 { + crc = if crc & 1 != 0 { 0xEDB8_8320 ^ (crc >> 1) } else { crc >> 1 }; + } + } + crc ^ 0xFFFF_FFFF +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn identities_fold_case_and_unicode_form() { + assert_eq!(identity("README.md"), identity("readme.MD")); + assert_eq!(identity("caf\u{e9}"), identity("CAFE\u{301}")); + assert_ne!(identity("plugin.json"), identity("plugin.jsan")); + } + + #[test] + fn entry_names_must_stay_inside_the_archive_root() { + assert_eq!(entry_parts("Test/plugin.json", 16), Some(vec!["Test".to_owned(), "plugin.json".to_owned()])); + assert_eq!(entry_parts("Test//a/", 16), Some(vec!["Test".to_owned(), "a".to_owned()])); + assert!(entry_parts("👨\u{200d}👩\u{200d}👧.txt", 16).is_some()); + let long = "x".repeat(256); + for name in ["/a", "a\\b", "C:a", "a\0b", "../a", "a/../b", "a/./b", "", "/", "a\u{202e}b", "a\nb", "a\u{feff}", long.as_str(), "1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17"] { + assert_eq!(entry_parts(name, 16), None, "{name:?}"); + } + } + + #[test] + fn written_archives_validate_and_inflate() { + assert_eq!(crc32(b"123456789"), 0xCBF4_3926); + let text = "plugin ".repeat(1000); + let archive = write_archive(&[("plugin.json", text.as_bytes()), ("README.md", b"")]).unwrap(); + assert_eq!(archive, write_archive(&[("plugin.json", text.as_bytes()), ("README.md", b"")]).unwrap()); + assert!(archive.len() < text.len() / 10); + let entries = validate_archive(&archive).unwrap(); + assert_eq!(entries.iter().map(|entry| entry.name.as_str()).collect::>(), ["plugin.json", "README.md"]); + assert_eq!(inflate(&archive, &entries[0], 1 << 20).unwrap(), text.as_bytes()); + assert_eq!(inflate(&archive, &entries[1], 1 << 20).unwrap(), b""); + assert_eq!(inflate(&archive, &entries[0], 100), Err(CorruptArchive)); + } +} diff --git a/Linux/crates/sw-core/src/plugins/manifest.rs b/Linux/crates/sw-core/src/plugins/manifest.rs new file mode 100644 index 00000000..966a5a3e --- /dev/null +++ b/Linux/crates/sw-core/src/plugins/manifest.rs @@ -0,0 +1,712 @@ +//! Data Source Plugin 1.0 manifests (`plugin.json`): the model, the strict +//! decoder and the validator. Ported from +//! `Windows/src/ScriptWidget.Core/Plugins/PluginManifest.cs`, itself a port of +//! `DataSourcePluginValidator` in Swift. + +use std::collections::HashSet; +use std::fmt; +use std::net::Ipv6Addr; + +use serde_json::Value; +use unicode_segmentation::UnicodeSegmentation; + +use crate::api::policy; +use crate::json::Object; +use crate::plugins::strict_json; + +/// Where a parameter value goes in the request (`DataSourceParameterLocation` in Swift). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PluginParameterLocation { + Path, + Query, + Header, + JsonBody, +} + +impl PluginParameterLocation { + /// The name in `plugin.json`. + pub fn wire_name(self) -> &'static str { + match self { + Self::Path => "path", + Self::Query => "query", + Self::Header => "header", + Self::JsonBody => "jsonBody", + } + } + + pub fn from_wire_name(name: &str) -> Option { + match name { + "path" => Some(Self::Path), + "query" => Some(Self::Query), + "header" => Some(Self::Header), + "jsonBody" => Some(Self::JsonBody), + _ => None, + } + } +} + +/// How a response body is handed to the widget (`DataSourceResponseType` in Swift). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PluginResponseType { + Json, + Text, +} + +impl PluginResponseType { + pub fn wire_name(self) -> &'static str { + match self { + Self::Json => "json", + Self::Text => "text", + } + } + + pub fn from_wire_name(name: &str) -> Option { + match name { + "json" => Some(Self::Json), + "text" => Some(Self::Text), + _ => None, + } + } +} + +/// One declared operation parameter. A plugin can only place values where it declared them. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PluginParameter { + pub id: String, + pub label: String, + pub location: PluginParameterLocation, + pub required: bool, + pub default_value: Option, +} + +/// One declared HTTPS request template. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PluginOperation { + pub id: String, + pub title: String, + pub method: String, + pub path: String, + pub response_type: PluginResponseType, + pub parameters: Vec, +} + +/// A Data Source Plugin 1.0 manifest. It is data: a mapping from parameters +/// to HTTPS requests against the hosts it declares. It carries no code and no +/// permissions. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PluginManifest { + pub format_version: i32, + pub id: String, + pub name: String, + pub version: String, + pub summary: String, + pub symbol: String, + pub author: String, + pub license: String, + pub minimum_runtime_version: String, + pub base_url: String, + pub hosts: Vec, + pub operations: Vec, +} + +impl PluginManifest { + /// The only supported `formatVersion`; any other value fails closed. + pub const CURRENT_FORMAT_VERSION: i32 = 1; + + /// `plugin.json` text with sorted keys and two-space indentation, the + /// form Swift writes (`.prettyPrinted, .sortedKeys, .withoutEscapingSlashes`). + /// An absent `defaultValue` is omitted. + pub fn to_json(&self) -> String { + let q = strict_json::quote; + let mut output = String::from("{\n"); + output.push_str(&format!(" \"author\": {},\n", q(&self.author))); + output.push_str(&format!(" \"baseURL\": {},\n", q(&self.base_url))); + output.push_str(&format!(" \"formatVersion\": {},\n", self.format_version)); + output.push_str(&format!(" \"hosts\": [{}],\n", self.hosts.iter().map(|host| q(host)).collect::>().join(", "))); + output.push_str(&format!(" \"id\": {},\n", q(&self.id))); + output.push_str(&format!(" \"license\": {},\n", q(&self.license))); + output.push_str(&format!(" \"minimumRuntimeVersion\": {},\n", q(&self.minimum_runtime_version))); + output.push_str(&format!(" \"name\": {},\n", q(&self.name))); + output.push_str(" \"operations\": ["); + for (index, operation) in self.operations.iter().enumerate() { + output.push_str(if index == 0 { "\n" } else { ",\n" }); + output.push_str(" {\n"); + output.push_str(&format!(" \"id\": {},\n", q(&operation.id))); + output.push_str(&format!(" \"method\": {},\n", q(&operation.method))); + output.push_str(" \"parameters\": ["); + for (parameter_index, parameter) in operation.parameters.iter().enumerate() { + output.push_str(if parameter_index == 0 { "\n" } else { ",\n" }); + output.push_str(" {"); + if let Some(default_value) = ¶meter.default_value { + output.push_str(&format!("\"defaultValue\": {}, ", q(default_value))); + } + output.push_str(&format!("\"id\": {}, \"label\": {}, ", q(¶meter.id), q(¶meter.label))); + output.push_str(&format!("\"location\": {}, \"required\": {}}}", q(parameter.location.wire_name()), parameter.required)); + } + output.push_str(if operation.parameters.is_empty() { "],\n" } else { "\n ],\n" }); + output.push_str(&format!(" \"path\": {},\n", q(&operation.path))); + output.push_str(&format!(" \"responseType\": {},\n", q(operation.response_type.wire_name()))); + output.push_str(&format!(" \"title\": {}\n", q(&operation.title))); + output.push_str(" }"); + } + output.push_str(if self.operations.is_empty() { "],\n" } else { "\n ],\n" }); + output.push_str(&format!(" \"summary\": {},\n", q(&self.summary))); + output.push_str(&format!(" \"symbol\": {},\n", q(&self.symbol))); + output.push_str(&format!(" \"version\": {}\n", q(&self.version))); + output.push_str("}\n"); + output + } +} + +/// What went wrong in the plugin layer (the cases of `DataSourcePluginError` in Swift). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PluginErrorKind { + InvalidManifest, + NotFound, + Permission, + InvalidParameters, + Network, + Storage, +} + +/// The single failure type of the plugin layer. `message` is the text shown +/// to the user or passed to the script's rejection handler. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PluginError { + pub kind: PluginErrorKind, + pub message: String, +} + +impl PluginError { + pub fn new(kind: PluginErrorKind, message: impl Into) -> Self { + Self { kind, message: message.into() } + } + + pub(crate) fn invalid(message: impl Into) -> Self { + Self::new(PluginErrorKind::InvalidManifest, message) + } +} + +impl fmt::Display for PluginError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(&self.message) + } +} + +impl std::error::Error for PluginError {} + +/// One reason a manifest is refused. `id` is the stable Swift issue identifier. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PluginValidationIssue { + pub id: String, + pub message: String, +} + +/// `DataSourcePluginValidator.maximumManifestBytes`: bounds parsing work for an untrusted file. +pub const MAXIMUM_MANIFEST_BYTES: usize = 256 * 1024; +/// `DataSourcePluginValidator.maximumOperations`. +pub const MAXIMUM_OPERATIONS: usize = 50; +/// `DataSourcePluginValidator.maximumParameters` (per operation). +pub const MAXIMUM_PARAMETERS: usize = 30; +/// Most host declarations a plugin may carry. +pub const MAXIMUM_HOSTS: usize = 20; +/// Longest plugin name, in user-perceived characters. +pub const MAXIMUM_NAME_CHARACTERS: usize = 80; +/// `ScriptWidgetBuildContract.runtimeAPIVersion`: the only `minimumRuntimeVersion` accepted. +pub const RUNTIME_API_VERSION: &str = "1.0"; + +const ROOT_KEYS: [&str; 12] = ["formatVersion", "id", "name", "version", "summary", "symbol", "author", "license", "minimumRuntimeVersion", "baseURL", "hosts", "operations"]; +const OPERATION_KEYS: [&str; 6] = ["id", "title", "method", "path", "responseType", "parameters"]; +const PARAMETER_KEYS: [&str; 5] = ["id", "label", "location", "required", "defaultValue"]; +const METHODS: [&str; 5] = ["GET", "POST", "PUT", "PATCH", "DELETE"]; + +fn identifier_character(byte: u8) -> bool { + byte.is_ascii_alphanumeric() || byte == b'.' || byte == b'-' +} + +/// `[A-Za-z0-9][A-Za-z0-9.-]{2,127}`. +fn is_identifier(text: &str) -> bool { + let bytes = text.as_bytes(); + (3..=128).contains(&bytes.len()) && bytes[0].is_ascii_alphanumeric() && bytes.iter().all(|byte| identifier_character(*byte)) +} + +/// `[0-9]+.[0-9]+.[0-9]+(-[0-9A-Za-z.-]+)?`. +fn is_semantic_version(text: &str) -> bool { + let (core, suffix) = match text.split_once('-') { + Some((core, suffix)) => (core, Some(suffix)), + None => (text, None), + }; + let parts: Vec<&str> = core.split('.').collect(); + parts.len() == 3 && parts.iter().all(|part| !part.is_empty() && part.bytes().all(|byte| byte.is_ascii_digit())) && suffix.is_none_or(|suffix| !suffix.is_empty() && suffix.bytes().all(identifier_character)) +} + +/// A token of RFC 9110. +fn is_header_name(text: &str) -> bool { + !text.is_empty() && text.bytes().all(|byte| byte.is_ascii_alphanumeric() || b"!#$%&'*+.^_`|~-".contains(&byte)) +} + +/// The names between braces in a path: `{([A-Za-z0-9.-]+)}`. +fn placeholders(path: &str) -> Vec<&str> { + let mut found = Vec::new(); + let mut rest = path; + while let Some(open) = rest.find('{') { + let after = &rest[open + 1..]; + let length = after.bytes().take_while(|byte| identifier_character(*byte)).count(); + if length > 0 && after.as_bytes().get(length) == Some(&b'}') { + found.push(&after[..length]); + rest = &after[length + 1..]; + } else { + rest = after; + } + } + found +} + +/// Decodes and validates `plugin.json` bytes. +/// +/// Security rules, each failing closed with [`PluginErrorKind::InvalidManifest`]: +/// - at most 256 KiB, strict UTF-8, strict JSON with no duplicate keys; +/// - unknown keys at any level are rejected, so a future or misspelled +/// capability is never silently ignored; +/// - every field must be present with its exact JSON type (no coercion); +/// - then every rule of [`validate`]. +pub fn decode(data: &[u8]) -> Result { + if data.len() > MAXIMUM_MANIFEST_BYTES { + return Err(PluginError::invalid("plugin.json exceeds 256 KiB.")); + } + let text = decode_utf8(data).ok_or_else(|| PluginError::invalid("plugin.json must be UTF-8 JSON."))?; + let parsed = strict_json::parse(text, false).map_err(|_| PluginError::invalid("plugin.json is not valid JSON."))?; + let Value::Object(root) = parsed else { return Err(PluginError::invalid("plugin.json must be an object.")) }; + let operations = objects(root.get("operations")); + let Some(operations) = operations.filter(|_| root.keys().all(|key| ROOT_KEYS.contains(&key.as_str()))) else { + return Err(PluginError::invalid("plugin.json contains unknown fields.")); + }; + for operation in &operations { + let parameters = objects(operation.get("parameters")); + let known = operation.keys().all(|key| OPERATION_KEYS.contains(&key.as_str())); + if !known || !parameters.is_some_and(|parameters| parameters.iter().all(|parameter| parameter.keys().all(|key| PARAMETER_KEYS.contains(&key.as_str())))) { + return Err(PluginError::invalid("Plugin operation contains unknown fields.")); + } + } + let hosts = || match root.get("hosts") { + Some(Value::Array(hosts)) => hosts.iter().map(|host| host.as_str().map(str::to_owned).ok_or_else(|| malformed("hosts"))).collect::, _>>(), + _ => Err(malformed("hosts")), + }; + // Fields are read in this order, so the first one that is wrong is the one reported. + let manifest = PluginManifest { + format_version: integer(&root, "formatVersion")?, + id: string(&root, "id")?, + name: string(&root, "name")?, + version: string(&root, "version")?, + summary: string(&root, "summary")?, + symbol: string(&root, "symbol")?, + author: string(&root, "author")?, + license: string(&root, "license")?, + minimum_runtime_version: string(&root, "minimumRuntimeVersion")?, + base_url: string(&root, "baseURL")?, + hosts: hosts()?, + operations: operations + .iter() + .map(|operation| { + Ok(PluginOperation { + id: string(operation, "id")?, + title: string(operation, "title")?, + method: string(operation, "method")?, + path: string(operation, "path")?, + response_type: PluginResponseType::from_wire_name(&string(operation, "responseType")?).ok_or_else(|| malformed("responseType"))?, + parameters: objects(operation.get("parameters")) + .unwrap_or_default() + .iter() + .map(|parameter| { + Ok(PluginParameter { + id: string(parameter, "id")?, + label: string(parameter, "label")?, + location: PluginParameterLocation::from_wire_name(&string(parameter, "location")?).ok_or_else(|| malformed("location"))?, + required: parameter.get("required").and_then(Value::as_bool).ok_or_else(|| malformed("required"))?, + default_value: match parameter.get("defaultValue") { + None | Some(Value::Null) => None, + Some(Value::String(value)) => Some(value.clone()), + Some(_) => return Err(malformed("defaultValue")), + }, + }) + }) + .collect::, PluginError>>()?, + }) + }) + .collect::, PluginError>>()?, + }; + let issues = validate(&manifest); + if !issues.is_empty() { + return Err(PluginError::invalid(issues.iter().map(|issue| issue.message.as_str()).collect::>().join("\n"))); + } + Ok(manifest) +} + +/// Semantic rules of Data Source Plugin 1.0. An empty list means the manifest is valid. +/// +/// - `formatVersion` must be 1 and `minimumRuntimeVersion` the supported +/// runtime: unsupported versions fail closed instead of being run with +/// guessed semantics. +/// - `baseURL` must be HTTPS without credentials, query or fragment: plugins +/// never send cleartext requests and never embed secrets in the manifest. +/// - 1–20 unique, well-formed `hosts` that include the `baseURL` host and are +/// public Internet names: a plugin can only reach hosts the user can +/// review, never the device or local network. +/// - 1–50 operations with unique ids, an upper-case standard method, and an +/// absolute path with no `..`, query or fragment: an operation cannot leave +/// its declared path space. +/// - At most 30 parameters per operation, unique ids, every `{placeholder}` +/// declared as a `path` parameter and every `path` parameter present in the +/// path; header parameter ids must be valid header names. +pub fn validate(manifest: &PluginManifest) -> Vec { + let mut issues: Vec = Vec::new(); + let mut reject = |id: &str, message: String| issues.push(PluginValidationIssue { id: id.to_owned(), message }); + if manifest.format_version != PluginManifest::CURRENT_FORMAT_VERSION { + reject("format", format!("Unsupported Data Source Plugin format {}.", manifest.format_version)); + } + if !is_identifier(&manifest.id) { + reject("id", "Plugin id must be a stable 3–128 character identifier.".into()); + } + if manifest.name.trim().is_empty() || manifest.name.graphemes(true).count() > MAXIMUM_NAME_CHARACTERS { + reject("name", "Plugin name must contain 1–80 characters.".into()); + } + if !is_semantic_version(&manifest.version) { + reject("version", "Plugin version must use semantic versioning.".into()); + } + if manifest.minimum_runtime_version != RUNTIME_API_VERSION { + reject("runtime", format!("Plugin requires unsupported runtime {}.", manifest.minimum_runtime_version)); + } + let address = PluginUrl::parse(&manifest.base_url); + let base_host = address.as_ref().and_then(|address| address.host.as_deref()).map(str::to_lowercase).filter(|host| !host.is_empty()); + let usable = address.as_ref().is_some_and(|address| address.scheme.as_deref().map(str::to_lowercase).as_deref() == Some("https") && !address.has_user_info && !address.has_query && !address.has_fragment); + let Some(base_host) = base_host.filter(|_| usable) else { + reject("base_url", "baseURL must be an HTTPS URL without credentials, query, or fragment.".into()); + return issues; + }; + if manifest.hosts.is_empty() || manifest.hosts.len() > MAXIMUM_HOSTS { + reject("hosts", "A plugin must declare 1–20 HTTPS hosts.".into()); + } + if manifest.hosts.iter().collect::>().len() != manifest.hosts.len() { + reject("hosts", "Plugin hosts must be unique.".into()); + } + for host in &manifest.hosts { + if !is_valid_domain_declaration(host) { + reject("host", format!("Invalid plugin host declaration: {host}.")); + } else { + // "*.local" covers only private names; test it as a name it would match. + let probe = match host.strip_prefix("*.") { + Some(suffix) => format!("x.{suffix}"), + None => host.clone(), + }; + if policy::blocked_host_name(&probe) { + reject("host", format!("Plugin host must be a public Internet host: {host}.")); + } + } + } + if policy::blocked_host_name(PluginUrl::unbracket(&base_host)) { + reject("base_host", "baseURL must use a public Internet host.".into()); + } + if !policy::host_matches(&base_host, manifest.hosts.iter().map(String::as_str)) { + reject("base_host", "baseURL host must be included in hosts.".into()); + } + if manifest.operations.is_empty() || manifest.operations.len() > MAXIMUM_OPERATIONS { + reject("operations", format!("A plugin must contain 1–{MAXIMUM_OPERATIONS} operations.")); + } + if manifest.operations.iter().map(|operation| operation.id.to_lowercase()).collect::>().len() != manifest.operations.len() { + reject("operation_ids", "Operation ids must be unique.".into()); + } + for operation in &manifest.operations { + let prefix = format!("operation.{}", operation.id); + if !is_identifier(&operation.id) { + reject(&prefix, format!("Operation id is invalid: {}.", operation.id)); + } + if !METHODS.contains(&operation.method.as_str()) { + reject(&prefix, format!("Operation {} has an unsupported HTTP method.", operation.id)); + } + if operation.path.is_empty() || !operation.path.starts_with('/') || operation.path.contains("..") || operation.path.contains('?') || operation.path.contains('#') { + reject(&prefix, "Operation path must be an absolute URL path without traversal, query, or fragment.".into()); + } + if operation.parameters.len() > MAXIMUM_PARAMETERS { + reject(&prefix, format!("Operation {} has too many parameters.", operation.id)); + } + if operation.parameters.iter().map(|parameter| parameter.id.to_lowercase()).collect::>().len() != operation.parameters.len() { + reject(&prefix, "Operation parameter ids must be unique.".into()); + } + for parameter in &operation.parameters { + let placeholder = format!("{{{}}}", parameter.id); + let in_path = operation.path.contains(&placeholder); + if !is_identifier(¶meter.id) { + reject(&prefix, format!("Invalid parameter id: {}.", parameter.id)); + } + if parameter.location == PluginParameterLocation::Path && !in_path { + reject(&prefix, format!("Path parameter {} has no matching placeholder.", parameter.id)); + } + if parameter.location != PluginParameterLocation::Path && in_path { + reject(&prefix, format!("Placeholder {} must be a path parameter.", parameter.id)); + } + if parameter.location == PluginParameterLocation::Header && !is_header_name(¶meter.id) { + reject(&prefix, format!("Header parameter {} is invalid.", parameter.id)); + } + } + for placeholder in placeholders(&operation.path) { + if !operation.parameters.iter().any(|parameter| parameter.id == placeholder) { + reject(&prefix, format!("Path placeholder {placeholder} has no declared parameter.")); + } + } + } + issues +} + +/// `ScriptWidgetNetworkPolicy.isValidDomainDeclaration`: a lower-case host +/// name, optionally `*.`-prefixed, with no scheme, path or port, so a +/// declaration cannot smuggle a URL. +pub fn is_valid_domain_declaration(value: &str) -> bool { + let normalized = value.to_lowercase(); + value == normalized + && !normalized.contains("://") + && !normalized.contains('/') + && !normalized.contains(':') + && !normalized.starts_with('.') + && !normalized.ends_with('.') + && !normalized.is_empty() + && normalized.bytes().all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || matches!(byte, b'*' | b'.' | b'-')) + && (!normalized.contains('*') || (normalized.starts_with("*.") && !normalized[1..].contains('*'))) +} + +fn objects(value: Option<&Value>) -> Option> { + value?.as_array()?.iter().map(Value::as_object).collect() +} + +fn string(map: &Object, key: &str) -> Result { + map.get(key).and_then(Value::as_str).map(str::to_owned).ok_or_else(|| malformed(key)) +} + +/// Whole JSON numbers only; `1.0` is accepted like Swift's `JSONDecoder`, `1.5` and `"1"` are not. +fn integer(map: &Object, key: &str) -> Result { + match map.get(key) { + Some(Value::Number(number)) => { + if let Some(whole) = number.as_i64() { + return i32::try_from(whole).map_err(|_| malformed(key)); + } + match number.as_f64() { + Some(value) if number.is_f64() && value == value.round_ties_even() && value.abs() <= f64::from(i32::MAX) => Ok(value as i32), + _ => Err(malformed(key)), + } + } + _ => Err(malformed(key)), + } +} + +/// Strict UTF-8 text without a byte order mark, or `None` when the bytes are malformed. +pub fn decode_utf8(bytes: &[u8]) -> Option<&str> { + std::str::from_utf8(bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes)).ok() +} + +fn malformed(key: &str) -> PluginError { + PluginError::invalid(format!("plugin.json field \"{key}\" is missing or has the wrong type.")) +} + +/// A URL split the way `java.net.URI` splits it, which is what the Android +/// validator and request builder rely on. It is deliberately stricter than a +/// browser's parser: no spaces, no backslashes, no characters outside the URI +/// grammar, and a host only when the authority is a plain server name, an +/// IPv4 address or a bracketed IPv6 address. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct PluginUrl { + pub scheme: Option, + /// The host as written (brackets included for IPv6), or `None` when there is none. + pub host: Option, + pub port: Option, + pub has_user_info: bool, + pub raw_path: String, + pub has_query: bool, + pub has_fragment: bool, +} + +impl PluginUrl { + pub fn unbracket(host: &str) -> &str { + host.strip_prefix('[').and_then(|rest| rest.strip_suffix(']')).unwrap_or(host) + } + + /// `None` when `text` is not a URI at all. + pub fn parse(text: &str) -> Option { + if text.is_empty() { + return Some(Self::default()); + } + let (mut rest, fragment) = match text.split_once('#') { + Some((rest, fragment)) => (rest, Some(fragment)), + None => (text, None), + }; + let has_fragment = fragment.is_some(); + if fragment.is_some_and(|fragment| !legal(fragment, false) || fragment.contains('#')) { + return None; + } + + let mut scheme = None; + if let Some(stop) = rest.find(['/', '?', ':']).filter(|stop| rest.as_bytes()[*stop] == b':') { + let name = &rest[..stop]; + let valid = name.as_bytes().first().is_some_and(u8::is_ascii_alphabetic) && name.bytes().all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'+' | b'-' | b'.')); + if !valid { + return None; + } + scheme = Some(name.to_owned()); + rest = &rest[stop + 1..]; + if rest.is_empty() { + return None; + } + // An opaque URI such as "mailto:someone" has no host, path or query. + if !rest.starts_with('/') { + return legal(rest, false).then(|| Self { scheme, has_fragment, ..Self::default() }); + } + } + + let mut authority = None; + if let Some(after) = rest.strip_prefix("//") { + let end = after.find(['/', '?']).unwrap_or(after.len()); + let found = &after[..end]; + rest = &after[end..]; + if !legal(found, true) { + return None; + } + authority = Some(found); + } + let (path, query) = match rest.split_once('?') { + Some((path, query)) => (path, Some(query)), + None => (rest, None), + }; + if !legal(path, false) || query.is_some_and(|query| !legal(query, false)) { + return None; + } + + let mut host = None; + let mut port = None; + let mut has_user_info = false; + if let Some(authority) = authority.filter(|authority| !authority.is_empty()) { + let (user, server) = match authority.split_once('@') { + Some((_, server)) => (true, server), + None => (false, authority), + }; + let mut name = ""; + let mut port_text = ""; + if server.starts_with('[') { + if let Some(close) = server.find(']') { + let after = &server[close + 1..]; + if after.is_empty() || after.starts_with(':') { + name = &server[..close + 1]; + port_text = after.strip_prefix(':').unwrap_or(""); + } + if !name.is_empty() && name[1..name.len() - 1].parse::().is_err() { + name = ""; + } + } + } else { + let (candidate, after) = server.split_once(':').unwrap_or((server, "")); + port_text = after; + if server_name(candidate) { + name = candidate; + } + } + // Anything else is a registry-based authority: it has no host, port or user information. + let digits = port_text.bytes().all(|byte| byte.is_ascii_digit()); + let number = if port_text.is_empty() { None } else { port_text.parse::().ok().filter(|port| *port <= i32::MAX as u32) }; + if !name.is_empty() && digits && (port_text.is_empty() || number.is_some()) { + host = Some(name.to_owned()); + has_user_info = user; + port = number; + } + } + Some(Self { scheme, host, port, has_user_info, raw_path: path.to_owned(), has_query: query.is_some(), has_fragment }) + } +} + +/// An IPv4 address, or a host name of letter-digit-hyphen labels whose last label starts with a letter. +fn server_name(name: &str) -> bool { + if name.is_empty() { + return false; + } + let mut labels: Vec<&str> = name.split('.').collect(); + if labels.len() == 4 && labels.iter().all(|label| (1..=3).contains(&label.len()) && label.bytes().all(|byte| byte.is_ascii_digit()) && label.parse::().is_ok_and(|value| value <= 255)) { + return true; + } + if labels.last().is_some_and(|label| label.is_empty()) { + labels.pop(); + } + if labels.is_empty() { + return false; + } + let well_formed = labels.iter().all(|label| !label.is_empty() && label.bytes().all(|byte| byte.is_ascii_alphanumeric() || byte == b'-') && !label.starts_with('-') && !label.ends_with('-')); + well_formed && labels.last().is_some_and(|label| label.as_bytes()[0].is_ascii_alphabetic()) +} + +/// True when every character may appear in a URI and every `%` starts a two-digit escape. +fn legal(part: &str, brackets: bool) -> bool { + let bytes = part.as_bytes(); + let mut characters = part.char_indices(); + while let Some((index, character)) = characters.next() { + match character { + '%' => { + if index + 2 >= bytes.len() || !bytes[index + 1].is_ascii_hexdigit() || !bytes[index + 2].is_ascii_hexdigit() { + return false; + } + characters.next(); + characters.next(); + } + '[' | ']' => { + if !brackets { + return false; + } + } + character if character.is_ascii() => { + if !character.is_ascii_alphanumeric() && !"-_.!~*'();/?:@&=+$,".contains(character) { + return false; + } + } + character => { + if character.is_control() || character.is_whitespace() { + return false; + } + } + } + } + true +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn urls_are_split_like_java_net_uri() { + let url = PluginUrl::parse("https://api.example.com:8443/v2?x=1#top").unwrap(); + assert_eq!((url.scheme.as_deref(), url.host.as_deref(), url.port, url.raw_path.as_str(), url.has_query, url.has_fragment), (Some("https"), Some("api.example.com"), Some(8443), "/v2", true, true)); + assert!(PluginUrl::parse("https://user:pw@api.example.com").unwrap().has_user_info); + assert_eq!(PluginUrl::parse("https://[2606:4700::1111]:443/x").unwrap().host.as_deref(), Some("[2606:4700::1111]")); + assert_eq!(PluginUrl::parse("https://10.0.0.1/").unwrap().host.as_deref(), Some("10.0.0.1")); + assert_eq!(PluginUrl::parse("mailto:someone").unwrap().host, None); + assert_eq!(PluginUrl::parse("api.example.com").unwrap().scheme, None); + // No host: an underscore, a numeric last label, a bad port, an empty authority, a bad IPv6 literal. + for text in ["https://api_internal.example.com", "https://example.123", "https://api.example.com:port", "https:///path", "https://[::g]/", "https://-a.example.com"] { + assert_eq!(PluginUrl::parse(text).and_then(|url| url.host), None, "{text}"); + } + // Not a URI at all. + for text in ["https://api example.com", "https://api.example.com\\@evil.example.net", "https:\\\\api.example.com", "https://api.example.com/%zz", "https://api.example.com/a b", " https://api.example.com", "https://api.example.com\n", "1https://x", "https:", "https://a/#a#b"] { + assert_eq!(PluginUrl::parse(text), None, "{text:?}"); + } + } + + #[test] + fn small_grammars_match_their_patterns() { + assert!(is_identifier("abc") && is_identifier("a.b-c") && is_identifier(&"a".repeat(128))); + for text in ["ab", "-abc", ".abc", "has space", "a/b/c", "abc\n", ""] { + assert!(!is_identifier(text), "{text:?}"); + } + assert!(!is_identifier(&"a".repeat(129))); + assert!(is_semantic_version("1.0.0") && is_semantic_version("10.20.30-beta.1") && is_semantic_version("1.0.0-a-b")); + for text in ["1.0", "v1.0.0", "1.0.0-", "1..0", "1.0.0.0", "1.0.0-beta+build", ""] { + assert!(!is_semantic_version(text), "{text:?}"); + } + assert_eq!(placeholders("/teams/{team}/users/{user}/{}/{a b}/{x.y-z}"), ["team", "user", "x.y-z"]); + assert_eq!(placeholders("/{{a}}"), ["a"]); + assert!(is_header_name("X-Token") && !is_header_name("X Token") && !is_header_name("") && !is_header_name("a:b")); + } +} diff --git a/Linux/crates/sw-core/src/plugins/mod.rs b/Linux/crates/sw-core/src/plugins/mod.rs new file mode 100644 index 00000000..1fc61e7d --- /dev/null +++ b/Linux/crates/sw-core/src/plugins/mod.rs @@ -0,0 +1,18 @@ +//! Data Source Plugin 1.0: declarative HTTPS mappings behind `$dataSource`. +//! Ported from `Windows/src/ScriptWidget.Core/Plugins`. +//! +//! A plugin is a `plugin.json` and an optional `README.md`. It is data, not +//! code: it cannot load anything native and cannot enlarge what a widget may +//! reach. Manifests, archives and responses are untrusted. + +pub mod files; +pub mod manifest; +pub mod module; +pub mod requests; +pub mod store; +pub mod strict_json; + +pub use manifest::{PluginError, PluginErrorKind, PluginManifest, PluginOperation, PluginParameter, PluginParameterLocation, PluginResponseType, PluginValidationIssue}; +pub use module::DataSourceModule; +pub use requests::PreparedDataSourceRequest; +pub use store::{InstalledPlugin, PluginStore}; diff --git a/Linux/crates/sw-core/src/plugins/module.rs b/Linux/crates/sw-core/src/plugins/module.rs new file mode 100644 index 00000000..c119a16f --- /dev/null +++ b/Linux/crates/sw-core/src/plugins/module.rs @@ -0,0 +1,225 @@ +//! `$dataSource.request(pluginID, operationID, parameters)`. Ported from +//! `Windows/src/ScriptWidget.Core/Plugins/DataSourceModule.cs` +//! (`ScriptWidgetRuntimeDataSource` in Swift). + +use std::sync::{Arc, OnceLock}; + +use serde_json::Value; + +use crate::api::fetch::{self, BLOCKED_MESSAGE, Cancel, FetchError, FetchModule, Response, Url}; +use crate::api::policy::{self, NetworkDeclaration}; +use crate::api::workers::BoundedExecutor; +use crate::executor::{Completion, ExecutionRequest, ExecutionSession, NativeModule}; +use crate::json::{Object, ObjectExt, quote}; +use crate::plugins::manifest::{PluginError, PluginErrorKind, PluginManifest, PluginUrl}; +use crate::plugins::requests; +use crate::plugins::store::PluginStore; + +/// Performs one HTTP exchange for a plugin request: method, URL, headers, +/// body, timeout in milliseconds, the redirect rule and cancellation. +/// Production uses [`FetchModule::perform`], which resolves every hop and +/// refuses private addresses; tests inject a fake so they stay offline. The +/// redirect rule must be consulted for every redirect target before it is +/// followed. +pub type DataSourceTransport = Arc, u64, &dyn Fn(&Url) -> bool, Option) -> Result + Send + Sync>; + +/// Runs the request off the JavaScript thread. +pub type DataSourceExecutor = Arc) + Send + Sync>; + +pub const SESSION_ENDED: &str = "Execution session is no longer active"; +pub const LIVE_ACTIVITY: &str = "[live_activity_unavailable] Live Activity presentations cannot request service data; update their state from the app"; +pub const TIMELINE: &str = "[timeline_read_only] Future entries cannot make live service requests"; +pub const NETWORK_PERMISSION: &str = "Plugin requests require the network permission"; +pub const HTTPS_ONLY: &str = "Data source requests require HTTPS"; +pub const PLUGIN_HOST: &str = "Plugin request escaped its declared hosts."; +pub const PACKAGE_HOST: &str = "Data source host is not declared in widget.json networkDomains."; +pub const BUDGET: &str = "Network request budget exceeded"; + +/// What the destination rules need to know about the package, detached from +/// the request so that a worker thread can apply them to redirect targets. +#[derive(Clone)] +pub struct PackageScope { + session: Arc, + has_manifest: bool, + network: NetworkDeclaration, +} + +impl PackageScope { + pub fn of(request: &ExecutionRequest<'_>) -> Self { + Self { session: request.session.clone(), has_manifest: request.prepared.has_manifest, network: NetworkDeclaration::of(request) } + } +} + +/// Presentation rules: a finished execution gets nothing; Live Activity +/// presentations and future timeline entries are rendered ahead of time and +/// must not make live requests. Returns the rejection message, the Swift +/// wording, or `None` when the rule is satisfied. +pub fn presentation_error(request: &ExecutionRequest<'_>) -> Option<&'static str> { + if request.session.finished() { + Some(SESSION_ENDED) + } else if policy::is_live_activity(request) { + Some(LIVE_ACTIVITY) + } else if policy::is_timeline_projection(request) { + Some(TIMELINE) + } else { + None + } +} + +/// Declaration rules: the package's validated `widget.json` must list +/// `plugin_id` in `plugins` and hold the `network` permission. A package +/// without a manifest declares nothing, so it is refused: installing a +/// plugin never grants a widget anything by itself. +pub fn declaration_error(request: &ExecutionRequest<'_>, plugin_id: &str) -> Option { + let manifest = request.prepared.manifest.as_ref().filter(|_| request.prepared.has_manifest); + let declared = manifest.and_then(|manifest| manifest.opt_array("plugins")).is_some_and(|plugins| plugins.iter().any(|item| item.as_str() == Some(plugin_id))); + if !declared { + return Some(format!("Plugin {plugin_id} is not declared in widget.json")); + } + (!policy::has_permission(request, "network")).then(|| NETWORK_PERMISSION.to_owned()) +} + +/// Destination rules for the initial URL and for every redirect target: +/// HTTPS only, a public host name without credentials, a host the plugin +/// declares, and a host the package allows through `network` and +/// `networkDomains`. A plugin therefore never enlarges what the package may +/// reach, and a package never reaches a host the plugin did not declare. +pub fn destination_error(scope: &PackageScope, plugin: &PluginManifest, url: &Url) -> Option<&'static str> { + if url.scheme() != "https" { + return Some(HTTPS_ONLY); + } + let host = url.host_str().unwrap_or("").to_lowercase(); + // Same textual rule as `fetch`: a host, no credentials, not local or private. + if PluginUrl::unbracket(&host).is_empty() { + return Some("URL host is required"); + } + if !url.username().is_empty() || url.password().is_some() { + return Some("URL credentials are not allowed"); + } + if policy::blocked_host_name(PluginUrl::unbracket(&host)) { + return Some(BLOCKED_MESSAGE); + } + if !policy::host_matches(&host, plugin.hosts.iter().map(String::as_str)) { + return Some(PLUGIN_HOST); + } + if !scope.has_manifest || !scope.network.allows_host(&host) { + return Some(PACKAGE_HOST); + } + None +} + +/// True when a redirect to `url` may be followed; the execution must still be running. +pub fn allows_redirect(scope: &PackageScope, plugin: &PluginManifest, url: &Url) -> bool { + !scope.session.finished() && destination_error(scope, plugin, url).is_none() +} + +/// Handles the asynchronous operation `datasource.request` with payload +/// `{pluginID, operationID, parameters}` and resolves with the mapped response. +/// +/// Every rule is checked here, in this order, with the Swift error strings: +/// execution still active, not a Live Activity, not a timeline projection, +/// plugin declared in `widget.json`, `network` permission, plugin installed +/// and valid, request built only from declared parameters, HTTPS destination +/// inside both the plugin's hosts and the package's `networkDomains`, and the +/// per-execution network budget shared with `fetch`. Redirects are +/// re-validated against the same destination rules. Errors never contain +/// response content, header values or parameter values. The JavaScript +/// installer is not a security boundary. +pub struct DataSourceModule { + store: Arc, + transport: DataSourceTransport, + executor: DataSourceExecutor, +} + +impl DataSourceModule { + pub const OPERATION: &'static str = "datasource.request"; + /// The Swift fetch session's per-request timeout (5 s); the 10 s total is enforced by the transport. + pub const TIMEOUT_MILLISECONDS: u64 = 5_000; + + /// The module over the bounded `fetch` transport and a bounded worker pool. + pub fn new(store: Arc) -> Self { + static FETCH: OnceLock = OnceLock::new(); + static POOL: OnceLock = OnceLock::new(); + let transport: DataSourceTransport = + Arc::new(|method, url, headers, body, timeout, allow_redirect, cancel| FETCH.get_or_init(FetchModule::new).perform(method, url, headers, body, timeout, allow_redirect, cancel)); + let executor: DataSourceExecutor = Arc::new(|work| POOL.get_or_init(|| BoundedExecutor::new(fetch::MAXIMUM_CONCURRENT)).execute(work)); + Self::with_parts(store, transport, executor) + } + + pub fn with_parts(store: Arc, transport: DataSourceTransport, executor: DataSourceExecutor) -> Self { + Self { store, transport, executor } + } +} + +impl NativeModule for DataSourceModule { + fn handles(&self, operation: &str) -> bool { + operation == Self::OPERATION + } + + fn apis(&self) -> &[&'static str] { + &["$dataSource"] + } + + fn start(&self, request: &ExecutionRequest<'_>, operation: &str, payload: Option<&Object>, done: Completion) { + let fail = |done: Completion, message: &str| done.complete(false, Some(quote(message))); + if operation != Self::OPERATION { + return fail(done, &format!("Unsupported operation {operation}")); + } + if let Some(presentation) = presentation_error(request) { + return fail(done, presentation); + } + let text = |name: &str| payload.and_then(|job| job.get(name)).and_then(Value::as_str).map(str::to_owned); + let (Some(plugin_id), Some(operation_id)) = (text("pluginID"), text("operationID")) else { + return fail(done, "Data source requests need a plugin id and an operation id"); + }; + if let Some(declaration) = declaration_error(request, &plugin_id) { + return fail(done, &declaration); + } + let values = payload.and_then(|job| job.opt_object("parameters")).cloned().unwrap_or_default(); + let scope = PackageScope::of(request); + let store = self.store.clone(); + let transport = self.transport.clone(); + + (self.executor)(Box::new(move || { + let session = scope.session.clone(); + let outcome = (|| -> Result, PluginError> { + let plugin = store.manifest(&plugin_id).ok_or_else(|| PluginError::new(PluginErrorKind::NotFound, format!("Plugin {plugin_id} is not installed")))?; + let prepared = requests::prepare(&plugin, &operation_id, &values)?; + if let Some(destination) = destination_error(&scope, &plugin, &prepared.url) { + return Err(PluginError::new(PluginErrorKind::Permission, destination)); + } + if session.finished() || session.add_network_request() > fetch::MAXIMUM_REQUESTS { + return Err(PluginError::new(PluginErrorKind::Network, BUDGET)); + } + let ended = session.clone(); + let response = transport( + &prepared.method, + &prepared.url, + &prepared.headers, + prepared.body.as_deref(), + Self::TIMEOUT_MILLISECONDS, + &|redirected: &Url| allows_redirect(&scope, &plugin, redirected), + Some(Arc::new(move || ended.finished())), + ) + .map_err(|error| { + let message = match &error { + FetchError::Failed(message) if !message.is_empty() => message.as_str(), + // Text from the network stack can quote the URL, which may carry parameter values. + _ => "Data source request failed", + }; + PluginError::new(PluginErrorKind::Network, message) + })?; + if session.finished() { + return Ok(None); + } + requests::map_response(&prepared.operation, response.status, &response.body).map(Some) + })(); + match outcome { + Ok(Some(json)) => done.complete(true, Some(json)), + // The execution ended: there is nobody to answer. + Ok(None) => drop(done), + Err(error) => done.complete(false, Some(quote(&error.message))), + } + })); + } +} diff --git a/Linux/crates/sw-core/src/plugins/requests.rs b/Linux/crates/sw-core/src/plugins/requests.rs new file mode 100644 index 00000000..f2a5441a --- /dev/null +++ b/Linux/crates/sw-core/src/plugins/requests.rs @@ -0,0 +1,247 @@ +//! Maps a plugin operation and script-supplied values to one request, and a +//! response to the value a widget receives. Ported from +//! `Windows/src/ScriptWidget.Core/Plugins/DataSourceRequests.cs` +//! (`DataSourceRequestBuilder.prepare` in Swift). + +use serde_json::Value; + +use crate::api::fetch::{self, Url}; +use crate::api::policy; +use crate::json::{Object, stringify_object}; +use crate::plugins::manifest::{PluginError, PluginErrorKind, PluginManifest, PluginOperation, PluginParameterLocation, PluginResponseType, PluginUrl, decode_utf8}; +use crate::plugins::strict_json; + +/// A fully resolved plugin request: nothing in it is taken from script input without validation. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PreparedDataSourceRequest { + pub plugin: PluginManifest, + pub operation: PluginOperation, + pub method: String, + pub url: Url, + pub headers: Vec<(String, String)>, + /// JSON body built from `jsonBody` parameters, or `None` when there are none. + pub body: Option>, +} + +impl PreparedDataSourceRequest { + pub fn header(&self, name: &str) -> Option<&str> { + self.headers.iter().find(|(known, _)| known == name).map(|(_, value)| value.as_str()) + } +} + +/// Sent with every plugin request, replacing any `User-Agent` header parameter. +pub const USER_AGENT: &str = "ScriptWidget-DataSource/1"; + +// CharacterSet.urlPathAllowed without "/?#{}": a path value can never add a segment, a query or a fragment. +const PATH_ALLOWED: &str = "-._~!$&'()*+,=:@"; +// What URLComponents.queryItems leaves unescaped: "&" and "=" are always encoded, so a value cannot add a query item. +const QUERY_ALLOWED: &str = "-._~!$'()*+,;:@/?"; + +fn invalid_parameters(message: String) -> PluginError { + PluginError::new(PluginErrorKind::InvalidParameters, message) +} + +/// Builds the request for `operation_id` of `plugin`. +/// +/// `values` come from package code and are untrusted. Strings, booleans and +/// numbers are accepted everywhere, objects and arrays for `jsonBody` +/// parameters only; null means "not supplied". Booleans are sent as `1`/`0` +/// in path, query and header positions, which is what the Swift runtime sends +/// for a JavaScript boolean. +/// +/// Security rules: +/// - only declared parameters are used; anything else in `values` is ignored, +/// so a script cannot add headers, query items or body fields the plugin +/// did not declare; +/// - path values are percent-encoded including `/`, `?`, `#`, and `.`/`..` +/// values are refused, so a value cannot change the path structure; +/// - query names and values are percent-encoded including `&` and `=`; +/// - header values with control characters are refused (no header injection); +/// - the final URL must be HTTPS and its host must match the plugin's declared hosts. +/// +/// Fails with `NotFound` for an unknown operation, `InvalidParameters` for a +/// missing or unusable value, `Permission` when the URL leaves the declared hosts. +pub fn prepare(plugin: &PluginManifest, operation_id: &str, values: &Object) -> Result { + let operation = plugin + .operations + .iter() + .find(|item| item.id == operation_id) + .ok_or_else(|| PluginError::new(PluginErrorKind::NotFound, format!("Operation {operation_id} was not found in {}.", plugin.id)))?; + let mut path = operation.path.clone(); + let mut query: Vec<(String, String)> = Vec::new(); + let mut headers: Vec<(String, String)> = Vec::new(); + let mut body = Object::new(); + for parameter in &operation.parameters { + let could_not_encode = || invalid_parameters(format!("Could not encode {}.", parameter.id)); + // JSON null and a missing member both mean "not supplied". + let raw: Option = values.get(¶meter.id).filter(|value| !value.is_null()).cloned().or_else(|| parameter.default_value.clone().map(Value::String)); + let structured = matches!(raw, Some(Value::Object(_) | Value::Array(_))); + let text = match &raw { + Some(value) if !structured => describe(value).ok_or_else(could_not_encode)?, + _ => String::new(), + }; + let Some(raw) = raw.filter(|_| structured || !text.is_empty()) else { + if parameter.required { + return Err(invalid_parameters(format!("Missing required parameter {}.", parameter.id))); + } + continue; + }; + if structured && parameter.location != PluginParameterLocation::JsonBody { + return Err(could_not_encode()); + } + match parameter.location { + PluginParameterLocation::Path => { + if text == "." || text == ".." { + return Err(could_not_encode()); + } + path = path.replace(&format!("{{{}}}", parameter.id), &percent_encode(&text, PATH_ALLOWED)); + } + PluginParameterLocation::Query => query.push((parameter.id.clone(), text)), + PluginParameterLocation::Header => { + if text.chars().any(|character| character < ' ' || character == '\u{7f}') { + return Err(could_not_encode()); + } + set_header(&mut headers, ¶meter.id, text); + } + PluginParameterLocation::JsonBody => { + body.insert(parameter.id.clone(), raw); + } + } + } + if path.contains(['{', '}']) { + return Err(invalid_parameters("A path parameter is unresolved.".into())); + } + let invalid_base = || PluginError::invalid("Invalid plugin baseURL."); + let escaped = || PluginError::new(PluginErrorKind::Permission, "Plugin request escaped its declared hosts."); + let address = PluginUrl::parse(&plugin.base_url).ok_or_else(invalid_base)?; + let (Some(scheme), Some(base_host)) = (address.scheme.as_deref(), address.host.as_deref().filter(|host| !host.is_empty())) else { return Err(invalid_base()) }; + let mut assembled = format!("{}://{base_host}", scheme.to_lowercase()); + if let Some(port) = address.port { + assembled.push_str(&format!(":{port}")); + } + if address.raw_path != "/" { + assembled.push_str(&address.raw_path); + } + assembled.push_str(&path); + if !query.is_empty() { + assembled.push('?'); + assembled.push_str(&query.iter().map(|(name, value)| format!("{}={}", percent_encode(name, QUERY_ALLOWED), percent_encode(value, QUERY_ALLOWED))).collect::>().join("&")); + } + let parsed = PluginUrl::parse(&assembled).ok_or_else(invalid_base)?; + let host = parsed.host.as_deref().map(str::to_lowercase); + if parsed.scheme.as_deref() != Some("https") || !host.is_some_and(|host| policy::host_matches(&host, plugin.hosts.iter().map(String::as_str))) { + return Err(escaped()); + } + // The transport sends what its own URL parser understood, so the same rule is applied to that reading of the URL. + let url = Url::parse(&assembled).map_err(|_| invalid_base())?; + if url.scheme() != "https" || !url.username().is_empty() || url.password().is_some() || !policy::host_matches(url.host_str().unwrap_or(""), plugin.hosts.iter().map(String::as_str)) { + return Err(escaped()); + } + let has_body = !body.is_empty(); + headers.retain(|(name, _)| !name.eq_ignore_ascii_case("User-Agent") && !(has_body && name.eq_ignore_ascii_case("Content-Type"))); + headers.push(("User-Agent".into(), USER_AGENT.into())); + if has_body { + headers.push(("Content-Type".into(), "application/json".into())); + } + Ok(PreparedDataSourceRequest { + plugin: plugin.clone(), + operation: operation.clone(), + method: operation.method.clone(), + url, + headers, + body: has_body.then(|| stringify_object(&body).into_bytes()), + }) +} + +fn set_header(headers: &mut Vec<(String, String)>, name: &str, value: String) { + match headers.iter_mut().find(|(known, _)| known == name) { + Some(entry) => entry.1 = value, + None => headers.push((name.to_owned(), value)), + } +} + +/// Text form of a scalar value, or `None` when it has none. +fn describe(raw: &Value) -> Option { + match raw { + Value::String(value) => Some(value.clone()), + Value::Bool(value) => Some(if *value { "1" } else { "0" }.to_owned()), + Value::Number(number) => match number.as_i64() { + Some(whole) => Some(whole.to_string()), + None => { + let value = number.as_f64().filter(|value| value.is_finite())?; + // A whole number is written without a fraction; anything else in the shortest + // decimal form that reads back as the same number, without an exponent. + Some(if value == value.round_ties_even() && value.abs() < 1e15 { (value as i64).to_string() } else { value.to_string() }) + } + }, + _ => None, + } +} + +/// Percent-encodes every UTF-8 byte that is not an ASCII letter, digit or in `allowed`. +pub fn percent_encode(value: &str, allowed: &str) -> String { + let mut output = String::with_capacity(value.len()); + for byte in value.bytes() { + if byte.is_ascii_alphanumeric() || allowed.as_bytes().contains(&byte) { + output.push(byte as char); + } else { + output.push_str(&format!("%{byte:02X}")); + } + } + output +} + +/// `ScriptWidgetFetchManager.maximumResponseBytes`: plugin responses share the 2 MiB fetch limit. +pub const MAXIMUM_RESPONSE_BYTES: usize = fetch::MAXIMUM_RESPONSE_BYTES; + +/// Turns an HTTP response into the JSON text delivered to the script: the +/// response document itself for a `json` operation, or a JSON string literal +/// for a `text` operation. +/// +/// Fails closed with [`PluginErrorKind::Network`]: a non-2xx status, more +/// than 2 MiB, a `json` body that is not one well-formed UTF-8 JSON object or +/// array, or a `text` body that is not valid UTF-8. Response content is never +/// included in the error message. +pub fn map_response(operation: &PluginOperation, status: u16, body: &[u8]) -> Result { + let network = |message: &str| PluginError::new(PluginErrorKind::Network, message); + if !(200..=299).contains(&status) { + return Err(network("Data source request failed")); + } + if body.len() > MAXIMUM_RESPONSE_BYTES { + return Err(network("Data source response exceeds 2 MiB.")); + } + let text = decode_utf8(body); + if operation.response_type == PluginResponseType::Text { + return text.map(strict_json::quote).ok_or_else(|| network("Data source returned non-UTF-8 text")); + } + let invalid = || network("Data source returned invalid JSON"); + let text = text.ok_or_else(invalid)?; + // JSONSerialization without .fragmentsAllowed accepts only containers at the top level. + match strict_json::validate(text) { + Ok(strict_json::Kind::Object | strict_json::Kind::Array) => Ok(text.to_owned()), + _ => Err(invalid()), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn scalars_have_one_text_form() { + for (value, expected) in [(json!("x"), "x"), (json!(true), "1"), (json!(false), "0"), (json!(42), "42"), (json!(-7), "-7"), (json!(2.0), "2"), (json!(1.5), "1.5"), (json!(1e-6), "0.000001"), (json!(1e20), "100000000000000000000"), (json!(-0.25), "-0.25")] { + assert_eq!(describe(&value).as_deref(), Some(expected), "{value}"); + } + assert_eq!(describe(&json!([1])), None); + assert_eq!(describe(&json!({})), None); + assert_eq!(describe(&Value::Null), None); + } + + #[test] + fn percent_encoding_covers_every_byte_outside_the_allowed_set() { + assert_eq!(percent_encode("a/b?c#d e%", PATH_ALLOWED), "a%2Fb%3Fc%23d%20e%25"); + assert_eq!(percent_encode("a&b=c", QUERY_ALLOWED), "a%26b%3Dc"); + assert_eq!(percent_encode("你", ""), "%E4%BD%A0"); + } +} diff --git a/Linux/crates/sw-core/src/plugins/store.rs b/Linux/crates/sw-core/src/plugins/store.rs new file mode 100644 index 00000000..43a2faf7 --- /dev/null +++ b/Linux/crates/sw-core/src/plugins/store.rs @@ -0,0 +1,338 @@ +//! Installed Data Source Plugins. Ported from +//! `Windows/src/ScriptWidget.Core/Plugins/PluginStore.cs`, itself a port of +//! `DataSourcePluginManager`. +//! +//! Each plugin is a directory below the root holding `plugin.json` and an +//! optional `README.md`, nothing else. Imported files are untrusted: they are +//! bounded, validated completely before anything is written, staged in a +//! private directory and published with one rename, so a failed import leaves +//! nothing behind and readers never see a partial plugin. + +use std::io::Read; +use std::os::unix::fs::DirBuilderExt; +use std::path::{Path, PathBuf}; +use std::sync::{Mutex, OnceLock}; + +use crate::plugins::files::{self, ArchiveEntry}; +use crate::plugins::manifest::{self, PluginError, PluginErrorKind, PluginManifest, PluginOperation, PluginParameter, PluginParameterLocation, PluginResponseType}; + +/// A plugin the store can serve. Built-in plugins ship with the app and have no directory. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct InstalledPlugin { + pub manifest: PluginManifest, + pub built_in: bool, + pub readme: Option, +} + +/// `DataSourcePluginManager.maximumPackageBytes`: the largest archive or manifest accepted for import. +pub const MAXIMUM_PACKAGE_BYTES: usize = 512 * 1024; +/// Largest `README.md` stored or shown (Swift bounds it only by the archive limits). +pub const MAXIMUM_README_BYTES: usize = 512 * 1024; +pub const MANIFEST_FILE: &str = "plugin.json"; +pub const README_FILE: &str = "README.md"; +const STAGING_DIRECTORY: &str = ".staging"; +const NOT_A_PACKAGE: &str = "Archive must contain one Data Source Plugin 1.0 package."; + +/// The plugins every ScriptWidget build ships, identical to `DataSourcePluginManager.builtIns`. +pub fn built_ins() -> &'static [PluginManifest] { + static BUILT_INS: OnceLock> = OnceLock::new(); + BUILT_INS.get_or_init(|| { + let parameter = |id: &str, label: &str, location, required, default_value: &str| PluginParameter { id: id.into(), label: label.into(), location, required, default_value: Some(default_value.into()) }; + let plugin = |id: &str, name: &str, summary: &str, symbol: &str, author: &str, license: &str, host: &str, operation: PluginOperation| PluginManifest { + format_version: 1, + id: id.into(), + name: name.into(), + version: "1.0.0".into(), + summary: summary.into(), + symbol: symbol.into(), + author: author.into(), + license: license.into(), + minimum_runtime_version: "1.0".into(), + base_url: format!("https://{host}"), + hosts: vec![host.into()], + operations: vec![operation], + }; + vec![ + plugin( + "app.scriptwidget.datasource.open-meteo", + "Open-Meteo", + "Weather forecasts without an API key.", + "cloud.sun", + "Open-Meteo", + "CC BY 4.0", + "api.open-meteo.com", + PluginOperation { + id: "forecast".into(), + title: "Forecast".into(), + method: "GET".into(), + path: "/v1/forecast".into(), + response_type: PluginResponseType::Json, + parameters: vec![ + parameter("latitude", "Latitude", PluginParameterLocation::Query, true, "37.7749"), + parameter("longitude", "Longitude", PluginParameterLocation::Query, true, "-122.4194"), + parameter("current", "Current fields", PluginParameterLocation::Query, false, "temperature_2m,weather_code"), + parameter("timezone", "Timezone", PluginParameterLocation::Query, false, "auto"), + ], + }, + ), + plugin( + "app.scriptwidget.datasource.github", + "GitHub Public API", + "Public repository metadata from GitHub.", + "chevron.left.forwardslash.chevron.right", + "GitHub", + "API Terms", + "api.github.com", + PluginOperation { + id: "repository".into(), + title: "Repository".into(), + method: "GET".into(), + path: "/repos/{owner}/{repo}".into(), + response_type: PluginResponseType::Json, + parameters: vec![parameter("owner", "Owner", PluginParameterLocation::Path, true, "everettjf"), parameter("repo", "Repository", PluginParameterLocation::Path, true, "scriptwidget")], + }, + ), + ] + }) +} + +pub struct PluginStore { + root: PathBuf, + staging: PathBuf, + gate: Mutex<()>, +} + +fn invalid(message: &str) -> PluginError { + PluginError::invalid(message) +} + +fn storage(message: &str) -> PluginError { + PluginError::new(PluginErrorKind::Storage, message) +} + +impl PluginStore { + pub fn new(root: impl AsRef) -> Self { + let root = std::path::absolute(root.as_ref()).unwrap_or_else(|_| root.as_ref().to_owned()); + Self { staging: root.join(STAGING_DIRECTORY), root, gate: Mutex::new(()) } + } + + /// Built-in plugins not replaced by an installed one, plus installed plugins, sorted by name. + pub fn list(&self) -> Vec { + let mut custom: Vec = Vec::new(); + for directory in self.directories() { + if let Some(manifest) = load(&directory).filter(|manifest| !custom.iter().any(|known| known.manifest.id == manifest.id)) { + custom.push(InstalledPlugin { manifest, built_in: false, readme: read_text(&directory, README_FILE, MAXIMUM_README_BYTES) }); + } + } + let mut plugins: Vec = built_ins() + .iter() + .filter(|manifest| !custom.iter().any(|known| known.manifest.id == manifest.id)) + .map(|manifest| InstalledPlugin { manifest: manifest.clone(), built_in: true, readme: Some(built_in_readme(manifest)) }) + .collect(); + plugins.append(&mut custom); + plugins.sort_by_cached_key(|plugin| plugin.manifest.name.to_uppercase()); + plugins + } + + /// The validated manifest for `id`, or `None`. A stored manifest is + /// validated again on every read, so a file altered on disk after import + /// is never used unless it still passes. + pub fn manifest(&self, id: &str) -> Option { + self.directories().iter().filter_map(|directory| load(directory)).find(|manifest| manifest.id == id).or_else(|| built_ins().iter().find(|manifest| manifest.id == id).cloned()) + } + + /// Installs a plugin from a `.swplugin` ZIP archive or a bare `plugin.json`. + /// + /// Security rules, each failing closed: + /// - at most 512 KiB of input; + /// - an archive passes the archive preflight (no traversal, absolute + /// paths, backslashes, symlinks, encrypted entries, case collisions or + /// forged directories); + /// - the package root (archive root or its single directory) holds only + /// `plugin.json` and an optional `README.md`, both regular files: a + /// plugin cannot carry code or other payloads; + /// - entries are inflated with their real sizes counted, never trusting + /// declared sizes, and their CRC-32 checked; `plugin.json` is at most + /// 256 KiB and `README.md` 512 KiB of UTF-8; + /// - the manifest passes [`manifest::decode`]; + /// - an id that is already installed or built in is refused rather than replaced. + pub fn install(&self, input: &mut dyn Read) -> Result { + let bytes = read_input(input)?; + let (manifest_bytes, readme) = if is_zip(&bytes) { unpack(&bytes)? } else { (bytes, None) }; + let manifest = manifest::decode(&manifest_bytes)?; + let _guard = self.gate.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + if self.manifest(&manifest.id).is_some() { + return Err(invalid(&format!("Plugin {} is already installed.", manifest.id))); + } + let work = self.work_directory("install")?; + let installed = (|| -> Result<(), PluginError> { + let failed = |_: std::io::Error| storage("The plugin could not be installed."); + let prepared = work.join("plugin"); + files::create_private_directory(&prepared).map_err(failed)?; + files::write_new(&prepared.join(MANIFEST_FILE), &manifest_bytes).map_err(failed)?; + if let Some(readme) = &readme { + files::write_new(&prepared.join(README_FILE), readme).map_err(failed)?; + } + files::publish(&prepared, &self.unique_directory(&manifest.name)).map_err(|error| storage(&error.0)) + })(); + files::delete_tree(&work); + installed.map(|()| manifest) + } + + /// Removes the installed plugin `id`. The directory is first renamed out + /// of the store, so the plugin disappears atomically. Built-in plugins + /// cannot be deleted. + pub fn delete(&self, id: &str) -> Result<(), PluginError> { + let _guard = self.gate.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + let Some(directory) = self.directory_of(id) else { + let built_in = built_ins().iter().any(|manifest| manifest.id == id); + return Err(PluginError::new(PluginErrorKind::NotFound, if built_in { "Built-in plugins cannot be deleted." } else { "Plugin not found." })); + }; + let work = self.work_directory("delete")?; + let moved = std::fs::rename(&directory, work.join("plugin")).map_err(|_| storage("The plugin could not be deleted.")); + files::delete_tree(&work); + moved + } + + /// Plugin `id` as a `.swplugin` ZIP with `plugin.json` and, when present, + /// `README.md` at the archive root. Only those two files are ever + /// exported, so nothing else placed in the plugin directory can leak. + pub fn export(&self, id: &str) -> Result, PluginError> { + let not_found = || PluginError::new(PluginErrorKind::NotFound, "Plugin not found."); + let mut contents: Vec<(&str, Vec)> = Vec::new(); + match self.directory_of(id) { + Some(directory) => { + contents.push((MANIFEST_FILE, read_bytes(&directory, MANIFEST_FILE, manifest::MAXIMUM_MANIFEST_BYTES).ok_or_else(not_found)?)); + if let Some(readme) = read_bytes(&directory, README_FILE, MAXIMUM_README_BYTES) { + contents.push((README_FILE, readme)); + } + } + None => { + let built_in = built_ins().iter().find(|manifest| manifest.id == id).ok_or_else(not_found)?; + contents.push((MANIFEST_FILE, built_in.to_json().into_bytes())); + contents.push((README_FILE, built_in_readme(built_in).into_bytes())); + } + } + let entries: Vec<(&str, &[u8])> = contents.iter().map(|(name, bytes)| (*name, bytes.as_slice())).collect(); + files::write_archive(&entries).map_err(|_| storage("Could not create the .swplugin archive.")) + } + + // MARK: - Storage + + /// Plugin directories: real directories (never links) whose names are not hidden. + fn directories(&self) -> Vec { + if !files::real_directory(&self.root) { + return Vec::new(); + } + let mut names: Vec = files::names(&self.root).unwrap_or_default().into_iter().filter(|name| !name.starts_with('.')).collect(); + names.sort_unstable(); + names.iter().map(|name| self.root.join(name)).filter(|path| files::real_directory(path)).collect() + } + + fn directory_of(&self, id: &str) -> Option { + self.directories().into_iter().find(|directory| load(directory).is_some_and(|manifest| manifest.id == id)) + } + + fn work_directory(&self, prefix: &str) -> Result { + let unavailable = || storage("The plugin directory is unavailable."); + // Private to the user, like every other directory of the data folder. + std::fs::DirBuilder::new().recursive(true).mode(0o700).create(&self.root).map_err(|_| unavailable())?; + if !files::real_directory(&self.root) { + return Err(unavailable()); + } + files::new_work_directory(&self.staging, prefix).map_err(|_| unavailable()) + } + + /// Directory names are derived from the plugin name but reduced to `[a-z0-9-]`, so they cannot traverse. + fn unique_directory(&self, name: &str) -> PathBuf { + let mut slug = String::new(); + for character in name.to_lowercase().chars() { + if character.is_ascii_lowercase() || character.is_ascii_digit() { + slug.push(character); + } else if !slug.is_empty() && !slug.ends_with('-') { + slug.push('-'); + } + } + let slug = slug.trim_end_matches('-'); + let base = if slug.is_empty() { "plugin" } else { slug }; + let mut path = self.root.join(base); + let mut index = 2; + while files::occupied(&path) { + path = self.root.join(format!("{base}-{index}")); + index += 1; + } + path + } +} + +// MARK: - Import + +fn read_input(input: &mut dyn Read) -> Result, PluginError> { + let mut bytes = Vec::new(); + input.take(MAXIMUM_PACKAGE_BYTES as u64 + 1).read_to_end(&mut bytes).map_err(|_| storage("The plugin file could not be read."))?; + if bytes.len() > MAXIMUM_PACKAGE_BYTES { + return Err(invalid("A plugin package may not exceed 512 KiB.")); + } + if bytes.is_empty() { + return Err(invalid(NOT_A_PACKAGE)); + } + Ok(bytes) +} + +fn is_zip(bytes: &[u8]) -> bool { + bytes.len() >= 4 && bytes.starts_with(b"PK") +} + +/// The bytes of `plugin.json` and `README.md` (or `None`) from a validated archive. +fn unpack(archive: &[u8]) -> Result<(Vec, Option>), PluginError> { + let entries = files::validate_archive(archive)?; + // Hidden files and the macOS resource directory are ignored, as in the Swift importer. + let visible: Vec<&ArchiveEntry> = entries.iter().filter(|entry| entry.parts[0] != "__MACOSX" && !entry.parts.iter().any(|part| part.starts_with('.'))).collect(); + let at_root = visible.iter().any(|entry| entry.parts.len() == 1 && !entry.is_directory && entry.parts[0] == MANIFEST_FILE); + let mut prefix = 0; + if !at_root { + let mut roots: Vec<&str> = visible.iter().filter(|entry| entry.parts.len() > 1 || entry.is_directory).map(|entry| entry.parts[0].as_str()).collect(); + roots.sort_unstable(); + roots.dedup(); + if roots.len() != 1 { + return Err(invalid(NOT_A_PACKAGE)); + } + prefix = 1; + } + let members: Vec<&ArchiveEntry> = visible.into_iter().filter(|entry| entry.parts.len() > prefix).collect(); + let allowed = |entry: &&ArchiveEntry| !entry.is_directory && entry.parts.len() == prefix + 1 && [MANIFEST_FILE, README_FILE].contains(&entry.parts[prefix].as_str()); + let member = |name: &str| members.iter().find(|entry| entry.parts[prefix] == name).copied(); + let Some(manifest_entry) = member(MANIFEST_FILE).filter(|_| members.iter().all(allowed)) else { + return Err(invalid("A plugin package allows only plugin.json and optional README.md.")); + }; + let inflate = |entry: &ArchiveEntry, maximum_bytes: usize, too_large: &str| -> Result, PluginError> { + if entry.uncompressed_size > maximum_bytes as u64 { + return Err(invalid(too_large)); + } + files::inflate(archive, entry, maximum_bytes).map_err(|_| invalid("The plugin archive is corrupt.")) + }; + let manifest = inflate(manifest_entry, manifest::MAXIMUM_MANIFEST_BYTES, "plugin.json exceeds 256 KiB.")?; + let readme = member(README_FILE).map(|entry| inflate(entry, MAXIMUM_README_BYTES, "README.md exceeds 512 KiB.")).transpose()?; + if readme.as_ref().is_some_and(|readme| manifest::decode_utf8(readme).is_none()) { + return Err(invalid("README.md must be UTF-8 text.")); + } + Ok((manifest, readme)) +} + +fn load(directory: &Path) -> Option { + manifest::decode(&read_bytes(directory, MANIFEST_FILE, manifest::MAXIMUM_MANIFEST_BYTES)?).ok() +} + +/// Reads a regular, singly linked file that is not a symlink and stays inside `directory`. +fn read_bytes(directory: &Path, name: &str, maximum_bytes: usize) -> Option> { + files::read_bounded(&files::existing_file(directory, name)?, maximum_bytes) +} + +fn read_text(directory: &Path, name: &str, maximum_bytes: usize) -> Option { + let bytes = read_bytes(directory, name, maximum_bytes)?; + manifest::decode_utf8(&bytes).map(str::to_owned) +} + +fn built_in_readme(manifest: &PluginManifest) -> String { + format!("# {}\n\n{}\n", manifest.name, manifest.summary) +} diff --git a/Linux/crates/sw-core/src/plugins/strict_json.rs b/Linux/crates/sw-core/src/plugins/strict_json.rs new file mode 100644 index 00000000..24856308 --- /dev/null +++ b/Linux/crates/sw-core/src/plugins/strict_json.rs @@ -0,0 +1,393 @@ +//! RFC 8259 JSON reader and writer for untrusted plugin manifests and data +//! source responses. Ported from +//! `Windows/src/ScriptWidget.Core/Plugins/StrictJson.cs`. +//! +//! Lenient readers accept unquoted strings, single quotes, comments and +//! trailing text, and platforms disagree on number types and duplicate keys. +//! A manifest that one platform reads and another refuses is not a contract, +//! so manifests are parsed here: exactly one JSON value, nothing after it, no +//! raw control characters, bounded nesting. + +use std::collections::HashSet; +use std::fmt; + +use serde_json::{Map, Number, Value}; + +/// Deepest container nesting accepted; `JSONSerialization` on Apple platforms stops at 512 as well. +pub const MAXIMUM_DEPTH: usize = 512; + +/// What a document's top-level value is, for callers that only validate. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Kind { + Object, + Array, + String, + Number, + Boolean, + Null, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct JsonError(pub String); + +impl fmt::Display for JsonError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(&self.0) + } +} + +impl std::error::Error for JsonError {} + +/// Parses `text`. Whole numbers that fit 64 bits stay integers; every other +/// number is a double. +/// +/// A repeated object key is rejected unless `allow_duplicate_keys`: two +/// values for one key let a validator and a later reader disagree about what +/// the document says. +pub fn parse(text: &str, allow_duplicate_keys: bool) -> Result { + let mut reader = Reader { text: text.as_bytes(), index: 0, build: true, allow_duplicate_keys }; + reader.document().map(|(_, value)| value) +} + +/// Checks that `text` is one well-formed JSON value without building it, and returns its kind. +pub fn validate(text: &str) -> Result { + let mut reader = Reader { text: text.as_bytes(), index: 0, build: false, allow_duplicate_keys: true }; + reader.document().map(|(kind, _)| kind) +} + +/// JSON string literal for `value`. Slashes are not escaped, like `.withoutEscapingSlashes` in Swift. +pub fn quote(value: &str) -> String { + let mut output = String::with_capacity(value.len() + 2); + output.push('"'); + for character in value.chars() { + match character { + '"' => output.push_str("\\\""), + '\\' => output.push_str("\\\\"), + '\n' => output.push_str("\\n"), + '\r' => output.push_str("\\r"), + '\t' => output.push_str("\\t"), + '\u{8}' => output.push_str("\\b"), + '\u{c}' => output.push_str("\\f"), + // Line and paragraph separators are escaped so the text is also a JavaScript literal. + character if character < ' ' || character == '\u{2028}' || character == '\u{2029}' => output.push_str(&format!("\\u{:04x}", character as u32)), + character => output.push(character), + } + } + output.push('"'); + output +} + +struct Reader<'a> { + text: &'a [u8], + index: usize, + build: bool, + allow_duplicate_keys: bool, +} + +impl Reader<'_> { + fn document(&mut self) -> Result<(Kind, Value), JsonError> { + self.skip_whitespace(); + let kind = match self.peek() { + b'{' => Kind::Object, + b'[' => Kind::Array, + b'"' => Kind::String, + b't' | b'f' => Kind::Boolean, + b'n' => Kind::Null, + _ => Kind::Number, + }; + let result = self.value(0)?; + self.skip_whitespace(); + if self.index != self.text.len() { + return Err(self.fail("Unexpected text after the JSON value")); + } + Ok((kind, result)) + } + + fn value(&mut self, depth: usize) -> Result { + if depth > MAXIMUM_DEPTH { + return Err(self.fail("JSON is nested too deeply")); + } + match self.peek() { + b'{' => self.object(depth), + b'[' => self.array(depth), + b'"' => self.string().map(Value::String), + b't' => self.literal("true", Value::Bool(true)), + b'f' => self.literal("false", Value::Bool(false)), + b'n' => self.literal("null", Value::Null), + _ => self.number(), + } + } + + fn object(&mut self, depth: usize) -> Result { + self.index += 1; + let mut result = Map::new(); + let mut seen: Option> = if self.allow_duplicate_keys { None } else { Some(HashSet::new()) }; + self.skip_whitespace(); + if self.peek() == b'}' { + self.index += 1; + return Ok(Value::Object(result)); + } + loop { + self.skip_whitespace(); + if self.peek() != b'"' { + return Err(self.fail("Expected an object key")); + } + let key = self.string()?; + if let Some(seen) = seen.as_mut() + && !seen.insert(key.clone()) + { + return Err(self.fail("Duplicate object key")); + } + self.skip_whitespace(); + if self.peek() != b':' { + return Err(self.fail("Expected ':' after an object key")); + } + self.index += 1; + self.skip_whitespace(); + let member = self.value(depth + 1)?; + if self.build { + result.insert(key, member); + } + self.skip_whitespace(); + match self.peek() { + b',' => self.index += 1, + b'}' => { + self.index += 1; + return Ok(Value::Object(result)); + } + _ => return Err(self.fail("Expected ',' or '}' in an object")), + } + } + } + + fn array(&mut self, depth: usize) -> Result { + self.index += 1; + let mut result = Vec::new(); + self.skip_whitespace(); + if self.peek() == b']' { + self.index += 1; + return Ok(Value::Array(result)); + } + loop { + self.skip_whitespace(); + let member = self.value(depth + 1)?; + if self.build { + result.push(member); + } + self.skip_whitespace(); + match self.peek() { + b',' => self.index += 1, + b']' => { + self.index += 1; + return Ok(Value::Array(result)); + } + _ => return Err(self.fail("Expected ',' or ']' in an array")), + } + } + } + + fn string(&mut self) -> Result { + self.index += 1; + let mut output: Vec = Vec::new(); + // A high surrogate escape waiting for its low half. + let mut pending: Option = None; + let flush = |pending: &mut Option, output: &mut Vec| { + if pending.take().is_some() { + // A lone surrogate cannot be text; the Windows port keeps the UTF-16 unit, which no reader can use either. + output.extend_from_slice("\u{fffd}".as_bytes()); + } + }; + loop { + let Some(&byte) = self.text.get(self.index) else { return Err(self.fail("Unterminated string")) }; + self.index += 1; + if byte == b'"' { + flush(&mut pending, &mut output); + // The input is a `str` and every escape appends whole characters, so this is UTF-8. + return String::from_utf8(output).map_err(|_| self.fail("Invalid text")); + } + if byte < b' ' { + return Err(self.fail("Control character in a string")); + } + if byte != b'\\' { + flush(&mut pending, &mut output); + output.push(byte); + continue; + } + let Some(&escape) = self.text.get(self.index) else { return Err(self.fail("Unterminated escape")) }; + self.index += 1; + if escape != b'u' { + flush(&mut pending, &mut output); + } + match escape { + b'"' | b'\\' | b'/' => output.push(escape), + b'b' => output.push(8), + b'f' => output.push(12), + b'n' => output.push(b'\n'), + b'r' => output.push(b'\r'), + b't' => output.push(b'\t'), + b'u' => { + let mut code = 0u32; + for _ in 0..4 { + let digit = self.hex_digit()?; + code = code * 16 + digit; + } + match (pending.take(), code) { + (Some(high), 0xDC00..=0xDFFF) => { + let scalar = 0x10000 + ((high - 0xD800) << 10) + (code - 0xDC00); + push_character(&mut output, char::from_u32(scalar).unwrap_or('\u{fffd}')); + } + (previous, _) => { + if previous.is_some() { + output.extend_from_slice("\u{fffd}".as_bytes()); + } + match code { + 0xD800..=0xDBFF => pending = Some(code), + _ => push_character(&mut output, char::from_u32(code).unwrap_or('\u{fffd}')), + } + } + } + } + _ => return Err(self.fail("Invalid escape")), + } + } + } + + /// One digit of a `\u` escape. Besides ASCII this accepts the full-width + /// digits and the full-width Latin letters A to F, which + /// `Character.digit(c, 16)` of the Android implementation accepts too. + fn hex_digit(&mut self) -> Result { + let rest = self.text.get(self.index..).unwrap_or(&[]); + let character = std::str::from_utf8(&rest[..rest.len().min(4)]).or_else(|error| std::str::from_utf8(&rest[..error.valid_up_to()])).ok().and_then(|text| text.chars().next()); + let Some(character) = character else { return Err(self.fail("Truncated unicode escape")) }; + let digit = match character { + '0'..='9' | 'a'..='f' | 'A'..='F' => character.to_digit(16), + '\u{ff10}'..='\u{ff19}' => Some(character as u32 - 0xFF10), + '\u{ff21}'..='\u{ff26}' => Some(character as u32 - 0xFF21 + 10), + '\u{ff41}'..='\u{ff46}' => Some(character as u32 - 0xFF41 + 10), + _ => None, + }; + let digit = digit.ok_or_else(|| self.fail("Invalid unicode escape"))?; + self.index += character.len_utf8(); + Ok(digit) + } + + fn number(&mut self) -> Result { + let start = self.index; + if self.peek() == b'-' { + self.index += 1; + } + match self.peek() { + b'0' => self.index += 1, + b'1'..=b'9' => self.digits()?, + _ => return Err(self.fail("Unexpected character")), + } + let mut integral = true; + if self.peek() == b'.' { + integral = false; + self.index += 1; + self.digits()?; + } + if matches!(self.peek(), b'e' | b'E') { + integral = false; + self.index += 1; + if matches!(self.peek(), b'+' | b'-') { + self.index += 1; + } + self.digits()?; + } + if !self.build { + return Ok(Value::Null); + } + let literal = std::str::from_utf8(&self.text[start..self.index]).map_err(|_| self.fail("Unexpected character"))?; + if integral && let Ok(whole) = literal.parse::() { + return Ok(Value::Number(Number::from(whole))); + } + // A number too large for a double has no value a manifest could mean. + literal.parse::().ok().and_then(Number::from_f64).map(Value::Number).ok_or_else(|| self.fail("Number is out of range")) + } + + fn digits(&mut self) -> Result<(), JsonError> { + let start = self.index; + while self.peek().is_ascii_digit() { + self.index += 1; + } + if self.index == start { Err(self.fail("Expected a digit")) } else { Ok(()) } + } + + fn literal(&mut self, word: &str, value: Value) -> Result { + if !self.text[self.index..].starts_with(word.as_bytes()) { + return Err(self.fail("Unexpected character")); + } + self.index += word.len(); + Ok(value) + } + + fn skip_whitespace(&mut self) { + while matches!(self.text.get(self.index), Some(b' ' | b'\n' | b'\r' | b'\t')) { + self.index += 1; + } + } + + /// The current byte, or NUL at the end (NUL is never valid outside a string). + fn peek(&self) -> u8 { + self.text.get(self.index).copied().unwrap_or(0) + } + + fn fail(&self, message: &str) -> JsonError { + JsonError(format!("{message} at offset {}", self.index)) + } +} + +fn push_character(output: &mut Vec, character: char) { + output.extend_from_slice(character.encode_utf8(&mut [0; 4]).as_bytes()); +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn accepts_only_the_standard_grammar() { + let parsed = parse(" {\"a\": [1, 2.5, true, null, \"x\\n\\u00e9\"]} ", false).unwrap(); + assert_eq!(parsed, json!({ "a": [1, 2.5, true, null, "x\né"] })); + assert!(parsed["a"][0].is_i64() && parsed["a"][1].is_f64()); + let deep = format!("{}{}", "[".repeat(600), "]".repeat(600)); + for text in ["01", "1.", ".5", "+1", "[1,]", "{\"a\":1,}", "\"a\tb\"", "nul", "NaN", "\"\\x41\"", deep.as_str(), "", "{", "1 2", "{'a':1}", "{a:1}", "[1 // c\n]", "\"\\u12\"", "\"\\u12G4\"", "1e999"] { + assert!(parse(text, false).is_err(), "{text}"); + } + assert!(parse(&format!("{}{}", "[".repeat(512), "]".repeat(512)), false).is_ok()); + assert_eq!(quote("a\"b\\c/d\n\u{1}"), "\"a\\\"b\\\\c/d\\n\\u0001\""); + assert_eq!(quote("\u{2028}\u{2029}"), "\"\\u2028\\u2029\""); + } + + #[test] + fn duplicate_keys_are_an_error_unless_allowed() { + assert!(parse("{\"a\":1,\"a\":2}", false).is_err()); + assert_eq!(parse("{\"a\":1,\"a\":2}", true).unwrap(), json!({ "a": 2 })); + assert!(parse("{\"a\":{\"b\":1},\"c\":{\"b\":2}}", false).is_ok()); + assert_eq!(validate("{\"a\":1,\"a\":2}"), Ok(Kind::Object)); + } + + #[test] + fn numbers_keep_their_kind_and_escapes_become_text() { + assert_eq!(parse("9223372036854775807", false).unwrap(), json!(9223372036854775807i64)); + assert!(parse("9223372036854775808", false).unwrap().is_f64()); + assert!(parse("1.0", false).unwrap().is_f64()); + assert_eq!(parse("-0", false).unwrap(), json!(0)); + assert_eq!(parse("\"\\ud83c\\udf27 \\uFF14\\uff11\"", false).unwrap(), json!("🌧 \u{ff14}\u{ff11}")); + // Full-width digits are digits of an escape, as on Android and Windows. + assert_eq!(parse("\"\\u0041\"", false).unwrap(), json!("A")); + assert_eq!(parse("\"\\ud83c x \\udf27\"", false).unwrap(), json!("\u{fffd} x \u{fffd}")); + } + + #[test] + fn validation_reports_the_kind_without_building() { + for (text, kind) in [("{}", Kind::Object), (" [1] ", Kind::Array), ("\"x\"", Kind::String), ("-1.5e3", Kind::Number), ("true", Kind::Boolean), ("false", Kind::Boolean), ("null", Kind::Null), ("1e999", Kind::Number)] { + assert_eq!(validate(text), Ok(kind), "{text}"); + } + for text in ["", "{} x", "", "{\"a\":}", "tru"] { + assert!(validate(text).is_err(), "{text}"); + } + } +} diff --git a/Linux/crates/sw-core/src/resources.rs b/Linux/crates/sw-core/src/resources.rs new file mode 100644 index 00000000..36241f67 --- /dev/null +++ b/Linux/crates/sw-core/src/resources.rs @@ -0,0 +1,157 @@ +//! Where the read-only files that ship with the programs are: the generated +//! JavaScript (`runtime/`, what `Linux/scripts/build-shared-js.mjs` writes) +//! and the bundled templates (`templates/template`, `templates/component`, +//! `templates/api`). Windows keeps both next to the program; a Linux package +//! installs them under a data directory: +//! +//! ```text +//! /usr/bin/scriptwidget +//! /usr/share/scriptwidget/runtime/kernel.js … +//! /usr/share/scriptwidget/templates/template//main.jsx … +//! ``` +//! +//! `Linux/scripts/stage-resources.sh` assembles that directory. The first +//! of these that holds `runtime/kernel.js` is used: +//! +//! 1. `SCRIPTWIDGET_RESOURCES`, when set. Nothing else is tried then. +//! 2. `../share/scriptwidget` from the directory of the program, so that an +//! unpacked release and any installation prefix work without settings. +//! 3. `scriptwidget` in each directory of `XDG_DATA_DIRS` +//! (`/usr/local/share` and `/usr/share` when it is unset). +//! +//! For development and tests each half can be named on its own, which takes +//! precedence over the above: `SCRIPTWIDGET_RUNTIME_ASSETS` (in a checkout, +//! `Linux/assets/scriptwidget`) and `SCRIPTWIDGET_TEMPLATES` (in a checkout, +//! `Shared/ScriptWidgetRuntime/Resource/Script.bundle`). Nothing is compiled +//! into the program, so it never needs the repository. +//! +//! These directories are trusted in the sense that the user or the package +//! manager chose them; the templates are still read as packages, through the +//! same checks as any other. + +use std::ffi::OsString; +use std::io; +use std::path::{Path, PathBuf}; + +pub const ROOT_VARIABLE: &str = "SCRIPTWIDGET_RESOURCES"; +pub const RUNTIME_VARIABLE: &str = "SCRIPTWIDGET_RUNTIME_ASSETS"; +pub const TEMPLATES_VARIABLE: &str = "SCRIPTWIDGET_TEMPLATES"; + +const APPLICATION: &str = "scriptwidget"; +const RUNTIME: &str = "runtime"; +const TEMPLATES: &str = "templates"; +const MARKER: &str = "kernel.js"; + +/// The resource directories of this process. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Resources { + /// The folder with `kernel.js` and the other generated scripts. + pub runtime: PathBuf, + /// The folder with the template groups; `None` when no location has one. + pub templates: Option, +} + +impl Resources { + /// The directories for this process, from its environment and the place of its executable. + pub fn locate() -> io::Result { + let executable = std::env::current_exe().ok().map(|path| std::fs::canonicalize(&path).unwrap_or(path)); + Self::locate_with(|name| std::env::var_os(name), executable.as_deref(), &|path| path.is_file(), &|path| path.is_dir()) + } + + /// [`Resources::locate`] with everything it reads passed in. + pub fn locate_with(variable: impl Fn(&str) -> Option, executable: Option<&Path>, is_file: &dyn Fn(&Path) -> bool, is_directory: &dyn Fn(&Path) -> bool) -> io::Result { + let named = |name: &str| variable(name).filter(|value| !value.is_empty()).map(|value| std::path::absolute(Path::new(&value)).unwrap_or_else(|_| PathBuf::from(value))); + let roots = roots(&variable, executable); + let root = roots.iter().find(|root| is_file(&root.join(RUNTIME).join(MARKER))); + let runtime = match (named(RUNTIME_VARIABLE), root) { + (Some(runtime), _) => runtime, + (None, Some(root)) => root.join(RUNTIME), + (None, None) => { + let tried: Vec = roots.iter().map(|root| root.join(RUNTIME).display().to_string()).collect(); + return Err(io::Error::new( + io::ErrorKind::NotFound, + format!("The ScriptWidget runtime files were not found (looked in {}). Install the package, or set {ROOT_VARIABLE} or {RUNTIME_VARIABLE}.", tried.join(", ")), + )); + } + }; + let templates = named(TEMPLATES_VARIABLE).or_else(|| root.map(|root| root.join(TEMPLATES)).filter(|path| is_directory(path))); + Ok(Self { runtime, templates }) + } +} + +/// The resource roots to try, in order. +fn roots(variable: &impl Fn(&str) -> Option, executable: Option<&Path>) -> Vec { + if let Some(root) = variable(ROOT_VARIABLE).filter(|value| !value.is_empty()) { + return vec![std::path::absolute(Path::new(&root)).unwrap_or_else(|_| PathBuf::from(root))]; + } + let mut roots = Vec::new(); + if let Some(prefix) = executable.and_then(Path::parent).and_then(Path::parent) { + roots.push(prefix.join("share").join(APPLICATION)); + } + let data_directories = variable("XDG_DATA_DIRS").filter(|value| !value.is_empty()).unwrap_or_else(|| "/usr/local/share:/usr/share".into()); + // As the XDG specification requires, a relative entry is ignored. + for directory in std::env::split_paths(&data_directories).filter(|path| path.is_absolute()) { + let root = directory.join(APPLICATION); + if !roots.contains(&root) { + roots.push(root); + } + } + roots +} + +#[cfg(test)] +mod tests { + use super::*; + + fn located(variables: &[(&str, &str)], executable: Option<&str>, files: &[&str]) -> io::Result { + Resources::locate_with( + |name| variables.iter().find(|(key, _)| *key == name).map(|(_, value)| OsString::from(value)), + executable.map(Path::new), + &|path| files.iter().any(|file| Path::new(file) == path), + &|path| files.iter().any(|file| Path::new(file).starts_with(path) && Path::new(file) != path), + ) + } + + #[test] + fn the_installed_layout_is_found_from_the_program() { + let found = located(&[], Some("/opt/sw/bin/scriptwidget"), &["/opt/sw/share/scriptwidget/runtime/kernel.js", "/opt/sw/share/scriptwidget/templates/api/x/main.jsx"]).unwrap(); + assert_eq!(found.runtime, Path::new("/opt/sw/share/scriptwidget/runtime")); + assert_eq!(found.templates.as_deref(), Some(Path::new("/opt/sw/share/scriptwidget/templates"))); + } + + #[test] + fn the_system_data_directories_follow() { + let found = located(&[], Some("/home/u/bin/scriptwidget"), &["/usr/share/scriptwidget/runtime/kernel.js"]).unwrap(); + assert_eq!(found.runtime, Path::new("/usr/share/scriptwidget/runtime")); + assert_eq!(found.templates, None, "a root without templates has none"); + let found = located(&[("XDG_DATA_DIRS", "relative:/nix/share")], None, &["/nix/share/scriptwidget/runtime/kernel.js", "/usr/share/scriptwidget/runtime/kernel.js"]).unwrap(); + assert_eq!(found.runtime, Path::new("/nix/share/scriptwidget/runtime")); + } + + #[test] + fn the_root_variable_replaces_every_other_place() { + let files = ["/usr/share/scriptwidget/runtime/kernel.js", "/dev/res/runtime/kernel.js", "/dev/res/templates/api/x/main.jsx"]; + let found = located(&[("SCRIPTWIDGET_RESOURCES", "/dev/res")], Some("/usr/bin/scriptwidget"), &files).unwrap(); + assert_eq!(found.runtime, Path::new("/dev/res/runtime")); + assert_eq!(found.templates.as_deref(), Some(Path::new("/dev/res/templates"))); + let missing = located(&[("SCRIPTWIDGET_RESOURCES", "/nowhere")], Some("/usr/bin/scriptwidget"), &files).unwrap_err(); + assert_eq!(missing.kind(), io::ErrorKind::NotFound); + assert!(missing.to_string().contains("/nowhere/runtime"), "{missing}"); + } + + #[test] + fn each_half_can_be_named_on_its_own() { + let found = located(&[("SCRIPTWIDGET_RUNTIME_ASSETS", "/repo/Linux/assets/scriptwidget"), ("SCRIPTWIDGET_TEMPLATES", "/repo/Script.bundle")], None, &[]).unwrap(); + assert_eq!(found.runtime, Path::new("/repo/Linux/assets/scriptwidget")); + assert_eq!(found.templates.as_deref(), Some(Path::new("/repo/Script.bundle"))); + let mixed = located(&[("SCRIPTWIDGET_TEMPLATES", "/repo/Script.bundle")], Some("/usr/bin/scriptwidget"), &["/usr/share/scriptwidget/runtime/kernel.js"]).unwrap(); + assert_eq!(mixed.runtime, Path::new("/usr/share/scriptwidget/runtime")); + assert_eq!(mixed.templates.as_deref(), Some(Path::new("/repo/Script.bundle"))); + } + + #[test] + fn nothing_found_is_an_error_that_names_the_places() { + let error = located(&[], Some("/usr/bin/scriptwidget"), &[]).unwrap_err(); + assert!(error.to_string().contains("/usr/share/scriptwidget/runtime") && error.to_string().contains("SCRIPTWIDGET_RESOURCES"), "{error}"); + } +} diff --git a/Linux/crates/sw-core/src/runtime.rs b/Linux/crates/sw-core/src/runtime.rs new file mode 100644 index 00000000..6665aa0b --- /dev/null +++ b/Linux/crates/sw-core/src/runtime.rs @@ -0,0 +1,412 @@ +//! The facade that compiles and executes one package with the native APIs of +//! this host. Ported from `Windows/src/ScriptWidget.Core/WidgetRuntime.cs`. +//! +//! The Windows class also paints. Here the painter (`sw-render`) depends on +//! this crate, not the other way round, so that `sw-core` builds without +//! Skia: `sw_render::widget::render_run` takes the result of [`WidgetRuntime::run`] +//! and does the last step. Everything that decides what a package may do is +//! here: the modules, the environment, the network policy for remote images. + +use std::collections::HashMap; +use std::io; +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use std::time::{Duration, Instant, SystemTime}; + +use serde_json::Value; +use sha2::{Digest, Sha256}; + +use crate::api::fetch::{self, FetchModule, Url}; +use crate::api::policy::{self, NetworkDeclaration}; +use crate::api::system::{self, Screen}; +use crate::api::weather::FetchWeatherTransport; +use crate::api::{StorageModule, WeatherModule}; +use crate::compiler::{PackageCompiler, PrepareOptions, PreparedPackage, RuntimeError, is_text_file}; +use crate::data_paths::DataPaths; +use crate::executor::{ExecutionError, ExecutionRequest, ExecutionResult, ExecutionSession, NativeModule, ScriptExecutor}; +use crate::json::{ObjectExt, parse_object, quote, stringify}; +use crate::package_files::PackageFiles; +use crate::plugins::{DataSourceModule, PluginStore}; +use crate::shared_scripts::SharedScripts; +use crate::timeline::TimelinePlanner; + +/// Everything needed to run one package once. +#[derive(Clone)] +pub struct RunRequest { + pub files: Arc, + /// Stable identity of the installed package; namespaces storage and caches. + pub package_id: String, + /// "small", "medium", "large" or "extraLarge". + pub family: String, + /// `$getenv("widget-param")`. + pub parameter: String, + pub dark_mode: bool, + /// Declared action to run (`actions[].id`), or `None` to render. + pub action_id: Option, + /// Legacy `onClick="name"` function to call after the entry. + pub call_function: Option, + /// Further `$getenv` values; they win over the ones the runtime sets. + pub extra_environments: Vec<(String, String)>, + /// Directory for the compiled-artifact cache; `None` disables caching. + pub build_directory: Option, + /// The display `$device` reports. + pub screen: Screen, +} + +impl RunRequest { + pub fn new(files: Arc, package_id: impl Into) -> Self { + Self { + files, + package_id: package_id.into(), + family: "medium".into(), + parameter: String::new(), + dark_mode: false, + action_id: None, + call_function: None, + extra_environments: Vec::new(), + build_directory: None, + screen: Screen::default(), + } + } +} + +/// What a finished execution may still fetch for its picture: the `` sources, under the package's own network declaration. +#[derive(Debug, Clone)] +pub struct RemoteImageScope { + package_id: String, + declaration: NetworkDeclaration, + session: Arc, + /// False for a future timeline entry and a Live Activity, which make no live requests. + live: bool, +} + +pub struct RunResult { + pub execution: ExecutionResult, + /// `None` when the package did not compile. + pub prepared: Option, + /// When the package's daily schedule next changes slot, in epoch milliseconds; the host runs the package again then. + pub next_timeline_transition: Option, + /// True when the script's output may depend on the light or dark theme it ran under. + pub depends_on_appearance: bool, + /// `None` when nothing was executed. + pub remote_images: Option, +} + +const MAXIMUM_CACHED_ARTIFACTS: usize = 6; +const MAXIMUM_REMOTE_IMAGES: usize = 8; +const IMAGE_BUDGET: Duration = Duration::from_millis(6000); +const IMAGE_CACHE: Duration = Duration::from_secs(15 * 60); +const IMAGE_TIMEOUT_MILLISECONDS: u64 = 4000; +const MAXIMUM_ERROR_CHARACTERS: usize = 400; + +/// The Linux runtime without the painter. One instance per process; every +/// method may be called from any thread. +pub struct WidgetRuntime { + scripts: Arc, + compiler: PackageCompiler, + executor: ScriptExecutor, + timeline: TimelinePlanner, + storage: Arc, + fetch: Arc, + plugins: Arc, + modules: Vec>, + image_cache: PathBuf, +} + +impl WidgetRuntime { + /// `scripts` are the generated JavaScript assets (see [`crate::resources`]); + /// `paths` is where storage, plugins and caches of this instance live. + pub fn new(scripts: Arc, paths: &DataPaths) -> io::Result { + Self::with_modules(scripts, paths, Vec::new()) + } + + /// [`WidgetRuntime::new`] with further native modules after the built-in ones. + pub fn with_modules(scripts: Arc, paths: &DataPaths, extra_modules: Vec>) -> io::Result { + let storage = Arc::new(StorageModule::new(paths.storage())); + let fetch = Arc::new(FetchModule::new()); + let plugins = Arc::new(PluginStore::new(paths.plugins())); + let weather = WeatherModule::new(Arc::new(FetchWeatherTransport::new(fetch.clone())), paths.cache().join("weather"), WeatherModule::user_agent(env!("CARGO_PKG_VERSION"))); + let mut modules: Vec> = vec![storage.clone(), fetch.clone(), Arc::new(DataSourceModule::new(plugins.clone())), Arc::new(weather)]; + modules.extend(extra_modules); + Ok(Self { + compiler: PackageCompiler::new(scripts.clone())?, + executor: ScriptExecutor::new(scripts.clone()), + timeline: TimelinePlanner::new(scripts.clone()), + scripts, + storage, + fetch, + plugins, + modules, + image_cache: paths.cache().join("remote-images"), + }) + } + + pub fn scripts(&self) -> &Arc { + &self.scripts + } + + pub fn compiler(&self) -> &PackageCompiler { + &self.compiler + } + + pub fn timeline(&self) -> &TimelinePlanner { + &self.timeline + } + + pub fn storage(&self) -> &StorageModule { + &self.storage + } + + pub fn plugins(&self) -> &PluginStore { + &self.plugins + } + + /// Compiles a package, reusing the cached artifact when no source changed. + pub fn prepare(&self, request: &RunRequest) -> Result { + let cache_file = request.build_directory.as_ref().map(|directory| directory.join(format!("prepared-{}.json", self.cache_key(request)))); + if let Some(cached) = cache_file.as_deref().and_then(read_artifact) { + return Ok(cached); + } + let options = PrepareOptions { action_id: request.action_id.as_deref(), call_function: request.call_function.as_deref(), ..Default::default() }; + let prepared = self.compiler.prepare(request.files.as_ref(), &options)?; + if let (Some(file), Some(directory)) = (&cache_file, &request.build_directory) { + // The cache is an optimization: a failure to write it changes nothing. + let _ = write_artifact(directory, file, &prepared); + } + Ok(prepared) + } + + fn cache_key(&self, request: &RunRequest) -> String { + let mut digest = Sha256::new(); + digest.update(self.scripts.contract().opt_string("compilerFingerprint").as_bytes()); + let mut add = |text: &str| { + digest.update(text.as_bytes()); + digest.update([0u8]); + }; + add(request.action_id.as_deref().unwrap_or("")); + add(request.call_function.as_deref().unwrap_or("")); + for path in request.files.paths() { + add(&path); + add(&if is_text_file(&path) { request.files.read_text(&path).unwrap_or_default() } else { String::new() }); + } + hex(&digest.finalize())[..32].to_owned() + } + + /// Compiles the package the way an installation would; the error says why it is invalid. + pub fn validate_package(&self, files: &dyn PackageFiles) -> Result<(), RuntimeError> { + self.compiler.prepare(files, &PrepareOptions::default()).map(|_| ()) + } + + /// Compiles and executes. A compile failure is returned as an execution error. + pub fn run(&self, request: &RunRequest) -> RunResult { + let prepared = match self.prepare(request) { + Ok(prepared) => prepared, + Err(error) => { + let execution = ExecutionResult { error: Some(ExecutionError { kind: error.kind, message: error.message }), ..Default::default() }; + return RunResult { execution, prepared: None, next_timeline_transition: None, depends_on_appearance: false, remote_images: None }; + } + }; + let mut environments = vec![ + ("widget-size".to_owned(), request.family.clone()), + ("widget-param".to_owned(), request.parameter.clone()), + // Linux draws every widget in full color, as Windows does. + ("widget-rendering-mode".to_owned(), "fullColor".to_owned()), + ]; + // The cached artifact carries the slot of the moment it was compiled; use the slot of now. + let schedule = self.timeline.state(prepared.manifest.as_ref()); + if let Some(schedule) = &schedule { + environments.extend(schedule.environments.iter().cloned()); + } + environments.extend(request.extra_environments.iter().cloned()); + + let session = Arc::new(ExecutionSession::default()); + let mut execution = ExecutionRequest::new(&prepared, request.files.as_ref()); + execution.environments = environments; + execution.dark_mode = request.dark_mode; + execution.device = Some(system::device_info(request.screen)); + execution.system = Some(system::system_info()); + execution.modules = self.modules.clone(); + execution.package_id = request.package_id.clone(); + execution.session = session.clone(); + let scope = RemoteImageScope { + package_id: request.package_id.clone(), + declaration: NetworkDeclaration::of(&execution), + live: !policy::is_timeline_projection(&execution) && !policy::is_live_activity(&execution), + session: session.clone(), + }; + let result = self.executor.execute(&execution); + RunResult { + execution: result, + next_timeline_transition: schedule.and_then(|schedule| schedule.next_transition_milliseconds), + depends_on_appearance: session.read_appearance(), + remote_images: Some(scope), + prepared: Some(prepared), + } + } + + /// A canonical tree that explains `error` with its category and recovery hint. + pub fn error_tree(&self, error: Option<&ExecutionError>) -> String { + let kind = error.map_or("internalError", |error| error.kind.as_str()); + let categories = self.scripts.error_categories(); + let category = categories.opt_object(kind).or_else(|| categories.opt_object("internalError")); + let title = category.and_then(|category| category.opt_string_or_none("category")).unwrap_or_else(|| "Internal".into()); + let recovery = category.map(|category| category.opt_string("recovery")).unwrap_or_default(); + let message: String = error.map_or("Unknown error", |error| error.message.as_str()).chars().take(MAXIMUM_ERROR_CHARACTERS).collect(); + // Fixed colors: the card keeps its light background in dark mode. + message_tree(&[ + (&format!("{title} error"), r##"{"color":"#b91c1c","font":"caption","fontWeight":"bold"}"##), + (&message, r##"{"color":"#1f2937","font":"caption"}"##), + (&recovery, r##"{"color":"#6b7280","font":"caption2"}"##), + ]) + } + + /// A card with a title and a line of explanation, for widgets that have nothing to run. + pub fn notice_tree(title: &str, detail: &str) -> String { + message_tree(&[(title, r##"{"color":"#1f2937","font":"caption","fontWeight":"bold"}"##), (detail, r##"{"color":"#6b7280","font":"caption2"}"##)]) + } + + /// Downloads the http(s) `` sources a tree references, bounded + /// in count, size and time, under the same network policy as `fetch`: + /// public hosts only, and for a Package 2.0 widget only the hosts it + /// declares. Returns the files by URL; an image that could not be fetched + /// is left out and drawn as a placeholder. + pub fn fetch_remote_images(&self, tree: &str, scope: &RemoteImageScope) -> HashMap { + let mut result = HashMap::new(); + if !scope.live { + return result; + } + // A tree nested deeper than serde_json reads by default gets no remote images. + let Ok(parsed) = serde_json::from_str::(tree) else { return result }; + let mut urls = Vec::new(); + collect_image_urls(&parsed, &mut urls); + if urls.is_empty() || create_private(&self.image_cache).is_err() { + return result; + } + let deadline = Instant::now() + IMAGE_BUDGET; + for text in urls.into_iter().take(MAXIMUM_REMOTE_IMAGES) { + let Ok(url) = Url::parse(&text) else { continue }; + let allowed = |target: &Url| fetch::validation_error(target).is_none() && target.host_str().is_some_and(|host| scope.declaration.allows_host(fetch::unbracket(host))); + if !allowed(&url) { + continue; + } + let file = self.image_cache.join(hex(&Sha256::digest(format!("{}\n{text}", scope.package_id).as_bytes()))); + let fresh = std::fs::metadata(&file).and_then(|metadata| metadata.modified()).is_ok_and(|modified| SystemTime::now().duration_since(modified).is_ok_and(|age| age < IMAGE_CACHE)); + if !fresh + && Instant::now() < deadline + && scope.session.add_network_request() <= fetch::MAXIMUM_REQUESTS + && let Ok(response) = self.fetch.perform("GET", &url, &[], None, IMAGE_TIMEOUT_MILLISECONDS, &allowed, None) + && (200..=299).contains(&response.status) + && !response.body.is_empty() + { + // A failure leaves a stale copy or the placeholder. + let _ = replace_file(&file, &response.body); + } + // A stale copy is better than a placeholder when the network is unavailable. + if file.is_file() { + result.insert(text, file); + } + } + result + } +} + +fn message_tree(lines: &[(&str, &str)]) -> String { + let children: Vec = lines.iter().map(|(text, props)| format!(r#"{{"$element":"text","props":{props},"children":[{}]}}"#, quote(text))).collect(); + format!( + r##"{{"$element":"vstack","props":{{"alignment":"leading","background":"#fef2f2","frame":"max,topLeading","padding":"12","spacing":4}},"children":[{}]}}"##, + children.join(",") + ) +} + +fn collect_image_urls(node: &Value, urls: &mut Vec) { + match node { + Value::Object(item) => { + if item.opt_string("$element") == "image" + && let Some(props) = item.opt_object("props") + { + let direct = props.opt_string("url"); + let url = if direct.is_empty() { props.opt_string("src") } else { direct }; + if (url.starts_with("http://") || url.starts_with("https://")) && !urls.contains(&url) { + urls.push(url); + } + } + if let Some(children) = item.get("children") { + collect_image_urls(children, urls); + } + } + Value::Array(items) => items.iter().for_each(|child| collect_image_urls(child, urls)), + _ => {} + } +} + +fn hex(bytes: &[u8]) -> String { + bytes.iter().map(|byte| format!("{byte:02x}")).collect() +} + +fn create_private(directory: &Path) -> io::Result<()> { + use std::os::unix::fs::DirBuilderExt; + std::fs::DirBuilder::new().recursive(true).mode(0o700).create(directory) +} + +/// Writes `bytes` beside `file` and renames the result over it, so a reader never sees half a file. +fn replace_file(file: &Path, bytes: &[u8]) -> io::Result<()> { + let mut temporary = file.as_os_str().to_owned(); + temporary.push(format!(".{}.tmp", std::process::id())); + let temporary = PathBuf::from(temporary); + std::fs::write(&temporary, bytes).and_then(|()| std::fs::rename(&temporary, file)).inspect_err(|_| { + let _ = std::fs::remove_file(&temporary); + }) +} + +fn read_artifact(file: &Path) -> Option { + PreparedPackage::from_json(&parse_object(&std::fs::read_to_string(file).ok()?).ok()?).ok() +} + +/// Keeps the cache small: one artifact per (source, action) pair, older ones removed. +fn write_artifact(directory: &Path, file: &Path, prepared: &PreparedPackage) -> io::Result<()> { + create_private(directory)?; + let mut old: Vec<(SystemTime, PathBuf)> = std::fs::read_dir(directory)? + .filter_map(|entry| { + let entry = entry.ok()?; + let name = entry.file_name().into_string().ok()?; + (name.starts_with("prepared-") && name.ends_with(".json")).then(|| Some((entry.metadata().ok()?.modified().ok()?, entry.path())))? + }) + .collect(); + old.sort_by_key(|(modified, _)| std::cmp::Reverse(*modified)); + for (_, stale) in old.into_iter().skip(MAXIMUM_CACHED_ARTIFACTS - 1) { + let _ = std::fs::remove_file(stale); + } + replace_file(file, stringify(&prepared.to_json()).as_bytes()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn remote_image_urls_are_collected_once_and_only_http() { + let tree: Value = serde_json::from_str( + r#"{"$element":"vstack","props":null,"children":[ + {"$element":"image","props":{"url":"https://example.com/a.png"},"children":[]}, + {"$element":"hstack","props":null,"children":[ + {"$element":"image","props":{"src":"http://example.com/b.png"},"children":[]}, + {"$element":"image","props":{"url":"https://example.com/a.png"},"children":[]}, + {"$element":"image","props":{"src":"res:local.png"},"children":[]}, + {"$element":"image","props":{"url":"","src":"file:///etc/passwd"},"children":[]}, + {"$element":"text","props":{"url":"https://example.com/not-an-image"},"children":["x"]}]}]}"#, + ) + .unwrap(); + let mut urls = Vec::new(); + collect_image_urls(&tree, &mut urls); + assert_eq!(urls, ["https://example.com/a.png", "http://example.com/b.png"]); + } + + #[test] + fn message_cards_are_canonical_trees() { + let tree: Value = serde_json::from_str(&WidgetRuntime::notice_tree("No \"widget\"", "line\ntwo")).unwrap(); + assert_eq!(tree["$element"], "vstack"); + assert_eq!(tree["children"][0]["children"][0], "No \"widget\""); + assert_eq!(tree["children"][1]["children"][0], "line\ntwo"); + assert_eq!(tree["children"][1]["props"]["font"], "caption2"); + } +} diff --git a/Linux/crates/sw-core/src/shared_scripts.rs b/Linux/crates/sw-core/src/shared_scripts.rs new file mode 100644 index 00000000..68a21ac2 --- /dev/null +++ b/Linux/crates/sw-core/src/shared_scripts.rs @@ -0,0 +1,83 @@ +//! The JavaScript assets generated by `Linux/scripts/build-shared-js.mjs`. + +use std::collections::HashMap; +use std::io; +use std::path::PathBuf; +use std::sync::{Arc, Mutex}; + +use crate::json::{Object, parse_object}; + +/// The kernel, the compiler bundle, the host files and the runtime contract. +/// Files are read once and kept. +pub struct SharedScripts { + directory: PathBuf, + cache: Mutex>>, + host_files: Vec, + contract: Object, + error_categories: Object, +} + +impl SharedScripts { + /// `directory` is the folder with kernel.js and the other assets. + pub fn new(directory: impl Into) -> io::Result { + let directory = directory.into(); + if !directory.join("kernel.js").is_file() { + return Err(io::Error::new( + io::ErrorKind::NotFound, + format!("ScriptWidget runtime assets are missing in {}. In a checkout, run node Linux/scripts/build-shared-js.mjs.", directory.display()), + )); + } + // host.js defines the registry the API files add themselves to. + let mut api_files: Vec = std::fs::read_dir(&directory)? + .filter_map(|entry| entry.ok()?.file_name().into_string().ok()) + .filter(|name| name.starts_with("apis-") && name.ends_with(".js")) + .collect(); + api_files.sort_unstable(); + let mut host_files = vec!["host.js".to_owned()]; + host_files.append(&mut api_files); + + let invalid = |message: String| io::Error::new(io::ErrorKind::InvalidData, format!("contract.json: {message}")); + let contract_file = parse_object(&std::fs::read_to_string(directory.join("contract.json"))?).map_err(invalid)?; + let member = |name: &str| contract_file.get(name).and_then(|value| value.as_object()).cloned().ok_or_else(|| invalid(format!("{name} is missing"))); + Ok(Self { contract: member("contract")?, error_categories: member("errorCategories")?, directory, cache: Mutex::new(HashMap::new()), host_files }) + } + + /// The assets of this installation: see [`crate::resources`] for where they are looked for. + pub fn default_directory() -> io::Result { + Ok(crate::resources::Resources::locate()?.runtime) + } + + /// Text of one asset. `name` is a file name chosen by the host, never by a package. + pub fn text(&self, name: &str) -> io::Result> { + let mut cache = self.cache.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + if let Some(text) = cache.get(name) { + return Ok(text.clone()); + } + let text: Arc = std::fs::read_to_string(self.directory.join(name)) + .map_err(|error| io::Error::new(error.kind(), format!("{name}: {error}")))? + .into(); + cache.insert(name.to_owned(), text.clone()); + Ok(text) + } + + /// Host files evaluated before the kernel, in order. + pub fn host_files(&self) -> &[String] { + &self.host_files + } + + pub fn contract(&self) -> &Object { + &self.contract + } + + pub fn error_categories(&self) -> &Object { + &self.error_categories + } + + /// The scripts a package may load with `$import`-style support calls; any other name is refused. + pub fn support_script(&self, name: &str) -> Option> { + match name { + "util.js" | "moment.min.js" => self.text(name).ok(), + _ => None, + } + } +} diff --git a/Linux/crates/sw-core/src/timeline.rs b/Linux/crates/sw-core/src/timeline.rs new file mode 100644 index 00000000..f5d6856a --- /dev/null +++ b/Linux/crates/sw-core/src/timeline.rs @@ -0,0 +1,110 @@ +//! Evaluates a package's `timeline.dailyTimes` schedule with the planner +//! shared with iOS and the web preview (`Web/preview-engine/src/timeline.js`), +//! so slots, time zones and daylight-saving rules agree on every platform. +//! Ported from `Windows/src/ScriptWidget.Core/Runtime/TimelinePlanner.cs`. +//! +//! iOS hands WidgetKit pre-rendered future entries. Linux, like Android and +//! Windows, wakes at the next transition and runs the script again as the +//! current entry. + +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; +use std::time::{SystemTime, UNIX_EPOCH}; + +use serde_json::Value; +use sw_quickjs::{DEFAULT_STACK_LIMIT, Engine, JS_THREAD_STACK, JsError, run_on_js_thread}; + +use crate::api::support::iso_dates; +use crate::intl; +use crate::json::{Object, ObjectExt, as_text, parse_object, stringify_object}; +use crate::shared_scripts::SharedScripts; + +/// The current timeline entry of a package and when the next one starts. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TimelineState { + /// In the order the planner produced them. + pub environments: Vec<(String, String)>, + /// Epoch milliseconds. + pub next_transition_milliseconds: Option, +} + +struct Cached { + state: TimelineState, + from: i64, + until: i64, +} + +const PLANNER_MEMORY: usize = 32 * 1024 * 1024; +const PLANNER_DEADLINE_MILLISECONDS: u64 = 6000; +const MAXIMUM_CACHED_PLANS: usize = 32; +const HOUR_MILLISECONDS: i64 = 3_600_000; + +pub struct TimelinePlanner { + scripts: Arc, + cache: Mutex>, +} + +impl TimelinePlanner { + pub fn new(scripts: Arc) -> Self { + Self { scripts, cache: Mutex::new(HashMap::new()) } + } + + /// The entry that applies now. `None` when the manifest declares no schedule or the schedule is invalid. + pub fn state(&self, manifest: Option<&Object>) -> Option { + let now = SystemTime::now().duration_since(UNIX_EPOCH).map_or(0, |elapsed| elapsed.as_millis() as i64); + self.state_at(manifest, now) + } + + /// The entry that applies at `now_milliseconds`. + pub fn state_at(&self, manifest: Option<&Object>, now_milliseconds: i64) -> Option { + let schedule = manifest?.opt_object("timeline")?; + // A plan stays correct until its next transition, so widgets that refresh often reuse it. + let key = format!("{}|{}", stringify_object(schedule), intl::default_time_zone()); + { + let cache = self.cache.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + if let Some(hit) = cache.get(&key).filter(|hit| now_milliseconds >= hit.from && now_milliseconds < hit.until) { + return Some(hit.state.clone()); + } + } + let state = self.evaluate(schedule, now_milliseconds)?; + let until = state.next_transition_milliseconds.unwrap_or(i64::MAX).min(now_milliseconds.saturating_add(HOUR_MILLISECONDS)); + let mut cache = self.cache.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + if cache.len() >= MAXIMUM_CACHED_PLANS { + cache.clear(); + } + cache.insert(key, Cached { state: state.clone(), from: now_milliseconds, until }); + Some(state) + } + + fn evaluate(&self, schedule: &Object, now_milliseconds: i64) -> Option { + // A failure is not reported in detail: the schedule is package data, and the manifest + // validator has already told the author what is wrong with it. + let plan = self.plan(schedule, now_milliseconds).ok().flatten().and_then(|text| parse_object(&text).ok())?; + let entries = plan.opt_array("entries").filter(|entries| !entries.is_empty())?; + let current = entries[0].as_object()?.opt_object("environments")?; + let environments = current.iter().map(|(name, value)| (name.clone(), as_text(value).unwrap_or_default())).collect(); + let next = entries.get(1).and_then(Value::as_object).map(|entry| entry.opt_string("date")).filter(|date| !date.is_empty()).and_then(|date| iso_dates::parse(&date)); + Some(TimelineState { environments, next_transition_milliseconds: next }) + } + + /// Runs the shared planner in a small context: it needs Intl and the prepare bundle, not Babel. + fn plan(&self, schedule: &Object, now_milliseconds: i64) -> Result, JsError> { + run_on_js_thread("scriptwidget-timeline", JS_THREAD_STACK, || { + let asset = |name: &str| self.scripts.text(name).map_err(|error| JsError { message: format!("ScriptWidget runtime asset could not be read: {error}"), interrupted: false }); + let mut engine = Engine::with_limits(|operation: &str, payload: Option<&str>| intl::call(operation, payload), PLANNER_MEMORY, DEFAULT_STACK_LIMIT)?; + engine.set_deadline(PLANNER_DEADLINE_MILLISECONDS); + engine.evaluate(&asset("host.js")?, "host.js")?; + engine.evaluate( + "globalThis.TextEncoder = class { encode(text) { return new Uint8Array(unescape(encodeURIComponent(String(text))).split('').map(function (c) { return c.charCodeAt(0); })); } };", + "prelude.js", + )?; + engine.evaluate(&asset("prepare.js")?, "prepare.js")?; + engine.evaluate( + "globalThis.__swPlan = function (schedule, now, zone) { var time = Number(now);\ + return JSON.stringify(__swPrepareBundle.buildTimelinePlan(JSON.parse(schedule), new Date(time), new Date(time + 3600000), zone)); };", + "timeline-driver.js", + )?; + engine.call("__swPlan", &[Some(&stringify_object(schedule)), Some(&now_milliseconds.to_string()), Some(&intl::default_time_zone())]) + }) + } +} diff --git a/Linux/crates/sw-core/src/xml.rs b/Linux/crates/sw-core/src/xml.rs new file mode 100644 index 00000000..4628274c --- /dev/null +++ b/Linux/crates/sw-core/src/xml.rs @@ -0,0 +1,361 @@ +//! Bounded XML parser behind `$xml.parse`, with the limits and output shape +//! of ScriptWidgetXMLParser: `{name, attributes, text, children}`. The Linux +//! counterpart of `Windows/src/ScriptWidget.Core/Runtime/XmlBridge.cs`. +//! +//! The document is untrusted. Document type and entity declarations are +//! rejected, so there are no entities to expand and nothing external to +//! resolve: only the five predefined entities and character references are +//! understood. Comments and processing instructions are skipped. +//! +//! The limit messages are the ones of the other platforms. The messages for +//! malformed documents are this parser's own: Windows reports the wording of +//! the .NET reader there, which is not part of the contract. + +use serde_json::{Value, json}; + +use crate::json::{Object, stringify}; + +pub const MAXIMUM_BYTES: usize = 256 * 1024; +pub const MAXIMUM_ELEMENTS: usize = 2048; +pub const MAXIMUM_DEPTH: usize = 32; + +/// JSON text: `{"ok":true,"value":…}` or `{"ok":false,"error":"…"}`. +pub fn parse(source: &str) -> String { + stringify(&match parse_document(source) { + Ok(value) => json!({ "ok": true, "value": value }), + Err(error) => json!({ "ok": false, "error": if error.is_empty() { "Invalid XML".to_owned() } else { error } }), + }) +} + +struct Node { + name: String, + attributes: Object, + text: String, + children: Vec, +} + +impl Node { + fn into_json(self) -> Value { + json!({ "name": self.name, "attributes": self.attributes, "text": self.text, "children": self.children }) + } +} + +struct Parser<'a> { + rest: &'a str, +} + +fn name_start(character: char) -> bool { + character.is_ascii_alphabetic() || character == '_' || character == ':' || character as u32 >= 0x80 +} + +fn name_part(character: char) -> bool { + name_start(character) || character.is_ascii_digit() || character == '-' || character == '.' +} + +/// Characters XML 1.0 allows in a document. +fn allowed(character: char) -> bool { + matches!(character, '\t' | '\n' | '\r' | '\u{20}'..='\u{d7ff}' | '\u{e000}'..='\u{fffd}' | '\u{10000}'..='\u{10ffff}') +} + +impl<'a> Parser<'a> { + fn starts_with(&self, prefix: &str) -> bool { + self.rest.starts_with(prefix) + } + + fn advance(&mut self, bytes: usize) { + self.rest = &self.rest[bytes..]; + } + + fn skip_spaces(&mut self) -> bool { + let trimmed = self.rest.trim_start_matches([' ', '\t', '\n', '\r']); + let skipped = trimmed.len() != self.rest.len(); + self.rest = trimmed; + skipped + } + + /// Skips to just after `end`; `what` names the construct in the error. + fn skip_past(&mut self, end: &str, what: &str) -> Result<&'a str, String> { + let at = self.rest.find(end).ok_or_else(|| format!("XML {what} is not closed"))?; + let inside = &self.rest[..at]; + self.advance(at + end.len()); + Ok(inside) + } + + fn name(&mut self) -> Result<&'a str, String> { + let length = self.rest.char_indices().find(|(index, character)| if *index == 0 { !name_start(*character) } else { !name_part(*character) }).map_or(self.rest.len(), |(index, _)| index); + if length == 0 { + return Err("XML name is missing".into()); + } + let name = &self.rest[..length]; + self.advance(length); + Ok(name) + } + + /// Appends text up to `stop`, decoding references. Returns the bytes appended. + fn text(&mut self, stop: &[char], attribute: bool, output: &mut String) -> Result { + let start = output.len(); + while let Some(character) = self.rest.chars().next() { + if stop.contains(&character) { + break; + } + if character == '<' { + // Only reachable in an attribute value. + return Err("XML attribute values cannot contain '<'".into()); + } + if character != '&' { + // Line breaks and tabs in an attribute value are spaces (XML 1.0, 3.3.3). + output.push(if attribute && matches!(character, '\t' | '\n') { ' ' } else { character }); + self.advance(character.len_utf8()); + continue; + } + let end = self.rest.find(';').filter(|end| *end <= 12).ok_or("XML reference is not closed")?; + let reference = &self.rest[1..end]; + let decoded = match reference { + "amp" => '&', + "lt" => '<', + "gt" => '>', + "quot" => '"', + "apos" => '\'', + _ => { + let Some(number) = reference.strip_prefix('#') else { return Err("XML entity references are not allowed".into()) }; + let code = match number.strip_prefix('x') { + Some(hexadecimal) if !hexadecimal.is_empty() && hexadecimal.bytes().all(|byte| byte.is_ascii_hexdigit()) => u32::from_str_radix(hexadecimal, 16).ok(), + None if !number.is_empty() && number.bytes().all(|byte| byte.is_ascii_digit()) => number.parse::().ok(), + _ => None, + }; + code.and_then(char::from_u32).filter(|character| allowed(*character)).ok_or("XML character reference is not valid")? + } + }; + output.push(decoded); + self.advance(end + 1); + } + Ok(output.len() - start) + } +} + +/// True when the text holds ` bool { + let bytes = source.as_bytes(); + let mut at = 0; + while let Some(found) = source[at..].find("= 6 && (word[..6].eq_ignore_ascii_case(b"ENTITY") || word.eq_ignore_ascii_case(b"DOCTYPE")) { + return true; + } + at += found + 2; + } + false +} + +fn parse_document(source: &str) -> Result { + if source.len() > MAXIMUM_BYTES { + return Err(format!("XML exceeds the {MAXIMUM_BYTES}-byte limit")); + } + if has_declaration(source) { + return Err("XML document type and entity declarations are not allowed".into()); + } + if !source.chars().all(allowed) { + return Err("XML contains a character that is not allowed".into()); + } + // A line break is one line feed, however the document wrote it (XML 1.0, 2.11). + let normalized = source.replace("\r\n", "\n").replace('\r', "\n"); + let mut parser = Parser { rest: normalized.strip_prefix('\u{feff}').unwrap_or(&normalized) }; + let mut stack: Vec = Vec::new(); + let mut root: Option = None; + let mut elements = 0usize; + let mut text_bytes = 0usize; + let too_much_text = || format!("XML text exceeds the {MAXIMUM_BYTES}-byte limit"); + + if parser.starts_with("", "declaration")?; + } + while !parser.rest.is_empty() { + if parser.starts_with("", "comment")?; + } else if parser.starts_with("", "CDATA section")?; + let Some(current) = stack.last_mut() else { return Err("XML has text outside the root element".into()) }; + text_bytes += text.len(); + if text_bytes > MAXIMUM_BYTES { + return Err(too_much_text()); + } + current.text.push_str(text); + } else if parser.starts_with("", "processing instruction")?; + } else if parser.starts_with("") { + return Err(format!("XML end tag {name} is not closed")); + } + parser.advance(1); + let node = stack.pop().ok_or_else(|| format!("XML end tag {name} has no start tag"))?; + if node.name != name { + return Err(format!("XML end tag {name} does not match start tag {}", node.name)); + } + close(node, &mut stack, &mut root); + } else if parser.starts_with("<") { + parser.advance(1); + elements += 1; + if elements > MAXIMUM_ELEMENTS { + return Err(format!("XML exceeds the {MAXIMUM_ELEMENTS}-element limit")); + } + if stack.len() >= MAXIMUM_DEPTH { + return Err(format!("XML exceeds the {MAXIMUM_DEPTH}-level depth limit")); + } + if stack.is_empty() && root.is_some() { + return Err("XML has more than one root element".into()); + } + let name = parser.name()?; + let mut node = Node { name: name.to_owned(), attributes: Object::new(), text: String::new(), children: Vec::new() }; + let empty = loop { + let spaced = parser.skip_spaces(); + if parser.starts_with("/>") { + parser.advance(2); + break true; + } + if parser.starts_with(">") { + parser.advance(1); + break false; + } + if parser.rest.is_empty() { + return Err(format!("XML start tag {name} is not closed")); + } + if !spaced { + return Err(format!("XML start tag {name} is malformed")); + } + let attribute = parser.name()?; + parser.skip_spaces(); + if !parser.starts_with("=") { + return Err(format!("XML attribute {attribute} has no value")); + } + parser.advance(1); + parser.skip_spaces(); + let quote = match parser.rest.chars().next() { + Some(quote @ ('"' | '\'')) => quote, + _ => return Err(format!("XML attribute {attribute} is not quoted")), + }; + parser.advance(1); + let mut value = String::new(); + parser.text(&[quote], true, &mut value)?; + if !parser.starts_with(quote.encode_utf8(&mut [0; 4])) { + return Err(format!("XML attribute {attribute} is not closed")); + } + parser.advance(1); + if node.attributes.insert(attribute.to_owned(), Value::String(value)).is_some() { + return Err(format!("XML attribute {attribute} is repeated")); + } + }; + if empty { + close(node, &mut stack, &mut root); + } else { + stack.push(node); + } + } else { + let mut text = String::new(); + text_bytes += parser.text(&['<'], false, &mut text)?; + if text_bytes > MAXIMUM_BYTES { + return Err(too_much_text()); + } + match stack.last_mut() { + Some(current) => current.text.push_str(&text), + None if text.trim_matches([' ', '\t', '\n']).is_empty() => {} + None => return Err("XML has text outside the root element".into()), + } + } + } + if let Some(open) = stack.last() { + return Err(format!("XML element {} is not closed", open.name)); + } + root.ok_or_else(|| "XML has no root element".into()) +} + +fn close(node: Node, stack: &mut [Node], root: &mut Option) { + match stack.last_mut() { + Some(parent) => parent.children.push(node.into_json()), + None => *root = Some(node.into_json()), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn value(source: &str) -> Value { + let result: Value = serde_json::from_str(&parse(source)).unwrap(); + assert_eq!(result["ok"], true, "{source}: {result}"); + result["value"].clone() + } + + fn error(source: &str) -> String { + let result: Value = serde_json::from_str(&parse(source)).unwrap(); + assert_eq!(result["ok"], false, "{source}: {result}"); + result["error"].as_str().unwrap().to_owned() + } + + #[test] + fn elements_attributes_text_and_children_keep_their_shape() { + let source = "\n\n\r\n \n News & <![CDATA[<b>more</b>]]> 你好\n \n \n \n\n"; + assert_eq!( + stringify(&value(source)), + r#"{"name":"rss","attributes":{"version":"2.0","xmlns:dc":"http://purl.org/dc/"},"text":"\n \n","children":[{"name":"channel","attributes":{},"text":"\n \n \n \n ","children":[{"name":"title","attributes":{},"text":"News & more 你好","children":[]},{"name":"dc:date","attributes":{"when":"a b c \"d\" <"},"text":"","children":[]}]}]}"# + ); + assert_eq!(stringify(&value("\u{feff}")), r#"{"name":"a","attributes":{},"text":"","children":[]}"#); + assert_eq!(value("xyz")["text"], "xz"); + assert_eq!(value("1 > 0 'A'")["text"], "1 > 0 'A'"); + } + + #[test] + fn declarations_and_entities_are_refused() { + let declaration = "XML document type and entity declarations are not allowed"; + assert_eq!(error("]>&x;"), declaration); + assert_eq!(error(""), declaration); + assert_eq!(error(""), declaration); + assert_eq!(error(""), declaration); + // A billion laughs needs entity declarations; without them a reference is refused. + assert_eq!(error("&lol9;"), "XML entity references are not allowed"); + assert_eq!(error(""), "XML entity references are not allowed"); + assert_eq!(error(""), "XML document type declarations are not allowed"); + assert_eq!(error("�"), "XML character reference is not valid"); + assert_eq!(error("�"), "XML character reference is not valid"); + assert_eq!(error("&"), "XML reference is not closed"); + } + + #[test] + fn the_limits_are_those_of_the_other_platforms() { + assert_eq!(error(&format!("{}", "x".repeat(MAXIMUM_BYTES))), "XML exceeds the 262144-byte limit"); + assert_eq!(value(&format!("{}", "x".repeat(MAXIMUM_BYTES - 7)))["text"].as_str().unwrap().len(), MAXIMUM_BYTES - 7); + assert_eq!(error(&format!("{}", "".repeat(MAXIMUM_ELEMENTS))), "XML exceeds the 2048-element limit"); + assert_eq!(value(&format!("{}", "".repeat(MAXIMUM_ELEMENTS - 1)))["children"].as_array().unwrap().len(), MAXIMUM_ELEMENTS - 1); + let nested = |depth: usize| format!("{}{}", "".repeat(depth), "".repeat(depth)); + assert_eq!(error(&nested(MAXIMUM_DEPTH + 1)), "XML exceeds the 32-level depth limit"); + assert_eq!(value(&nested(MAXIMUM_DEPTH))["name"], "a"); + } + + #[test] + fn malformed_documents_are_errors() { + for source in [ + "", " ", "plain", "", "", "", "", "text", "text", "", "", "", "", "", "", "x", + ] { + assert!(!error(source).is_empty(), "{source:?}"); + } + assert_eq!(error(""), "XML has more than one root element"); + assert_eq!(error(""), "XML has no root element"); + assert_eq!(error(""), "XML end tag a does not match start tag b"); + } +} diff --git a/Linux/crates/sw-core/tests/api_common/mod.rs b/Linux/crates/sw-core/tests/api_common/mod.rs new file mode 100644 index 00000000..da39eddd --- /dev/null +++ b/Linux/crates/sw-core/tests/api_common/mod.rs @@ -0,0 +1,300 @@ +//! Shared by the `api_*` and `plugins_*` tests: the JavaScript runtime, an +//! HTTP exchange that never leaves the process, and helpers to call a module +//! the way the script bridge does. Nothing here reaches the network. +#![allow(dead_code)] + +use std::io::Read; +use std::net::IpAddr; +use std::path::PathBuf; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex, OnceLock}; +use std::time::Duration; + +use serde_json::{Value, json}; +use sw_core::api::fetch::{FetchError, FetchModule, Resolver, Transport, TransportRequest, TransportResponse, Url, Wait}; +use sw_core::compiler::PrepareOptions; +use sw_core::json::Object; +use sw_core::{Completion, ExecutionRequest, ExecutionResult, MemoryPackageFiles, NativeModule, PackageCompiler, PreparedPackage, ScriptExecutor, SharedScripts}; + +pub const BLOCKED: &str = "Local and private network hosts are not allowed"; +pub const PUBLIC: &str = "93.184.216.34"; + +pub fn scripts() -> Arc { + static SCRIPTS: OnceLock> = OnceLock::new(); + SCRIPTS.get_or_init(|| Arc::new(SharedScripts::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets/scriptwidget")).expect("shared scripts"))).clone() +} + +pub fn compiler() -> &'static PackageCompiler { + static COMPILER: OnceLock = OnceLock::new(); + COMPILER.get_or_init(|| PackageCompiler::new(scripts()).expect("compiler thread")) +} + +/// A directory of its own for one test. +pub fn temporary_directory(name: &str) -> PathBuf { + static COUNTER: AtomicU64 = AtomicU64::new(0); + let directory = std::env::temp_dir().join(format!("scriptwidget-test-{name}-{}-{}", std::process::id(), COUNTER.fetch_add(1, Ordering::SeqCst))); + let _ = std::fs::remove_dir_all(&directory); + std::fs::create_dir_all(&directory).expect("temporary directory"); + directory +} + +/// A Package 2.0 manifest as the validator accepts it. +pub fn widget_manifest(permissions: &str, domains: &str) -> String { + format!( + r#"{{"entry":"main.jsx","formatVersion":2,"id":"com.example.test","name":"Test","networkDomains":{domains},"permissions":{permissions},"plugins":[],"runtimeVersion":"1.0","supportedFamilies":["systemSmall"],"version":"1.0.0"}}"# + ) +} + +/// Runs a script and returns the execution result. +pub fn run(source: &str, manifest: Option<&str>, modules: Vec>, environments: &[(&str, &str)], configure: impl FnOnce(&mut ExecutionRequest<'_>)) -> ExecutionResult { + let mut text = vec![("main.jsx".to_owned(), source.to_owned())]; + if let Some(manifest) = manifest { + text.push(("widget.json".to_owned(), manifest.to_owned())); + } + let files = MemoryPackageFiles::new(text, []); + let prepared = compiler().prepare(&files, &PrepareOptions::default()).expect("prepare"); + let mut request = ExecutionRequest::new(&prepared, &files); + request.environments = vec![("widget-size".into(), "small".into())]; + request.environments.extend(environments.iter().map(|(name, value)| (name.to_string(), value.to_string()))); + request.modules = modules; + request.package_id = "pkg-a".into(); + configure(&mut request); + ScriptExecutor::new(scripts()).execute(&request) +} + +/// Runs a script whose result is the text of the rendered ``. +pub fn render(source: &str, manifest: Option<&str>, modules: Vec>, environments: &[(&str, &str)]) -> String { + let result = run(source, manifest, modules, environments, |_| {}); + assert!(result.error.is_none(), "error: {:?}", result.error); + let tree: Value = serde_json::from_str(result.tree.as_deref().expect("tree")).expect("tree JSON"); + tree["children"].as_array().expect("children").iter().map(|child| child.as_str().unwrap_or("").to_owned()).collect() +} + +/// A prepared package without code, for calling a module directly. +pub struct Package { + pub prepared: PreparedPackage, + pub files: MemoryPackageFiles, + pub environments: Vec<(String, String)>, + pub package_id: String, +} + +impl Package { + /// A legacy package: no manifest, so every public host is allowed. + pub fn legacy() -> Self { + Self { prepared: PreparedPackage::default(), files: MemoryPackageFiles::default(), environments: vec![("widget-size".into(), "medium".into())], package_id: String::new() } + } + + pub fn with_manifest(manifest: Value) -> Self { + let mut package = Self::legacy(); + package.prepared.has_manifest = true; + package.prepared.manifest = manifest.as_object().cloned(); + package + } + + /// A Package 2.0 widget with the `network` permission and these domains. + pub fn with_domains(domains: &[&str]) -> Self { + Self::with_manifest(json!({ "formatVersion": 2, "permissions": ["network"], "networkDomains": domains })) + } + + pub fn environments(mut self, environments: &[(&str, &str)]) -> Self { + self.environments = environments.iter().map(|(name, value)| (name.to_string(), value.to_string())).collect(); + self + } + + pub fn request(&self) -> ExecutionRequest<'_> { + let mut request = ExecutionRequest::new(&self.prepared, &self.files); + request.environments = self.environments.clone(); + request.package_id = self.package_id.clone(); + request + } +} + +/// Starts an asynchronous operation and waits for its single answer. +pub fn start(module: &dyn NativeModule, request: &ExecutionRequest<'_>, operation: &str, payload: Option<&Object>) -> (bool, Value) { + let (done, answers) = Completion::detached(request.session.clone()); + module.start(request, operation, payload, done); + let (_, ok, json) = answers.recv_timeout(Duration::from_secs(20)).expect("the operation did not complete"); + assert!(answers.recv_timeout(Duration::from_millis(50)).is_err(), "the operation completed twice"); + (ok, serde_json::from_str(&json.expect("an answer")).expect("answer JSON")) +} + +/// The rejection message of an operation that must fail. +pub fn rejection(module: &dyn NativeModule, request: &ExecutionRequest<'_>, operation: &str, payload: &Value) -> String { + let (ok, value) = start(module, request, operation, payload.as_object()); + assert!(!ok, "expected a rejection, got {value}"); + value.as_str().expect("a message").to_owned() +} + +pub fn get(url: &str) -> Value { + json!({ "method": "GET", "url": url }) +} + +pub fn addresses(texts: &[&str]) -> Vec { + texts.iter().map(|text| text.parse().expect("address")).collect() +} + +/// A resolver that answers every name with the same addresses. +pub fn fixed_resolver(texts: &[&str]) -> Resolver { + let answer = addresses(texts); + Arc::new(move |_| Ok(answer.clone())) +} + +pub fn public_resolver() -> Resolver { + fixed_resolver(&[PUBLIC]) +} + +/// One request as the transport received it. +#[derive(Debug, Clone)] +pub struct Recorded { + pub method: String, + pub url: Url, + pub headers: Vec<(String, String)>, + pub body: Option>, +} + +impl Recorded { + pub fn header(&self, name: &str) -> Option<&str> { + self.headers.iter().find(|(known, _)| known.eq_ignore_ascii_case(name)).map(|(_, value)| value.as_str()) + } + + pub fn path(&self) -> &str { + self.url.path() + } +} + +pub enum FakeBody { + Bytes(Vec), + /// Zeros for as long as it is read, or until `length`; `read` counts what was handed out. + /// A stalled body sends its first bytes and then nothing, like a server that stops answering. + Zeros { length: Option, stall: bool, read: Arc }, +} + +pub struct FakeResponse { + pub status: u16, + pub headers: Vec<(String, String)>, + pub body: FakeBody, +} + +impl FakeResponse { + pub fn text(status: u16, body: &str) -> Self { + Self::typed(status, body, "text/plain; charset=utf-8") + } + + pub fn typed(status: u16, body: &str, content_type: &str) -> Self { + Self { status, headers: vec![("Content-Type".into(), content_type.into())], body: FakeBody::Bytes(body.as_bytes().to_vec()) } + } + + pub fn bytes(status: u16, body: &[u8]) -> Self { + Self { status, headers: Vec::new(), body: FakeBody::Bytes(body.to_vec()) } + } + + pub fn redirect(status: u16, location: Option<&str>) -> Self { + Self { status, headers: location.map(|location| ("Location".to_owned(), location.to_owned())).into_iter().collect(), body: FakeBody::Bytes(Vec::new()) } + } + + pub fn zeros(length: Option, stall: bool, read: &Arc) -> Self { + Self { status: 200, headers: Vec::new(), body: FakeBody::Zeros { length, stall, read: read.clone() } } + } + + pub fn header(mut self, name: &str, value: &str) -> Self { + self.headers.push((name.into(), value.into())); + self + } +} + +struct ZeroReader { + left: Option, + stall: bool, + read: Arc, + wait: Wait, +} + +impl Read for ZeroReader { + fn read(&mut self, buffer: &mut [u8]) -> std::io::Result { + if self.stall && self.read.load(Ordering::SeqCst) > 0 { + return Err(self.wait.expire().into_io()); + } + let take = buffer.len().min(self.left.map_or(usize::MAX, |left| left as usize)); + buffer[..take].fill(0); + self.left = self.left.map(|left| left - take as u64); + self.read.fetch_add(take as u64, Ordering::SeqCst); + Ok(take) + } +} + +type Respond = Box Result + Send + Sync>; + +/// An HTTP exchange that never leaves the process: records each request and answers from a function. +pub struct FakeTransport { + respond: Respond, + requests: Mutex>, +} + +impl FakeTransport { + pub fn new(respond: impl Fn(&Recorded) -> FakeResponse + Send + Sync + 'static) -> Arc { + Self::waiting(move |request, _| Ok(respond(request))) + } + + /// A transport whose answer may depend on the wait, or fail. + pub fn waiting(respond: impl Fn(&Recorded, &Wait) -> Result + Send + Sync + 'static) -> Arc { + Arc::new(Self { respond: Box::new(respond), requests: Mutex::new(Vec::new()) }) + } + + pub fn ok() -> Arc { + Self::new(|_| FakeResponse::text(200, "ok")) + } + + pub fn requests(&self) -> Vec { + self.requests.lock().unwrap().clone() + } + + pub fn count(&self) -> usize { + self.requests.lock().unwrap().len() + } + + pub fn clear(&self) { + self.requests.lock().unwrap().clear(); + } +} + +impl Transport for FakeTransport { + fn send(&self, request: &TransportRequest<'_>, wait: &Wait) -> Result { + let recorded = Recorded { method: request.method.to_owned(), url: request.url.clone(), headers: request.headers.to_vec(), body: request.body.map(<[u8]>::to_vec) }; + self.requests.lock().unwrap().push(recorded.clone()); + let response = (self.respond)(&recorded, wait)?; + let body: Box = match response.body { + FakeBody::Bytes(bytes) => Box::new(std::io::Cursor::new(bytes)), + FakeBody::Zeros { length, stall, read } => Box::new(ZeroReader { left: length, stall, read, wait: wait.clone() }), + }; + Ok(TransportResponse { status: response.status, headers: response.headers, body }) + } +} + +/// A fetch module that answers every request in the process, for a name that resolves to a public address. +pub fn offline(transport: &Arc) -> FetchModule { + FetchModule::with_parts(public_resolver(), transport.clone()) +} + +pub fn url(text: &str) -> Url { + Url::parse(text).expect("URL") +} + +pub fn no_headers() -> Vec<(String, String)> { + Vec::new() +} + +pub fn headers(pairs: &[(&str, &str)]) -> Vec<(String, String)> { + pairs.iter().map(|(name, value)| (name.to_string(), value.to_string())).collect() +} + +pub fn any_redirect(_: &Url) -> bool { + true +} + +/// The message of a request that must fail with one of the module's own sentences. +pub fn failure(result: Result) -> String { + match result { + Err(FetchError::Failed(message)) => message, + other => panic!("expected a refusal, got {other:?}"), + } +} diff --git a/Linux/crates/sw-core/tests/api_fetch.rs b/Linux/crates/sw-core/tests/api_fetch.rs new file mode 100644 index 00000000..4636ac67 --- /dev/null +++ b/Linux/crates/sw-core/tests/api_fetch.rs @@ -0,0 +1,527 @@ +//! `fetch` and `$http`: policy, redirects, budgets and results. Ported from +//! `Windows/tests/ScriptWidget.Core.Tests/Api/FetchModuleTests.cs` and +//! `FetchUserAgentTests.cs`. Every exchange is answered inside the process; +//! the transport over real sockets is tested in `api_http.rs`. + +mod api_common; + +use std::net::IpAddr; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use api_common::*; +use serde_json::{Value, json}; +use sw_core::api::fetch::{self, FetchError, FetchModule, Resolver, Url, system_resolver}; +use sw_core::{Completion, NativeModule}; + +const HTTP: &str = "http.request"; + +fn rendered(source: &str, manifest: Option<&str>, module: FetchModule, environments: &[(&str, &str)]) -> String { + render(source, manifest, vec![Arc::new(module)], environments) +} + +// MARK: - From a script (NativeApiTest on Android) + +#[test] +fn fetch_requires_a_declared_host() { + let script = r#"fetch("https://example.org/").then(v => $render(ok), e => $render({String(e)}))"#; + let transport = FakeTransport::ok(); + assert_eq!(rendered(script, Some(&widget_manifest(r#"["network"]"#, r#"["example.com"]"#)), offline(&transport), &[]), "Network host is not declared in widget.json"); + assert_eq!(rendered(script, Some(&widget_manifest("[]", "[]")), offline(&transport), &[]), "Network host is not declared in widget.json"); + assert_eq!(transport.count(), 0); +} + +#[test] +fn fetch_rejects_private_hosts_by_name_and_by_resolved_address() { + let by_name = r#"fetch("http://192.168.1.1/").then(v => $render(ok), e => $render({String(e)}))"#; + let transport = FakeTransport::ok(); + assert_eq!(rendered(by_name, None, offline(&transport), &[]), BLOCKED); + let rebinding = FetchModule::with_parts(fixed_resolver(&["10.0.0.5"]), transport.clone()); + let by_address = r#"fetch("https://public.example.com/").then(v => $render(ok), e => $render({String(e)}))"#; + assert_eq!(rendered(by_address, None, rebinding, &[]), BLOCKED); + assert_eq!(transport.count(), 0); +} + +#[test] +fn fetch_policy_holds_when_built_ins_are_tampered() { + let script = r#" + RegExp.prototype.exec = function () { return ["https://example.com", "https", "example.com"]; }; + String.prototype.startsWith = function () { return false; }; + Array.prototype.includes = function () { return true; }; + fetch("http://127.0.0.1:8080/").then(v => $render(ok), e => $render({String(e)})) + "#; + let transport = FakeTransport::ok(); + assert_eq!(rendered(script, Some(&widget_manifest(r#"["network"]"#, r#"["example.com"]"#)), offline(&transport), &[]), BLOCKED); + assert_eq!(transport.count(), 0); +} + +#[test] +fn fetch_is_read_only_in_future_timeline_entries() { + let script = r#"fetch("https://example.com/").then(v => $render(ok), e => $render({String(e).slice(0, 20)}))"#; + assert_eq!(rendered(script, None, offline(&FakeTransport::ok()), &[("timeline-is-projection", "true")]), "[timeline_read_only]"); +} + +/// The Android test reads example.com; here the exchange is answered in the process. +#[test] +fn fetch_returns_bodies_and_metadata() { + let script = r#" + $http.getResponse("https://example.com/").then( + r => $render({[r.status, r.body.includes("Example Domain"), typeof r.headers["content-type"]].join("|")}), + e => $render({"error: " + String(e)})) + "#; + let manifest = widget_manifest(r#"["network"]"#, r#"["example.com"]"#); + let transport = FakeTransport::new(|_| FakeResponse::typed(200, "

Example Domain

", "text/html")); + assert_eq!(rendered(script, Some(&manifest), offline(&transport), &[]), "200|true|string"); + let requests = transport.requests(); + assert_eq!(requests.len(), 1); + assert_eq!(requests[0].url.as_str(), "https://example.com/"); + let plain = r#"fetch("https://example.com/a").then(v => $render({v}), e => $render({"error: " + String(e)}))"#; + assert_eq!(rendered(plain, Some(&manifest), offline(&FakeTransport::ok()), &[]), "ok"); + // POST with a JSON body, as a script writes it. + let post = r#"$http.post("https://example.com/items", { body: { a: 1 }, headers: { "X-Token": "t" } }).then(v => $render({v}), e => $render({"error: " + String(e)}))"#; + let echo = FakeTransport::new(|request| FakeResponse::text(200, &format!("{} {} {}", request.method, String::from_utf8_lossy(request.body.as_deref().unwrap_or_default()), request.header("content-type").unwrap_or("-")))); + assert_eq!(rendered(post, Some(&manifest), offline(&echo), &[]), r#"POST {"a":1} application/json"#); +} + +// MARK: - The module without a script + +fn perform(module: &FetchModule, method: &str, address: &str, headers: &[(String, String)], body: Option<&[u8]>) -> Result { + module.perform(method, &url(address), headers, body, 5000, &any_redirect, None) +} + +#[test] +fn handles_only_the_http_operation_and_reports_presentation_and_url_errors() { + let module = offline(&FakeTransport::ok()); + assert!(module.handles("http.request")); + assert!(!module.handles("datasource.request")); + let reject = |package: &Package, job: Value| rejection(&module, &package.request(), HTTP, &job); + assert_eq!( + reject(&Package::legacy().environments(&[("widget-size", "live-activity")]), get("https://example.com/")), + "[live_activity_unavailable] Live Activity presentations cannot request network data; update their state from the app" + ); + assert_eq!( + reject(&Package::legacy().environments(&[("widget-size", "medium"), ("timeline-is-projection", "true")]), get("https://example.com/")), + "[timeline_read_only] Future entries cannot make live network requests; read a saved snapshot" + ); + let legacy = Package::legacy(); + assert_eq!(reject(&legacy, get("ftp://example.com/x")), "Only HTTP and HTTPS URLs are allowed"); + assert_eq!(reject(&legacy, get("file:///etc/passwd")), "Only HTTP and HTTPS URLs are allowed"); + assert_eq!(reject(&legacy, get("example.com/x")), "Only HTTP and HTTPS URLs are allowed"); + assert_eq!(reject(&legacy, json!({})), "Only HTTP and HTTPS URLs are allowed"); + assert_eq!(reject(&legacy, get("https:///status")), "https:///status is not url"); + assert_eq!(reject(&legacy, get("https://user:pw@example.com/")), "URL credentials are not allowed"); + assert_eq!(reject(&legacy, get("https://user@example.com/")), "URL credentials are not allowed"); + assert_eq!(reject(&legacy, get("http://localhost:8080/")), BLOCKED); + assert_eq!(reject(&legacy, get("http://printer.local/")), BLOCKED); + // A package with a manifest needs the network permission and the host in networkDomains. + assert_eq!(reject(&Package::with_domains(&["example.com"]), get("https://example.org/")), "Network host is not declared in widget.json"); + assert_eq!(reject(&Package::with_domains(&["*.example.com"]), get("https://example.com/")), "Network host is not declared in widget.json"); + assert!(start(&module, &Package::with_domains(&["*.example.com"]).request(), HTTP, get("https://api.example.com/").as_object()).0); + let no_permission = Package::with_manifest(json!({ "formatVersion": 2, "permissions": [], "networkDomains": ["example.com"] })); + assert_eq!(reject(&no_permission, get("https://example.com/")), "Network host is not declared in widget.json"); + let no_domains = Package::with_manifest(json!({ "formatVersion": 2, "permissions": ["network"] })); + assert_eq!(reject(&no_domains, get("https://example.com/")), "Network host is not declared in widget.json"); + // A manifest that is present but could not be read declares nothing; it is not a legacy package. + let mut unreadable = Package::legacy(); + unreadable.prepared.has_manifest = true; + assert_eq!(reject(&unreadable, get("https://example.com/")), "Network host is not declared in widget.json"); +} + +#[test] +fn hosts_resolving_to_private_addresses_are_refused_before_anything_is_sent() { + let refused = [ + "127.0.0.1", "127.8.9.10", "10.1.2.3", "169.254.169.254", "::1", "fc00::1", "fd12:3456:789a::1", "::ffff:127.0.0.1", "::ffff:10.0.0.1", "::ffff:169.254.169.254", "192.168.0.10", "172.16.5.4", "172.31.255.255", + "100.64.0.1", "0.0.0.0", "::", "fe80::1", "fec0::1", "ff02::1", "64:ff9b::7f00:1", "2002:7f00:1::1", "224.0.0.1", "255.255.255.255", + ]; + let transport = FakeTransport::new(|_| FakeResponse::text(200, "secret")); + for text in refused { + let module = FetchModule::with_parts(fixed_resolver(&[text]), transport.clone()); + assert_eq!(failure(perform(&module, "GET", "https://public.example.com/", &[], None)), BLOCKED, "{text}"); + // One private address among public ones is enough: the connection could go to it. + let mixed = FetchModule::with_parts(fixed_resolver(&[PUBLIC, text]), transport.clone()); + assert_eq!(failure(perform(&mixed, "GET", "https://public.example.com/", &[], None)), BLOCKED, "{text}"); + assert_eq!(rejection(&module, &Package::legacy().request(), HTTP, &get("https://public.example.com/")), BLOCKED, "{text}"); + } + let none = FetchModule::with_parts(fixed_resolver(&[]), transport.clone()); + assert_eq!(failure(perform(&none, "GET", "https://public.example.com/", &[], None)), BLOCKED); + let unknown: Resolver = Arc::new(|_| Err(std::io::Error::other("no such host"))); + assert_eq!(failure(perform(&FetchModule::with_parts(unknown, transport.clone()), "GET", "https://public.example.com/", &[], None)), "Could not resolve public.example.com"); + assert_eq!(transport.count(), 0); + // Public addresses pass. + for text in ["93.184.216.34", "2606:2800:220:1:248:1893:25c8:1946", "::ffff:93.184.216.34", "172.32.0.1", "100.128.0.1"] { + let module = FetchModule::with_parts(fixed_resolver(&[text]), FakeTransport::ok()); + assert_eq!(perform(&module, "GET", "https://public.example.com/", &[], None).unwrap().status, 200, "{text}"); + } +} + +/// Address literals need no DNS: every spelling of a local address is refused by name or by value. +#[test] +fn address_literals_in_blocked_ranges_are_refused() { + let transport = FakeTransport::new(|_| FakeResponse::text(200, "secret")); + let module = FetchModule::with_parts(Arc::new(system_resolver), transport.clone()); + for address in [ + "http://127.0.0.1/", "http://[::1]/", "http://[::ffff:127.0.0.1]/", "http://[::ffff:7f00:1]/", "http://[fd00::1]/", "http://[fe80::1]/", "http://169.254.169.254/latest/meta-data/", "http://0.0.0.0/", + "http://2130706433/", "http://0x7f.0.0.1/", "http://127.1/", "http://10.0.0.1:8080/", "https://192.168.1.1/", "http://100.64.0.1/", "http://[64:ff9b::a00:1]/", "http://017700000001/", "http://[0:0:0:0:0:0:0:1]/", + "http://127.0.0.1.:80/", "http://LOCALHOST/", + ] { + assert_eq!(rejection(&module, &Package::legacy().request(), HTTP, &get(address)), BLOCKED, "{address}"); + } + assert_eq!(transport.count(), 0); +} + +/// DNS rebinding: the name is public when the request is checked and local +/// when the connection is opened. The production transport resolves again at +/// connect and refuses, so nothing is sent. +#[test] +fn a_dns_answer_that_changes_before_the_connect_is_refused() { + let answers: Arc>> = Arc::default(); + let rebinding = |answers: Arc>>| -> Resolver { + Arc::new(move |host: &str| { + let mut answers = answers.lock().unwrap(); + answers.push(host.to_owned()); + Ok(vec![if answers.len() == 1 { PUBLIC.parse::().unwrap() } else { "127.0.0.1".parse().unwrap() }]) + }) + }; + let module = FetchModule::with_resolver(rebinding(answers.clone())); + assert_eq!(failure(perform(&module, "GET", "http://rebind.example.com:9/", &[], None)), BLOCKED); + assert_eq!(*answers.lock().unwrap(), ["rebind.example.com", "rebind.example.com"]); + let second = FetchModule::with_resolver(rebinding(Arc::default())); + assert_eq!(rejection(&second, &Package::with_domains(&["rebind.example.com"]).request(), HTTP, &get("https://rebind.example.com/")), BLOCKED); +} + +#[test] +fn redirects_to_blocked_or_undeclared_hosts_are_refused() { + let location: Arc> = Arc::default(); + let target = location.clone(); + let transport = FakeTransport::new(move |request| if request.path() == "/start" { FakeResponse::redirect(302, Some(&target.lock().unwrap())) } else { FakeResponse::text(200, "arrived") }); + let resolver: Resolver = Arc::new(|host: &str| match host { + "example.com" | "other.example.org" => Ok(addresses(&[PUBLIC])), + "cdn.example.com" => Ok(addresses(&["10.0.0.1"])), + _ => Err(std::io::Error::other("unknown host")), + }); + let module = FetchModule::with_parts(resolver, transport.clone()); + let follow = |to: &str, domains: Option<&[&str]>| -> (bool, String, usize) { + transport.clear(); + *location.lock().unwrap() = to.to_owned(); + let package = domains.map_or_else(Package::legacy, Package::with_domains); + let (ok, value) = start(&module, &package.request(), HTTP, get("https://example.com/start").as_object()); + (ok, value.as_str().unwrap_or("").to_owned(), transport.count()) + }; + let declared: &[&str] = &["example.com", "cdn.example.com"]; + assert_eq!(follow("https://example.com/next", Some(declared)), (true, "arrived".into(), 2)); + assert_eq!(follow("/next", Some(declared)), (true, "arrived".into(), 2)); + let refused = (false, "Redirect target is not allowed".to_owned(), 1); + // Not in networkDomains. + assert_eq!(follow("https://other.example.org/x", Some(declared)), refused); + // Local by name, by literal address, with credentials, or not HTTP at all. + for to in ["http://localhost/x", "http://127.0.0.1/x", "http://[::1]/x", "http://192.168.1.1/x", "https://user:pw@example.com/x", "ftp://example.com/x", "file:///etc/passwd", "javascript:alert(1)", "http://2130706433/x"] { + assert_eq!(follow(to, Some(declared)), refused, "{to}"); + assert_eq!(follow(to, None), refused, "{to}"); + } + // Declared, but the name resolves to a private address: refused when the hop is resolved. + assert_eq!(follow("https://cdn.example.com/x", Some(declared)), (false, BLOCKED.into(), 1)); +} + +#[test] +fn redirects_follow_the_method_rules_and_stop_at_the_limit() { + let transport = FakeTransport::new(|request| match request.path() { + "/found" => FakeResponse::redirect(302, Some("/after-found")), + "/moved" => FakeResponse::redirect(301, Some("/after-moved")), + "/other" => FakeResponse::redirect(303, Some("/after-other")), + "/temporary" => FakeResponse::redirect(307, Some("/after-temporary")), + "/permanent" => FakeResponse::redirect(308, Some("https://example.com/after-permanent")), + "/loop" => FakeResponse::redirect(302, Some("/loop")), + "/nowhere" => FakeResponse::redirect(302, None), + "/unchanged" => FakeResponse::redirect(304, Some("/never")), + _ => FakeResponse::text(200, "done"), + }); + let module = offline(&transport); + let traced = headers(&[("X-Trace", "1")]); + let trace = |method: &str, path: &str| -> String { + transport.clear(); + let response = perform(&module, method, &format!("https://example.com{path}"), &traced, Some(b"payload")).unwrap(); + let steps: Vec = transport + .requests() + .iter() + .map(|request| format!("{}:{}:{}", request.method, request.path(), request.body.as_deref().filter(|body| !body.is_empty()).map_or("-".into(), |body| String::from_utf8_lossy(body).into_owned()))) + .collect(); + format!("{} {}", response.status, steps.join(" ")) + }; + assert_eq!(trace("post", "/found"), "200 POST:/found:payload GET:/after-found:-"); + assert_eq!(trace("POST", "/moved"), "200 POST:/moved:payload GET:/after-moved:-"); + assert_eq!(trace("PUT", "/found"), "200 PUT:/found:payload PUT:/after-found:payload"); + assert_eq!(trace("PUT", "/other"), "200 PUT:/other:payload GET:/after-other:-"); + assert_eq!(trace("POST", "/temporary"), "200 POST:/temporary:payload POST:/after-temporary:payload"); + assert_eq!(trace("DELETE", "/permanent"), "200 DELETE:/permanent:payload DELETE:/after-permanent:payload"); + assert_eq!(trace("GET", "/found"), "200 GET:/found:- GET:/after-found:-"); + assert_eq!(trace("GET", "/unchanged"), "304 GET:/unchanged:-"); + assert!(transport.requests().iter().all(|request| request.header("X-Trace") == Some("1"))); + + transport.clear(); + assert_eq!(failure(perform(&module, "GET", "https://example.com/loop", &[], None)), "Redirect target is not allowed"); + assert_eq!(transport.count(), fetch::MAXIMUM_REDIRECTS + 1); + assert_eq!(failure(perform(&module, "GET", "https://example.com/nowhere", &[], None)), "Redirect without a location"); + // The caller's rule is asked for every hop, with the resolved target. + let asked: Mutex> = Mutex::default(); + let refuse = |target: &Url| { + asked.lock().unwrap().push(target.as_str().to_owned()); + false + }; + assert_eq!(failure(module.perform("GET", &url("https://example.com/found"), &[], None, 5000, &refuse, None)), "Redirect target is not allowed"); + assert_eq!(*asked.lock().unwrap(), ["https://example.com/after-found"]); + assert_eq!(failure(perform(&module, "brew", "https://example.com/", &[], None)), "Unknown method 'BREW'"); +} + +#[test] +fn over_budget_bodies_are_cut_off() { + let limit = format!("Response exceeds the {}-byte limit", fetch::MAXIMUM_RESPONSE_BYTES); + assert_eq!(limit, "Response exceeds the 2097152-byte limit"); + let maximum = fetch::MAXIMUM_RESPONSE_BYTES as u64; + let module = |length: Option, declared: Option, read: &Arc| { + let read = read.clone(); + offline(&FakeTransport::new(move |_| { + let response = FakeResponse::zeros(length, false, &read); + match declared { + Some(declared) => response.header("Content-Length", &declared.to_string()), + None => response, + } + })) + }; + + // No length at all: the body is counted while it is read and reading stops at the limit. + let endless: Arc = Arc::default(); + assert_eq!(failure(perform(&module(None, None, &endless), "GET", "https://example.com/", &[], None)), limit); + assert!((maximum..=maximum + 256 * 1024).contains(&endless.load(Ordering::SeqCst))); + + // A length that understates the body is not trusted. + let lying: Arc = Arc::default(); + assert_eq!(failure(perform(&module(None, Some(10), &lying), "GET", "https://example.com/", &[], None)), limit); + assert!((maximum..=maximum + 256 * 1024).contains(&lying.load(Ordering::SeqCst))); + + // A declared length above the limit is refused without reading. + let unread: Arc = Arc::default(); + assert_eq!(failure(perform(&module(None, Some(maximum + 1), &unread), "GET", "https://example.com/", &[], None)), limit); + assert_eq!(unread.load(Ordering::SeqCst), 0); + + // Exactly the limit is delivered; the script sees the same message for one byte more. + let exact = module(Some(maximum), None, &Arc::default()); + assert_eq!(perform(&exact, "GET", "https://example.com/", &[], None).unwrap().body.len(), fetch::MAXIMUM_RESPONSE_BYTES); + let over = module(Some(maximum + 1), None, &Arc::default()); + assert_eq!(rejection(&over, &Package::legacy().request(), HTTP, &get("https://example.com/")), limit); +} + +#[test] +fn the_request_budget_and_the_request_body_limit_are_enforced() { + let transport = FakeTransport::ok(); + let module = offline(&transport); + let package = Package::legacy(); + let request = package.request(); + for _ in 0..fetch::MAXIMUM_REQUESTS { + assert!(start(&module, &request, HTTP, get("https://example.com/").as_object()).0); + } + assert_eq!(rejection(&module, &request, HTTP, &get("https://example.com/")), "Network request budget exceeded"); + assert_eq!(transport.count(), fetch::MAXIMUM_REQUESTS as usize); + // Requests that policy refuses before the budget do not count. + let other = package.request(); + rejection(&module, &other, HTTP, &get("http://localhost/")); + assert_eq!(other.session.network_requests(), 0); + + transport.clear(); + let large = json!({ "method": "POST", "url": "https://example.com/", "body": "x".repeat(fetch::MAXIMUM_REQUEST_BODY_BYTES + 1) }); + assert_eq!(rejection(&module, &package.request(), HTTP, &large), "Request body exceeds the 1048576-byte limit"); + assert_eq!(transport.count(), 0); + let fits = json!({ "method": "POST", "url": "https://example.com/", "body": "x".repeat(fetch::MAXIMUM_REQUEST_BODY_BYTES) }); + assert!(start(&module, &package.request(), HTTP, fits.as_object()).0); + assert_eq!(transport.requests()[0].body.as_ref().unwrap().len(), fetch::MAXIMUM_REQUEST_BODY_BYTES); +} + +#[test] +fn requests_carry_the_script_headers_and_body_but_not_host_or_length() { + let transport = FakeTransport::ok(); + let module = offline(&transport); + let package = Package::legacy(); + let job = json!({ + "method": "POST", "url": "https://example.com/items?x=1", "body": "{\"a\":1}", "jsonBody": true, + "headers": { "X-Token": "t", "Host": "evil.example.net", "Content-Length": "999", "Accept": "application/json" }, + }); + assert!(start(&module, &package.request(), HTTP, job.as_object()).0); + let sent = transport.requests().remove(0); + assert_eq!(sent.method, "POST"); + assert_eq!(sent.url.as_str(), "https://example.com/items?x=1"); + assert_eq!(sent.body.as_deref(), Some(&b"{\"a\":1}"[..])); + assert_eq!(sent.header("X-Token"), Some("t")); + assert_eq!(sent.header("Accept"), Some("application/json")); + assert_eq!(sent.header("Content-Type"), Some("application/json")); + for absent in ["Host", "Content-Length", "Cookie", "Authorization"] { + assert_eq!(sent.header(absent), None, "{absent}"); + } + + // The script's own content type wins over the JSON default; a GET sends no body. + transport.clear(); + let job = json!({ "method": "PUT", "url": "https://example.com/", "body": "a=1", "jsonBody": true, "headers": { "content-type": "application/x-www-form-urlencoded" } }); + assert!(start(&module, &package.request(), HTTP, job.as_object()).0); + assert_eq!(transport.requests()[0].header("Content-Type"), Some("application/x-www-form-urlencoded")); + transport.clear(); + assert!(start(&module, &package.request(), HTTP, json!({ "method": "GET", "url": "https://example.com/", "body": "ignored" }).as_object()).0); + assert_eq!(transport.requests()[0].body, None); + + // A header value cannot add a header line, and a header name cannot hold a separator. + transport.clear(); + let job = json!({ "method": "GET", "url": "https://example.com/", "headers": { "X-Token": "a\r\nX-Evil: 1" } }); + assert_eq!(rejection(&module, &package.request(), HTTP, &job), "Request header X-Token is not valid"); + for name in ["X Token", "X-Token:", "X\r\nEvil", "", "Näme", "X(1)"] { + let job = json!({ "method": "GET", "url": "https://example.com/", "headers": { name: "v" } }); + assert!(rejection(&module, &package.request(), HTTP, &job).ends_with("is not valid"), "{name:?}"); + } + let job = json!({ "method": "GET", "url": "https://example.com/", "headers": { "X-Token": "a\u{0}b" } }); + assert_eq!(rejection(&module, &package.request(), HTTP, &job), "Request header X-Token is not valid"); + assert_eq!(transport.count(), 0); +} + +#[test] +fn responses_are_text_metadata_or_base64() { + let transport = FakeTransport::new(|request| { + if request.path() == "/binary" { + return FakeResponse::bytes(200, &[0xFF, 0x00, 0x10]); + } + FakeResponse::typed(if request.path() == "/missing" { 404 } else { 200 }, "body \u{e9}", "application/json") + .header("Cache-Control", "max-age=60") + .header("Set-Cookie", "session=secret") + .header("X-Internal", "secret") + .header("Retry-After", &"9".repeat(2049)) + .header("Last-Modified", "Fri, 13 Nov 2026 07:00:00 GMT") + }); + let module = offline(&transport); + let package = Package::legacy(); + let value = |job: Value| start(&module, &package.request(), HTTP, job.as_object()).1; + assert_eq!(value(get("https://example.com/")), "body \u{e9}"); + // An error status still delivers its body, as on the other platforms. + assert_eq!(value(get("https://example.com/missing")), "body \u{e9}"); + + let metadata = value(json!({ "method": "GET", "url": "https://example.com/missing", "metadata": true })); + assert_eq!(metadata["status"], 404); + assert_eq!(metadata["body"], "body \u{e9}"); + let mut names: Vec<&String> = metadata["headers"].as_object().unwrap().keys().collect(); + names.sort_unstable(); + assert_eq!(names, ["cache-control", "content-type", "last-modified"]); + assert_eq!(metadata["headers"]["content-type"], "application/json"); + assert_eq!(metadata["headers"]["last-modified"], "Fri, 13 Nov 2026 07:00:00 GMT"); + + assert_eq!(value(json!({ "method": "GET", "url": "https://example.com/binary", "base64": true })), "/wAQ"); + assert_eq!(rejection(&module, &package.request(), HTTP, &get("https://example.com/binary")), "https://example.com/binary is empty"); + assert_eq!(rejection(&module, &package.request(), HTTP, &json!({ "method": "GET", "url": "https://example.com/binary", "metadata": true })), "Response is not HTTP or has invalid UTF-8"); +} + +#[test] +fn timeouts_and_cancellation_stop_the_request() { + // An exchange that never answers. + let waiting = offline(&FakeTransport::waiting(|_, wait| Err(wait.expire()))); + let started = std::time::Instant::now(); + assert_eq!(failure(waiting.perform("GET", &url("https://example.com/"), &[], None, 150, &any_redirect, None)), "The request timed out."); + assert!(started.elapsed() < Duration::from_secs(5)); + + // A body that stops arriving is given up on as well. + let read: Arc = Arc::default(); + let counter = read.clone(); + let stalled = offline(&FakeTransport::new(move |_| FakeResponse::zeros(None, true, &counter))); + assert_eq!(failure(stalled.perform("GET", &url("https://example.com/"), &[], None, 150, &any_redirect, None)), "The request timed out."); + assert!(read.load(Ordering::SeqCst) > 0); + + // The end of the execution cancels the request. + let ended = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let flag = ended.clone(); + let timer = std::thread::spawn(move || { + std::thread::sleep(Duration::from_millis(150)); + flag.store(true, Ordering::SeqCst); + }); + let flag = ended.clone(); + let started = std::time::Instant::now(); + assert_eq!(waiting.perform("GET", &url("https://example.com/"), &[], None, 5000, &any_redirect, Some(Arc::new(move || flag.load(Ordering::SeqCst)))), Err(FetchError::Cancelled)); + assert!(started.elapsed() < Duration::from_secs(3)); + timer.join().unwrap(); + let transport = FakeTransport::ok(); + assert_eq!(offline(&transport).perform("GET", &url("https://example.com/"), &[], None, 5000, &any_redirect, Some(Arc::new(|| true))), Err(FetchError::Cancelled)); + assert_eq!(transport.count(), 0); + + // A result that arrives after the execution ended is dropped. + let package = Package::legacy(); + let request = package.request(); + let session = request.session.clone(); + let late = offline(&FakeTransport::new(move |_| { + session.finish(); + FakeResponse::text(200, "late") + })); + let (done, answers) = Completion::detached(request.session.clone()); + late.start(&request, HTTP, get("https://example.com/").as_object(), done); + assert!(answers.recv_timeout(Duration::from_millis(500)).is_err()); +} + +// MARK: - FetchUserAgentTests + +fn user_agent(given: &[(String, String)]) -> Option { + let transport = FakeTransport::ok(); + offline(&transport).perform("GET", &url("https://example.com/"), given, None, 3000, &any_redirect, None).unwrap(); + let requests = transport.requests(); + assert_eq!(requests.len(), 1); + assert_eq!(requests[0].headers.iter().filter(|(name, _)| name.eq_ignore_ascii_case("User-Agent")).count(), 1); + requests[0].header("User-Agent").map(str::to_owned) +} + +#[test] +fn requests_name_the_client_when_the_script_does_not() { + let agent = user_agent(&[]).unwrap(); + assert_eq!(agent, fetch::DEFAULT_USER_AGENT); + assert_eq!(agent, format!("ScriptWidget-Linux/{}", env!("CARGO_PKG_VERSION"))); + // Nothing about the user or the machine. + for variable in ["USER", "LOGNAME", "HOSTNAME"] { + if let Ok(value) = std::env::var(variable).map(|value| value.to_lowercase()) + && value.len() > 2 + { + assert!(!agent.to_lowercase().contains(&value), "{variable}"); + } + } +} + +#[test] +fn credentials_stay_with_the_origin_the_script_addressed() { + let transport = FakeTransport::new(|request| match (request.url.host_str().unwrap_or(""), request.path()) { + ("api.example.com", "/start") => FakeResponse::redirect(302, Some("/same-origin")), + ("api.example.com", "/same-origin") => FakeResponse::redirect(302, Some("https://cdn.example.net/elsewhere")), + ("cdn.example.net", "/elsewhere") => FakeResponse::redirect(302, Some("https://api.example.com/back")), + _ => FakeResponse::text(200, "arrived"), + }); + let given = headers(&[("Authorization", "Bearer secret"), ("cookie", "session=1"), ("Proxy-Authorization", "Basic x"), ("Accept", "application/json")]); + offline(&transport).perform("GET", &url("https://api.example.com/start"), &given, None, 3000, &any_redirect, None).unwrap(); + let requests = transport.requests(); + assert_eq!(requests.len(), 4); + // The same origin keeps the credentials across a redirect. + assert_eq!(requests[1].header("Authorization"), Some("Bearer secret")); + assert_eq!(requests[1].header("Cookie"), Some("session=1")); + // Another origin gets the ordinary headers only. + let elsewhere = &requests[2]; + assert_eq!(elsewhere.url.host_str(), Some("cdn.example.net")); + for credential in ["Authorization", "Cookie", "Proxy-Authorization"] { + assert_eq!(elsewhere.header(credential), None, "{credential}"); + } + assert_eq!(elsewhere.header("Accept"), Some("application/json")); + // Back at the first origin they are sent again: they were meant for it. + assert_eq!(requests[3].header("Authorization"), Some("Bearer secret")); + + // Another scheme or port of the same host is another origin. + for other in ["http://api.example.com/plain", "https://api.example.com:8443/port"] { + let target = other.to_owned(); + let transport = FakeTransport::new(move |request| if request.path() == "/start" { FakeResponse::redirect(307, Some(&target)) } else { FakeResponse::text(200, "arrived") }); + offline(&transport).perform("POST", &url("https://api.example.com/start"), &given, Some(b"x"), 3000, &any_redirect, None).unwrap(); + let requests = transport.requests(); + assert_eq!(requests[1].url.as_str(), other); + assert_eq!(requests[1].header("Authorization"), None, "{other}"); + assert_eq!(requests[1].header("Cookie"), None, "{other}"); + assert_eq!(requests[1].header("Accept"), Some("application/json")); + } +} + +#[test] +fn a_scripts_own_user_agent_wins() { + assert_eq!(user_agent(&headers(&[("user-agent", "my-widget/2.0")])).as_deref(), Some("my-widget/2.0")); +} diff --git a/Linux/crates/sw-core/tests/api_fixtures/met-norway-compact.json b/Linux/crates/sw-core/tests/api_fixtures/met-norway-compact.json new file mode 100644 index 00000000..6559c366 --- /dev/null +++ b/Linux/crates/sw-core/tests/api_fixtures/met-norway-compact.json @@ -0,0 +1 @@ +{"type":"Feature","geometry":{"type":"Point","coordinates":[10.75,59.91,3]},"properties":{"meta":{"updated_at":"2026-11-13T06:30:33Z","units":{"air_pressure_at_sea_level":"hPa","air_temperature":"celsius","cloud_area_fraction":"%","precipitation_amount":"mm","relative_humidity":"%","wind_from_direction":"degrees","wind_speed":"m/s"}},"timeseries":[{"time":"2026-11-13T07:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":990.9,"air_temperature":1.7,"cloud_area_fraction":37.8,"relative_humidity":61.6,"wind_from_direction":345.0,"wind_speed":3.0}},"next_12_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"fair_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T08:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":991.7,"air_temperature":2.0,"cloud_area_fraction":16.4,"relative_humidity":60.8,"wind_from_direction":349.0,"wind_speed":3.5}},"next_12_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"fair_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T09:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":992.4,"air_temperature":3.0,"cloud_area_fraction":16.0,"relative_humidity":58.3,"wind_from_direction":346.0,"wind_speed":4.7}},"next_12_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"fair_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T10:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":993.1,"air_temperature":4.1,"cloud_area_fraction":13.0,"relative_humidity":54.9,"wind_from_direction":350.0,"wind_speed":5.6}},"next_12_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"fair_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T11:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":993.8,"air_temperature":4.7,"cloud_area_fraction":5.4,"relative_humidity":53.2,"wind_from_direction":351.0,"wind_speed":5.7}},"next_12_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T12:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":994.5,"air_temperature":5.3,"cloud_area_fraction":0.8,"relative_humidity":51.8,"wind_from_direction":348.0,"wind_speed":5.4}},"next_12_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T13:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":995.5,"air_temperature":5.5,"cloud_area_fraction":1.0,"relative_humidity":51.2,"wind_from_direction":342.0,"wind_speed":4.8}},"next_12_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T14:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":996.5,"air_temperature":5.1,"cloud_area_fraction":2.0,"relative_humidity":53.4,"wind_from_direction":326.0,"wind_speed":3.6}},"next_12_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T15:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":997.4,"air_temperature":3.3,"cloud_area_fraction":6.3,"relative_humidity":60.1,"wind_from_direction":319.0,"wind_speed":2.6}},"next_12_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T16:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":998.2,"air_temperature":2.1,"cloud_area_fraction":5.2,"relative_humidity":60.7,"wind_from_direction":325.0,"wind_speed":2.8}},"next_12_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T17:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":999.0,"air_temperature":1.5,"cloud_area_fraction":0.1,"relative_humidity":59.7,"wind_from_direction":328.0,"wind_speed":2.3}},"next_12_hours":{"summary":{"symbol_code":"fair_night"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T18:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1000.0,"air_temperature":0.9,"cloud_area_fraction":0.0,"relative_humidity":63.1,"wind_from_direction":242.0,"wind_speed":1.3}},"next_12_hours":{"summary":{"symbol_code":"fair_night"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T19:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1001.0,"air_temperature":0.6,"cloud_area_fraction":0.0,"relative_humidity":65.3,"wind_from_direction":241.0,"wind_speed":2.3}},"next_12_hours":{"summary":{"symbol_code":"fair_night"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T20:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1001.7,"air_temperature":0.3,"cloud_area_fraction":0.0,"relative_humidity":66.0,"wind_from_direction":259.0,"wind_speed":0.9}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T21:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1002.5,"air_temperature":0.3,"cloud_area_fraction":0.0,"relative_humidity":65.9,"wind_from_direction":201.0,"wind_speed":1.1}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T22:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1003.2,"air_temperature":0.3,"cloud_area_fraction":0.3,"relative_humidity":66.0,"wind_from_direction":205.0,"wind_speed":1.7}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"fair_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-13T23:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1003.8,"air_temperature":0.2,"cloud_area_fraction":0.7,"relative_humidity":71.9,"wind_from_direction":191.0,"wind_speed":2.5}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T00:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1004.2,"air_temperature":0.1,"cloud_area_fraction":0.1,"relative_humidity":75.7,"wind_from_direction":182.0,"wind_speed":2.6}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T01:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1004.3,"air_temperature":-0.2,"cloud_area_fraction":11.4,"relative_humidity":75.5,"wind_from_direction":154.0,"wind_speed":2.4}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T02:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1004.4,"air_temperature":-0.6,"cloud_area_fraction":14.6,"relative_humidity":75.1,"wind_from_direction":126.0,"wind_speed":2.0}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"fair_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T03:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1004.5,"air_temperature":-0.8,"cloud_area_fraction":37.6,"relative_humidity":74.8,"wind_from_direction":114.0,"wind_speed":1.8}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"fair_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T04:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1004.4,"air_temperature":-0.6,"cloud_area_fraction":83.0,"relative_humidity":73.2,"wind_from_direction":110.0,"wind_speed":1.7}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T05:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1004.5,"air_temperature":-0.1,"cloud_area_fraction":92.3,"relative_humidity":70.9,"wind_from_direction":130.0,"wind_speed":1.1}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T06:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1004.6,"air_temperature":0.4,"cloud_area_fraction":97.2,"relative_humidity":68.3,"wind_from_direction":157.0,"wind_speed":2.0}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T07:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1004.6,"air_temperature":0.7,"cloud_area_fraction":91.0,"relative_humidity":74.7,"wind_from_direction":169.0,"wind_speed":2.0}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T08:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1004.4,"air_temperature":1.4,"cloud_area_fraction":72.8,"relative_humidity":77.0,"wind_from_direction":168.0,"wind_speed":2.2}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T09:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1004.3,"air_temperature":2.3,"cloud_area_fraction":19.6,"relative_humidity":82.0,"wind_from_direction":181.0,"wind_speed":1.9}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"fair_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T10:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1004.1,"air_temperature":3.0,"cloud_area_fraction":2.4,"relative_humidity":86.4,"wind_from_direction":173.0,"wind_speed":1.6}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"clearsky_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T11:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1003.9,"air_temperature":3.9,"cloud_area_fraction":24.1,"relative_humidity":84.4,"wind_from_direction":179.0,"wind_speed":1.9}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"fair_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T12:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1003.7,"air_temperature":4.3,"cloud_area_fraction":73.4,"relative_humidity":81.4,"wind_from_direction":189.0,"wind_speed":1.7}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T13:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1003.4,"air_temperature":4.4,"cloud_area_fraction":90.8,"relative_humidity":81.2,"wind_from_direction":169.0,"wind_speed":1.2}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T14:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1003.2,"air_temperature":4.3,"cloud_area_fraction":99.4,"relative_humidity":81.8,"wind_from_direction":155.0,"wind_speed":2.2}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T15:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1003.0,"air_temperature":4.2,"cloud_area_fraction":99.7,"relative_humidity":85.0,"wind_from_direction":162.0,"wind_speed":3.4}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T16:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1002.9,"air_temperature":4.1,"cloud_area_fraction":99.8,"relative_humidity":89.7,"wind_from_direction":176.0,"wind_speed":3.2}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T17:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1002.7,"air_temperature":4.2,"cloud_area_fraction":99.9,"relative_humidity":91.2,"wind_from_direction":184.0,"wind_speed":3.0}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T18:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1002.7,"air_temperature":4.4,"cloud_area_fraction":99.8,"relative_humidity":91.7,"wind_from_direction":180.0,"wind_speed":3.9}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T19:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1002.5,"air_temperature":4.7,"cloud_area_fraction":99.7,"relative_humidity":90.8,"wind_from_direction":178.0,"wind_speed":3.9}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T20:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1002.2,"air_temperature":4.9,"cloud_area_fraction":99.9,"relative_humidity":90.2,"wind_from_direction":170.0,"wind_speed":4.2}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T21:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1002.2,"air_temperature":4.8,"cloud_area_fraction":99.9,"relative_humidity":90.4,"wind_from_direction":167.0,"wind_speed":4.9}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T22:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1001.9,"air_temperature":4.6,"cloud_area_fraction":99.6,"relative_humidity":90.8,"wind_from_direction":167.0,"wind_speed":4.1}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-14T23:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1001.8,"air_temperature":4.7,"cloud_area_fraction":99.4,"relative_humidity":90.2,"wind_from_direction":156.0,"wind_speed":3.7}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T00:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1001.4,"air_temperature":4.6,"cloud_area_fraction":99.7,"relative_humidity":87.0,"wind_from_direction":146.0,"wind_speed":3.6}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T01:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1001.0,"air_temperature":4.5,"cloud_area_fraction":100.0,"relative_humidity":87.4,"wind_from_direction":141.0,"wind_speed":3.5}},"next_1_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T02:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1000.6,"air_temperature":4.6,"cloud_area_fraction":100.0,"relative_humidity":89.1,"wind_from_direction":151.0,"wind_speed":3.9}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T03:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1000.2,"air_temperature":4.8,"cloud_area_fraction":100.0,"relative_humidity":88.7,"wind_from_direction":178.0,"wind_speed":4.2}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T04:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":999.9,"air_temperature":5.2,"cloud_area_fraction":99.9,"relative_humidity":86.5,"wind_from_direction":188.0,"wind_speed":4.2}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T05:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":999.8,"air_temperature":5.5,"cloud_area_fraction":99.3,"relative_humidity":86.1,"wind_from_direction":197.0,"wind_speed":5.1}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T06:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":999.6,"air_temperature":5.8,"cloud_area_fraction":91.9,"relative_humidity":85.0,"wind_from_direction":197.0,"wind_speed":5.0}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T07:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":999.4,"air_temperature":6.0,"cloud_area_fraction":93.2,"relative_humidity":83.4,"wind_from_direction":204.0,"wind_speed":5.1}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T08:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":999.1,"air_temperature":6.3,"cloud_area_fraction":100.0,"relative_humidity":84.7,"wind_from_direction":213.0,"wind_speed":4.4}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T09:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":999.0,"air_temperature":6.9,"cloud_area_fraction":99.5,"relative_humidity":87.1,"wind_from_direction":210.0,"wind_speed":5.1}},"next_1_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T10:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":998.7,"air_temperature":7.5,"cloud_area_fraction":82.3,"relative_humidity":84.2,"wind_from_direction":199.0,"wind_speed":5.6}},"next_1_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T11:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":998.2,"air_temperature":8.0,"cloud_area_fraction":50.8,"relative_humidity":78.7,"wind_from_direction":201.0,"wind_speed":5.7}},"next_1_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T12:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":997.4,"air_temperature":8.3,"cloud_area_fraction":66.9,"relative_humidity":76.2,"wind_from_direction":192.0,"wind_speed":5.8}},"next_12_hours":{"summary":{"symbol_code":"lightrain"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-15T18:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":992.6,"air_temperature":6.4,"cloud_area_fraction":100.0,"relative_humidity":94.6,"wind_from_direction":169.1,"wind_speed":4.2}},"next_12_hours":{"summary":{"symbol_code":"rain"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"rain"},"details":{"precipitation_amount":2.6}}}},{"time":"2026-11-16T00:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":988.1,"air_temperature":5.4,"cloud_area_fraction":100.0,"relative_humidity":97.0,"wind_from_direction":109.6,"wind_speed":3.1}},"next_12_hours":{"summary":{"symbol_code":"rain"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"rain"},"details":{"precipitation_amount":5.0}}}},{"time":"2026-11-16T06:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":981.3,"air_temperature":5.6,"cloud_area_fraction":100.0,"relative_humidity":97.0,"wind_from_direction":59.7,"wind_speed":4.2}},"next_12_hours":{"summary":{"symbol_code":"rain"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"rain"},"details":{"precipitation_amount":4.1}}}},{"time":"2026-11-16T12:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":980.2,"air_temperature":6.3,"cloud_area_fraction":100.0,"relative_humidity":95.2,"wind_from_direction":160.9,"wind_speed":4.9}},"next_12_hours":{"summary":{"symbol_code":"lightrain"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"rain"},"details":{"precipitation_amount":2.8}}}},{"time":"2026-11-16T18:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":980.4,"air_temperature":5.9,"cloud_area_fraction":100.0,"relative_humidity":95.5,"wind_from_direction":162.7,"wind_speed":3.3}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-17T00:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":982.4,"air_temperature":4.4,"cloud_area_fraction":100.0,"relative_humidity":93.1,"wind_from_direction":161.0,"wind_speed":2.5}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-17T06:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":981.8,"air_temperature":3.5,"cloud_area_fraction":99.2,"relative_humidity":92.5,"wind_from_direction":324.1,"wind_speed":2.1}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-17T12:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":982.6,"air_temperature":4.1,"cloud_area_fraction":100.0,"relative_humidity":91.6,"wind_from_direction":293.7,"wind_speed":1.6}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-17T18:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":982.2,"air_temperature":3.9,"cloud_area_fraction":99.6,"relative_humidity":93.5,"wind_from_direction":18.1,"wind_speed":1.6}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-18T00:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":982.5,"air_temperature":2.7,"cloud_area_fraction":73.0,"relative_humidity":88.1,"wind_from_direction":339.1,"wind_speed":1.8}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-18T06:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":984.2,"air_temperature":1.6,"cloud_area_fraction":57.4,"relative_humidity":86.8,"wind_from_direction":340.1,"wind_speed":2.2}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-18T12:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":987.2,"air_temperature":2.7,"cloud_area_fraction":30.5,"relative_humidity":75.8,"wind_from_direction":330.9,"wind_speed":2.5}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"fair_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-18T18:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":992.5,"air_temperature":0.6,"cloud_area_fraction":98.4,"relative_humidity":85.2,"wind_from_direction":343.0,"wind_speed":2.9}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-19T00:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":990.0,"air_temperature":2.3,"cloud_area_fraction":16.4,"relative_humidity":80.5,"wind_from_direction":337.9,"wind_speed":3.8}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"fair_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-19T06:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":998.4,"air_temperature":-1.0,"cloud_area_fraction":98.8,"relative_humidity":85.1,"wind_from_direction":336.2,"wind_speed":4.0}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-19T12:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":992.5,"air_temperature":2.8,"cloud_area_fraction":100.0,"relative_humidity":80.1,"wind_from_direction":19.9,"wind_speed":3.9}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-19T18:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":994.6,"air_temperature":1.6,"cloud_area_fraction":28.5,"relative_humidity":80.7,"wind_from_direction":346.4,"wind_speed":3.5}},"next_12_hours":{"summary":{"symbol_code":"fair_night"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"fair_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-20T00:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1006.7,"air_temperature":-0.1,"cloud_area_fraction":35.9,"relative_humidity":83.1,"wind_from_direction":342.6,"wind_speed":3.7}},"next_12_hours":{"summary":{"symbol_code":"fair_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"fair_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-20T06:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1007.7,"air_temperature":-1.9,"cloud_area_fraction":42.2,"relative_humidity":83.3,"wind_from_direction":339.7,"wind_speed":3.3}},"next_12_hours":{"summary":{"symbol_code":"fair_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-20T12:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1006.9,"air_temperature":1.9,"cloud_area_fraction":24.6,"relative_humidity":69.9,"wind_from_direction":343.6,"wind_speed":3.1}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"fair_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-20T18:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1013.0,"air_temperature":0.5,"cloud_area_fraction":28.1,"relative_humidity":81.6,"wind_from_direction":336.1,"wind_speed":2.9}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"fair_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-21T00:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1015.7,"air_temperature":-2.1,"cloud_area_fraction":99.6,"relative_humidity":82.0,"wind_from_direction":339.6,"wind_speed":2.5}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-21T06:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1012.4,"air_temperature":-0.8,"cloud_area_fraction":33.6,"relative_humidity":83.6,"wind_from_direction":347.5,"wind_speed":2.4}},"next_12_hours":{"summary":{"symbol_code":"fair_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"fair_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-21T12:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1016.7,"air_temperature":0.9,"cloud_area_fraction":21.9,"relative_humidity":72.7,"wind_from_direction":337.8,"wind_speed":2.0}},"next_12_hours":{"summary":{"symbol_code":"fair_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"fair_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-21T18:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1018.7,"air_temperature":-1.3,"cloud_area_fraction":31.2,"relative_humidity":83.4,"wind_from_direction":345.9,"wind_speed":2.3}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"fair_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-22T00:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1020.5,"air_temperature":-2.1,"cloud_area_fraction":43.7,"relative_humidity":85.4,"wind_from_direction":17.9,"wind_speed":2.2}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_night"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-22T06:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1018.7,"air_temperature":-0.4,"cloud_area_fraction":100.0,"relative_humidity":91.9,"wind_from_direction":32.4,"wind_speed":2.1}},"next_12_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-22T12:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1016.6,"air_temperature":1.6,"cloud_area_fraction":46.1,"relative_humidity":75.9,"wind_from_direction":25.3,"wind_speed":2.1}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"partlycloudy_day"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-22T18:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1017.6,"air_temperature":0.7,"cloud_area_fraction":100.0,"relative_humidity":91.8,"wind_from_direction":19.6,"wind_speed":2.4}},"next_12_hours":{"summary":{"symbol_code":"cloudy"},"details":{}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-23T00:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1016.7,"air_temperature":1.1,"cloud_area_fraction":100.0,"relative_humidity":90.9,"wind_from_direction":29.1,"wind_speed":2.3}},"next_6_hours":{"summary":{"symbol_code":"cloudy"},"details":{"precipitation_amount":0.0}}}},{"time":"2026-11-23T06:00:00Z","data":{"instant":{"details":{"air_pressure_at_sea_level":1015.4,"air_temperature":1.3,"cloud_area_fraction":100.0,"relative_humidity":91.4,"wind_from_direction":33.5,"wind_speed":2.5}}}}]}} \ No newline at end of file diff --git a/Linux/crates/sw-core/tests/api_http.rs b/Linux/crates/sw-core/tests/api_http.rs new file mode 100644 index 00000000..98e5c5fa --- /dev/null +++ b/Linux/crates/sw-core/tests/api_http.rs @@ -0,0 +1,614 @@ +//! The production transport of `fetch` over real sockets: the connect step, +//! HTTP/1.1 framing, TLS, limits, timeouts and redirects. +//! +//! Nothing here reaches the Internet. The servers listen on this machine, +//! which the transport refuses to connect to, as it must. The tests therefore +//! replace the two things the host application may replace: the resolver, +//! which answers the fixture names with public addresses, and the dialer, +//! which routes those addresses to the local listeners (the counterpart of +//! the replaceable resolver and handler of the Windows tests). Every address +//! check stays in force, and package code can reach neither hook. + +mod api_common; + +use std::io::{Read, Write}; +use std::net::{SocketAddr, TcpListener, TcpStream}; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use api_common::*; +use rustls::pki_types::pem::PemObject; +use rustls::pki_types::{CertificateDer, PrivateKeyDer}; +use sw_core::api::fetch::{self, FetchError, FetchModule, Resolver, Wait, system_resolver}; +use sw_core::api::http::{Dialer, HttpTransport, SystemDialer, client_config}; + +/// One request as a test server received it. +#[derive(Debug, Clone)] +struct Seen { + head: String, + body: Vec, +} + +impl Seen { + fn line(&self) -> &str { + self.head.lines().next().unwrap_or("") + } + + fn path(&self) -> &str { + self.line().split(' ').nth(1).unwrap_or("") + } + + fn header(&self, name: &str) -> Option<&str> { + self.head.lines().skip(1).filter_map(|line| line.split_once(':')).find(|(known, _)| known.eq_ignore_ascii_case(name)).map(|(_, value)| value.trim()) + } +} + +type Handler = Arc; + +struct Server { + port: u16, + seen: Arc>>, + connections: Arc, +} + +impl Server { + fn seen(&self) -> Vec { + self.seen.lock().unwrap().clone() + } +} + +fn read_request(stream: &mut dyn Read) -> Option { + let mut data = Vec::new(); + let mut chunk = [0u8; 4096]; + let end = loop { + if let Some(end) = data.windows(4).position(|window| window == b"\r\n\r\n") { + break end + 4; + } + match stream.read(&mut chunk) { + Ok(count) if count > 0 => data.extend_from_slice(&chunk[..count]), + _ => return None, + } + }; + let head = String::from_utf8_lossy(&data[..end]).into_owned(); + let seen = Seen { head, body: Vec::new() }; + let length: usize = seen.header("Content-Length").and_then(|text| text.parse().ok()).unwrap_or(0); + let mut body = data[end..].to_vec(); + while body.len() < length { + match stream.read(&mut chunk) { + Ok(count) if count > 0 => body.extend_from_slice(&chunk[..count]), + _ => break, + } + } + Some(Seen { body, ..seen }) +} + +/// A server on this machine. `tls` makes it speak HTTPS with that configuration. +fn serve(tls: Option>, handler: impl Fn(&Seen, &mut dyn Write) + Send + Sync + 'static) -> Server { + let listener = TcpListener::bind("127.0.0.1:0").expect("listener"); + let port = listener.local_addr().unwrap().port(); + let seen: Arc>> = Arc::default(); + let connections: Arc = Arc::default(); + let handler: Handler = Arc::new(handler); + let (record, count) = (seen.clone(), connections.clone()); + std::thread::spawn(move || { + for socket in listener.incoming().flatten() { + count.fetch_add(1, Ordering::SeqCst); + let (handler, record, tls) = (handler.clone(), record.clone(), tls.clone()); + std::thread::spawn(move || { + let _ = socket.set_read_timeout(Some(Duration::from_secs(10))); + let mut socket = socket; + match tls { + None => { + if let Some(request) = read_request(&mut socket) { + record.lock().unwrap().push(request.clone()); + handler(&request, &mut socket); + } + } + Some(config) => { + let Ok(connection) = rustls::ServerConnection::new(config) else { return }; + let mut stream = rustls::StreamOwned::new(connection, socket); + if let Some(request) = read_request(&mut stream) { + record.lock().unwrap().push(request.clone()); + handler(&request, &mut stream); + stream.conn.send_close_notify(); + let _ = stream.flush(); + } + } + } + }); + } + }); + Server { port, seen, connections } +} + +fn respond(status: &str, headers: &[(&str, &str)], body: &[u8]) -> Vec { + let mut response = format!("HTTP/1.1 {status}\r\n"); + for (name, value) in headers { + response.push_str(&format!("{name}: {value}\r\n")); + } + response.push_str(&format!("Content-Length: {}\r\n\r\n", body.len())); + let mut bytes = response.into_bytes(); + bytes.extend_from_slice(body); + bytes +} + +/// Routes every address to a listener on this machine and records what it was asked for. +struct Routed { + port: u16, + dialed: Mutex>, + /// Addresses that refuse the connection. + dead: Vec, +} + +impl Routed { + fn to(port: u16) -> Arc { + Arc::new(Self { port, dialed: Mutex::default(), dead: Vec::new() }) + } + + fn dialed(&self) -> Vec { + self.dialed.lock().unwrap().iter().map(SocketAddr::to_string).collect() + } +} + +impl Dialer for Routed { + fn dial(&self, address: SocketAddr, timeout: Duration) -> std::io::Result { + self.dialed.lock().unwrap().push(address); + if self.dead.contains(&address) { + return Err(std::io::ErrorKind::ConnectionRefused.into()); + } + TcpStream::connect_timeout(&SocketAddr::from(([127, 0, 0, 1], self.port)), timeout) + } +} + +/// A dialer that must never be reached. +struct NoDialer(AtomicUsize); + +impl Dialer for NoDialer { + fn dial(&self, _: SocketAddr, _: Duration) -> std::io::Result { + self.0.fetch_add(1, Ordering::SeqCst); + Err(std::io::ErrorKind::ConnectionRefused.into()) + } +} + +/// The names of the fixtures: public ones, and one that points into a private network. +fn fixture_resolver() -> Resolver { + Arc::new(|host: &str| match host { + "internal.example.com" => Ok(addresses(&["10.0.0.8"])), + "six.example.com" => Ok(addresses(&["2606:2800:220:1:248:1893:25c8:1946"])), + "unknown.example.com" => Err(std::io::Error::other("no such host")), + // An address literal is itself, as with the system resolver. + literal if literal.parse::().is_ok() => Ok(addresses(&[literal])), + _ => Ok(addresses(&[PUBLIC])), + }) +} + +fn module(routed: &Arc) -> FetchModule { + FetchModule::with_parts(fixture_resolver(), Arc::new(HttpTransport::with_parts(fixture_resolver(), routed.clone(), None))) +} + +fn get_from(module: &FetchModule, address: &str) -> Result { + module.perform("GET", &url(address), &[], None, 5000, &any_redirect, None) +} + +fn transport_error(result: Result) -> String { + match result { + Err(FetchError::Transport(message)) => message, + other => panic!("expected a transport failure, got {other:?}"), + } +} + +#[test] +fn the_request_goes_to_the_checked_address_and_is_plain_http_1_1() { + let server = serve(None, |_, output| { + let _ = output.write_all(&respond("200 OK", &[("Content-Type", "text/plain"), ("X-Reply", "one"), ("x-reply", "two")], b"hello")); + }); + let routed = Routed::to(server.port); + let module = module(&routed); + let sent = headers(&[("X-Token", "t"), ("Connection", "keep-alive"), ("Transfer-Encoding", "chunked"), ("Upgrade", "websocket")]); + let response = module.perform("POST", &url("http://example.com/items?x=1&y=%20#fragment"), &sent, Some(b"payload"), 5000, &any_redirect, None).unwrap(); + assert_eq!((response.status, response.body.as_slice()), (200, &b"hello"[..])); + assert_eq!(response.headers.get("content-type").map(String::as_str), Some("text/plain")); + assert_eq!(response.headers.get("x-reply").map(String::as_str), Some("two"), "the last value wins"); + // The socket was opened to the address the resolver returned and the check accepted. + assert_eq!(routed.dialed(), [format!("{PUBLIC}:80")]); + + let seen = server.seen().remove(0); + assert_eq!(seen.line(), "POST /items?x=1&y=%20 HTTP/1.1"); + assert_eq!(seen.header("Host"), Some("example.com")); + assert_eq!(seen.header("X-Token"), Some("t")); + assert_eq!(seen.header("User-Agent"), Some(fetch::DEFAULT_USER_AGENT)); + assert_eq!(seen.header("Content-Length"), Some("7")); + assert_eq!(seen.header("Accept-Encoding"), Some("gzip, deflate")); + // One request per connection; a script does not get to say otherwise. + assert_eq!(seen.header("Connection"), Some("close")); + assert_eq!(seen.header("Transfer-Encoding"), None); + assert_eq!(seen.header("Upgrade"), None); + assert_eq!(seen.body, b"payload"); + // Nothing identifies a proxy, a cookie store or stored credentials. + for absent in ["Cookie", "Authorization", "Proxy-Authorization", "Proxy-Connection", "Via", "Forwarded"] { + assert_eq!(seen.header(absent), None, "{absent}"); + } + + // Another port and an IPv6 answer are dialed as resolved. + get_from(&module, "http://example.com:8080/").unwrap(); + get_from(&module, "http://six.example.com/").unwrap(); + assert_eq!(routed.dialed()[1..], [format!("{PUBLIC}:8080"), "[2606:2800:220:1:248:1893:25c8:1946]:80".to_owned()]); + assert_eq!(server.seen()[1].header("Host"), Some("example.com:8080")); + assert_eq!(server.seen()[1].line(), "GET / HTTP/1.1"); + assert_eq!(server.seen()[1].header("Content-Length"), None); +} + +#[test] +fn the_connect_step_refuses_blocked_names_and_addresses_without_connecting() { + let dialer = NoDialer(AtomicUsize::new(0)); + let wait = || { + let wait = Wait::new(Duration::from_secs(10), Duration::from_secs(5), None); + wait.arm().unwrap(); + wait + }; + let refusal = |host: &str, answers: &[&str]| -> String { + let resolved = Arc::new(AtomicUsize::new(0)); + let (counter, answer) = (resolved.clone(), addresses(answers)); + let resolver: Resolver = Arc::new(move |_| { + counter.fetch_add(1, Ordering::SeqCst); + Ok(answer.clone()) + }); + let error = HttpTransport::connect(&resolver, &dialer, host, 443, &wait()).unwrap_err(); + format!("{}|{}", error.message(), resolved.load(Ordering::SeqCst)) + }; + for address in ["127.0.0.1", "10.1.2.3", "169.254.169.254", "::1", "fc00::1", "fd00::1", "::ffff:127.0.0.1", "192.168.1.1", "172.16.0.1", "0.0.0.0", "100.64.0.1", "fe80::1", "64:ff9b::a00:1", "2002:a00:1::1", "224.0.0.1"] { + assert_eq!(refusal("public.example.com", &[address]), format!("{BLOCKED}|1"), "{address}"); + assert_eq!(refusal("public.example.com", &[PUBLIC, address]), format!("{BLOCKED}|1"), "{address}"); + } + assert_eq!(refusal("public.example.com", &[]), format!("{BLOCKED}|1")); + // A blocked name is refused without asking the resolver. + for host in ["localhost", "LOCALHOST", "printer.local", "127.0.0.1", "10.0.0.1", "192.168.1.1", "172.16.0.1", "::1", "[::1]", ""] { + assert_eq!(refusal(host, &[PUBLIC]), format!("{BLOCKED}|0"), "{host}"); + } + let unknown: Resolver = Arc::new(|_| Err(std::io::Error::other("no such host"))); + assert_eq!(HttpTransport::connect(&unknown, &dialer, "public.example.com", 443, &wait()).unwrap_err(), FetchError::failed("Could not resolve public.example.com")); + assert_eq!(dialer.0.load(Ordering::SeqCst), 0, "nothing was dialed"); + // A public answer is dialed, and only then. + assert!(matches!(HttpTransport::connect(&public_resolver(), &dialer, "public.example.com", 443, &wait()), Err(FetchError::Transport(_)))); + assert_eq!(dialer.0.load(Ordering::SeqCst), 1); +} + +/// A service on this machine is not reachable by any spelling, with the production dialer. +#[test] +fn this_machine_is_not_reachable_without_the_test_network() { + let server = serve(None, |_, output| { + let _ = output.write_all(&respond("200 OK", &[], b"secret")); + }); + let port = server.port; + let production = FetchModule::new(); + for address in [format!("http://127.0.0.1:{port}/"), format!("http://localhost:{port}/"), format!("http://[::1]:{port}/"), format!("http://[::ffff:127.0.0.1]:{port}/"), format!("http://2130706433:{port}/"), format!("http://127.1:{port}/")] { + assert_eq!(failure(get_from(&production, &address)), BLOCKED, "{address}"); + let package = Package::legacy(); + assert_eq!(rejection(&production, &package.request(), "http.request", &get(&address)), BLOCKED, "{address}"); + } + // A public name that resolves to this machine is refused at the pre-check and again at the connect step. + let pointing_here = FetchModule::with_resolver(fixed_resolver(&["127.0.0.1"])); + assert_eq!(failure(get_from(&pointing_here, &format!("http://public.example.com:{port}/"))), BLOCKED); + let wait = Wait::new(Duration::from_secs(5), Duration::from_secs(5), None); + wait.arm().unwrap(); + assert_eq!(HttpTransport::connect(&fixed_resolver(&["127.0.0.1"]), &SystemDialer, "public.example.com", port, &wait).unwrap_err(), FetchError::failed(BLOCKED)); + assert_eq!(HttpTransport::connect(&(Arc::new(system_resolver) as Resolver), &SystemDialer, "localhost", port, &wait).unwrap_err(), FetchError::failed(BLOCKED)); + std::thread::sleep(Duration::from_millis(100)); + assert_eq!(server.connections.load(Ordering::SeqCst), 0, "no connection reached the local service"); +} + +#[test] +fn the_next_address_is_tried_when_one_does_not_answer() { + let server = serve(None, |_, output| { + let _ = output.write_all(&respond("200 OK", &[], b"ok")); + }); + let first: SocketAddr = "198.51.100.7:80".parse().unwrap(); + let routed = Arc::new(Routed { port: server.port, dialed: Mutex::default(), dead: vec![first] }); + let resolver = fixed_resolver(&["198.51.100.7", PUBLIC]); + let module = FetchModule::with_parts(resolver.clone(), Arc::new(HttpTransport::with_parts(resolver, routed.clone(), None))); + assert_eq!(get_from(&module, "http://example.com/").unwrap().body, b"ok"); + assert_eq!(routed.dialed(), ["198.51.100.7:80".to_owned(), format!("{PUBLIC}:80")]); +} + +fn gzip(data: &[u8]) -> Vec { + let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default()); + encoder.write_all(data).unwrap(); + encoder.finish().unwrap() +} + +fn zlib(data: &[u8]) -> Vec { + let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); + encoder.write_all(data).unwrap(); + encoder.finish().unwrap() +} + +#[test] +fn bodies_are_delimited_by_length_chunks_or_the_end_of_the_connection() { + let server = serve(None, |request, output| { + let reply: Vec = match request.path() { + "/chunked" => b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n1;ext=1\r\n \r\n6\r\nworld!\r\n0\r\nX-Trailer: 1\r\n\r\nignored".to_vec(), + "/close" => b"HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nuntil the end".to_vec(), + "/gzip" => respond("200 OK", &[("Content-Encoding", "gzip")], &gzip(b"compressed text")), + "/deflate" => respond("200 OK", &[("Content-Encoding", "deflate")], &zlib(b"deflated text")), + "/brotli" => respond("200 OK", &[("Content-Encoding", "br")], b"\x0b\x02\x80raw"), + "/chunked-gzip" => { + let packed = gzip(b"both at once"); + let mut reply = b"HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nTransfer-Encoding: chunked\r\n\r\n".to_vec(); + reply.extend_from_slice(format!("{:x}\r\n", packed.len()).as_bytes()); + reply.extend_from_slice(&packed); + reply.extend_from_slice(b"\r\n0\r\n\r\n"); + reply + } + "/short" => b"HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nshort".to_vec(), + "/short-chunk" => b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n10\r\nshort".to_vec(), + "/bad-chunk" => b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\nzz\r\nhello\r\n0\r\n\r\n".to_vec(), + "/two-lengths" => b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nContent-Length: 5\r\n\r\nhello".to_vec(), + "/negative" => b"HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nhello".to_vec(), + "/head" => b"HTTP/1.1 200 OK\r\nContent-Length: 1000\r\n\r\n".to_vec(), + "/empty" => b"HTTP/1.1 204 No Content\r\n\r\nstray".to_vec(), + "/continue" => b"HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 103 Early Hints\r\nLink: \r\n\r\nHTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndone".to_vec(), + "/garbage" => b"SSH-2.0-OpenSSH_9.9\r\n\r\n".to_vec(), + "/silent" => Vec::new(), + "/huge-head" => { + let mut reply = b"HTTP/1.1 200 OK\r\n".to_vec(); + for index in 0..40 { + reply.extend_from_slice(format!("X-Pad-{index}: {}\r\n", "p".repeat(2000)).as_bytes()); + } + reply.extend_from_slice(b"Content-Length: 2\r\n\r\nok"); + reply + } + _ => respond("404 Not Found", &[], b"missing"), + }; + let _ = output.write_all(&reply); + }); + let module = module(&Routed::to(server.port)); + let body = |path: &str| String::from_utf8(get_from(&module, &format!("http://example.com{path}")).unwrap().body).unwrap(); + assert_eq!(body("/chunked"), "hello world!"); + assert_eq!(body("/close"), "until the end"); + assert_eq!(body("/gzip"), "compressed text"); + assert_eq!(body("/deflate"), "deflated text"); + assert_eq!(body("/chunked-gzip"), "both at once"); + assert_eq!(body("/empty"), ""); + assert_eq!(body("/continue"), "done"); + assert_eq!(body("/nothing-here"), "missing"); + // A decoded body no longer has its encoded length or encoding; an unknown encoding is handed on as it came. + let decoded = get_from(&module, "http://example.com/gzip").unwrap(); + assert_eq!((decoded.headers.get("content-encoding"), decoded.headers.get("content-length")), (None, None)); + let raw = get_from(&module, "http://example.com/brotli").unwrap(); + assert_eq!((raw.body.as_slice(), raw.headers.get("content-encoding").map(String::as_str)), (&b"\x0b\x02\x80raw"[..], Some("br"))); + // HEAD has no body, whatever length it declares. + let head = module.perform("HEAD", &url("http://example.com/head"), &[], None, 5000, &any_redirect, None).unwrap(); + assert_eq!((head.status, head.body.len()), (200, 0)); + + let broken = |path: &str| transport_error(get_from(&module, &format!("http://example.com{path}"))); + assert_eq!(broken("/short"), "The response ended early"); + assert_eq!(broken("/short-chunk"), "The response ended early"); + assert_eq!(broken("/bad-chunk"), "The server sent an invalid response"); + assert_eq!(broken("/two-lengths"), "The server sent an invalid response"); + assert_eq!(broken("/negative"), "The server sent an invalid response"); + assert_eq!(broken("/garbage"), "The server sent an invalid response"); + assert_eq!(broken("/silent"), "The connection closed before a response arrived"); + assert_eq!(broken("/huge-head"), "The response headers are too large"); +} + +#[test] +fn bodies_over_the_limit_are_cut_off_on_the_wire() { + let stopped = Arc::new(AtomicBool::new(false)); + let flag = stopped.clone(); + let server = serve(None, move |request, output| match request.path() { + "/endless" => { + let _ = output.write_all(b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); + let chunk = [b"10000\r\n".as_slice(), &vec![0u8; 0x10000], b"\r\n"].concat(); + // The client hangs up at the limit; the server notices on a later write. + for _ in 0..4096 { + if output.write_all(&chunk).is_err() { + flag.store(true, Ordering::SeqCst); + return; + } + } + } + "/bomb" => { + let _ = output.write_all(&respond("200 OK", &[("Content-Encoding", "gzip")], &gzip(&vec![0u8; 16 * 1024 * 1024]))); + } + "/declared" => { + let _ = output.write_all(format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", fetch::MAXIMUM_RESPONSE_BYTES + 1).as_bytes()); + } + _ => { + let _ = output.write_all(&respond("200 OK", &[], &vec![b'x'; fetch::MAXIMUM_RESPONSE_BYTES])); + } + }); + let module = module(&Routed::to(server.port)); + let limit = "Response exceeds the 2097152-byte limit"; + assert_eq!(failure(get_from(&module, "http://example.com/endless")), limit); + // A small compressed body that expands past the limit is stopped while it is decoded. + assert_eq!(failure(get_from(&module, "http://example.com/bomb")), limit); + assert_eq!(failure(get_from(&module, "http://example.com/declared")), limit); + assert_eq!(get_from(&module, "http://example.com/exact").unwrap().body.len(), fetch::MAXIMUM_RESPONSE_BYTES); + let deadline = Instant::now() + Duration::from_secs(10); + while !stopped.load(Ordering::SeqCst) && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(20)); + } + assert!(stopped.load(Ordering::SeqCst), "the endless response was not abandoned"); +} + +#[test] +fn slow_servers_time_out_and_cancellation_hangs_up() { + let server = serve(None, |request, output| match request.path() { + "/stalled-body" => { + let _ = output.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\npartial"); + let _ = output.flush(); + std::thread::sleep(Duration::from_secs(4)); + } + "/trickle" => { + // Each read arrives in time, but the whole exchange may not outlast the total deadline. + let _ = output.write_all(b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); + for _ in 0..200 { + if output.write_all(b"1\r\nx\r\n").and_then(|()| output.flush()).is_err() { + return; + } + std::thread::sleep(Duration::from_millis(100)); + } + } + _ => std::thread::sleep(Duration::from_secs(4)), + }); + let module = module(&Routed::to(server.port)); + let timed = |path: &str, timeout: u64| { + let started = Instant::now(); + let result = module.perform("GET", &url(&format!("http://example.com{path}")), &[], None, timeout, &any_redirect, None); + (failure(result), started.elapsed()) + }; + let (message, elapsed) = timed("/no-answer", 300); + assert_eq!(message, "The request timed out."); + assert!(elapsed < Duration::from_secs(3), "{elapsed:?}"); + let (message, elapsed) = timed("/stalled-body", 300); + assert_eq!(message, "The request timed out."); + assert!(elapsed < Duration::from_secs(3), "{elapsed:?}"); + let (message, elapsed) = timed("/trickle", 2000); + assert_eq!(message, "The request timed out."); + assert!(elapsed >= Duration::from_millis(fetch::MAXIMUM_TOTAL_MILLISECONDS - 500) && elapsed < Duration::from_secs(15), "{elapsed:?}"); + + // The end of the execution stops a request that is waiting for its answer. + let ended = Arc::new(AtomicBool::new(false)); + let flag = ended.clone(); + std::thread::spawn(move || { + std::thread::sleep(Duration::from_millis(200)); + flag.store(true, Ordering::SeqCst); + }); + let started = Instant::now(); + let flag = ended.clone(); + let result = module.perform("GET", &url("http://example.com/no-answer"), &[], None, 5000, &any_redirect, Some(Arc::new(move || flag.load(Ordering::SeqCst)))); + assert_eq!(result, Err(FetchError::Cancelled)); + assert!(started.elapsed() < Duration::from_secs(2), "{:?}", started.elapsed()); +} + +/// Redirects are followed by this code, hop by hop, never by the transport. +#[test] +fn redirects_are_checked_at_every_hop_and_credentials_stay_with_their_origin() { + let port = Arc::new(AtomicUsize::new(0)); + let own_port = port.clone(); + let server = serve(None, move |request, output| { + let host = request.header("Host").unwrap_or("").to_owned(); + let redirect = |to: &str| respond("302 Found", &[("Location", to), ("Set-Cookie", "tracker=1")], b""); + let reply = match (host.as_str(), request.path()) { + ("example.com", "/start") => redirect("/second"), + ("example.com", "/second") => redirect("http://other.example.net/third"), + ("other.example.net", "/third") => respond("200 OK", &[], b"arrived"), + ("example.com", "/to-this-machine") => redirect(&format!("http://127.0.0.1:{}/secret", own_port.load(Ordering::SeqCst))), + ("example.com", "/to-localhost") => redirect(&format!("http://localhost:{}/secret", own_port.load(Ordering::SeqCst))), + ("example.com", "/to-private-name") => redirect("http://internal.example.com/secret"), + ("example.com", "/to-file") => redirect("file:///etc/passwd"), + _ => respond("200 OK", &[], b"secret"), + }; + let _ = output.write_all(&reply); + }); + port.store(server.port as usize, Ordering::SeqCst); + let routed = Routed::to(server.port); + let module = module(&routed); + let sent = headers(&[("Authorization", "Bearer secret"), ("Cookie", "session=1"), ("Accept", "application/json")]); + + // The transport alone reports the redirect; it does not follow it. + let transport = HttpTransport::with_parts(fixture_resolver(), routed.clone(), None); + let wait = Wait::new(Duration::from_secs(10), Duration::from_secs(5), None); + wait.arm().unwrap(); + let address = url("http://example.com/start"); + let single = sw_core::api::fetch::Transport::send(&transport, &sw_core::api::fetch::TransportRequest { method: "GET", url: &address, headers: &[], body: None }, &wait).unwrap(); + assert_eq!(single.status, 302); + assert_eq!(server.seen().len(), 1); + + let response = module.perform("GET", &address, &sent, None, 5000, &any_redirect, None).unwrap(); + assert_eq!(response.body, b"arrived"); + let seen = server.seen(); + let hops: Vec = seen[1..].iter().map(|request| format!("{}{}", request.header("Host").unwrap_or(""), request.path())).collect(); + assert_eq!(hops, ["example.com/start", "example.com/second", "other.example.net/third"]); + assert_eq!(seen[2].header("Authorization"), Some("Bearer secret")); + assert_eq!(seen[2].header("Cookie"), Some("session=1"), "the script's own cookie, not the one the server set"); + let elsewhere = &seen[3]; + assert_eq!((elsewhere.header("Authorization"), elsewhere.header("Cookie"), elsewhere.header("Accept")), (None, None, Some("application/json"))); + + // The script's rule, the fetch policy of a package, sees every target; local ones never get a connection. + let before = server.seen().len(); + let dialed = routed.dialed().len(); + let package = Package::legacy(); + for path in ["/to-this-machine", "/to-localhost", "/to-file"] { + assert_eq!(rejection(&module, &package.request(), "http.request", &get(&format!("http://example.com{path}"))), "Redirect target is not allowed", "{path}"); + } + assert_eq!(rejection(&module, &package.request(), "http.request", &get("http://example.com/to-private-name")), BLOCKED); + assert_eq!(server.seen().len(), before + 4, "only the four first hops were sent"); + assert_eq!(routed.dialed().len(), dialed + 4); + assert!(server.seen().iter().all(|request| request.path() != "/secret")); + // Even a caller that allows every redirect cannot reach a local address: the hop is checked again. + assert_eq!(failure(get_from(&module, "http://example.com/to-this-machine")), BLOCKED); + assert_eq!(failure(get_from(&module, "http://example.com/to-private-name")), BLOCKED); + assert!(server.seen().iter().all(|request| request.path() != "/secret")); +} + +// A certificate for secure.example.com that signs itself, made for these tests +// only (valid until 2126). The key protects nothing. +const CERTIFICATE: &str = "-----BEGIN CERTIFICATE----- +MIIBhzCCAS2gAwIBAgIJAOotGN6ePOf3MAoGCCqGSM49BAMCMB0xGzAZBgNVBAMM +EnNlY3VyZS5leGFtcGxlLmNvbTAgFw0yNjExMTgwNDE0NDNaGA8yMTI2MTAyNTA0 +MTQ0M1owHTEbMBkGA1UEAwwSc2VjdXJlLmV4YW1wbGUuY29tMFkwEwYHKoZIzj0C +AQYIKoZIzj0DAQcDQgAEUWTZstAJrWCtM8bb6oUDS/opdmumxe0NrEG5pvQ+IiJ4 +Wl5sk/uESXV+WPy98bnqWcvcpDkI8tIU4uAxNMqxm6NUMFIwHQYDVR0RBBYwFIIS +c2VjdXJlLmV4YW1wbGUuY29tMAwGA1UdEwEB/wQCMAAwDgYDVR0PAQH/BAQDAgeA +MBMGA1UdJQQMMAoGCCsGAQUFBwMBMAoGCCqGSM49BAMCA0gAMEUCIFjz65KVKyvh +UgjoRKWCEKSlKF6eJfB4Kc+irvbEdG4KAiEAkeLb1l+o2fK7h128sV+nYvhXOmfs +ooC8P7eLdL2emyg= +-----END CERTIFICATE----- +"; +const TEST_ONLY_KEY: &str = "-----BEGIN PRIVATE KEY----- +MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg7499otSzGQP1kRG/ +ieeAblKYaw4QFwFxAntSehzjmKOhRANCAARRZNmy0AmtYK0zxtvqhQNL+il2a6bF +7Q2sQbmm9D4iInhaXmyT+4RJdX5Y/L3xuepZy9ykOQjy0hTi4DE0yrGb +-----END PRIVATE KEY----- +"; + +fn tls_server_config() -> Arc { + let certificate = CertificateDer::from_pem_slice(CERTIFICATE.as_bytes()).expect("certificate"); + let key = PrivateKeyDer::from_pem_slice(TEST_ONLY_KEY.as_bytes()).expect("key"); + let config = rustls::ServerConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider())) + .with_safe_default_protocol_versions() + .unwrap() + .with_no_client_auth() + .with_single_cert(vec![certificate], key) + .expect("server configuration"); + Arc::new(config) +} + +/// The connection goes to an address, but the certificate must be valid for the name in the URL. +#[test] +fn tls_is_verified_against_the_host_name() { + let server = serve(Some(tls_server_config()), |request, output| { + let _ = output.write_all(&respond("200 OK", &[("Content-Type", "text/plain")], format!("secure {}", request.path()).as_bytes())); + }); + let routed = Routed::to(server.port); + let mut roots = rustls::RootCertStore::empty(); + roots.add(CertificateDer::from_pem_slice(CERTIFICATE.as_bytes()).unwrap()).unwrap(); + let trusting = FetchModule::with_parts(fixture_resolver(), Arc::new(HttpTransport::with_parts(fixture_resolver(), routed.clone(), Some(client_config(roots).unwrap())))); + + let response = trusting.perform("POST", &url("https://secure.example.com/data"), &headers(&[("X-Token", "t")]), Some(b"{}"), 5000, &any_redirect, None).unwrap(); + assert_eq!((response.status, response.body.as_slice()), (200, &b"secure /data"[..])); + assert_eq!(routed.dialed(), [format!("{PUBLIC}:443")]); + let seen = server.seen().remove(0); + assert_eq!((seen.line(), seen.header("Host"), seen.header("X-Token"), seen.body.as_slice()), ("POST /data HTTP/1.1", Some("secure.example.com"), Some("t"), &b"{}"[..])); + + // The same server, the same address, another name: the certificate does not cover it. + let other = transport_error(get_from(&trusting, "https://other.example.com/data")); + assert!(other.to_lowercase().contains("certificate"), "{other}"); + // The address itself is not a name the certificate covers either. + let literal = transport_error(get_from(&trusting, "https://93.184.216.34/data")); + assert!(literal.to_lowercase().contains("certificate"), "{literal}"); + // With the certificate authorities of the system the test certificate is not trusted at all. + let system = module(&routed); + let untrusted = transport_error(get_from(&system, "https://secure.example.com/data")); + assert!(untrusted.to_lowercase().contains("certificate"), "{untrusted}"); + // A server that does not speak TLS is a failure, not a downgrade. + let plain = serve(None, |_, output| { + let _ = output.write_all(&respond("200 OK", &[], b"plain")); + }); + let downgraded = FetchModule::with_parts(fixture_resolver(), Arc::new(HttpTransport::with_parts(fixture_resolver(), Routed::to(plain.port), None))); + assert!(downgraded.perform("GET", &url("https://secure.example.com/data"), &[], None, 500, &any_redirect, None).is_err()); + assert_eq!(server.seen().len(), 1, "no request was sent over a connection that failed verification"); + assert!(plain.seen().is_empty()); +} diff --git a/Linux/crates/sw-core/tests/api_proxy.rs b/Linux/crates/sw-core/tests/api_proxy.rs new file mode 100644 index 00000000..0166637b --- /dev/null +++ b/Linux/crates/sw-core/tests/api_proxy.rs @@ -0,0 +1,74 @@ +//! The transport does not use a proxy, whatever the environment says. This is +//! a file of its own because it changes the environment of the process. + +mod api_common; + +use std::io::{Read, Write}; +use std::net::{SocketAddr, TcpListener, TcpStream}; +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::time::Duration; + +use api_common::*; +use sw_core::api::fetch::FetchModule; +use sw_core::api::http::{Dialer, HttpTransport}; + +/// Accepts connections, counts them, and answers each with `reply` after reading the request. +fn listen(reply: &'static [u8]) -> (u16, Arc, Arc>>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + let (count, lines): (Arc, Arc>>) = Default::default(); + let (counter, record) = (count.clone(), lines.clone()); + std::thread::spawn(move || { + for mut socket in listener.incoming().flatten() { + counter.fetch_add(1, Ordering::SeqCst); + let _ = socket.set_read_timeout(Some(Duration::from_secs(5))); + let mut data = Vec::new(); + let mut chunk = [0u8; 1024]; + while !data.windows(4).any(|window| window == b"\r\n\r\n") { + match socket.read(&mut chunk) { + Ok(count) if count > 0 => data.extend_from_slice(&chunk[..count]), + _ => break, + } + } + record.lock().unwrap().push(String::from_utf8_lossy(&data).lines().next().unwrap_or("").to_owned()); + let _ = socket.write_all(reply); + } + }); + (port, count, lines) +} + +struct ToPort(u16); + +impl Dialer for ToPort { + fn dial(&self, _: SocketAddr, timeout: Duration) -> std::io::Result { + TcpStream::connect_timeout(&SocketAddr::from(([127, 0, 0, 1], self.0)), timeout) + } +} + +#[test] +fn proxy_settings_of_the_environment_are_not_used() { + let (origin, origin_connections, origin_lines) = listen(b"HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\norigin"); + let (proxy, proxy_connections, _) = listen(b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nproxy"); + let address = format!("http://127.0.0.1:{proxy}"); + // SAFETY: this is the only test of this program, and nothing else is running yet. + unsafe { + for name in ["http_proxy", "https_proxy", "all_proxy", "HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY"] { + std::env::set_var(name, &address); + } + std::env::remove_var("no_proxy"); + std::env::remove_var("NO_PROXY"); + } + let module = FetchModule::with_parts(public_resolver(), Arc::new(HttpTransport::with_parts(public_resolver(), Arc::new(ToPort(origin)), None))); + let response = module.perform("GET", &url("http://example.com/direct?x=1"), &[], None, 5000, &any_redirect, None).unwrap(); + assert_eq!(response.body, b"origin"); + assert_eq!(origin_connections.load(Ordering::SeqCst), 1); + // The request line names a path, as for an origin server, not a whole URL, as for a proxy. + assert_eq!(*origin_lines.lock().unwrap(), ["GET /direct?x=1 HTTP/1.1"]); + assert_eq!(proxy_connections.load(Ordering::SeqCst), 0, "the proxy was not contacted"); + + // The production module does not reach the proxy either: it is a local address like any other. + let production = FetchModule::new(); + assert_eq!(failure(production.perform("GET", &url(&address), &[], None, 2000, &any_redirect, None)), BLOCKED); + assert_eq!(proxy_connections.load(Ordering::SeqCst), 0); +} diff --git a/Linux/crates/sw-core/tests/api_system.rs b/Linux/crates/sw-core/tests/api_system.rs new file mode 100644 index 00000000..a9e2f423 --- /dev/null +++ b/Linux/crates/sw-core/tests/api_system.rs @@ -0,0 +1,275 @@ +//! `$device`, `$system`, `$xml` and the globals Linux does not implement. +//! The device and system snapshots are built from a made-up `/etc`, `/proc` +//! and `/sys`, and once from the machine the test runs on. + +mod api_common; + +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use api_common::*; +use serde_json::{Value, json}; +use sw_core::api::system::{self, Screen}; +use sw_core::api::{FetchModule, WeatherModule}; +use sw_core::plugins::{DataSourceModule, PluginStore}; +use sw_core::{ExecutionRequest, NativeModule}; + +fn write(root: &Path, relative: &str, text: &str) { + let path = root.join(relative); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(path, text).unwrap(); +} + +/// A laptop running Omarchy, as its files describe it. +fn laptop() -> PathBuf { + let root = temporary_directory("system-root"); + write(&root, "etc/os-release", "NAME=\"Arch Linux\"\nPRETTY_NAME=\"Omarchy \\\"Quattro\\\"\"\nID=arch\n# comment\nBUILD_ID=rolling\nANSI_COLOR='38;2;23;147;209'\n\nBROKEN LINE\n"); + write(&root, "proc/sys/kernel/hostname", "framework\n"); + write(&root, "proc/sys/kernel/osrelease", "6.17.7-arch1-1\n"); + write(&root, "proc/uptime", "12345.67 98765.43\n"); + write(&root, "proc/meminfo", "MemTotal: 16303428 kB\nMemFree: 1234567 kB\n"); + write(&root, "sys/class/power_supply/AC/type", "Mains\n"); + write(&root, "sys/class/power_supply/BAT1/type", "Battery\n"); + write(&root, "sys/class/power_supply/BAT1/capacity", "73\n"); + write(&root, "sys/class/power_supply/BAT1/status", "Discharging\n"); + // A wireless mouse reports a battery too; it is not the machine's. + write(&root, "sys/class/power_supply/hidpp_battery_0/type", "Battery\n"); + write(&root, "sys/class/power_supply/hidpp_battery_0/scope", "Device\n"); + write(&root, "sys/class/power_supply/hidpp_battery_0/capacity", "5\n"); + write(&root, "sys/class/power_supply/hidpp_battery_0/status", "Charging\n"); + write(&root, "sys/firmware/acpi/platform_profile", "low-power\n"); + root +} + +#[test] +fn the_device_is_described_by_files_anyone_may_read() { + let root = laptop(); + let device = Value::Object(system::device_from(&root, Screen { width: 1280.0, height: 800.0, scale: 2.0 })); + assert_eq!(device.as_object().unwrap().keys().map(String::as_str).collect::>(), ["screen", "language", "model", "name", "battery", "systemVersion", "totalDiskSpace", "freeDiskSpace"]); + assert_eq!(device["screen"], json!({ "width": 1280.0, "height": 800.0, "scale": 2.0 })); + assert_eq!(device["model"], "PC"); + assert_eq!(device["name"], "framework"); + assert_eq!(device["battery"], json!({ "level": 0.73, "state": "unplugged" })); + // A rolling release has no version: the kernel release stands in. + assert_eq!(device["systemVersion"], "6.17.7-arch1-1"); + assert_eq!(device["language"].as_str().map(str::len), Some(2)); + let (total, free) = (device["totalDiskSpace"].as_u64().unwrap(), device["freeDiskSpace"].as_u64().unwrap()); + assert!(total > 0 && free <= total, "{total} {free}"); + + let battery = |status: &str, capacity: &str| { + write(&root, "sys/class/power_supply/BAT1/status", status); + write(&root, "sys/class/power_supply/BAT1/capacity", capacity); + Value::Object(system::device_from(&root, Screen::default()))["battery"].clone() + }; + assert_eq!(battery("Charging\n", "40\n"), json!({ "level": 0.4, "state": "charging" })); + assert_eq!(battery("Full\n", "100\n"), json!({ "level": 1.0, "state": "full" })); + assert_eq!(battery("Not charging\n", "80\n"), json!({ "level": 0.8, "state": "charging" })); + assert_eq!(battery("Not charging\n", "100\n"), json!({ "level": 1.0, "state": "full" })); + assert_eq!(battery("Unknown\n", "55\n"), json!({ "level": 0.55, "state": "unknown" })); + assert_eq!(battery("Charging\n", "garbage\n"), json!({ "level": -1.0, "state": "charging" })); + assert_eq!(battery("Charging\n", "250\n"), json!({ "level": -1.0, "state": "charging" })); + // A desktop: no battery of its own, so level 1 and "full", as on Windows. + std::fs::remove_dir_all(root.join("sys/class/power_supply/BAT1")).unwrap(); + assert_eq!(Value::Object(system::device_from(&root, Screen::default()))["battery"], json!({ "level": 1.0, "state": "full" })); + // A release with a version reports it. + write(&root, "etc/os-release", "NAME=Fedora\nVERSION_ID=43\nPRETTY_NAME=\"Fedora Linux 43 (Workstation Edition)\"\n"); + assert_eq!(Value::Object(system::device_from(&root, Screen::default()))["systemVersion"], "43"); + let _ = std::fs::remove_dir_all(root); +} + +#[test] +fn the_system_is_described_with_the_fixed_values_of_the_other_desktops() { + let root = laptop(); + let info = Value::Object(system::system_from(&root)); + assert_eq!( + info.as_object().unwrap().keys().map(String::as_str).collect::>(), + [ + "appInfo", "locale", "preferredLanguages", "timeZone", "is24HourClock", "calendarInfo", "systemUptime", "memory", "thermalState", "lowPowerMode", "brightness", "reduceMotionEnabled", "platform", "hostName", + "processName", "osVersionString", "processorCount", "activeProcessorCount" + ] + ); + assert_eq!(info["appInfo"], json!({ "name": "ScriptWidget", "bundleId": "com.atomsprout.scriptwidget", "version": env!("CARGO_PKG_VERSION"), "build": "0" })); + assert_eq!(info["platform"], "linux"); + assert_eq!(info["hostName"], "framework"); + assert_eq!(info["processName"], "ScriptWidget"); + assert_eq!(info["osVersionString"], "Omarchy \"Quattro\" (Linux 6.17.7-arch1-1)"); + assert_eq!(info["systemUptime"], 12345.67); + assert_eq!(info["memory"], json!({ "physical": 16_303_428u64 * 1024 })); + assert_eq!((&info["thermalState"], &info["brightness"], &info["reduceMotionEnabled"]), (&json!("nominal"), &json!(1.0), &json!(false))); + assert_eq!(info["lowPowerMode"], true); + assert!(info["processorCount"].as_u64().unwrap() >= info["activeProcessorCount"].as_u64().unwrap() && info["activeProcessorCount"].as_u64().unwrap() >= 1); + assert_eq!(info["calendarInfo"]["identifier"], "gregorian"); + assert!((1..=7).contains(&info["calendarInfo"]["firstWeekday"].as_u64().unwrap())); + assert!([1, 4].contains(&info["calendarInfo"]["minimumDaysInFirstWeek"].as_u64().unwrap())); + assert!(info["is24HourClock"].is_boolean()); + // The locale is written like on iOS, the languages like BCP 47. + let locale = info["locale"].as_str().unwrap(); + assert!(!locale.is_empty() && !locale.contains('-') && !locale.contains('.'), "{locale}"); + assert!(info["preferredLanguages"].as_array().is_some_and(|languages| !languages.is_empty() && languages.iter().all(|language| language.as_str().is_some_and(|tag| !tag.contains('_'))))); + // The zone, its offset and the offset's name agree with each other. + let zone = &info["timeZone"]; + assert!(!zone["identifier"].as_str().unwrap().is_empty()); + let offset = zone["offsetSeconds"].as_i64().unwrap(); + assert!((-14 * 3600..=14 * 3600).contains(&offset)); + let hours = offset.abs() / 3600; + assert!(zone["abbreviation"].as_str().unwrap().starts_with(&format!("GMT{}{hours}", if offset < 0 { '-' } else { '+' })), "{zone}"); + + // Another profile, or none at all, is not low power. + write(&root, "sys/firmware/acpi/platform_profile", "balanced\n"); + assert_eq!(system::system_from(&root)["lowPowerMode"], false); + std::fs::remove_file(root.join("sys/firmware/acpi/platform_profile")).unwrap(); + assert_eq!(system::system_from(&root)["lowPowerMode"], false); + let _ = std::fs::remove_dir_all(root); +} + +/// The clock style, the week and the languages follow the locale; ICU answers the first. +#[test] +fn the_locale_decides_the_clock_and_the_week() { + let root = temporary_directory("system-locale"); + let expected = [("de-DE", true, 2, 4, "de_DE"), ("en-US", false, 1, 1, "en_US"), ("zh-CN", true, 2, 1, "zh_CN"), ("ar-EG", false, 7, 1, "ar_EG"), ("fa-IR", true, 7, 1, "fa_IR")]; + for (tag, twenty_four, first_weekday, minimum_days, locale) in expected { + sw_core::intl::set_locale_override(Some(tag)); + let info = system::system_from(&root); + let device = system::device_from(&root, Screen::default()); + sw_core::intl::set_locale_override(None); + assert_eq!(info["is24HourClock"], twenty_four, "{tag}"); + assert_eq!(info["calendarInfo"], json!({ "identifier": "gregorian", "firstWeekday": first_weekday, "minimumDaysInFirstWeek": minimum_days }), "{tag}"); + assert_eq!(info["locale"], locale); + assert_eq!(device["language"], tag[..2]); + } + let _ = std::fs::remove_dir_all(root); +} + +/// Nothing is assumed about the machine: a system without any of the files still answers. +#[test] +fn missing_files_leave_plain_defaults() { + let root = temporary_directory("system-empty"); + let device = system::device_from(&root, Screen::default()); + assert_eq!(device["screen"], json!({ "width": 1920.0, "height": 1080.0, "scale": 1.0 })); + assert_eq!(device["battery"], json!({ "level": 1.0, "state": "full" })); + assert_eq!(device["systemVersion"], ""); + assert!(!device["name"].as_str().unwrap().is_empty()); + let info = system::system_from(&root); + assert_eq!(info["osVersionString"], "Linux (Linux )"); + assert_eq!((info["systemUptime"].as_f64(), info["memory"]["physical"].as_u64()), (Some(0.0), Some(0))); + // A file that is too large to be what it claims is not read. + write(&root, "etc/os-release", &format!("PRETTY_NAME=\"{}\"\n", "x".repeat(70_000))); + assert_eq!(system::system_from(&root)["osVersionString"], "Linux (Linux )"); + let _ = std::fs::remove_dir_all(root); +} + +fn configured(request: &mut ExecutionRequest<'_>) { + request.device = Some(system::device_info(Screen { width: 800.0, height: 600.0, scale: 1.5 })); + request.system = Some(system::system_info()); +} + +#[test] +fn scripts_read_this_machine_through_device_and_system() { + let script = r#" + const battery = $device.battery(), zone = $system.timeZone(), screen = $device.screen(); + $render({JSON.stringify({ + platform: $system.platform(), model: $device.model(), sameName: $device.name() === $system.hostName() && $device.name().length > 0, + screen: [screen.width, screen.height, screen.scale], battery: typeof battery.level === "number" && ["charging", "unplugged", "full", "unknown"].includes(battery.state), + disk: $device.totalDiskSpace() >= $device.freeDiskSpace() && $device.totalDiskSpace() > 0, memory: $system.memory().physical > 64 * 1024 * 1024, + brightness: $system.brightness(), thermal: $system.thermalState(), app: $system.appInfo().bundleId, process: $system.processName(), + zone: zone.identifier === Intl.DateTimeFormat().resolvedOptions().timeZone, offset: zone.offsetSeconds === -new Date().getTimezoneOffset() * 60, + uptime: $system.systemUptime() > 0, cores: $system.activeProcessorCount() >= 1, os: $system.osVersionString().includes("(Linux "), + })}) + "#; + let result = run(script, None, Vec::new(), &[], configured); + assert!(result.error.is_none(), "{:?}", result.error); + let tree: Value = serde_json::from_str(result.tree.as_deref().unwrap()).unwrap(); + let values: Value = serde_json::from_str(tree["children"][0].as_str().unwrap()).unwrap(); + let mut expected = json!({ + "platform": "linux", "model": "PC", "sameName": true, "screen": [800, 600, 1.5], "battery": true, "disk": true, "memory": true, "brightness": 1, "thermal": "nominal", + "app": "com.atomsprout.scriptwidget", "process": "ScriptWidget", "zone": true, "offset": true, "uptime": true, "cores": true, "os": true, + }); + if !cfg!(target_os = "linux") { + // The quick loop on a Mac has no /proc: these three are read on Linux only. + for name in ["memory", "uptime", "os"] { + expected[name] = values[name].clone(); + } + } + assert_eq!(values, expected); +} + +#[test] +fn scripts_parse_xml_with_the_bounded_parser() { + let script = r#" + const feed = $xml.parse('News & more'); + const channel = feed.children[0]; + let refused = []; + for (const source of [']>&x;', '&undeclared;', '', '' + 'x'.repeat(300000) + '']) { + try { $xml.parse(source); refused.push("parsed"); } catch (error) { refused.push(String(error.message)); } + } + $render({JSON.stringify([feed.name, feed.attributes.version, channel.children[0].text, channel.children.map(c => c.name + (c.attributes.id ?? "")).join(","), refused])}) + "#; + let result = run(script, None, Vec::new(), &[], |_| {}); + assert!(result.error.is_none(), "{:?}", result.error); + let tree: Value = serde_json::from_str(result.tree.as_deref().unwrap()).unwrap(); + let values: Value = serde_json::from_str(tree["children"][0].as_str().unwrap()).unwrap(); + assert_eq!( + values, + json!([ + "rss", "2.0", "News & more", "title,item1,item2", + ["XML document type and entity declarations are not allowed", "XML entity references are not allowed", "XML element a is not closed", "XML exceeds the 262144-byte limit"] + ]) + ); +} + +/// Globals without a module here answer for themselves: not available, and a call says so. +#[test] +fn globals_linux_does_not_implement_are_unavailable_and_say_so() { + let script = r#" + const sync = ["$health", "$reminders", "$music", "$photos", "$calendar", "$location"].map(name => globalThis[name].isAvailable()); + const calls = [ + () => $calendar.events({}), () => $reminders.lists(), () => $health.stepCountToday(), () => $photos.items({}), () => $contacts.selected(), () => $documents.list(), + () => $home.items(), () => $icloud.status(), () => $alarms.pending(), () => $maps.search({}), () => $services.status(), () => $music.current(), () => $notifications.pending(), + () => $location.current(), + ]; + const messages = new Array(calls.length); + let left = calls.length; + const settle = (index, message) => { messages[index] = message; if (--left === 0) $render({JSON.stringify({ sync, messages })}); }; + calls.forEach((call, index) => call().then(() => settle(index, "resolved"), error => settle(index, String(error)))); + "#; + let directory = temporary_directory("unsupported"); + let fetch = Arc::new(FetchModule::with_parts(public_resolver(), FakeTransport::ok())); + let modules: Vec> = vec![ + fetch.clone(), + Arc::new(DataSourceModule::new(Arc::new(PluginStore::new(directory.join("Plugins"))))), + Arc::new(WeatherModule::new(Arc::new(sw_core::api::weather::FetchWeatherTransport::new(fetch)), directory.join("weather"), WeatherModule::user_agent("1.0"))), + ]; + let result = run(script, None, modules.clone(), &[], |_| {}); + assert!(result.error.is_none(), "{:?}", result.error); + let tree: Value = serde_json::from_str(result.tree.as_deref().unwrap()).unwrap(); + let values: Value = serde_json::from_str(tree["children"][0].as_str().unwrap()).unwrap(); + assert_eq!(values["sync"], json!([false, false, false, false, false, false])); + let names = ["$calendar", "$reminders", "$health", "$photos", "$contacts", "$documents", "$home", "$icloud", "$alarms", "$maps", "$services", "$music", "$notifications", "$location"]; + let messages = values["messages"].as_array().unwrap_or_else(|| panic!("{values}")); + assert_eq!(messages.len(), names.len()); + for (name, message) in names.iter().zip(messages) { + let message = message.as_str().unwrap_or_else(|| panic!("{values}")); + assert!(message.contains("[unsupported_platform]") && message.contains(name) && message.contains("is not supported on Linux"), "{name}: {message}"); + } + + // The globals that do have a module are not reported as unsupported: they apply their own rules. + let supported = r#" + const messages = [null, null]; + let left = 2; + const settle = (index, message) => { messages[index] = message; if (--left === 0) $render({JSON.stringify(messages)}); }; + [$weather.forecast({ latitude: 1, longitude: 2 }), $dataSource.request("app.scriptwidget.datasource.github", "repository", {})] + .forEach((call, index) => call.then(() => settle(index, "resolved"), error => settle(index, String(error)))); + "#; + let result = run(supported, None, modules, &[], |_| {}); + let tree: Value = serde_json::from_str(result.tree.as_deref().unwrap()).unwrap(); + let messages: Vec = serde_json::from_str(tree["children"][0].as_str().unwrap()).unwrap(); + assert!(messages[0].contains("[permission_denied]"), "{messages:?}"); + assert!(messages[1].contains("is not declared in widget.json"), "{messages:?}"); + assert!(messages.iter().all(|message| !message.contains("unsupported_platform")), "{messages:?}"); + // Without the modules the same two globals are unsupported as well. + let result = run(supported, None, Vec::new(), &[], |_| {}); + let tree: Value = serde_json::from_str(result.tree.as_deref().unwrap()).unwrap(); + let messages: Vec = serde_json::from_str(tree["children"][0].as_str().unwrap()).unwrap(); + assert!(messages.iter().all(|message| message.contains("[unsupported_platform]")), "{messages:?}"); + let _ = std::fs::remove_dir_all(directory); +} diff --git a/Linux/crates/sw-core/tests/api_weather.rs b/Linux/crates/sw-core/tests/api_weather.rs new file mode 100644 index 00000000..d2cdd6ad --- /dev/null +++ b/Linux/crates/sw-core/tests/api_weather.rs @@ -0,0 +1,546 @@ +//! `$weather`: options, provider normalisation, the cache and the policy +//! order. Ported from +//! `Windows/tests/ScriptWidget.Core.Tests/Api/WeatherModuleTests.cs`. + +mod api_common; + +use std::path::PathBuf; +use std::sync::atomic::{AtomicI64, AtomicU32, Ordering}; +use std::sync::{Arc, Mutex}; + +use api_common::*; +use serde_json::{Value, json}; +use sw_core::NativeModule; +use sw_core::api::fetch::{Cancel, FetchModule, Url}; +use sw_core::api::support::{CallContext, SystemReject, canonical, iso_dates, system_log}; +use sw_core::api::weather::{FetchWeatherTransport, WeatherModule, WeatherOptions, WeatherResponse, WeatherTransport, WeatherTransportError, weather_rules}; +use sw_core::json::{Object, stringify_object}; + +const OWNER: &str = "11111111-1111-4111-8111-111111111111"; +const OTHER_OWNER: &str = "22222222-2222-4222-8222-222222222222"; +const DECLARED: &[&str] = &["weather", "network"]; +const DOMAINS: &[&str] = &["api.met.no"]; + +/// A recorded response for lat=59.91&lon=10.75: 85 entries from +/// 2026-11-13T07:00:00Z, hourly for 53 hours and without `next_1_hours` after that. +fn recorded() -> Vec { + std::fs::read(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/api_fixtures/met-norway-compact.json")).expect("fixture") +} + +#[derive(Default)] +struct State { + status: u16, + body: Vec, + urls: Vec, + agents: Vec>, + timeout: u64, + fail: bool, + redirect: Option, + redirect_allowed: Option, +} + +struct FakeWeather(Mutex); + +impl WeatherTransport for FakeWeather { + fn get(&self, url: &str, headers: &[(String, String)], timeout_milliseconds: u64, allow_redirect: &dyn Fn(&Url) -> bool, _: Option) -> Result { + let mut state = self.0.lock().unwrap(); + state.urls.push(url.to_owned()); + state.agents.push(headers.iter().find(|(name, _)| name == "User-Agent").map(|(_, value)| value.clone())); + state.timeout = timeout_milliseconds; + if let Some(target) = state.redirect.clone() { + state.redirect_allowed = Some(allow_redirect(&target)); + } + if state.fail { + return Err(WeatherTransportError); + } + Ok(WeatherResponse { status: state.status, body: state.body.clone() }) + } +} + +struct Fixture { + now: Arc, + directory: PathBuf, + transport: Arc, + module: WeatherModule, +} + +impl Fixture { + fn new() -> Self { + let now = Arc::new(AtomicI64::new(iso_dates::parse("2026-11-13T07:30:00Z").unwrap())); + let directory = temporary_directory("weather"); + let transport = Arc::new(FakeWeather(Mutex::new(State { status: 200, body: recorded(), ..State::default() }))); + let clock = now.clone(); + let module = WeatherModule::with_clock(transport.clone(), &directory, WeatherModule::user_agent("1.1.0"), Arc::new(move || clock.load(Ordering::SeqCst))); + Self { now, directory, transport, module } + } + + fn now(&self) -> i64 { + self.now.load(Ordering::SeqCst) + } + + fn advance(&self, milliseconds: i64) { + self.now.fetch_add(milliseconds, Ordering::SeqCst); + } + + fn state(&self) -> std::sync::MutexGuard<'_, State> { + self.transport.0.lock().unwrap() + } + + fn urls(&self) -> usize { + self.state().urls.len() + } +} + +impl Drop for Fixture { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.directory); + } +} + +/// A call context with adjustable budgets. +fn context(permissions: &[&str], domains: &[&str], installation: &str, queries: u32, network: u32) -> CallContext { + let (used_queries, used_network) = (AtomicU32::new(0), AtomicU32::new(0)); + CallContext::new(installation, true, permissions, domains, Some("1.6")).with_probes( + move || used_queries.fetch_add(1, Ordering::SeqCst) < queries, + move || used_network.fetch_add(1, Ordering::SeqCst) < network, + || true, + ) +} + +fn widget() -> CallContext { + context(DECLARED, DOMAINS, OWNER, 8, 32) +} + +fn options(value: Value) -> Object { + value.as_object().cloned().expect("options") +} + +fn oslo(hours: Option) -> Object { + let mut options = options(json!({ "latitude": 59.91, "longitude": 10.75 })); + if let Some(hours) = hours { + options.insert("hours".into(), json!(hours)); + } + options +} + +fn with(mut target: Object, name: &str, value: Value) -> Object { + target.insert(name.into(), value); + target +} + +fn rejects(code: &str, result: Result) { + match result { + Err(error) => assert_eq!(system_log::code(&error.0), code, "{}", error.0), + Ok(value) => panic!("expected [{code}], got {value:?}"), + } +} + +fn series(items: &[Value]) -> Vec { + json!({ "properties": { "timeseries": items } }).to_string().into_bytes() +} + +fn entry(time: &str, details: Value, next: Option) -> Value { + let mut data = json!({ "instant": { "details": details } }); + if let Some(next) = next { + data["next_1_hours"] = next; + } + json!({ "time": time, "data": data }) +} + +fn details() -> Value { + json!({ "air_temperature": 1.5, "wind_speed": 2.0 }) +} + +fn hours(result: &Object) -> &Vec { + result["hours"].as_array().expect("hours") +} + +fn keys(value: &Value) -> Vec<&str> { + value.as_object().unwrap().keys().map(String::as_str).collect() +} + +#[test] +fn option_validation_and_rounding() { + let parsed = WeatherOptions::parse(&options(json!({ "latitude": 37.3349, "longitude": -122.00902 }))).unwrap(); + assert_eq!((parsed.latitude, parsed.longitude, parsed.hours), (37.33, -122.01, 24)); + assert_eq!(parsed.url(), "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=37.33&lon=-122.01"); + // Halves round away from zero, and whole numbers keep one decimal in the URL. + assert_eq!(WeatherOptions::round2(0.125), 0.13); + assert_eq!(WeatherOptions::round2(-0.125), -0.13); + assert_eq!(WeatherOptions::parse(&options(json!({ "latitude": 60, "longitude": -180 }))).unwrap().url(), "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=60.0&lon=-180.0"); + assert_eq!(WeatherOptions::parse(&oslo(Some(48))).unwrap().hours, 48); + assert_eq!(WeatherOptions::parse(&oslo(Some(1))).unwrap().hours, 1); + let bad = [ + options(json!({})), + options(json!({ "latitude": 59.91 })), + options(json!({ "longitude": 10.75 })), + with(oslo(None), "latitude", json!(90.01)), + with(oslo(None), "latitude", json!(-90.01)), + with(oslo(None), "longitude", json!(180.5)), + with(oslo(None), "longitude", json!(-181)), + with(oslo(None), "latitude", json!("59.91")), + with(oslo(None), "latitude", json!(true)), + with(oslo(None), "latitude", Value::Null), + oslo(Some(0)), + oslo(Some(49)), + with(oslo(None), "hours", json!(1.5)), + with(oslo(None), "hours", json!("24")), + with(oslo(None), "hours", Value::Null), + with(oslo(None), "units", json!("imperial")), + options(json!({ "invalid": true })), + with(oslo(None), "padding", json!("x".repeat(17_000))), + ]; + for item in &bad { + rejects("invalid_options", WeatherOptions::parse(item)); + } +} + +#[test] +fn normalizes_a_recorded_met_norway_payload() { + let now = iso_dates::parse("2026-11-13T07:30:00Z").unwrap(); + let result = weather_rules::normalize(&recorded(), &WeatherOptions::parse(&oslo(None)).unwrap(), now).unwrap(); + assert_eq!(result.keys().map(String::as_str).collect::>(), ["provider", "attribution", "latitude", "longitude", "fetchedAt", "isStale", "hours"]); + assert_eq!(result["provider"], "MET Norway"); + assert_eq!(result["attribution"].to_string(), r#"{"text":"Weather data from MET Norway","url":"https://www.met.no/en/free-meteorological-data/Download-services"}"#); + assert_eq!((result["latitude"].as_f64(), result["longitude"].as_f64()), (Some(59.91), Some(10.75))); + assert_eq!(result["fetchedAt"], "2026-11-13T07:30:00Z"); + assert_eq!(result["isStale"], false); + assert_eq!(hours(&result).len(), 24); + let first = &hours(&result)[0]; + assert_eq!(keys(first), ["time", "temperatureC", "windMetersPerSecond", "precipitationMM", "condition"]); + assert_eq!(first["time"], "2026-11-13T07:00:00Z"); + assert_eq!((first["temperatureC"].as_f64(), first["windMetersPerSecond"].as_f64(), first["precipitationMM"].as_f64()), (Some(1.7), Some(3.0), Some(0.0))); + assert_eq!(first["condition"], "fair_day"); + assert_eq!(hours(&result)[23]["time"], "2026-11-14T06:00:00Z"); + + // Entries more than an hour old are skipped; the rest keep their order. + let later = weather_rules::normalize(&recorded(), &WeatherOptions::parse(&oslo(Some(48))).unwrap(), iso_dates::parse("2026-11-13T17:30:00Z").unwrap()).unwrap(); + assert_eq!(hours(&later).len(), 48); + assert_eq!(hours(&later)[0]["time"], "2026-11-13T17:00:00Z"); + // After the hourly part the provider has no next-hour data: both fields stay null. + assert!(!hours(&later)[42]["condition"].is_null()); + assert_eq!(hours(&later)[43]["time"], "2026-11-15T12:00:00Z"); + for row in &hours(&later)[43..=47] { + assert_eq!((row.get("precipitationMM"), row.get("condition")), (Some(&Value::Null), Some(&Value::Null))); + } + assert_eq!(hours(&weather_rules::normalize(&recorded(), &WeatherOptions::parse(&oslo(Some(1))).unwrap(), now).unwrap()).len(), 1); + // A forecast that is entirely in the past has no usable row. + rejects("unavailable", weather_rules::normalize(&recorded(), &WeatherOptions::parse(&oslo(None)).unwrap(), iso_dates::parse("2026-12-24T00:00:00Z").unwrap())); +} + +#[test] +fn normalization_bounds() { + let now = iso_dates::parse("2026-11-13T07:30:00Z").unwrap(); + let query = WeatherOptions::parse(&oslo(None)).unwrap(); + let normalize = |body: &[u8]| weather_rules::normalize(body, &query, now); + for body in ["not json", "[]", "{}", r#"{"properties":{}}"#, r#"{"properties":{"timeseries":{}}}"#, r#"{"properties":{"timeseries":[1]}}"#, r#"{"properties":{"timeseries":[]}}"#] { + rejects("unavailable", normalize(body.as_bytes())); + } + // A repeated member name makes the document ambiguous: it is refused, not half read. + rejects("unavailable", normalize(br#"{"properties":{"timeseries":[]},"properties":{"timeseries":[]}}"#)); + let good = entry("2026-11-13T08:00:00Z", details(), None).to_string(); + rejects("unavailable", normalize(format!(r#"{{"properties":{{"timeseries":[]}},"properties":{{"timeseries":[{good}]}}}}"#).as_bytes())); + assert_eq!(hours(&normalize(format!(r#"{{"properties":{{"timeseries":[{good}]}}}}"#).as_bytes()).unwrap()).len(), 1); + rejects("unavailable", normalize(&vec![b' '; weather_rules::MAXIMUM_RESPONSE_BYTES + 1])); + let many = |count: usize| series(&vec![entry("2026-11-13T08:00:00Z", details(), None); count]); + rejects("unavailable", normalize(&many(301))); + assert_eq!(hours(&normalize(&many(300)).unwrap()).len(), 24); + // Entries without a usable time or instant details are skipped, not fatal. + let skipped = normalize(&series(&[ + json!({ "time": 5 }), + json!({ "time": "2026-11-13T08:00" }), + json!({ "time": "2026-11-13T08:00:00Z" }), + json!({ "time": "2026-11-13T08:00:00Z", "data": { "instant": {} } }), + entry("2026-11-13T06:29:59Z", details(), None), + entry("2026-11-13T06:30:00Z", json!({ "air_temperature": -40, "wind_speed": 0 }), None), + ])) + .unwrap(); + assert_eq!(hours(&skipped).len(), 1); + assert_eq!(hours(&skipped)[0]["temperatureC"].as_f64(), Some(-40.0)); + // Values outside the physical bounds reject the payload. + let out_of_bounds = [ + json!({ "air_temperature": 100.1, "wind_speed": 2.0 }), + json!({ "air_temperature": -150.5, "wind_speed": 2.0 }), + json!({ "wind_speed": 2.0 }), + json!({ "air_temperature": "1", "wind_speed": 2.0 }), + json!({ "air_temperature": 1.5, "wind_speed": -0.1 }), + json!({ "air_temperature": 1.5, "wind_speed": 200.5 }), + json!({ "air_temperature": 1.5 }), + ]; + for bad in out_of_bounds { + rejects("invalid_options", normalize(&series(&[entry("2026-11-13T08:00:00Z", bad, None)]))); + } + let next = |amount: Option, symbol: Option| { + let mut next = json!({}); + if let Some(amount) = amount { + next["details"] = json!({ "precipitation_amount": amount }); + } + if let Some(symbol) = symbol { + next["summary"] = json!({ "symbol_code": symbol }); + } + Some(next) + }; + let one = |next: Option| normalize(&series(&[entry("2026-11-13T08:00:00Z", details(), next)])); + rejects("invalid_options", one(next(Some(json!(1000.5)), None))); + rejects("invalid_options", one(next(Some(json!(-1)), None))); + rejects("invalid_options", one(next(Some(Value::Null), None))); + let row = hours(&one(next(Some(json!(1000)), Some(json!("x".repeat(70))))).unwrap())[0].clone(); + assert_eq!(row["precipitationMM"].as_f64(), Some(1000.0)); + assert_eq!(row["condition"], "x".repeat(64)); + let untyped = hours(&one(next(None, Some(json!(7)))).unwrap())[0].clone(); + assert!(untyped["precipitationMM"].is_null() && untyped["condition"].is_null()); + // A condition is cut by characters, never through the middle of one. + let emoji = hours(&one(next(None, Some(json!("🌧".repeat(70))))).unwrap())[0].clone(); + assert_eq!(emoji["condition"], "🌧".repeat(64)); +} + +#[test] +fn cache_freshness_rules() { + let now = iso_dates::parse("2026-11-13T07:30:00Z").unwrap(); + let query = WeatherOptions::parse(&oslo(None)).unwrap(); + let fetched = stringify_object(&weather_rules::normalize(&recorded(), &query, now).unwrap()); + let cached = |age: i64, query: &WeatherOptions| weather_rules::from_cache(Some(&fetched), query, now + age); + assert_eq!(cached(0, &query).unwrap()["isStale"], false); + assert_eq!(cached(3_599_999, &query).unwrap()["isStale"], false); + assert_eq!(cached(3_600_000, &query).unwrap()["isStale"], true); + assert_eq!(cached(6 * 3_600_000, &query).unwrap()["isStale"], true); + assert_eq!(cached(6 * 3_600_000 + 1, &query), None); + assert_eq!(cached(-1, &query), None, "fetched in the future: the clock moved"); + // Fewer hours are served from a longer cache; more are not. + assert_eq!(hours(&cached(0, &WeatherOptions::parse(&oslo(Some(6))).unwrap()).unwrap()).len(), 6); + assert_eq!(cached(0, &WeatherOptions::parse(&oslo(Some(25))).unwrap()), None); + // Other coordinates never match, even when they are close. + assert_eq!(cached(0, &WeatherOptions::parse(&with(oslo(None), "latitude", json!(59.92))).unwrap()), None); + assert_eq!(cached(0, &WeatherOptions::parse(&with(oslo(None), "longitude", json!(-10.75))).unwrap()), None); + let oversize = "x".repeat(weather_rules::MAXIMUM_CACHE_BYTES + 1); + let duplicate = fetched.replacen('{', r#"{"latitude":0,"#, 1); + for text in [None, Some(""), Some("not json"), Some("{}"), Some(r#"{"latitude":59.91,"longitude":10.75,"fetchedAt":"yesterday","hours":[]}"#), Some(oversize.as_str()), Some(duplicate.as_str())] { + assert_eq!(weather_rules::from_cache(text, &query, now), None); + } +} + +#[test] +fn policy_order() { + let fixture = Fixture::new(); + let module = &fixture.module; + let oslo_options = oslo(None); + let forecast = |context: CallContext| module.forecast(&context, Some(&oslo_options)); + let plain = |permissions: &[&str], domains: &[&str]| context(permissions, domains, OWNER, 8, 32); + rejects("permission_denied", forecast(plain(&["network"], DOMAINS))); + let mut old = widget(); + old.minimum_runtime_version = Some("1.5".into()); + rejects("permission_denied", forecast(old)); + let mut legacy = widget(); + legacy.has_manifest = false; + rejects("permission_denied", forecast(legacy)); + rejects("permission_denied", forecast(context(DECLARED, DOMAINS, "preview", 8, 32))); + let mut projection = widget(); + projection.is_timeline_projection = true; + rejects("read_only", forecast(projection)); + let mut live = widget(); + live.is_live_activity = true; + rejects("read_only", forecast(live)); + rejects("query_limit", forecast(context(DECLARED, DOMAINS, OWNER, 0, 32))); + // Options are checked before the network declaration. + rejects("invalid_options", module.forecast(&plain(&["weather"], &[]), Some(&oslo(Some(99))))); + rejects("invalid_options", module.forecast(&widget(), None)); + rejects("permission_denied", forecast(plain(&["weather"], DOMAINS))); + rejects("permission_denied", forecast(plain(DECLARED, &[]))); + rejects("permission_denied", forecast(plain(DECLARED, &["met.no", "*.example.com"]))); + rejects("query_limit", forecast(context(DECLARED, DOMAINS, OWNER, 8, 0))); + assert_eq!(fixture.urls(), 0, "nothing reached the network"); + assert_eq!(hours(&forecast(plain(DECLARED, &["*.met.no"])).unwrap()).len(), 24); +} + +#[test] +fn request_shape_and_fresh_cache() { + let fixture = Fixture::new(); + let module = &fixture.module; + let first = module.forecast(&widget(), Some(&options(json!({ "latitude": 59.9149, "longitude": 10.7461, "hours": 12 })))).unwrap(); + assert_eq!(fixture.state().urls, ["https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=59.91&lon=10.75"]); + assert_eq!(fixture.state().agents, [Some("ScriptWidget-Linux/1.1.0 https://github.com/everettjf/scriptwidget".to_owned())]); + assert_eq!(fixture.state().timeout, 3000); + assert_eq!(hours(&first).len(), 12); + assert_eq!(first["isStale"], false); + + // Within the hour the cache answers without a request and without the network budget. + fixture.advance(3_599_000); + let second = module.forecast(&context(DECLARED, DOMAINS, OWNER, 8, 0), Some(&oslo(Some(6)))).unwrap(); + assert_eq!(fixture.urls(), 1); + assert_eq!(hours(&second).len(), 6); + assert_eq!(second["isStale"], false); + assert_eq!(second["fetchedAt"], "2026-11-13T07:30:00Z"); + // More hours than were cached, other coordinates and another installation all go to the network. + module.forecast(&widget(), Some(&oslo(Some(24)))).unwrap(); + assert_eq!(fixture.urls(), 2); + module.forecast(&widget(), Some(&with(oslo(None), "latitude", json!(60)))).unwrap(); + assert_eq!(fixture.urls(), 3); + module.forecast(&context(DECLARED, DOMAINS, OTHER_OWNER, 8, 32), Some(&with(oslo(None), "latitude", json!(60)))).unwrap(); + assert_eq!(fixture.urls(), 4); + assert_eq!(WeatherModule::user_agent(""), "ScriptWidget-Linux/0 https://github.com/everettjf/scriptwidget"); +} + +#[test] +fn a_stale_cache_stands_in_for_a_failed_request() { + let fixture = Fixture::new(); + let module = &fixture.module; + let oslo_options = oslo(None); + let forecast = || module.forecast(&widget(), Some(&oslo_options)); + forecast().unwrap(); + fixture.advance(3_600_000); + fixture.state().fail = true; + let stale = module.forecast(&widget(), Some(&oslo(Some(3)))).unwrap(); + assert_eq!(stale["isStale"], true); + assert_eq!(hours(&stale).len(), 3); + assert_eq!(stale["fetchedAt"], "2026-11-13T07:30:00Z"); + assert_eq!(fixture.urls(), 2); + fixture.state().fail = false; + for status in [304, 403, 429, 500] { + fixture.state().status = status; + assert_eq!(forecast().unwrap()["isStale"], true, "{status}"); + } + fixture.state().status = 200; + fixture.state().body = b"{}".to_vec(); + assert_eq!(forecast().unwrap()["isStale"], true); + // A stale cache is not returned when the network budget is spent: the request was never attempted. + rejects("query_limit", module.forecast(&context(DECLARED, DOMAINS, OWNER, 8, 0), Some(&oslo_options))); + fixture.advance(5 * 3_600_000 + 1); + rejects("unavailable", forecast()); + fixture.state().fail = true; + rejects("unavailable", forecast()); + // A good response replaces the cache and is fresh again. + fixture.state().fail = false; + fixture.state().body = recorded(); + fixture.now.store(iso_dates::parse("2026-11-13T15:00:00Z").unwrap(), Ordering::SeqCst); + let fresh = forecast().unwrap(); + assert_eq!(fresh["isStale"], false); + assert_eq!(fresh["fetchedAt"], "2026-11-13T15:00:00Z"); + assert_eq!(fixture.now(), iso_dates::parse("2026-11-13T15:00:00Z").unwrap()); +} + +#[test] +fn redirects_stay_inside_the_declared_hosts_and_caches_are_retired() { + use std::os::unix::fs::PermissionsExt; + let fixture = Fixture::new(); + let module = &fixture.module; + let oslo_options = oslo(None); + fixture.state().redirect = Some(url("https://example.com/forecast")); + module.forecast(&widget(), Some(&oslo_options)).unwrap(); + assert_eq!(fixture.state().redirect_allowed, Some(false)); + fixture.state().redirect = Some(url("https://api.met.no/weatherapi/other")); + fixture.advance(3_600_000); + module.forecast(&widget(), Some(&oslo_options)).unwrap(); + assert_eq!(fixture.state().redirect_allowed, Some(true)); + fixture.advance(3_600_000); + let finished = CallContext::new(OWNER, true, DECLARED, DOMAINS, Some("1.6")).with_probes(|| true, || true, || false); + module.forecast(&finished, Some(&oslo_options)).unwrap(); + assert_eq!(fixture.state().redirect_allowed, Some(false), "a finished execution follows no redirect"); + + let entries: Vec<_> = std::fs::read_dir(&fixture.directory).unwrap().map(|entry| entry.unwrap()).collect(); + let names: Vec = entries.iter().map(|entry| entry.file_name().to_string_lossy().into_owned()).collect(); + assert_eq!(names, [canonical::owner_file_name(OWNER, ".json")]); + assert!(!names[0].contains(OWNER), "the installation identity is not a file name"); + assert_eq!(entries[0].metadata().unwrap().permissions().mode() & 0o777, 0o600, "the cache is private to the user"); + module.retire(OWNER); + assert_eq!(std::fs::read_dir(&fixture.directory).unwrap().count(), 0); + // A cache file that is not a regular bounded file is ignored, never trusted. + let cache = fixture.directory.join(canonical::owner_file_name(OWNER, ".json")); + std::fs::write(&cache, "x".repeat(weather_rules::MAXIMUM_CACHE_BYTES + 1)).unwrap(); + fixture.state().fail = true; + rejects("unavailable", module.forecast(&widget(), Some(&oslo_options))); + // Nor is a link that someone put in its place. + fixture.state().fail = false; + let good = fixture.directory.join("elsewhere.json"); + module.forecast(&widget(), Some(&oslo_options)).unwrap(); + std::fs::rename(&cache, &good).unwrap(); + std::os::unix::fs::symlink(&good, &cache).unwrap(); + fixture.state().fail = true; + rejects("unavailable", module.forecast(&widget(), Some(&oslo_options))); +} + +/// The ISO-8601 reader is written by hand, so its edges are pinned here. +#[test] +fn iso_dates_accept_only_full_instants() { + assert_eq!(iso_dates::parse("2026-11-12T12:00:00Z"), Some(1_794_484_800_000)); + assert_eq!(iso_dates::parse("2026-11-12T13:00:00+01:00"), Some(1_794_484_800_000)); + assert_eq!(iso_dates::parse("2026-11-12T06:30:00-05:30"), Some(1_794_484_800_000)); + assert_eq!(iso_dates::parse("2026-11-12T12:00:00.123456789Z"), Some(1_794_484_800_123)); + assert_eq!(iso_dates::parse("2026-11-12T12:00:00.5Z"), Some(1_794_484_800_500)); + assert_eq!(iso_dates::parse("1969-12-31T23:59:59Z"), Some(-1000)); + assert_eq!(iso_dates::parse("2024-02-29T00:00:00Z"), Some(1_709_164_800_000)); + assert_eq!(iso_dates::parse("2026-11-17T08:00:00.000Z"), Some(1_794_902_400_000)); + for text in [ + "", "2026-11-12", "2026-11-12T12:00:00", "2026-11-12T12:00Z", "2026-11-12 12:00:00Z", "2026-13-12T12:00:00Z", "2026-02-30T12:00:00Z", "2026-11-12T24:00:00Z", "2026-11-12T12:60:00Z", "2026-11-12T12:00:60Z", + "2026-11-12T12:00:00+19:00", "2026-11-12T12:00:00+0100", "2026-11-12T12:00:00.Z", "2026-11-12T12:00:00Z\n", " 2026-11-12T12:00:00Z", "2025-02-29T00:00:00Z", "0000-01-01T00:00:00Z", "2026-11-12T12:00:00.1234567890Z", + "2026-11-12T12:00:00+18:01", "2026-11-12T12:00:00Z", + ] { + assert_eq!(iso_dates::parse(text), None, "{text:?}"); + } + assert_eq!(iso_dates::format(1_794_484_800_999), "2026-11-12T12:00:00Z"); + assert_eq!(iso_dates::format(-1), "1969-12-31T23:59:59Z"); + assert_eq!(iso_dates::format(0), "1970-01-01T00:00:00Z"); + assert_eq!(iso_dates::format(951_782_400_000), "2000-02-29T00:00:00Z"); +} + +/// The operation as the script bridge calls it, and the transport over `fetch`. +#[test] +fn the_operation_resolves_once_and_the_fetch_transport_stays_on_declared_public_hosts() { + let fixture = Fixture::new(); + let module = &fixture.module; + let package = |domains: &[&str], package_id: &str| { + let mut package = Package::with_manifest(json!({ "minimumRuntimeVersion": "1.6", "permissions": DECLARED, "networkDomains": domains })); + package.package_id = package_id.to_owned(); + package + }; + let payload = json!({ "options": oslo(Some(2)) }); + assert_eq!(module.apis(), ["$weather"]); + assert!(module.handles("weather.forecast")); + assert!(!module.handles("http.request")); + + let lines: Arc>> = Arc::default(); + let sink = lines.clone(); + let previous = system_log::set_sink(Some(Arc::new(move |line: &str| sink.lock().unwrap().push(line.to_owned())))); + let (ok, resolved) = start(module, &package(DOMAINS, OWNER).request(), "weather.forecast", payload.as_object()); + assert!(ok); + assert_eq!(resolved["hours"].as_array().unwrap().len(), 2); + assert_eq!( + rejection(module, &package(&["example.com"], OTHER_OWNER).request(), "weather.forecast", &payload), + "[permission_denied] System operation could not complete. Existing user data is preserved." + ); + assert_eq!(rejection(module, &package(DOMAINS, OWNER).request(), "weather.other", &payload), "Unsupported operation weather.other"); + // An installed package without the runtime version in its manifest is refused too. + let mut unversioned = Package::with_manifest(json!({ "permissions": DECLARED, "networkDomains": DOMAINS })); + unversioned.package_id = OWNER.to_owned(); + assert!(rejection(module, &unversioned.request(), "weather.forecast", &payload).starts_with("[permission_denied]")); + system_log::set_sink(previous); + { + let lines = lines.lock().unwrap(); + assert!(lines.contains(&"weather.forecast -> ok".to_owned()), "{lines:?}"); + assert!(lines.contains(&"weather.forecast -> permission_denied".to_owned()), "{lines:?}"); + assert!(!lines.iter().any(|line| line.contains("59.9") || line.contains(OWNER)), "{lines:?}"); + } + + // The real transport: the forecast host must resolve to a public address, and a redirect must stay declared. + let location: Arc> = Arc::new(Mutex::new("https://api.met.no/weatherapi/locationforecast/2.0/moved".into())); + let address: Arc> = Arc::new(Mutex::new("157.249.81.141".into())); + let (target, body) = (location.clone(), recorded()); + let exchange = FakeTransport::new(move |request| if request.path().ends_with("/compact") { FakeResponse::redirect(307, Some(&target.lock().unwrap())) } else { FakeResponse::bytes(200, &body) }); + let answer = address.clone(); + let fetch = FetchModule::with_parts(Arc::new(move |_| Ok(addresses(&[answer.lock().unwrap().as_str()]))), exchange.clone()); + let now = fixture.now.clone(); + let online = WeatherModule::with_clock(Arc::new(FetchWeatherTransport::new(Arc::new(fetch))), fixture.directory.join("fetch"), WeatherModule::user_agent("1.0"), Arc::new(move || now.load(Ordering::SeqCst))); + let oslo_options = oslo(None); + assert_eq!(hours(&online.forecast(&widget(), Some(&oslo_options)).unwrap()).len(), 24); + let requests = exchange.requests(); + assert_eq!(requests.iter().map(|request| request.url.as_str()).collect::>(), ["https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=59.91&lon=10.75", "https://api.met.no/weatherapi/locationforecast/2.0/moved"]); + assert_eq!(requests[0].header("User-Agent"), Some("ScriptWidget-Linux/1.0 https://github.com/everettjf/scriptwidget")); + let other = || context(DECLARED, DOMAINS, OTHER_OWNER, 8, 32); + *location.lock().unwrap() = "https://example.com/forecast".into(); + rejects("unavailable", online.forecast(&other(), Some(&oslo_options))); + *location.lock().unwrap() = "http://127.0.0.1/forecast".into(); + rejects("unavailable", online.forecast(&other(), Some(&oslo_options))); + *address.lock().unwrap() = "10.0.0.9".into(); + exchange.clear(); + rejects("unavailable", online.forecast(&other(), Some(&oslo_options))); + assert_eq!(exchange.count(), 0); +} diff --git a/Linux/crates/sw-core/tests/json_accessors.rs b/Linux/crates/sw-core/tests/json_accessors.rs new file mode 100644 index 00000000..58c39ff1 --- /dev/null +++ b/Linux/crates/sw-core/tests/json_accessors.rs @@ -0,0 +1,88 @@ +//! The lenient JSON accessors. Ported from `JsonTests.cs`, for the part of +//! the Windows `Json` helper that `sw_core::json` has: `OptLong`, `Append` +//! and the nullable `Quote` have no counterpart here. + +use serde_json::{Value, json}; +use sw_core::json::{Object, ObjectExt, as_f64, as_text, parse_object, quote, stringify, stringify_object}; + +fn object(value: Value) -> Object { + match value { + Value::Object(object) => object, + other => panic!("not an object: {other}"), + } +} + +#[test] +fn numbers_read_the_same_whether_parsed_or_built_in_memory() { + let parsed = parse_object(r#"{"i":15,"d":1.5,"s":"42","b":true,"n":null}"#).unwrap(); + let built = object(json!({ "i": 15, "d": 1.5, "s": "42", "b": true, "n": null, "l": 9_000_000_000i64, "f": 2.5f32 })); + for json in [&parsed, &built] { + assert_eq!(json.opt_i32("i", 0), 15); + assert_eq!(json.opt_f64("d"), 1.5); + assert_eq!(json.opt_i32("s", 0), 42); + assert_eq!(json.opt_i32("b", 7), 7); + assert_eq!(json.opt_i32("n", 7), 7); + assert_eq!(json.opt_i32("missing", 7), 7); + } + assert_eq!(built.opt_f64("l"), 9_000_000_000.0); + assert_eq!(built.opt_f64("f"), 2.5); + assert!(built.opt_f64("missing").is_nan()); + // A number too large for the type saturates instead of wrapping. + assert_eq!(built.opt_i32("l", 0), i32::MAX); +} + +#[test] +fn text_accessors_convert_scalars_like_org_json() { + let json = object(json!({ "s": "text", "i": 3, "b": false, "o": {}, "a": [], "n": null })); + assert_eq!(json.opt_string("s"), "text"); + assert_eq!(json.opt_string("i"), "3"); + assert_eq!(json.opt_string("b"), "false"); + assert_eq!(json.opt_string_or_none("o"), None); + assert_eq!(json.opt_string_or_none("a"), None); + assert_eq!(json.opt_string_or_none("n"), None); + assert_eq!(json.opt_string("o"), ""); + assert_eq!(json.opt_string_or_none("missing"), None); + assert!(json.is_null("n")); + assert!(json.is_null("missing")); + assert!(!json.is_null("s")); + assert!(json.has("n") && !json.has("missing")); + assert_eq!(as_text(&json!(1.5)).as_deref(), Some("1.5")); + assert_eq!(as_f64(&json!(" 2.5 ")), Some(2.5)); + assert_eq!(as_f64(&json!("two")), None); +} + +#[test] +fn booleans_objects_and_arrays_are_read_leniently() { + let json = object(json!({ "t": true, "text": "TRUE", "no": "false", "n": 1, "o": { "k": 1 }, "a": [1, 2] })); + assert!(json.opt_bool("t", false)); + assert!(json.opt_bool("text", false)); + assert!(!json.opt_bool("no", true)); + assert!(json.opt_bool("n", true) && !json.opt_bool("n", false)); + assert!(json.opt_bool("missing", true)); + assert_eq!(json.opt_object("o").map(|object| object.len()), Some(1)); + assert_eq!(json.opt_array("a").map(Vec::len), Some(2)); + assert!(json.opt_object("a").is_none() && json.opt_array("o").is_none()); +} + +#[test] +fn stringify_keeps_non_ascii_text_readable() { + assert_eq!(stringify(&json!({ "a": "你好 & 'c'" })), r#"{"a":"你好 & 'c'"}"#); + assert_eq!(quote("line\nbreak"), "\"line\\nbreak\""); + assert_eq!(stringify(&Value::Null), "null"); + assert_eq!(stringify(&json!([{ "k": 1 }, "text", null])), r#"[{"k":1},"text",null]"#); +} + +#[test] +fn objects_keep_the_order_of_their_members() { + let parsed = parse_object(r#"{"z":1,"a":2,"m":3}"#).unwrap(); + assert_eq!(parsed.keys().collect::>(), ["z", "a", "m"]); + assert_eq!(stringify_object(&parsed), r#"{"z":1,"a":2,"m":3}"#); +} + +#[test] +fn only_a_well_formed_object_parses() { + for text in ["", "[]", "1", "\"text\"", "null", "{", "{\"a\":1,}", "{\"a\":1} x", "{'a':1}", "{\"a\":1 /* c */}", "{a:1}"] { + assert!(parse_object(text).is_err(), "{text:?}"); + } + assert!(parse_object(" {\"a\":[1,{\"b\":null}]} ").is_ok()); +} diff --git a/Linux/crates/sw-core/tests/library_runtime.rs b/Linux/crates/sw-core/tests/library_runtime.rs new file mode 100644 index 00000000..1580a251 --- /dev/null +++ b/Linux/crates/sw-core/tests/library_runtime.rs @@ -0,0 +1,225 @@ +//! The library of installed packages with the bundled templates, and the +//! facade that compiles and runs one package: the Linux counterparts of +//! `Hosting/Library.cs` and `WidgetRuntime.cs` on Windows. Needs the assets of +//! `node Linux/scripts/build-shared-js.mjs`. + +#[path = "packages_support/mod.rs"] +mod support; + +use std::path::PathBuf; +use std::sync::Arc; + +use support::{TemporaryFolder, assert_code}; +use sw_core::library::Library; +use sw_core::packages::{ArchiveLimits, DiskPackageFiles, validate_source_tree, validate_tree}; +use sw_core::{DataPaths, MemoryPackageFiles, PackageFiles, RunRequest, SharedScripts, WidgetRuntime}; + +fn scripts() -> Arc { + Arc::new(SharedScripts::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets/scriptwidget")).expect("shared scripts")) +} + +fn memory(files: &[(&str, &str)]) -> Arc { + Arc::new(MemoryPackageFiles::new(files.iter().map(|(path, text)| (path.to_string(), text.to_string())), [])) +} + +/// A templates folder with one runnable template, one that needs an API Linux lacks, and entries that are no templates. +fn templates(scratch: &TemporaryFolder) -> PathBuf { + scratch.new_file("templates/template/Clock/main.jsx", "$import('lib/label.js');\n$render({label()});"); + scratch.new_file("templates/template/Clock/lib/label.js", "function label() { return 'clock'; }"); + scratch.new_file("templates/template/Clock/meta.json", r#"{"description":"A clock"}"#); + scratch.new_file("templates/template/Clock/.DS_Store", "noise"); + scratch.new_file("templates/template/Clock/__Build/prepared-1.json", "{}"); + scratch.new_file("templates/template/Quote/main.jsx", "const quote = await fetch(\"https://zenquotes.io/api/today\");\n$render(q);"); + scratch.new_file("templates/component/steps/main.jsx", "$render({String(await $health.steps())});"); + scratch.new_file("templates/api/declared/main.jsx", "$render(declared);"); + scratch.new_file("templates/api/declared/widget.json", &support::manifest("Template Name")); + scratch.new_file("templates/api/no-entry/readme.txt", "no main.jsx"); + scratch.new_file("templates/other/ignored/main.jsx", "$render(not a group);"); + scratch.new_file("templates/template/.hidden/main.jsx", "$render(hidden);"); + scratch.new_file("outside.jsx", "$render(outside);"); + scratch.new_folder("templates/template/Linked"); + support::symlink(&scratch.path("templates/template/Linked/main.jsx"), &scratch.path("outside.jsx")); + scratch.path("templates") +} + +fn library(scratch: &TemporaryFolder) -> Library { + Library::new(&DataPaths::new(Some(&scratch.path("data"))).unwrap(), Some(templates(scratch))) +} + +#[test] +fn bundled_templates_are_listed_by_group_and_filtered_by_api() { + let scratch = TemporaryFolder::new(); + let library = library(&scratch); + let bundled = library.bundled(); + let names: Vec<(&str, &str, bool)> = bundled.iter().map(|package| (package.group.as_str(), package.name.as_str(), package.network)).collect(); + assert_eq!(names, [("template", "Clock", false), ("template", "Quote", true), ("api", "declared", false)], "groups in the order of the Apple apps; no links, dot-folders or other groups"); + let unavailable = library.unavailable(); + assert_eq!(unavailable.len(), 1); + assert_eq!((unavailable[0].group.as_str(), unavailable[0].name.as_str(), unavailable[0].api.as_str()), ("component", "steps", "$health")); + assert_eq!(library.find_bundled("template/Clock").map(|package| package.name), Some("Clock".into())); + assert_eq!(library.find_bundled("declared").map(|package| package.group), Some("api".into())); + assert!(library.find_bundled("component/steps").is_none() && library.find_bundled("api/Clock").is_none()); + // No templates folder at all is an empty list, not an error. + let none = Library::new(&DataPaths::new(Some(&scratch.path("data"))).unwrap(), None); + assert!(none.bundled().is_empty() && none.unavailable().is_empty()); +} + +#[test] +fn a_bundled_template_is_copied_into_the_library_with_a_manifest() { + let scratch = TemporaryFolder::new(); + let library = library(&scratch); + let clock = library.find_bundled("template/Clock").unwrap(); + assert_eq!(library.bundled_files(&clock).keys().map(String::as_str).collect::>(), ["lib/label.js", "main.jsx", "meta.json"]); + + let installed = library.install_bundled(&clock).unwrap(); + assert_eq!(installed.name, "Clock"); + assert!(installed.manifest_present); + assert_eq!(installed.description(), "A clock"); + assert!(installed.permissions().is_empty()); + assert_eq!(installed.files().paths(), ["lib/label.js", "main.jsx", "meta.json", "widget.json"]); + // A second copy gets a numbered name and an identity of its own. + let second = library.install_bundled(&clock).unwrap(); + assert_eq!(second.name, "Clock (1)"); + assert_ne!(second.installation, installed.installation); + + // The hosts a template names are declared for it, so its requests are allowed. + let quote = library.install_bundled(&library.find_bundled("Quote").unwrap()).unwrap(); + assert_eq!(quote.permissions(), ["network"]); + assert_eq!(quote.manifest.as_ref().unwrap()["networkDomains"], serde_json::json!(["zenquotes.io"])); + // A template with a manifest keeps it, under the installed name and a fresh id. + let declared = library.install_bundled(&library.find_bundled("api/declared").unwrap()).unwrap(); + assert_eq!(declared.name, "declared"); + assert_eq!(declared.display_name(), "declared"); + assert!(declared.manifest.as_ref().unwrap()["id"].as_str().unwrap().starts_with("local.")); + assert_eq!(declared.families(), ["small"]); + + let names: Vec = library.list().unwrap().into_iter().map(|package| package.name).collect(); + assert_eq!(names, ["Clock", "Clock (1)", "declared", "Quote"], "sorted without regard to case"); +} + +#[test] +fn the_installation_identity_survives_a_rename_and_is_not_copied() { + let scratch = TemporaryFolder::new(); + let library = library(&scratch); + let created = library.create("Widget", support::SOURCE, None).unwrap(); + assert!((8..=64).contains(&created.installation.len())); + assert_eq!(library.find("widget").unwrap().installation, created.installation, "found without regard to case, same identity"); + assert_eq!(library.find_by_installation(&created.installation).unwrap().name, "Widget"); + + let renamed = library.rename("Widget", "Renamed").unwrap(); + assert_eq!(renamed.installation, created.installation); + let copy = library.duplicate("Renamed").unwrap(); + assert_ne!(copy.installation, created.installation); + + // An exported archive carries no identity, and importing gives a new one. + let mut archive = Vec::new(); + library.export_archive("Renamed", &mut archive).unwrap(); + assert!(support::entry_names(&archive).iter().all(|name| !name.contains(".scriptwidget-installation"))); + let imported = library.import_archive(&mut archive.as_slice(), None, None).unwrap(); + assert_ne!(imported.installation, created.installation); + + // A marker that is a link is never read through: the package gets a fresh identity instead. + let other = scratch.new_file("other-identity", "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"); + let marker = renamed.directory.join(sw_core::library::INSTALLATION_FILE); + std::fs::remove_file(&marker).unwrap(); + support::symlink(&marker, &other); + let relisted = library.find("Renamed").unwrap(); + assert_ne!(relisted.installation, "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"); + assert_eq!(std::fs::read_to_string(&other).unwrap(), "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"); + assert_eq!(library.find("Renamed").unwrap().installation, relisted.installation, "and keeps it"); +} + +#[test] +fn writing_a_file_clears_the_build_cache() { + let scratch = TemporaryFolder::new(); + let library = library(&scratch); + let created = library.create("Widget", support::SOURCE, None).unwrap(); + let runtime = WidgetRuntime::new(scripts(), &DataPaths::new(Some(&scratch.path("data"))).unwrap()).unwrap(); + let mut request = RunRequest::new(Arc::new(created.files()), created.installation.clone()); + request.build_directory = Some(created.build_directory()); + assert!(runtime.run(&request).execution.tree.unwrap().contains("Hello")); + let cached: Vec<_> = std::fs::read_dir(created.build_directory()).unwrap().map(|entry| entry.unwrap().file_name().into_string().unwrap()).collect(); + assert!(cached.len() == 1 && cached[0].starts_with("prepared-") && cached[0].ends_with(".json"), "{cached:?}"); + assert_eq!(created.files().paths(), ["main.jsx", "widget.json"], "the cache is not a package file"); + // The second run reads the artifact; the result is the same. + assert!(runtime.run(&request).execution.tree.unwrap().contains("Hello")); + + library.write_file("Widget", "main.jsx", b"$render(Changed);").unwrap(); + assert!(!created.build_directory().exists()); + assert!(runtime.run(&request).execution.tree.unwrap().contains("Changed")); +} + +#[test] +fn the_runtime_sets_the_environment_and_returns_compile_errors_as_results() { + let scratch = TemporaryFolder::new(); + let runtime = WidgetRuntime::new(scripts(), &DataPaths::new(Some(&scratch.path("data"))).unwrap()).unwrap(); + let mut request = RunRequest::new(memory(&[("main.jsx", "$render({$getenv('widget-size')}|{$getenv('widget-param')}|{$getenv('widget-rendering-mode')}|{$getenv('extra')}|{String($device.isdarkmode())});")]), "test:env"); + request.family = "large".into(); + request.parameter = "p".into(); + request.dark_mode = true; + request.extra_environments = vec![("extra".into(), "x".into())]; + let result = runtime.run(&request); + assert_eq!(result.execution.error, None); + assert!(result.execution.tree.as_deref().unwrap().contains(r#""children":["large","|","p","|","fullColor","|","x","|","true"]"#), "{:?}", result.execution.tree); + assert!(result.depends_on_appearance, "the script asked for the theme"); + assert_eq!(result.next_timeline_transition, None); + assert!(result.prepared.is_some() && result.remote_images.is_some()); + + let broken = runtime.run(&RunRequest::new(memory(&[("main.jsx", "$render(")]), "test:broken")); + assert!(broken.prepared.is_none() && broken.execution.tree.is_none() && broken.remote_images.is_none()); + let error = broken.execution.error.expect("the compile error"); + assert!(error.message.contains("SyntaxError"), "{error:?}"); + assert!(runtime.validate_package(memory(&[("main.jsx", "$render(")]).as_ref()).is_err()); + + // The card for an error is a canonical tree with the category and the message, cut to a bounded length. + let card: serde_json::Value = serde_json::from_str(&runtime.error_tree(Some(&sw_core::ExecutionError { kind: "resourceLimit".into(), message: "x".repeat(5000) }))).unwrap(); + assert_eq!(card["$element"], "vstack"); + assert!(card["children"][0]["children"][0].as_str().unwrap().ends_with(" error")); + assert_eq!(card["children"][1]["children"][0].as_str().unwrap().len(), 400); + assert!(serde_json::from_str::(&runtime.error_tree(None)).is_ok()); + + // An image on a private address is never fetched, and a tree without images costs nothing. + let scope = result.remote_images.unwrap(); + let images = runtime.fetch_remote_images(r#"{"$element":"image","props":{"url":"http://127.0.0.1:9/a.png"},"children":[]}"#, &scope); + assert!(images.is_empty()); + assert!(!scratch.path("data/Cache/remote-images").join("x").exists()); +} + +#[test] +fn a_source_folder_may_hold_dot_files_but_no_links_among_its_package_files() { + let scratch = TemporaryFolder::new(); + let package = scratch.new_folder("Widget"); + scratch.new_file("Widget/main.jsx", support::SOURCE); + scratch.new_file("Widget/.git/config", "x"); + scratch.new_file("outside.txt", "outside"); + let limits = ArchiveLimits::default(); + validate_source_tree(&package, &limits).unwrap(); + + // What is not part of the authored package is not looked at, whatever it is. + support::symlink(&package.join(".venv"), &scratch.root); + support::symlink(&package.join("__Build"), &scratch.root); + validate_source_tree(&package, &limits).unwrap(); + assert_code("unsafe_file", validate_tree(&package, &limits)); + assert_eq!(DiskPackageFiles::new(&package).paths(), ["main.jsx"]); + + // Among the package files a link, a hard link or a special file fails the folder. + support::symlink(&package.join("data.txt"), &scratch.path("outside.txt")); + assert_code("unsafe_file", validate_source_tree(&package, &limits)); + std::fs::remove_file(package.join("data.txt")).unwrap(); + support::hard_link(&package.join("hard.txt"), &scratch.path("outside.txt")); + assert_code("unsafe_file", validate_source_tree(&package, &limits)); + std::fs::remove_file(package.join("hard.txt")).unwrap(); + support::make_fifo(&package.join("pipe")); + assert_code("unsafe_file", validate_source_tree(&package, &limits)); + std::fs::remove_file(package.join("pipe")).unwrap(); + // Two spellings of one name, where the filesystem keeps both (the Mac's default one does not). + if !package.join("MAIN.JSX").exists() { + scratch.new_file("Widget/Main.JSX", "other"); + assert_code("unsafe_file", validate_source_tree(&package, &limits)); + std::fs::remove_file(package.join("Main.JSX")).unwrap(); + } + validate_source_tree(&package, &limits).unwrap(); + // The folder itself must be a real directory. + support::symlink(&scratch.path("Alias"), &package); + assert_code("unsafe_file", validate_source_tree(&scratch.path("Alias"), &limits)); +} diff --git a/Linux/crates/sw-core/tests/packages_archive.rs b/Linux/crates/sw-core/tests/packages_archive.rs new file mode 100644 index 00000000..97fb1e36 --- /dev/null +++ b/Linux/crates/sw-core/tests/packages_archive.rs @@ -0,0 +1,562 @@ +//! `.swt` archives: preflight, extraction and export. Ported from `PackageArchiveTests.cs`. + +#[path = "packages_support/mod.rs"] +mod support; + +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; + +use support::{ + RawEntry, SYMBOLIC_LINK, TemporaryFolder, assert_code, assert_lying_size_rejected, deflated_zip, entry, entry_names, forge_declared_size, manifest, nested, raw_zip, read_zip, symlink, tree, widget_zip, + write_file, +}; +use sw_core::PackageFiles; +use sw_core::packages::limits::{MAX_FILE_BYTES, MAX_FILE_COUNT}; +use sw_core::packages::{ArchiveLimits, DiskPackageFiles, PackageError, archive}; + +struct Fixture { + temporary: TemporaryFolder, + staging: PathBuf, + library: PathBuf, +} + +impl Fixture { + fn new() -> Self { + let temporary = TemporaryFolder::new(); + Self { staging: temporary.new_folder("staging"), library: temporary.new_folder("library"), temporary } + } + + fn extract(&self, bytes: &[u8]) -> Result { + archive::extract(&mut &bytes[..], &self.staging, &ArchiveLimits::default()) + } + + fn import_to(&self, bytes: &[u8], destination: &Path) -> Result, PackageError> { + archive::import_to(&mut &bytes[..], &self.staging, destination, &ArchiveLimits::default()) + } + + /// Refused by the preflight alone and by an import, which must leave nothing behind. + #[track_caller] + fn assert_rejected_with(&self, code: &str, bytes: &[u8], limits: &ArchiveLimits) { + assert_code(code, archive::preflight(bytes, limits)); + let destination = self.library.join("Target"); + assert_code(code, archive::import_to(&mut &bytes[..], &self.staging, &destination, limits)); + assert!(!destination.exists()); + assert!(tree(&self.staging).is_empty(), "staging: {:?}", tree(&self.staging)); + assert!(tree(&self.library).is_empty()); + } + + #[track_caller] + fn assert_rejected(&self, code: &str, bytes: &[u8]) { + self.assert_rejected_with(code, bytes, &ArchiveLimits::default()); + } + + #[track_caller] + fn assert_rejected_on_extract(&self, code: &str, bytes: &[u8]) { + assert_code(code, self.extract(bytes)); + assert!(tree(&self.staging).is_empty()); + } + + /// A package directory with authored files, caches, hidden files and links to a file and a directory outside. + fn sample_package(&self) -> PathBuf { + let root = self.temporary.new_folder("source/My Widget"); + write_file(&root.join("widget.json"), manifest("My Widget").as_bytes()); + write_file(&root.join("main.jsx"), "$render(阅读);".as_bytes()); + write_file(&root.join("src/b.js"), b"b"); + write_file(&root.join("src/a.js"), b"a"); + write_file(&root.join("image/logo.png"), &(0..2048).map(|index| (index * 31) as u8).collect::>()); + write_file(&root.join("__Build/main.js"), b"compiled"); + write_file(&root.join("src/__Build/a.js"), b"compiled"); + write_file(&root.join(".DS_Store"), b"junk"); + write_file(&root.join(".git/config"), b"junk"); + let secret = self.temporary.new_file("secret.txt", "secret"); + symlink(&root.join("link.txt"), &secret); + symlink(&root.join("linkdir"), secret.parent().unwrap()); + root + } +} + +fn limits(change: impl FnOnce(&mut ArchiveLimits)) -> ArchiveLimits { + let mut limits = ArchiveLimits::default(); + change(&mut limits); + limits +} + +fn export_with(root: &Path, name: &str, limits: &ArchiveLimits) -> Result, PackageError> { + let mut output = Vec::new(); + archive::export(root, name, &mut output, &BTreeMap::new(), limits).map(|()| output) +} + +fn export(root: &Path) -> Vec { + export_with(root, root.file_name().unwrap().to_str().unwrap(), &ArchiveLimits::default()).unwrap() +} + +// Preflight: paths + +#[test] +fn rejects_traversal_paths() { + let fixture = Fixture::new(); + for name in ["../escape.txt", "Widget/../../escape.txt", "Widget/../main.jsx", "..", "Widget/./main.jsx", "./main.jsx"] { + fixture.assert_rejected("archive_unsafe_path", &raw_zip(&[entry("Widget/main.jsx"), entry(name)])); + } +} + +#[test] +fn rejects_absolute_backslash_drive_and_control_paths() { + let fixture = Fixture::new(); + let too_long = format!("Widget/{}", "n".repeat(256)); + let names = [ + "/etc/passwd", + "/Widget/main.jsx", + "\\Widget\\main.jsx", + "Widget\\main.jsx", + "Widget/..\\..\\escape.txt", + "C:/Widget/main.jsx", + "C:main.jsx", + "Widget/main.jsx:stream", + "Widget/ma\0in.jsx", + "Widget/line\nbreak.js", + "Widget/bidi\u{202E}.js", + &too_long, + ]; + for name in names { + fixture.assert_rejected("archive_unsafe_path", &raw_zip(&[entry("Widget/main.jsx"), entry(name)])); + } +} + +#[test] +fn rejects_non_utf8_names() { + let fixture = Fixture::new(); + fixture.assert_rejected("archive_unsafe_path", &raw_zip(&[entry("ignored").name_bytes(&[b'W', b'/', 0xC3, 0x28])])); +} + +#[test] +fn rejects_excessive_nesting() { + let fixture = Fixture::new(); + let deep = nested("d", 16); + assert_eq!(archive::preflight(&raw_zip(&[entry(&deep)]), &ArchiveLimits::default()).unwrap(), [deep.as_str()]); + fixture.assert_rejected("archive_unsafe_path", &raw_zip(&[entry(&format!("{deep}/main.jsx"))])); +} + +#[test] +fn rejects_duplicate_and_case_colliding_paths() { + let fixture = Fixture::new(); + fixture.assert_rejected("archive_path_collision", &raw_zip(&[RawEntry::new("Widget/main.jsx", "one"), RawEntry::new("widget/MAIN.jsx", "two")])); + fixture.assert_rejected("archive_path_collision", &raw_zip(&[RawEntry::new("Widget/main.jsx", "one"), RawEntry::new("Widget/main.jsx", "two")])); + fixture.assert_rejected("archive_path_collision", &raw_zip(&[entry("Widget/main.jsx"), entry("Widget/a.js"), entry("Widget/A.js")])); + fixture.assert_rejected("archive_path_collision", &raw_zip(&[entry("Widget/main.jsx"), entry("Widget/src/a.js"), entry("Widget/SRC/b.js")])); +} + +#[test] +fn rejects_unicode_normalization_collisions() { + let fixture = Fixture::new(); + let composed = "Widget/caf\u{E9}.js"; + let decomposed = "Widget/cafe\u{301}.js"; + fixture.assert_rejected("archive_path_collision", &raw_zip(&[entry("Widget/main.jsx"), entry(composed), entry(decomposed)])); +} + +#[test] +fn rejects_a_path_used_as_both_file_and_directory() { + let fixture = Fixture::new(); + fixture.assert_rejected("archive_path_collision", &raw_zip(&[entry("Widget/main.jsx"), entry("Widget/data"), entry("Widget/data/a.json")])); + fixture.assert_rejected("archive_path_collision", &raw_zip(&[entry("Widget/main.jsx"), entry("Widget/data/a.json"), entry("Widget/data")])); +} + +// Preflight: entry kinds + +#[test] +fn rejects_symbolic_link_entries() { + let fixture = Fixture::new(); + fixture.assert_rejected("archive_symlink", &raw_zip(&[entry("Widget/main.jsx"), RawEntry::new("Widget/link.js", "/etc/passwd").mode(SYMBOLIC_LINK)])); +} + +#[test] +fn rejects_symbolic_link_directory_entries() { + let fixture = Fixture::new(); + // A link named like a directory, followed by a file that would be written through it. + fixture.assert_rejected("archive_symlink", &raw_zip(&[entry("Widget/main.jsx"), RawEntry::new("Widget/out/", "/tmp").mode(SYMBOLIC_LINK), entry("Widget/out/escape.txt")])); + fixture.assert_rejected("archive_path_collision", &raw_zip(&[entry("Widget/main.jsx"), RawEntry::new("Widget/out", "/tmp"), entry("Widget/out/escape.txt")])); +} + +#[test] +fn rejects_special_files_encryption_and_unknown_compression() { + let fixture = Fixture::new(); + // FIFO, character device, block device and socket. + for kind in [0x11A4u32, 0x21A4, 0x61A4, 0xC1A4] { + fixture.assert_rejected("archive_symlink", &raw_zip(&[RawEntry::new("Widget/special", "").mode(kind << 16)])); + } + fixture.assert_rejected("archive_encrypted", &raw_zip(&[entry("Widget/main.jsx").flags(0x0801)])); + fixture.assert_rejected("archive_unsupported_compression", &raw_zip(&[entry("Widget/main.jsx").method(12)])); +} + +// Preflight: limits + +#[test] +fn rejects_too_many_entries() { + let fixture = Fixture::new(); + let entries: Vec = (0..=MAX_FILE_COUNT).map(|index| entry(&format!("Widget/f{index}.js"))).collect(); + assert_eq!(entries.len(), 501); + fixture.assert_rejected("archive_too_many_entries", &raw_zip(&entries)); + assert_eq!(archive::preflight(&raw_zip(&entries[..500]), &ArchiveLimits::default()).unwrap().len(), 500); +} + +#[test] +fn rejects_oversized_entry_by_declared_size() { + let fixture = Fixture::new(); + fixture.assert_rejected("archive_file_too_large", &raw_zip(&[entry("Widget/main.jsx"), entry("Widget/big.bin").declared_size(MAX_FILE_BYTES + 1)])); + archive::preflight(&raw_zip(&[entry("Widget/main.jsx"), entry("Widget/big.bin").declared_size(MAX_FILE_BYTES)]), &ArchiveLimits::default()).unwrap(); +} + +#[test] +fn rejects_oversized_total_by_declared_size() { + let fixture = Fixture::new(); + let mut entries = vec![entry("Widget/main.jsx")]; + entries.extend((1..=3).map(|index| entry(&format!("Widget/part{index}.bin")).declared_size(22 * 1024 * 1024))); + fixture.assert_rejected("archive_expanded_too_large", &raw_zip(&entries)); +} + +#[test] +fn rejects_oversized_entry_and_total_with_real_data() { + let fixture = Fixture::new(); + let tight = limits(|limits| { + limits.maximum_file_bytes = 1024; + limits.maximum_expanded_bytes = 2048; + }); + fixture.assert_rejected_with("archive_file_too_large", &raw_zip(&[entry("Widget/main.jsx"), RawEntry::new("Widget/big.bin", vec![0; 1025])]), &tight); + let mut entries = vec![entry("Widget/main.jsx")]; + entries.extend((1..=3).map(|index| RawEntry::new(&format!("Widget/part{index}.bin"), vec![0; 1000]))); + fixture.assert_rejected_with("archive_expanded_too_large", &raw_zip(&entries), &tight); +} + +#[test] +fn rejects_empty_oversized_and_malformed_archives() { + let fixture = Fixture::new(); + fixture.assert_rejected("archive_size", &[]); + fixture.assert_rejected_with("archive_size", &widget_zip(), &limits(|limits| limits.maximum_archive_bytes = 64)); + fixture.assert_rejected("archive_malformed", &[0; 10]); + fixture.assert_rejected("archive_malformed", b"this is not a zip archive at all, just text"); + let valid = widget_zip(); + fixture.assert_rejected("archive_malformed", &[&valid[..], &[1, 2, 3]].concat()); + let mut bad_offset = valid.clone(); + let end = valid.len() - 22; + bad_offset[end + 16] = 0x7F; + fixture.assert_rejected("archive_malformed", &bad_offset); + let mut second_disk = valid.clone(); + second_disk[end + 4] = 1; + fixture.assert_rejected("archive_multi_disk", &second_disk); + let mut wrong_count = valid.clone(); + wrong_count[end + 10] = 1; + fixture.assert_rejected("archive_multi_disk", &wrong_count); + let mut wrong_signature = valid.clone(); + let central = u32::from_le_bytes(valid[end + 16..end + 20].try_into().unwrap()) as usize; + wrong_signature[central] = b'X'; + fixture.assert_rejected("archive_malformed", &wrong_signature); + fixture.assert_rejected("archive_invalid_ratio", &raw_zip(&[RawEntry::new("Widget/main.jsx", "").declared_size(2 * 1024 * 1024).declared_compressed_size(0)])); +} + +#[test] +fn an_archive_larger_than_the_limit_is_not_read_to_the_end() { + let fixture = Fixture::new(); + // The input would never end; reading stops one byte past the limit. + let mut endless = std::io::repeat(0x50); + assert_code("archive_size", archive::extract(&mut endless, &fixture.staging, &limits(|limits| limits.maximum_archive_bytes = 4096))); + assert!(tree(&fixture.staging).is_empty()); +} + +// Extraction + +#[test] +fn zip_bomb_with_lying_size_headers_stops_at_the_declared_size() { + let fixture = Fixture::new(); + // More than the 64 MiB a package may expand to: an extraction that only compared sizes at the end of + // the entry would run into that limit, and report it, long after it should have stopped. + let bomb = deflated_zip(&[("Widget/main.jsx", b"x"), ("Widget/bomb.bin", &vec![0; 80 * 1024 * 1024])]); + assert!(bomb.len() < 128 * 1024, "the bomb must be small on the wire"); + let forged = forge_declared_size(&bomb, 1, 16); + assert_eq!(archive::preflight(&forged, &ArchiveLimits::default()).unwrap().len(), 2); + let destination = fixture.library.join("Bomb"); + assert_lying_size_rejected(fixture.import_to(&forged, &destination)); + assert!(!destination.exists()); + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn inflation_is_counted_against_the_limits_even_when_headers_agree() { + let fixture = Fixture::new(); + // Declared sizes pass the preflight, then the real stream is checked against them. + let archive_bytes = deflated_zip(&[("Widget/main.jsx", b"x"), ("Widget/a.bin", &[0; 4096])]); + assert_lying_size_rejected(fixture.extract(&forge_declared_size(&archive_bytes, 1, 100))); + assert_code("archive_size_mismatch", fixture.extract(&forge_declared_size(&archive_bytes, 1, 8192))); + assert!(tree(&fixture.staging).is_empty()); + // With honest headers the same archive is refused by limits that are tighter than its content. + let tight = limits(|limits| limits.maximum_expanded_bytes = 4096); + assert_code("archive_expanded_too_large", archive::extract(&mut &archive_bytes[..], &fixture.staging, &tight)); + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn stored_entry_with_lying_size_is_rejected() { + let fixture = Fixture::new(); + let archive_bytes = raw_zip(&[entry("Widget/main.jsx"), RawEntry::new("Widget/data.bin", vec![7; 4096]).declared_size(16)]); + assert_lying_size_rejected(fixture.extract(&archive_bytes)); + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn corrupted_content_fails_the_checksum() { + let fixture = Fixture::new(); + let mut archive_bytes = raw_zip(&[RawEntry::new("Widget/main.jsx", "render()")]); + archive_bytes[30 + "Widget/main.jsx".len()] = b'X'; + assert_code("archive_corrupt", fixture.extract(&archive_bytes)); + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn a_corrupt_deflate_stream_is_rejected() { + let fixture = Fixture::new(); + let mut archive_bytes = deflated_zip(&[("Widget/main.jsx", &b"abcdefgh".repeat(64))]); + // Inside the compressed data of the only entry. + let start = 30 + "Widget/main.jsx".len(); + for byte in &mut archive_bytes[start..start + 6] { + *byte = 0xFF; + } + let error = fixture.extract(&archive_bytes).expect_err("corrupt stream"); + assert!(["archive_corrupt", "archive_size_mismatch"].contains(&error.code.as_str()), "{error}"); + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn local_headers_that_disagree_with_the_central_directory_are_corrupt() { + let fixture = Fixture::new(); + let valid = raw_zip(&[RawEntry::new("Widget/main.jsx", "render()")]); + let end = valid.len() - 22; + let central = u32::from_le_bytes(valid[end + 16..end + 20].try_into().unwrap()) as usize; + // The central directory points past the archive, at something that is not a local header, or at a header for another name. + let mut bad_offset = valid.clone(); + bad_offset[central + 42..central + 46].copy_from_slice(&0x00FF_FFFFu32.to_le_bytes()); + assert_code("archive_corrupt", fixture.extract(&bad_offset)); + let mut not_a_header = valid.clone(); + not_a_header[central + 42] = 4; + assert_code("archive_corrupt", fixture.extract(¬_a_header)); + let mut other_name = valid.clone(); + other_name[30] = b'X'; + assert_code("archive_corrupt", fixture.extract(&other_name)); + let mut other_method = valid.clone(); + other_method[8] = 8; + assert_code("archive_corrupt", fixture.extract(&other_method)); + // Encrypted according to the local header only. + let mut encrypted = valid.clone(); + encrypted[6] |= 1; + assert_code("archive_encrypted", fixture.extract(&encrypted)); + // The data would run past the end of the archive. + let truncated = raw_zip(&[RawEntry::new("Widget/main.jsx", "x").declared_compressed_size(1_000_000).declared_size(1_000_000)]); + assert_code("archive_corrupt", fixture.extract(&truncated)); + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn extracts_only_authored_files_of_the_single_package_root() { + let fixture = Fixture::new(); + let archive_bytes = raw_zip(&[ + RawEntry::new("Widget/widget.json", "{}"), + RawEntry::new("Widget/main.jsx", "main"), + RawEntry::new("Widget/src/util.js", "util"), + RawEntry::new("Widget/empty/", ""), + RawEntry::new("Widget/__Build/main.js", "planted"), + RawEntry::new("Widget/src/__build/x.js", "planted"), + RawEntry::new("Widget/src/deep/__BUILD/y.js", "planted"), + RawEntry::new("Widget/.DS_Store", "junk"), + RawEntry::new("Widget/.git/config", "junk"), + RawEntry::new("__MACOSX/Widget/._main.jsx", "junk"), + RawEntry::new("__MACOSX/other.txt", "junk"), + RawEntry::new("README.txt", "stray top-level file"), + ]); + { + let extracted = fixture.extract(&archive_bytes).unwrap(); + assert_eq!(extracted.root_name(), Some("Widget")); + assert_eq!(tree(extracted.directory()), ["main.jsx", "src/", "src/util.js", "widget.json"]); + assert_eq!(std::fs::read_to_string(extracted.directory().join("src/util.js")).unwrap(), "util"); + assert!(extracted.directory().starts_with(&fixture.staging)); + } + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn accepts_files_at_the_archive_root_and_rejects_ambiguous_roots() { + let fixture = Fixture::new(); + let flat = raw_zip(&[RawEntry::new("main.jsx", "main"), RawEntry::new("image/a.png", "png"), RawEntry::new("__Build/main.js", "planted")]); + { + let extracted = fixture.extract(&flat).unwrap(); + assert_eq!(extracted.root_name(), None); + assert_eq!(tree(extracted.directory()), ["image/", "image/a.png", "main.jsx"]); + } + fixture.assert_rejected_on_extract("archive_no_package", &raw_zip(&[entry("One/main.jsx"), entry("Two/main.jsx")])); + fixture.assert_rejected_on_extract("archive_no_package", &raw_zip(&[entry("readme.txt")])); + fixture.assert_rejected_on_extract("archive_no_package", &raw_zip(&[entry("__Build/main.jsx"), entry(".hidden/main.jsx")])); + fixture.assert_rejected_on_extract("archive_no_package", &raw_zip(&[RawEntry::new("Widget/", "")])); +} + +#[test] +fn import_moves_into_place_and_never_replaces_an_existing_directory() { + let fixture = Fixture::new(); + let destination = fixture.library.join("Installed"); + assert_eq!(fixture.import_to(&widget_zip(), &destination).unwrap().as_deref(), Some("Widget")); + assert_eq!(tree(&destination), ["main.jsx", "widget.json"]); + assert!(tree(&fixture.staging).is_empty()); + std::fs::write(destination.join("main.jsx"), "mine").unwrap(); + assert_code("package_exists", fixture.import_to(&widget_zip(), &destination)); + assert_eq!(std::fs::read_to_string(destination.join("main.jsx")).unwrap(), "mine"); + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn imports_deflated_archives() { + let fixture = Fixture::new(); + let text = "a".repeat(100_000); + let extracted = fixture.extract(&deflated_zip(&[("Widget/main.jsx", text.as_bytes()), ("Widget/widget.json", b"{}")])).unwrap(); + assert_eq!(std::fs::read_to_string(extracted.directory().join("main.jsx")).unwrap(), text); +} + +// Export + +const EXPORTED: [&str; 5] = ["My Widget/image/logo.png", "My Widget/main.jsx", "My Widget/src/a.js", "My Widget/src/b.js", "My Widget/widget.json"]; + +#[test] +fn export_skips_caches_dot_files_and_links_and_sorts_entries() { + let fixture = Fixture::new(); + let archive_bytes = export(&fixture.sample_package()); + assert_eq!(entry_names(&archive_bytes), EXPORTED); + assert_eq!(archive::preflight(&archive_bytes, &ArchiveLimits::default()).unwrap(), EXPORTED); + let contents = read_zip(&archive_bytes); + assert_eq!(contents[1].1, "$render(阅读);".as_bytes()); + assert_eq!(contents[0].1.len(), 2048); +} + +#[test] +fn export_is_deterministic() { + let fixture = Fixture::new(); + let root = fixture.sample_package(); + let first = export(&root); + let main = std::fs::File::options().write(true).open(root.join("main.jsx")).unwrap(); + main.set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_000_000_000)).unwrap(); + assert_eq!(export(&root), first); +} + +#[test] +fn export_then_import_round_trips_every_file() { + let fixture = Fixture::new(); + let root = fixture.sample_package(); + let destination = fixture.library.join("Copy"); + assert_eq!(fixture.import_to(&export(&root), &destination).unwrap().as_deref(), Some("My Widget")); + let original = DiskPackageFiles::new(&root); + let copy = DiskPackageFiles::new(&destination); + assert_eq!(original.paths(), copy.paths()); + assert_eq!(copy.paths().len(), 5); + for path in original.paths() { + assert_eq!(original.read_bytes(&path), copy.read_bytes(&path), "{path}"); + } + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn export_applies_limits_and_sanitizes_the_root_name() { + let fixture = Fixture::new(); + let root = fixture.sample_package(); + assert_eq!(entry_names(&export_with(&root, "Ratio 16:9", &ArchiveLimits::default()).unwrap())[0], "Ratio 16-9/image/logo.png"); + assert_code("file_too_large", export_with(&root, "W", &limits(|limits| limits.maximum_file_bytes = 1024))); + assert_code("package_too_large", export_with(&root, "W", &limits(|limits| limits.maximum_expanded_bytes = 2048))); + assert_code("too_many_entries", export_with(&root, "W", &limits(|limits| limits.maximum_file_count = 4))); + assert_code("archive_size", export_with(&root, "W", &limits(|limits| limits.maximum_archive_bytes = 512))); + assert_code("archive_no_package", export_with(&fixture.temporary.new_folder("empty"), "W", &ArchiveLimits::default())); + // A file nested 16 deep lists on disk, but under the archive's root directory it would be 17 components. + write_file(&root.join(nested("d", 15)).join("deep.txt"), b"deep"); + assert!(DiskPackageFiles::new(&root).paths().contains(&(nested("d", 15) + "/deep.txt"))); + let mut output = Vec::new(); + assert_code("invalid_path", archive::export(&root, "W", &mut output, &BTreeMap::new(), &ArchiveLimits::default())); + assert!(output.is_empty(), "nothing is written unless the whole archive was produced"); +} + +#[test] +fn a_file_that_cannot_be_an_archive_entry_stops_the_export() { + let fixture = Fixture::new(); + let root = fixture.sample_package(); + // As in the Android test: the name lists on disk, but an archive entry may not hold a colon. + write_file(&root.join("a:b.txt"), b"colon"); + assert!(DiskPackageFiles::new(&root).paths().contains(&"a:b.txt".to_owned())); + assert_code("invalid_path", export_with(&root, "W", &ArchiveLimits::default())); +} + +#[test] +fn extra_files_are_added_and_replace_disk_files() { + let fixture = Fixture::new(); + let root = fixture.sample_package(); + let mut output = Vec::new(); + archive::export(&root, "W", &mut output, &support::extras(&[("widget.json", "{\"generated\":true}"), ("notes/new.txt", "new")]), &ArchiveLimits::default()).unwrap(); + let contents: BTreeMap> = read_zip(&output).into_iter().collect(); + assert_eq!(contents["W/widget.json"], b"{\"generated\":true}"); + assert_eq!(contents["W/notes/new.txt"], b"new"); + assert_eq!(contents.len(), 6); + for path in ["../escape.txt", "/abs.txt", ".hidden", "__Build/x.js", "a\\b"] { + assert_code("invalid_path", archive::export(&root, "W", &mut Vec::new(), &support::extras(&[(path, "x")]), &ArchiveLimits::default())); + } +} + +#[test] +fn checksum_matches_the_zip_crc32() { + assert_eq!(archive::crc32(b"123456789"), 0xCBF4_3926); + assert_eq!(archive::crc32(b""), 0); + assert_eq!(support::crc32(b"123456789"), 0xCBF4_3926); +} + +// Other ZIP software + +fn run(command: &mut std::process::Command) -> String { + let output = command.output().unwrap_or_else(|error| panic!("{command:?}: {error}")); + assert!(output.status.success(), "{command:?}: {}{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr)); + String::from_utf8(output.stdout).unwrap() +} + +/// The archives this crate reads and writes are checked everywhere else with +/// fixtures and a reader written for these tests. This one asks software that +/// shares no code with it: Info-ZIP and Python's `zipfile`. +#[test] +#[ignore = "needs the zip, unzip and python3 commands; run with `cargo test -- --ignored`"] +fn other_zip_software_reads_exports_and_writes_importable_archives() { + use std::process::Command; + let fixture = Fixture::new(); + let root = fixture.sample_package(); + let exported = fixture.temporary.path("exported.swt"); + std::fs::write(&exported, export(&root)).unwrap(); + + // Reading an export. + run(Command::new("unzip").arg("-t").arg(&exported)); + assert_eq!(run(Command::new("unzip").arg("-Z1").arg(&exported)).lines().collect::>(), EXPORTED); + let listed = run(Command::new("python3").args(["-c", "import sys, zipfile\nz = zipfile.ZipFile(sys.argv[1])\nassert z.testzip() is None\nprint('\\n'.join(z.namelist()))"]).arg(&exported)); + assert_eq!(listed.lines().collect::>(), EXPORTED); + let unpacked = fixture.temporary.new_folder("unpacked"); + run(Command::new("unzip").arg("-q").arg(&exported).arg("-d").arg(&unpacked)); + let original = DiskPackageFiles::new(&root); + for path in original.paths() { + assert_eq!(std::fs::read(unpacked.join("My Widget").join(&path)).ok(), original.read_bytes(&path), "{path}"); + } + + // Importing what they write: directory entries, extra fields, stored and deflated files. + let made = fixture.temporary.new_folder("made"); + write_file(&made.join("Widget/widget.json"), manifest("Widget").as_bytes()); + write_file(&made.join("Widget/main.jsx"), "$render(阅读);".as_bytes()); + write_file(&made.join("Widget/src/data.txt"), "compressible ".repeat(10_000).as_bytes()); + write_file(&made.join("Widget/image/tiny.bin"), &[0, 1, 2]); + let by_zip = fixture.temporary.path("by-zip.zip"); + run(Command::new("zip").current_dir(&made).arg("-r").arg("-q").arg(&by_zip).arg("Widget")); + let by_python = fixture.temporary.path("by-python.zip"); + let script = "import os, sys, zipfile\nwith zipfile.ZipFile(sys.argv[1], 'w', zipfile.ZIP_DEFLATED) as z:\n for base, _, names in os.walk('Widget'):\n for name in names:\n z.write(os.path.join(base, name))"; + run(Command::new("python3").current_dir(&made).args(["-c", script]).arg(&by_python)); + for written in [&by_zip, &by_python] { + let extracted = fixture.extract(&std::fs::read(written).unwrap()).unwrap_or_else(|error| panic!("{written:?}: {error}")); + assert_eq!(extracted.root_name(), Some("Widget")); + assert_eq!(tree(extracted.directory()), ["image/", "image/tiny.bin", "main.jsx", "src/", "src/data.txt", "widget.json"], "{written:?}"); + for path in ["widget.json", "main.jsx", "src/data.txt", "image/tiny.bin"] { + assert_eq!(std::fs::read(extracted.directory().join(path)).unwrap(), std::fs::read(made.join("Widget").join(path)).unwrap(), "{written:?} {path}"); + } + } + assert!(tree(&fixture.staging).is_empty()); +} diff --git a/Linux/crates/sw-core/tests/packages_delete_file.rs b/Linux/crates/sw-core/tests/packages_delete_file.rs new file mode 100644 index 00000000..f084a153 --- /dev/null +++ b/Linux/crates/sw-core/tests/packages_delete_file.rs @@ -0,0 +1,57 @@ +//! Removing one file of an installed package (`PackageStore::delete_file`), +//! which `scriptwidget dev` uses to make an installed package follow a folder. + +#![allow(dead_code)] + +#[path = "packages_support/mod.rs"] +mod support; + +use support::{SOURCE, TemporaryFolder, assert_code, hard_link, symlink, write_file}; +use sw_core::packages::PackageStore; + +#[test] +fn removes_only_regular_files_inside_the_package() { + let temporary = TemporaryFolder::new(); + let library = temporary.path("library"); + let store = PackageStore::new(&library, temporary.path("staging")); + store.create("Widget", SOURCE, None).unwrap(); + store.write_file("Widget", "lib/util.js", b"1").unwrap(); + store.write_file("Widget", "notes.txt", b"2").unwrap(); + let package = library.join("Widget"); + + store.delete_file("Widget", "lib/util.js").unwrap(); + assert!(!package.join("lib/util.js").exists()); + assert!(package.join("lib").is_dir(), "the directory stays"); + // Already gone, or never there, is not an error. + store.delete_file("Widget", "lib/util.js").unwrap(); + store.delete_file("Widget", "nowhere/at/all.js").unwrap(); + assert_code("package_not_found", store.delete_file("Missing", "main.jsx")); + + // Nothing outside the package, no dot-file, no cache. + let outside = temporary.new_file("outside.txt", "keep"); + for path in ["../outside.txt", "/etc/hostname", "lib/../../outside.txt", "a\\b", "", ".scriptwidget-installation", "__Build/prepared.json", "lib/.hidden"] { + assert_code("invalid_path", store.delete_file("Widget", path)); + } + assert!(outside.exists()); + + // A link is refused and its target stays; so does a file with a second hard link, and a directory. + symlink(&package.join("link.txt"), &outside); + assert_code("unsafe_file", store.delete_file("Widget", "link.txt")); + assert!(outside.exists() && package.join("link.txt").is_symlink()); + std::fs::remove_file(package.join("link.txt")).unwrap(); + hard_link(&package.join("hard.txt"), &outside); + assert_code("unsafe_file", store.delete_file("Widget", "hard.txt")); + assert_eq!(std::fs::read_to_string(&outside).unwrap(), "keep"); + std::fs::remove_file(package.join("hard.txt")).unwrap(); + assert_code("unsafe_file", store.delete_file("Widget", "lib")); + + // A directory on the way that is a link is not walked through. + let elsewhere = temporary.new_folder("elsewhere"); + write_file(&elsewhere.join("victim.js"), b"keep"); + symlink(&package.join("linked"), &elsewhere); + assert_code("unsafe_file", store.delete_file("Widget", "linked/victim.js")); + assert!(elsewhere.join("victim.js").exists()); + + store.delete_file("Widget", "notes.txt").unwrap(); + assert!(!package.join("notes.txt").exists() && package.join("main.jsx").exists()); +} diff --git a/Linux/crates/sw-core/tests/packages_disk_files.rs b/Linux/crates/sw-core/tests/packages_disk_files.rs new file mode 100644 index 00000000..411415d9 --- /dev/null +++ b/Linux/crates/sw-core/tests/packages_disk_files.rs @@ -0,0 +1,233 @@ +//! The read-only view of a package directory. Ported from `DiskPackageFilesTests.cs`. + +#[path = "packages_support/mod.rs"] +mod support; + +use std::path::PathBuf; + +use support::{TemporaryFolder, assert_code, hard_link, nested, symlink, write_file}; +use sw_core::PackageFiles; +use sw_core::packages::DiskPackageFiles; +use sw_core::packages::limits::{MAX_BINARY_READ_BYTES, MAX_FILE_COUNT, MAX_SCRIPT_BYTES, MAX_TEXT_BYTES}; + +struct Fixture { + temporary: TemporaryFolder, + root: PathBuf, + outside: PathBuf, + files: DiskPackageFiles, +} + +impl Fixture { + fn new() -> Self { + let temporary = TemporaryFolder::new(); + let root = temporary.new_folder("library/Widget"); + let outside = temporary.new_folder("outside"); + write_file(&outside.join("secret.txt"), b"secret"); + write_file(&outside.join("secret.js"), b"secret()"); + let fixture = Self { files: DiskPackageFiles::new(&root), temporary, root, outside }; + fixture.write("main.jsx", b"$render();"); + fixture.write("widget.json", b"{}"); + fixture.write("src/util/helper.js", b"export const a = 1;"); + fixture.write("image/logo.png", b"PNG"); + fixture + } + + fn write(&self, path: &str, bytes: &[u8]) { + write_file(&self.root.join(path), bytes); + } + + fn link(&self, path: &str, target: PathBuf) { + symlink(&self.root.join(path), &target); + } + + fn assert_refused(&self, path: &str) { + assert!(!self.files.exists(path), "{path:?} exists"); + assert_eq!(self.files.read_text(path), None, "{path:?}"); + assert_eq!(self.files.read_bytes(path), None, "{path:?}"); + } +} + +const LISTED: [&str; 4] = ["image/logo.png", "main.jsx", "src/util/helper.js", "widget.json"]; + +#[test] +fn lists_regular_files_as_sorted_normalized_paths() { + let fixture = Fixture::new(); + assert_eq!(fixture.files.paths(), LISTED); + assert_eq!(fixture.files.list().unwrap(), LISTED); +} + +#[test] +fn listing_skips_dot_files_build_cache_and_links() { + let fixture = Fixture::new(); + fixture.write(".DS_Store", b"x"); + fixture.write(".git/config", b"x"); + fixture.write("src/.cache/tmp.js", b"x"); + fixture.write("__Build/main.js", b"compiled"); + fixture.write("src/__build/main.js", b"compiled"); + fixture.link("linked.js", fixture.outside.join("secret.js")); + fixture.link("linkedDir", fixture.outside.clone()); + fixture.link("dangling.js", fixture.outside.join("missing.js")); + assert_eq!(fixture.files.paths(), LISTED); +} + +#[test] +fn reads_existing_files() { + let fixture = Fixture::new(); + assert!(fixture.files.exists("main.jsx")); + assert!(fixture.files.exists("src/util/helper.js")); + assert_eq!(fixture.files.read_text("src/util/helper.js").as_deref(), Some("export const a = 1;")); + assert_eq!(fixture.files.read_bytes("image/logo.png").as_deref(), Some(&b"PNG"[..])); + assert_eq!(fixture.files.read_bytes("widget.json").as_deref(), Some(&b"{}"[..])); +} + +#[test] +fn rejects_paths_that_are_not_normalized_package_relative_paths() { + let fixture = Fixture::new(); + write_file(&fixture.root.parent().unwrap().join("sibling.js"), b"sibling"); + let absolute_inside = fixture.root.join("main.jsx").to_string_lossy().into_owned(); + let absolute_outside = fixture.outside.join("secret.js").to_string_lossy().into_owned(); + let too_deep = nested("d", 17) + ".js"; + let too_long = "x".repeat(256) + ".js"; + let unsafe_paths = [ + "", + "/", + ".", + "..", + "../sibling.js", + "../Widget/main.jsx", + "src/../main.jsx", + "src/../../sibling.js", + "./main.jsx", + "src//util/helper.js", + "main.jsx/", + "/main.jsx", + &absolute_inside, + &absolute_outside, + "src\\util\\helper.js", + "..\\sibling.js", + "main.jsx\0", + "main\0.jsx", + "C:main.jsx", + "src/util/helper.js\n", + &too_deep, + &too_long, + ]; + for path in unsafe_paths { + fixture.assert_refused(path); + } +} + +#[test] +fn exists_is_true_only_for_regular_files() { + let fixture = Fixture::new(); + assert!(!fixture.files.exists("src")); + assert!(!fixture.files.exists("src/util")); + assert!(!fixture.files.exists("missing.js")); + assert!(!fixture.files.exists("main.jsx/child.js")); + assert_eq!(fixture.files.read_bytes("src"), None); +} + +#[test] +fn rejects_symbolic_links_at_every_component() { + let fixture = Fixture::new(); + fixture.link("linked.js", fixture.outside.join("secret.js")); + fixture.link("linkedDir", fixture.outside.clone()); + fixture.link("src/deep/link", fixture.outside.clone()); + fixture.link("inside.js", fixture.root.join("main.jsx")); + fixture.link("relative.js", PathBuf::from("main.jsx")); + fixture.link("dangling.js", fixture.outside.join("missing.js")); + for path in ["linked.js", "linkedDir/secret.js", "linkedDir/secret.txt", "src/deep/link/secret.js", "inside.js", "relative.js", "dangling.js", "linkedDir"] { + fixture.assert_refused(path); + } +} + +#[test] +fn rejects_a_package_root_that_is_itself_a_link() { + let fixture = Fixture::new(); + let alias = fixture.temporary.path("alias"); + symlink(&alias, &fixture.root); + let linked = DiskPackageFiles::new(&alias); + assert!(linked.paths().is_empty()); + assert!(!linked.exists("main.jsx")); + assert_eq!(linked.read_text("main.jsx"), None); + assert_eq!(linked.read_bytes("main.jsx"), None); + assert!(DiskPackageFiles::new(fixture.temporary.path("missing")).paths().is_empty()); +} + +#[test] +fn rejects_hard_linked_files() { + let fixture = Fixture::new(); + hard_link(&fixture.root.join("hard.js"), &fixture.outside.join("secret.js")); + fixture.assert_refused("hard.js"); + assert!(!fixture.files.paths().contains(&"hard.js".to_owned())); + // A second link from inside the package is as much a second name as one from outside. + hard_link(&fixture.root.join("twin.jsx"), &fixture.root.join("main.jsx")); + fixture.assert_refused("twin.jsx"); + fixture.assert_refused("main.jsx"); +} + +#[test] +fn rejects_case_aliases_and_hidden_or_cache_paths() { + let fixture = Fixture::new(); + fixture.write(".env", b"TOKEN=1"); + fixture.write("__Build/main.js", b"compiled"); + for path in ["MAIN.jsx", "Main.JSX", "SRC/util/helper.js", "Widget.json", ".env", "__Build/main.js", "__build/main.js"] { + assert!(!fixture.files.exists(path), "{path}"); + assert_eq!(fixture.files.read_bytes(path), None, "{path}"); + } +} + +#[test] +fn read_text_returns_only_bounded_utf8_text_files() { + let fixture = Fixture::new(); + fixture.write("data.bin", b"text in a binary file"); + fixture.write("bad.txt", &[0x66, 0xC3, 0x28]); + fixture.write("big.jsx", &vec![b'a'; MAX_SCRIPT_BYTES + 1]); + fixture.write("edge.jsx", &vec![b'a'; MAX_SCRIPT_BYTES]); + fixture.write("EDGE.MJS", &vec![b'a'; MAX_SCRIPT_BYTES + 1]); + fixture.write("notes.txt", &vec![b'a'; MAX_SCRIPT_BYTES + 1]); + fixture.write("huge.txt", &vec![b'a'; MAX_TEXT_BYTES + 1]); + fixture.write("unicode.md", "阅读 👩‍💻".as_bytes()); + assert_eq!(fixture.files.read_text("image/logo.png"), None); + assert_eq!(fixture.files.read_text("data.bin"), None); + assert_eq!(fixture.files.read_text("bad.txt"), None); + assert_eq!(fixture.files.read_text("big.jsx"), None); + assert_eq!(fixture.files.read_text("EDGE.MJS"), None); + assert_eq!(fixture.files.read_text("edge.jsx").map(|text| text.len()), Some(MAX_SCRIPT_BYTES)); + assert_eq!(fixture.files.read_text("notes.txt").map(|text| text.len()), Some(MAX_SCRIPT_BYTES + 1)); + assert_eq!(fixture.files.read_text("huge.txt"), None); + assert_eq!(fixture.files.read_text("unicode.md").as_deref(), Some("阅读 👩‍💻")); + assert_eq!(fixture.files.read_bytes("huge.txt").map(|bytes| bytes.len()), Some(MAX_TEXT_BYTES + 1)); +} + +#[test] +fn read_bytes_is_capped_at_sixteen_mebibytes() { + let fixture = Fixture::new(); + fixture.write("edge.bin", &vec![0; MAX_BINARY_READ_BYTES]); + fixture.write("big.bin", &vec![0; MAX_BINARY_READ_BYTES + 1]); + assert_eq!(fixture.files.read_bytes("edge.bin").map(|bytes| bytes.len()), Some(MAX_BINARY_READ_BYTES)); + assert_eq!(fixture.files.read_bytes("big.bin"), None); + assert!(fixture.files.exists("big.bin")); + assert_eq!(fixture.files.read_bytes_up_to("big.bin", MAX_BINARY_READ_BYTES as u64 + 1).map(|bytes| bytes.len()), Some(MAX_BINARY_READ_BYTES + 1)); +} + +#[test] +fn listing_fails_closed_beyond_the_entry_limit() { + let fixture = Fixture::new(); + for index in 0..MAX_FILE_COUNT { + fixture.write(&format!("many/f{index}.txt"), b"x"); + } + assert_code("too_many_entries", fixture.files.list()); + // Through the runtime's view, a package with too many files has none: it cannot be compiled or run. + assert!(fixture.files.paths().is_empty()); +} + +#[test] +fn listing_stops_at_the_depth_limit() { + let fixture = Fixture::new(); + fixture.write(&(nested("d", 15) + "/ok.js"), b"x"); + fixture.write(&(nested("e", 16) + "/deep.js"), b"x"); + let listed = fixture.files.paths(); + assert!(listed.iter().any(|path| path.ends_with("/ok.js"))); + assert!(!listed.iter().any(|path| path.ends_with("/deep.js"))); +} diff --git a/Linux/crates/sw-core/tests/packages_names.rs b/Linux/crates/sw-core/tests/packages_names.rs new file mode 100644 index 00000000..69fcaaa2 --- /dev/null +++ b/Linux/crates/sw-core/tests/packages_names.rs @@ -0,0 +1,123 @@ +//! Package names. Ported from `PackageNamesTests.cs`, itself +//! `testNamesPreserveUnicodeButRejectDirectoryAndCacheEscapes` and +//! `packageNames.test.js`. + +use sw_core::packages::names; + +fn repeat(text: &str, count: usize) -> String { + text.repeat(count) +} + +#[test] +fn rejects_names_that_could_become_traversal_or_reserved_cache_paths() { + // The Windows list also holds a name with an unpaired surrogate, which a Rust string cannot hold. + let invalid = [ + "".to_owned(), + " ".into(), + "\t\n".into(), + ".".into(), + "..".into(), + "../outside".into(), + "/outside".into(), + "folder/child".into(), + "folder\\child".into(), + "__Build".into(), + "__BUILD".into(), + "__build".into(), + ".hidden".into(), + "name\0bad".into(), + "name\nbad".into(), + "name\u{202E}bad".into(), + "C:relative".into(), + "c:\\windows".into(), + repeat("a", 81), + repeat("😀", 61), + repeat("漢", 80) + "x", + ]; + for name in invalid { + assert!(!names::is_valid(&name), "{name:?}"); + } +} + +#[test] +fn accepts_unicode_display_names() { + // "Ratio 16:9" is in the Android list and not in the Windows one: only Win32 reads a colon as stream syntax. + let valid = [ + "My Widget".to_owned(), + "阅读记录".into(), + "👩‍💻 Focus".into(), + "e\u{301}".into(), + repeat("e\u{301}", 80), + repeat("😀", 60), + "Foo.Bar".into(), + repeat("a", 80), + repeat("漢", 80), + "Ratio 16:9".into(), + "Ratio 16-9".into(), + "Build".into(), + "__Builder".into(), + "नमस्ते\u{200D}क".into(), + ]; + for name in valid { + assert!(names::is_valid(&name), "{name:?}"); + } +} + +#[test] +fn byte_limit_applies_independently_of_grapheme_limit() { + // 61 four-byte scalars are 244 UTF-8 bytes; joined into fewer graphemes they still exceed 240 bytes. + assert!(!names::is_valid(&repeat("😀", 61))); + assert!(names::is_valid(&repeat("漢", 80))); + assert_eq!(repeat("漢", 80).len(), 240); +} + +#[test] +fn identity_folds_case_and_unicode_normalization() { + assert_eq!(names::identity("Café"), names::identity("CAFE\u{301}")); + assert_eq!(names::identity("__BUILD"), "__build"); + assert_ne!(names::identity("Widget"), names::identity("Widget 2")); + // Compared in upper case, as NTFS does: the long s and the Kelvin sign meet their ASCII letters. + assert_eq!(names::identity("s"), names::identity("\u{17F}")); + assert_eq!(names::identity("k"), names::identity("\u{212A}")); + assert_eq!(names::identity("STRASSE"), names::identity("strasse")); +} + +#[test] +fn suffix_truncates_the_stem_by_graphemes_to_keep_limits() { + assert_eq!(names::with_suffix("Widget", " (1)"), "Widget (1)"); + let long_name = repeat("a", 80); + assert_eq!(names::with_suffix(&long_name, " (1)"), repeat("a", 76) + " (1)"); + assert_eq!(names::with_suffix(&long_name, " Remix"), repeat("a", 74) + " Remix"); + let wide = names::with_suffix(&repeat("漢", 80), " (10000)"); + assert!(names::is_valid(&wide), "{wide}"); + assert!(wide.ends_with(" (10000)")); + assert_eq!(names::grapheme_count(&wide), 72 + 8); + assert_eq!(names::with_suffix(&repeat("e\u{301}", 80), " (1)"), repeat("e\u{301}", 76) + " (1)"); + // A family emoji is one grapheme of many bytes: the byte limit shortens the stem further. + let family = names::with_suffix(&repeat("👨‍👩‍👧‍👦", 12), " (1)"); + assert!(names::is_valid(&family) && family.ends_with(" (1)"), "{family}"); + assert_eq!(names::grapheme_count(&family), 9 + 4); +} + +#[test] +fn legacy_identifiers_and_archive_roots_match_the_shared_engine() { + assert_eq!(names::legacy_id("My Widget!"), "legacy.my-widget"); + assert_eq!(names::legacy_id("阅读记录"), "legacy.widget"); + assert_eq!(names::legacy_id(" A--b 2 "), "legacy.a-b-2"); + assert_eq!(names::archive_root_name("Ratio 16:9"), "Ratio 16-9"); + assert_eq!(names::archive_root_name("..."), "Widget"); + assert_eq!(names::archive_root_name("a/b\\c*d?e\"fh|i"), "a-b-c-d-e-f-g-h-i"); + assert_eq!(names::archive_root_name(&repeat("a", 100)), repeat("a", 80)); + // 80 UTF-16 units, never half a pair: forty emoji. + assert_eq!(names::archive_root_name(&repeat("😀", 50)), repeat("😀", 40)); +} + +#[test] +fn control_and_format_characters_are_found_except_the_joiners() { + for text in ["a\u{0}b", "a\u{1F}b", "a\u{7F}b", "a\u{85}b", "a\u{202E}b", "a\u{200B}b", "a\u{FEFF}b", "a\u{E0001}b"] { + assert!(names::has_control_characters(text), "{text:?}"); + } + for text in ["plain", "a\u{200C}b", "a\u{200D}b", "阅读 👩‍💻", "e\u{301}"] { + assert!(!names::has_control_characters(text), "{text:?}"); + } +} diff --git a/Linux/crates/sw-core/tests/packages_security.rs b/Linux/crates/sw-core/tests/packages_security.rs new file mode 100644 index 00000000..e51e9ccf --- /dev/null +++ b/Linux/crates/sw-core/tests/packages_security.rs @@ -0,0 +1,799 @@ +//! The package security rules of `docs/linux/plan.md` section 6, one test per +//! rule. The Linux counterpart of `WindowsPackageSecurityTests.cs`: where +//! that file tests what Win32 does to names, this one tests what a Unix +//! filesystem allows (symbolic links, hard links, special files, file modes, +//! names that are not UTF-8, two spellings of one name side by side). +//! +//! The descriptor-level rules (`openat2` with `RESOLVE_BENEATH`, and its +//! fallback) are tested next to the code, in `src/packages/safe_files.rs`. + +#[path = "packages_support/mod.rs"] +mod support; + +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, OnceLock}; + +use support::{RawEntry, SOURCE, TemporaryFolder, assert_code, entry, hard_link, manifest, mode, nested, package_zip, permissions_are_not_enforced, raw_zip, set_mode, symlink, tree, widget_zip, write_file}; +use sw_core::compiler::PrepareOptions; +use sw_core::json::ObjectExt; +use sw_core::packages::{ArchiveLimits, PackageError, PackageStore, archive, manifests, names, path_rules, validate_tree}; +use sw_core::{ExecutionRequest, PackageCompiler, PackageFiles, ScriptExecutor, SharedScripts}; + +struct Fixture { + temporary: TemporaryFolder, + library: PathBuf, + staging: PathBuf, + store: PackageStore, +} + +impl Fixture { + fn new() -> Self { + let temporary = TemporaryFolder::new(); + let library = temporary.path("library"); + let staging = temporary.path("staging"); + Self { store: PackageStore::new(&library, &staging), temporary, library, staging } + } + + /// A store with one package, "Widget". + fn with_widget() -> Self { + let fixture = Self::new(); + fixture.store.create("Widget", SOURCE, None).unwrap(); + fixture + } + + fn in_library(&self, path: &str) -> PathBuf { + self.library.join(path) + } + + #[cfg(target_os = "linux")] + fn find_manifest_name(&self, name: &str) -> Option { + self.store.list().unwrap().into_iter().find(|package| package.name == name).and_then(|package| package.manifest_name) + } + + fn names(&self) -> Vec { + self.store.list().unwrap().into_iter().map(|package| package.name).collect() + } + + fn import(&self, archive: &[u8]) -> Result { + self.store.import_archive(&mut &archive[..], None, None).map(|package| package.name) + } + + fn exported_names(&self, name: &str) -> Vec { + let mut output = Vec::new(); + self.store.export_archive(name, &mut output).unwrap(); + archive::preflight(&output, &ArchiveLimits::default()).unwrap() + } + + /// The path is refused for reading and for writing, and nothing on disk changes. + #[track_caller] + fn assert_invalid_path(&self, path: &str) { + let before = tree(&self.temporary.root); + assert!(path_rules::components(path).is_none(), "{path:?}"); + assert!(path_rules::visible_components(path).is_none(), "{path:?}"); + assert_code("invalid_path", self.store.write_file("Widget", path, b"owned")); + assert_code("invalid_path", self.store.read_file("Widget", path)); + let files = self.store.files("Widget").unwrap(); + assert!(!files.exists(path), "{path:?}"); + assert_eq!(files.read_bytes(path), None, "{path:?}"); + assert_eq!(files.read_text(path), None, "{path:?}"); + assert_eq!(tree(&self.temporary.root), before, "{path:?}"); + } + + /// The archive is refused by the preflight, by a bare import and by the store, and nothing is left anywhere. + #[track_caller] + fn assert_archive_refused(&self, code: &str, archive_bytes: &[u8]) { + let before = tree(&self.library); + assert_code(code, archive::preflight(archive_bytes, &ArchiveLimits::default())); + assert_code(code, archive::import_to(&mut &archive_bytes[..], &self.staging, &self.library.join("Target"), &ArchiveLimits::default())); + assert_code(code, self.import(archive_bytes)); + assert_eq!(tree(&self.library), before); + assert!(tree(&self.staging).is_empty()); + } +} + +fn default_limits() -> ArchiveLimits { + ArchiveLimits::default() +} + +/// The name alone, as a directory, and below a directory. +fn in_every_segment(name: &str) -> [String; 4] { + [name.to_owned(), format!("{name}/main.jsx"), format!("src/{name}"), format!("src/{name}/deep/x.js")] +} + +// Paths: traversal, absolute paths, backslashes + +#[test] +fn traversal_is_refused_for_reads_writes_and_archives() { + let fixture = Fixture::with_widget(); + write_file(&fixture.in_library("Sibling/secret.js"), b"secret"); + for path in ["..", "../Sibling/secret.js", "src/../../Sibling/secret.js", "src/../main.jsx", "./main.jsx", "src/./a.js", "src//a.js", "main.jsx/..", "a/../../../etc/passwd"] { + fixture.assert_invalid_path(path); + } + for name in ["../Sibling/owned.js", "Widget/../../Sibling/owned.js", "Widget/../main.jsx", "..", "Widget/./main.jsx", "./main.jsx", "Widget/a/../../../escape"] { + fixture.assert_archive_refused("archive_unsafe_path", &raw_zip(&[RawEntry::new("Widget/main.jsx", SOURCE), RawEntry::new(name, "owned")])); + } + assert_eq!(tree(&fixture.in_library("Sibling")), ["secret.js"]); +} + +#[test] +fn absolute_paths_are_refused_for_reads_writes_and_archives() { + let fixture = Fixture::with_widget(); + write_file(&fixture.in_library("Sibling/secret.js"), b"secret"); + let inside = fixture.in_library("Widget/main.jsx").to_string_lossy().into_owned(); + let outside = fixture.in_library("Sibling/secret.js").to_string_lossy().into_owned(); + for path in ["/", "/etc/passwd", "/main.jsx", "//server/share/x.js", &inside, &outside] { + fixture.assert_invalid_path(path); + } + let absolute_entry = format!("{}/owned.js", fixture.in_library("Sibling").to_string_lossy()); + for name in ["/etc/passwd", "/Widget/main.jsx", "//server/share/x.js", &absolute_entry] { + fixture.assert_archive_refused("archive_unsafe_path", &raw_zip(&[RawEntry::new("Widget/main.jsx", SOURCE), RawEntry::new(name, "owned")])); + } + assert_eq!(tree(&fixture.in_library("Sibling")), ["secret.js"]); +} + +#[test] +fn backslashes_are_refused_for_reads_writes_and_archives() { + let fixture = Fixture::with_widget(); + // On Linux a backslash is an ordinary character of a file name; in a package it never is. + for path in ["src\\x.js", "..\\Sibling\\x.js", "\\x", "a\\..\\b", "C:\\x", "\\\\server\\share\\x.js", "\\\\?\\C:\\x"] { + fixture.assert_invalid_path(path); + } + for name in ["Widget\\main2.jsx", "\\Widget\\main.jsx", "Widget/..\\..\\escape.txt", "Widget/a\\b.js"] { + fixture.assert_archive_refused("archive_unsafe_path", &raw_zip(&[RawEntry::new("Widget/main.jsx", SOURCE), RawEntry::new(name, "owned")])); + } + // A file with such a name that got into the directory some other way is not part of the package either. + write_file(&fixture.in_library("Widget/back\\slash.js"), b"x"); + assert_eq!(fixture.store.files("Widget").unwrap().paths(), ["main.jsx", "widget.json"]); + assert_code("invalid_path", validate_tree(&fixture.in_library("Widget"), &default_limits())); + assert_code("invalid_path", fixture.store.duplicate("Widget")); +} + +#[test] +fn control_characters_drive_prefixes_and_overlong_names_are_refused() { + let fixture = Fixture::with_widget(); + for name in ["a\u{1}b", "a\u{1F}b", "a\tb", "a\rb", "line\nbreak.js", "bidi\u{202E}.js", "zero\u{200B}width.js"] { + for path in in_every_segment(name) { + fixture.assert_invalid_path(&path); + } + } + // What Windows would read as a drive: refused at the start of a path, as on every platform. + for path in ["C:x.js", "c:", "Z:main.jsx", "C:x/main.jsx", "C:/Widget/main.jsx"] { + fixture.assert_invalid_path(path); + } + fixture.assert_invalid_path("main\0.jsx"); + assert_eq!(path_rules::components(&"x".repeat(255)).map(|parts| parts.len()), Some(1)); + fixture.assert_invalid_path(&"x".repeat(256)); + fixture.assert_invalid_path(&format!("src/{}/a.js", "x".repeat(256))); + // 86 three-byte characters are 258 UTF-8 bytes, more than a Linux file name holds. + fixture.assert_invalid_path(&"漢".repeat(86)); + assert!(path_rules::components(&"漢".repeat(85)).is_some()); + assert_eq!(path_rules::components(&nested("d", 16)).map(|parts| parts.len()), Some(16)); + fixture.assert_invalid_path(&nested("d", 17)); +} + +// Names that only Windows refuses + +#[test] +fn names_only_win32_cannot_store_are_ordinary_names_as_on_android() { + let fixture = Fixture::with_widget(); + let store = &fixture.store; + // Reserved device names, trailing dots and spaces, and the characters Win32 reserves. + let windows_only = ["CON", "nul.txt", "COM1.jsx", "aux", "name.", "name ", "a.js", "what?.js", "star*.js", "pipe|line", "say \"hi\".txt"]; + for name in windows_only { + assert!(path_rules::valid_component(name), "{name:?}"); + let path = format!("data/{name}"); + store.write_file("Widget", &path, name.as_bytes()).unwrap(); + assert_eq!(store.read_file("Widget", &path).unwrap(), name.as_bytes(), "{name:?}"); + } + assert_eq!(store.files("Widget").unwrap().paths().len(), 2 + windows_only.len()); + validate_tree(&fixture.in_library("Widget"), &default_limits()).unwrap(); + + // They survive an export and an import, as they do between two Android devices. + let mut exported = Vec::new(); + store.export_archive("Widget", &mut exported).unwrap(); + let copy = store.import_archive(&mut &exported[..], None, None).unwrap(); + assert_eq!(store.files(©.name).unwrap().paths(), store.files("Widget").unwrap().paths()); + + // The same goes for package names; the manifest name is the directory name, unchanged. + for name in ["Ratio 16:9", "Notes?", "a*b", "Trailing.", "CON", "nul.txt"] { + assert!(names::is_valid(name), "{name:?}"); + assert_eq!(fixture.import(&package_zip("W", name, &[])).unwrap(), name); + } + assert_eq!(store.rename("Notes?", "What?").unwrap().name, "What?"); +} + +#[test] +fn a_colon_in_an_archive_entry_is_refused_on_every_platform() { + let fixture = Fixture::with_widget(); + // Drive and stream syntax on Windows: every importer refuses it, so an archive holding it is not a package anywhere. + for name in ["Widget/a:b.txt", "Widget/main.jsx:stream", "Widget/main.jsx::$DATA", "C:/Widget/x.js", "C:x.js", "Widget/:x", "Widget/sub:dir/x.js"] { + fixture.assert_archive_refused("archive_unsafe_path", &raw_zip(&[RawEntry::new("Widget/main.jsx", SOURCE), RawEntry::new(name, "owned")])); + } + // Entries below __MACOSX, __Build and dot-directories are never extracted, but the whole archive is still refused. + for name in ["__MACOSX/Widget/a:b", "Widget/__Build/a:b.js", "Widget/.git/a:b", "Widget/.cache/../x", "__MACOSX/../x", "Widget/.hidden/a\\b"] { + fixture.assert_archive_refused("archive_unsafe_path", &raw_zip(&[RawEntry::new("Widget/main.jsx", SOURCE), RawEntry::new(name, "x")])); + } +} + +// Symbolic links + +#[test] +fn symbolic_links_are_refused_at_every_component_and_never_followed() { + let fixture = Fixture::with_widget(); + let store = &fixture.store; + let outside = fixture.temporary.new_folder("outside"); + write_file(&outside.join("secret.js"), b"secret()"); + symlink(&fixture.in_library("Widget/out"), &outside); + symlink(&fixture.in_library("Widget/src/deep/out"), &outside); + symlink(&fixture.in_library("Widget/self"), &fixture.in_library("Widget")); + symlink(&fixture.in_library("Widget/up"), Path::new("..")); + symlink(&fixture.in_library("Widget/secret.js"), &outside.join("secret.js")); + symlink(&fixture.in_library("Widget/relative.jsx"), Path::new("main.jsx")); + symlink(&fixture.in_library("Widget/dangling.js"), &outside.join("created.js")); + symlink(&fixture.in_library("Aliased"), &fixture.in_library("Widget")); + assert!(fixture.in_library("Widget/out/secret.js").exists(), "the links work for a program that follows them"); + + let files = store.files("Widget").unwrap(); + assert_eq!(files.paths(), ["main.jsx", "widget.json"]); + for path in ["out/secret.js", "src/deep/out/secret.js", "self/main.jsx", "up/Widget/main.jsx", "secret.js", "relative.jsx", "dangling.js", "out", "self"] { + assert!(!files.exists(path), "{path}"); + assert_eq!(files.read_bytes(path), None, "{path}"); + assert_eq!(files.read_text(path), None, "{path}"); + assert_code("file_not_found", store.read_file("Widget", path)); + } + for path in ["out/new.js", "out/sub/new.js", "src/deep/out/new.js", "self/main.jsx", "up/owned.js", "secret.js", "relative.jsx", "dangling.js", "out"] { + assert_code("unsafe_file", store.write_file("Widget", path, b"owned")); + } + assert_eq!(tree(&outside), ["secret.js"]); + assert_eq!(std::fs::read(outside.join("secret.js")).unwrap(), b"secret()"); + assert_eq!(std::fs::read_to_string(fixture.in_library("Widget/main.jsx")).unwrap(), SOURCE); + + assert_code("unsafe_file", validate_tree(&fixture.in_library("Widget"), &default_limits())); + assert_code("unsafe_file", validate_tree(&fixture.in_library("Aliased"), &default_limits())); + assert_code("unsafe_file", store.duplicate("Widget")); + assert_eq!(fixture.names(), ["Widget"]); + assert_code("package_not_found", store.files("Aliased")); + assert_code("package_not_found", store.write_file("Aliased", "main.jsx", b"owned")); + assert_code("package_not_found", store.delete("Aliased")); + + // Export leaves the links out; delete removes them without touching their targets. + assert_eq!(fixture.exported_names("Widget"), ["Widget/main.jsx", "Widget/widget.json"]); + store.delete("Widget").unwrap(); + assert!(!fixture.in_library("Widget").exists()); + assert_eq!(tree(&outside), ["secret.js"]); + assert_eq!(std::fs::read(outside.join("secret.js")).unwrap(), b"secret()"); + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn staging_and_library_roots_must_not_be_links() { + let fixture = Fixture::new(); + let real = fixture.temporary.new_folder("real-staging"); + let alias = fixture.temporary.path("staging-alias"); + symlink(&alias, &real); + let linked = PackageStore::new(&fixture.library, &alias); + assert_code("storage_unavailable", linked.create("Widget", SOURCE, None)); + assert_code("storage_unavailable", linked.import_archive(&mut &widget_zip()[..], None, None)); + assert_code("storage_unavailable", archive::extract(&mut &widget_zip()[..], &alias, &default_limits())); + assert!(tree(&real).is_empty()); + assert!(linked.list().unwrap().is_empty()); + + let real_library = fixture.temporary.new_folder("real-library"); + let library_alias = fixture.temporary.path("library-alias"); + symlink(&library_alias, &real_library); + let linked = PackageStore::new(&library_alias, &fixture.staging); + assert_code("storage_unavailable", linked.list()); + assert_code("storage_unavailable", linked.create("Widget", SOURCE, None)); + assert_code("storage_unavailable", linked.import_archive(&mut &widget_zip()[..], None, None)); + assert!(tree(&real_library).is_empty()); + assert!(tree(&fixture.staging).is_empty()); +} + +// Hard links + +#[test] +fn hard_linked_files_are_refused_before_anything_is_read() { + let fixture = Fixture::with_widget(); + let store = &fixture.store; + let secret = fixture.temporary.new_file("outside/secret.js", "secret()"); + hard_link(&fixture.in_library("Widget/hard.js"), &secret); + hard_link(&fixture.in_library("Widget/src/deep/hard.js"), &secret); + + let files = store.files("Widget").unwrap(); + assert_eq!(files.paths(), ["main.jsx", "widget.json"]); + for path in ["hard.js", "src/deep/hard.js"] { + assert!(!files.exists(path), "{path}"); + assert_eq!(files.read_bytes(path), None, "{path}"); + assert_eq!(files.read_text(path), None, "{path}"); + assert_code("file_not_found", store.read_file("Widget", path)); + // Writing would replace only the name inside the package, but the name is not a package file to begin with. + assert_code("unsafe_file", store.write_file("Widget", path, b"owned")); + } + assert_code("unsafe_file", validate_tree(&fixture.in_library("Widget"), &default_limits())); + assert_code("unsafe_file", store.duplicate("Widget")); + assert_eq!(fixture.exported_names("Widget"), ["Widget/main.jsx", "Widget/widget.json"]); + assert_eq!(std::fs::read_to_string(&secret).unwrap(), "secret()"); + + // The manifest itself with a second link is no manifest that can be read: the package is not exported as legacy. + hard_link(&fixture.temporary.path("outside/manifest-copy.json"), &fixture.in_library("Widget/widget.json")); + let mut output = Vec::new(); + assert_code("invalid_manifest", store.export_archive("Widget", &mut output)); + assert!(output.is_empty()); + + store.delete("Widget").unwrap(); + assert_eq!(std::fs::read_to_string(&secret).unwrap(), "secret()"); +} + +// Special files + +#[test] +fn sockets_and_fifos_are_never_opened() { + let fixture = Fixture::with_widget(); + let store = &fixture.store; + support::make_socket(&fixture.in_library("Widget/socket")); + support::make_fifo(&fixture.in_library("Widget/pipe.txt")); + + // Opening a FIFO for reading blocks until a writer appears; these calls return at once because it is never opened. + let files = store.files("Widget").unwrap(); + assert_eq!(files.paths(), ["main.jsx", "widget.json"]); + for path in ["socket", "pipe.txt"] { + assert!(!files.exists(path), "{path}"); + assert_eq!(files.read_bytes(path), None, "{path}"); + assert_eq!(files.read_text(path), None, "{path}"); + assert_code("file_not_found", store.read_file("Widget", path)); + assert_code("unsafe_file", store.write_file("Widget", path, b"owned")); + } + assert_code("unsafe_file", validate_tree(&fixture.in_library("Widget"), &default_limits())); + assert_code("unsafe_file", store.duplicate("Widget")); + assert_eq!(fixture.exported_names("Widget"), ["Widget/main.jsx", "Widget/widget.json"]); + store.delete("Widget").unwrap(); + assert!(!fixture.in_library("Widget").exists()); +} + +// Case and normalization collisions. Linux keeps both spellings as two files; +// the default macOS filesystem keeps one, so these run on Linux only. + +#[cfg(target_os = "linux")] +#[test] +fn case_collisions_on_disk_are_ambiguous() { + let fixture = Fixture::with_widget(); + let store = &fixture.store; + write_file(&fixture.in_library("Widget/MAIN.jsx"), b"other"); + write_file(&fixture.in_library("Widget/src/a.js"), b"lower"); + write_file(&fixture.in_library("Widget/SRC/b.js"), b"upper"); + assert_eq!(tree(&fixture.in_library("Widget")).len(), 7); + + let files = store.files("Widget").unwrap(); + assert_eq!(files.paths(), ["widget.json"]); + for path in ["main.jsx", "MAIN.jsx", "src/a.js", "SRC/b.js"] { + assert!(!files.exists(path), "{path}"); + assert_eq!(files.read_bytes(path), None, "{path}"); + assert_code("file_not_found", store.read_file("Widget", path)); + assert_code("unsafe_file", store.write_file("Widget", path, b"owned")); + } + assert_code("unsafe_file", store.write_file("Widget", "Main.jsx", b"owned")); + assert_code("unsafe_file", validate_tree(&fixture.in_library("Widget"), &default_limits())); + assert_code("unsafe_file", store.duplicate("Widget")); + assert_eq!(std::fs::read_to_string(fixture.in_library("Widget/main.jsx")).unwrap(), SOURCE); + assert_eq!(std::fs::read(fixture.in_library("Widget/MAIN.jsx")).unwrap(), b"other"); + + // The same in the library: two directories that differ in case are not packages, and their name is taken. + std::fs::create_dir(fixture.in_library("WIDGET")).unwrap(); + assert!(fixture.names().is_empty()); + assert_code("package_not_found", store.files("Widget")); + assert_code("package_not_found", store.files("WIDGET")); + assert_code("package_exists", store.create("widget", SOURCE, None)); + assert_eq!(store.unique_name("widget").unwrap(), "widget (1)"); + // A wrongly cased manifest next to the real one is an error, not a second file. + store.create("Other", SOURCE, None).unwrap(); + write_file(&fixture.in_library("Other/Widget.JSON"), b"{}"); + assert_code("invalid_manifest_case", store.export_archive("Other", &mut Vec::new())); + assert_code("unsafe_file", store.duplicate("Other")); + assert_eq!(fixture.find_manifest_name("Other"), None, "with two spellings present, neither is read as the manifest"); +} + +#[cfg(target_os = "linux")] +#[test] +fn normalization_aliases_on_disk_are_ambiguous() { + let fixture = Fixture::with_widget(); + let store = &fixture.store; + let composed = "caf\u{E9}.js"; + let decomposed = "cafe\u{301}.js"; + write_file(&fixture.in_library(&format!("Widget/{composed}")), b"composed"); + write_file(&fixture.in_library(&format!("Widget/{decomposed}")), b"decomposed"); + assert_eq!(tree(&fixture.in_library("Widget")).len(), 4); + + let files = store.files("Widget").unwrap(); + assert_eq!(files.paths(), ["main.jsx", "widget.json"]); + for name in [composed, decomposed] { + assert!(!files.exists(name)); + assert_code("file_not_found", store.read_file("Widget", name)); + assert_code("unsafe_file", store.write_file("Widget", name, b"owned")); + } + assert_code("unsafe_file", validate_tree(&fixture.in_library("Widget"), &default_limits())); + assert_eq!(std::fs::read(fixture.in_library(&format!("Widget/{composed}"))).unwrap(), b"composed"); +} + +#[cfg(target_os = "linux")] +#[test] +fn names_that_are_not_utf8_are_never_part_of_a_package() { + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + let fixture = Fixture::with_widget(); + let store = &fixture.store; + let foreign = OsStr::from_bytes(b"bad\xFF.js"); + std::fs::write(fixture.in_library("Widget").join(foreign), b"x").unwrap(); + std::fs::create_dir(fixture.library.join(OsStr::from_bytes(b"Pack\xFFage"))).unwrap(); + + assert_eq!(store.files("Widget").unwrap().paths(), ["main.jsx", "widget.json"]); + assert_eq!(fixture.names(), ["Widget"]); + assert_code("invalid_path", validate_tree(&fixture.in_library("Widget"), &default_limits())); + assert_code("invalid_path", store.duplicate("Widget")); + assert_eq!(fixture.exported_names("Widget"), ["Widget/main.jsx", "Widget/widget.json"]); + store.delete("Widget").unwrap(); + assert!(!fixture.in_library("Widget").exists()); +} + +// Archives: preflight before extraction, unique temporary directory, transactional install + +#[test] +fn the_whole_archive_is_checked_before_anything_is_written() { + let fixture = Fixture::new(); + // Staging cannot be used at all: its place is taken by a file. + let blocked = fixture.temporary.new_file("blocked", ""); + let mut entries: Vec = (0..300).map(|index| RawEntry::new(&format!("Widget/f{index}.js"), "x")).collect(); + entries.push(RawEntry::new("Widget/main.jsx", SOURCE)); + // A well-formed archive gets as far as the staging directory. + assert_code("storage_unavailable", archive::extract(&mut &raw_zip(&entries)[..], &blocked, &default_limits())); + // One bad entry at the very end, and the archive is refused before staging is even looked at. + for (code, last) in [ + ("archive_unsafe_path", RawEntry::new("Widget/../../escape.js", "owned")), + ("archive_symlink", RawEntry::new("Widget/link", "/etc/passwd").mode(support::SYMBOLIC_LINK)), + ("archive_path_collision", RawEntry::new("Widget/F0.js", "x")), + ("archive_file_too_large", RawEntry::new("Widget/big.bin", "x").declared_size(26 * 1024 * 1024)), + ] { + let mut bad = entries.clone(); + bad.push(last); + assert_code(code, archive::extract(&mut &raw_zip(&bad)[..], &blocked, &default_limits())); + } + assert_eq!(std::fs::read(&blocked).unwrap(), b""); +} + +#[test] +fn archive_limits_hold_for_entry_count_file_size_and_expanded_size() { + let fixture = Fixture::new(); + let tight = ArchiveLimits { maximum_file_count: 3, maximum_file_bytes: 1000, maximum_expanded_bytes: 1500, ..ArchiveLimits::default() }; + let store = PackageStore::with_limits(&fixture.library, &fixture.staging, tight.clone()); + let import = |entries: &[RawEntry]| store.import_archive(&mut &raw_zip(entries)[..], None, None); + let main = || RawEntry::new("W/main.jsx", SOURCE); + assert_code("archive_too_many_entries", import(&[main(), entry("W/a"), entry("W/b"), entry("W/c")])); + assert_code("archive_file_too_large", import(&[main(), RawEntry::new("W/big", vec![0; 1001])])); + assert_code("archive_expanded_too_large", import(&[main(), RawEntry::new("W/a", vec![0; 1000]), RawEntry::new("W/b", vec![0; 1000])])); + assert!(tree(&fixture.library).is_empty() && tree(&fixture.staging).is_empty()); + // At the limits the package installs: a file of exactly 1000 bytes, and three files with the generated manifest. + assert_eq!(import(&[main(), RawEntry::new("W/a", vec![0; 1000])]).unwrap().name, "W"); + assert_eq!(tree(&fixture.in_library("W")), ["a", "main.jsx", "widget.json"]); + validate_tree(&fixture.in_library("W"), &tight).unwrap(); + // The limits also hold for what is already on disk: a file grown behind the store's back. + write_file(&fixture.in_library("W/a"), &[0; 1001]); + assert_code("file_too_large", validate_tree(&fixture.in_library("W"), &tight)); + assert_code("file_too_large", store.duplicate("W")); + write_file(&fixture.in_library("W/a"), &[0; 1000]); + write_file(&fixture.in_library("W/b"), &[0; 100]); + assert_code("too_many_entries", validate_tree(&fixture.in_library("W"), &tight)); + assert_code("too_many_entries", store.duplicate("W")); + assert_code("too_many_entries", store.export_archive("W", &mut Vec::new())); + std::fs::remove_file(fixture.in_library("W/widget.json")).unwrap(); + write_file(&fixture.in_library("W/b"), &[0; 1000]); + assert_code("package_too_large", validate_tree(&fixture.in_library("W"), &tight)); + assert_code("package_too_large", store.duplicate("W")); + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn each_extraction_gets_a_unique_private_directory_that_is_removed_afterwards() { + let fixture = Fixture::new(); + let first = archive::extract(&mut &widget_zip()[..], &fixture.staging, &default_limits()).unwrap(); + let second = archive::extract(&mut &widget_zip()[..], &fixture.staging, &default_limits()).unwrap(); + assert_ne!(first.directory(), second.directory()); + for extracted in [&first, &second] { + let work = extracted.directory().parent().unwrap(); + assert_eq!(work.parent().unwrap(), fixture.staging); + let name = work.file_name().unwrap().to_str().unwrap(); + assert!(name.starts_with("import-") && name.len() == "import-".len() + 36, "{name}"); + assert_eq!(mode(work), 0o700); + assert_eq!(mode(extracted.directory()), 0o700); + assert_eq!(mode(&extracted.directory().join("main.jsx")), 0o600); + } + assert_eq!(mode(&fixture.staging), 0o700); + assert_eq!(tree(&fixture.staging).len(), 2 * 4); + drop(first); + assert_eq!(tree(&fixture.staging).len(), 4); + drop(second); + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn publishing_never_replaces_whatever_holds_the_name() { + let fixture = Fixture::new(); + let import_to = |destination: &Path| archive::import_to(&mut &widget_zip()[..], &fixture.staging, destination, &default_limits()); + // An empty directory is the one thing a plain rename(2) would silently replace. + let empty = fixture.temporary.new_folder("library/Empty"); + assert_code("package_exists", import_to(&empty)); + assert!(tree(&empty).is_empty()); + let file = fixture.temporary.new_file("library/File", "mine"); + assert_code("package_exists", import_to(&file)); + assert_eq!(std::fs::read_to_string(&file).unwrap(), "mine"); + let dangling = fixture.library.join("Dangling"); + symlink(&dangling, &fixture.temporary.path("elsewhere")); + assert_code("package_exists", import_to(&dangling)); + assert!(!fixture.temporary.path("elsewhere").exists(), "nothing was installed through the link"); + // A destination whose directory does not exist cannot be published to, and nothing is created for it. + assert_code("storage_unavailable", import_to(&fixture.library.join("missing/Widget"))); + assert!(!fixture.library.join("missing").exists()); + assert!(tree(&fixture.staging).is_empty()); +} + +/// A rename cannot cross filesystems, so a staging directory on another one +/// must be refused instead of falling back to a copy that is not atomic. +#[cfg(target_os = "linux")] +#[test] +fn staging_on_another_filesystem_is_refused() { + use std::os::unix::fs::MetadataExt; + let disk = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("cross-filesystem-{}", std::process::id())); + let memory = PathBuf::from(format!("/dev/shm/scriptwidget-tests-cross-filesystem-{}", std::process::id())); + struct Cleanup([PathBuf; 2]); + impl Drop for Cleanup { + fn drop(&mut self) { + for directory in &self.0 { + let _ = std::fs::remove_dir_all(directory); + } + } + } + let _cleanup = Cleanup([disk.clone(), memory.clone()]); + std::fs::create_dir_all(&disk).unwrap(); + std::fs::create_dir_all(&memory).unwrap(); + assert_ne!(std::fs::metadata(&disk).unwrap().dev(), std::fs::metadata(&memory).unwrap().dev(), "this test needs /dev/shm and the build directory on two filesystems"); + let store = PackageStore::new(disk.join("library"), memory.join("staging")); + assert_code("storage_unavailable", store.create("Widget", SOURCE, None)); + let error = store.import_archive(&mut &widget_zip()[..], None, None).expect_err("cross-filesystem import"); + assert_eq!(error.code, "storage_unavailable"); + assert_eq!(error.message, "Staging and library directories must share a filesystem."); + assert!(tree(&disk.join("library")).is_empty()); + assert!(tree(&memory.join("staging")).is_empty()); + // Deleting needs no rename into staging to succeed: the package is removed where it is. + write_file(&disk.join("library/Manual/main.jsx"), SOURCE.as_bytes()); + store.delete("Manual").unwrap(); + assert!(tree(&disk.join("library")).is_empty()); + assert!(tree(&memory.join("staging")).is_empty()); +} + +#[test] +fn an_install_is_all_or_nothing() { + let fixture = Fixture::with_widget(); + let before = tree(&fixture.library); + // The last entry is the one that fails, after the others were extracted: by its checksum, and by its size. + let mut corrupt = raw_zip(&[RawEntry::new("New/widget.json", manifest("New")), RawEntry::new("New/a.js", "a"), RawEntry::new("New/main.jsx", SOURCE)]); + let position = corrupt.windows(SOURCE.len()).position(|window| window == SOURCE.as_bytes()).unwrap(); + corrupt[position] = b'#'; + assert_code("archive_corrupt", fixture.import(&corrupt)); + let lying = raw_zip(&[RawEntry::new("New/widget.json", manifest("New")), RawEntry::new("New/main.jsx", SOURCE), RawEntry::new("New/z.bin", vec![1; 4096]).declared_size(8)]); + assert_code("archive_size_mismatch", fixture.import(&lying)); + // A veto after extraction and a manifest that cannot be read are no different. + let veto = |_: &dyn PackageFiles| -> Result<(), PackageError> { Err(PackageError::new("vetoed", "no")) }; + assert_code("vetoed", fixture.store.import_archive(&mut &package_zip("New", "New", &[])[..], None, Some(&veto))); + assert_eq!(tree(&fixture.library), before); + assert!(tree(&fixture.staging).is_empty()); + // And a package that does install appears complete, under its own name, next to the untouched one. + assert_eq!(fixture.import(&package_zip("New", "New", &[("src/a.js", "a")])).unwrap(), "New"); + assert_eq!(tree(&fixture.in_library("New")), ["main.jsx", "src/", "src/a.js", "widget.json"]); + assert_eq!(std::fs::read_to_string(fixture.in_library("Widget/main.jsx")).unwrap(), SOURCE); +} + +// Manifests + +#[test] +fn a_manifest_that_is_present_but_malformed_fails_closed() { + let fixture = Fixture::new(); + let store = &fixture.store; + let malformed: [&[u8]; 9] = [b"", b"{bad", b"[1]", b"\"text\"", b"{\"name\":\"W\",}", b"{\"name\":\"W\"} x", b"// c\n{\"name\":\"W\"}", b"{\"name\":\"W\",\"name\":\"X\"}", &[0x7B, 0xC3, 0x28, 0x7D]]; + for (index, content) in malformed.iter().enumerate() { + // On disk, with a perfectly good main.jsx next to it: the package is never treated as legacy. + let name = format!("Broken {index}"); + write_file(&fixture.in_library(&format!("{name}/widget.json")), content); + write_file(&fixture.in_library(&format!("{name}/main.jsx")), SOURCE.as_bytes()); + let listed = store.list().unwrap().into_iter().find(|package| package.name == name).unwrap(); + assert!(listed.has_manifest, "{name}"); + assert_eq!((listed.manifest_id, listed.manifest_name, listed.manifest_version), (None, None, None), "{name}"); + let mut output = Vec::new(); + assert_code("invalid_manifest", store.export_archive(&name, &mut output)); + assert!(output.is_empty()); + assert_code("invalid_manifest", store.duplicate(&name)); + // In an archive. + assert_code("invalid_manifest", fixture.import(&raw_zip(&[RawEntry::new("W/widget.json", *content), RawEntry::new("W/main.jsx", SOURCE)]))); + } + assert_eq!(fixture.names().len(), malformed.len()); + assert!(tree(&fixture.staging).is_empty()); + + // The reader itself. + assert!(manifests::parse(None).is_none()); + assert!(manifests::parse(Some("\u{FEFF}{}")).is_none(), "a byte order mark is not JSON"); + assert!(manifests::parse(Some("{\"a\":{\"b\":1,\"b\":2}}")).is_none(), "a duplicate key at any depth"); + assert!(manifests::parse(Some(&format!("{}1{}", "[".repeat(100_000), "]".repeat(100_000)))).is_none(), "nesting is bounded"); + let parsed = manifests::parse(Some("{\"name\":\"W\",\"n\":1.5,\"big\":12345678901234567890,\"list\":[true,null,{\"k\":\"v\"}]}")).unwrap(); + assert_eq!(manifests::string_value(&parsed, "name").as_deref(), Some("W")); + assert_eq!(manifests::string_value(&parsed, "n"), None); + assert_eq!(sw_core::json::stringify_object(&parsed), "{\"name\":\"W\",\"n\":1.5,\"big\":12345678901234567890,\"list\":[true,null,{\"k\":\"v\"}]}"); +} + +fn scripts() -> Arc { + static SCRIPTS: OnceLock> = OnceLock::new(); + SCRIPTS.get_or_init(|| Arc::new(SharedScripts::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets/scriptwidget")).expect("shared scripts"))).clone() +} + +fn compiler() -> &'static PackageCompiler { + static COMPILER: OnceLock = OnceLock::new(); + COMPILER.get_or_init(|| PackageCompiler::new(scripts()).expect("compiler thread")) +} + +#[test] +fn shared_manifest_validator_plugs_into_the_import_hook() { + let fixture = Fixture::new(); + let validate = |files: &dyn PackageFiles| -> Result<(), PackageError> { + let result = compiler().validate_manifest(&files.read_text("widget.json").unwrap(), &files.paths()).map_err(|error| PackageError::new("invalid_manifest", error.message))?; + if result.opt_bool("ok", false) { Ok(()) } else { Err(PackageError::new("invalid_manifest", sw_core::json::stringify_object(&result))) } + }; + let import = |archive: &[u8]| fixture.store.import_archive(&mut &archive[..], None, Some(&validate)); + assert_eq!(import(&widget_zip()).unwrap().name, "Widget"); + let unknown_key = manifest("Extra").replace("\"permissions\":[]", "\"permissions\":[],\"surprise\":true"); + let unsupported = manifest("Future").replace("\"formatVersion\":2", "\"formatVersion\":99"); + let missing_entry = manifest("NoEntry").replace("\"main.jsx\"", "\"missing.jsx\""); + for manifest in [unknown_key, unsupported, missing_entry] { + assert_code("invalid_manifest", import(&raw_zip(&[RawEntry::new("W/widget.json", manifest), RawEntry::new("W/main.jsx", SOURCE)]))); + } + assert_eq!(fixture.names(), ["Widget"]); + assert!(tree(&fixture.staging).is_empty()); +} + +// $file + +/// `$file` and `$import` of the shared engine read through `DiskPackageFiles`. +#[test] +fn the_runtime_reads_package_files_from_disk_and_cannot_leave_the_package() { + let fixture = Fixture::new(); + let store = &fixture.store; + let main = "$import('lib/label.js'); + const read = (path) => { try { return $file.read(path); } catch (error) { return 'threw'; } }; + $render({label($file.readJSON('data.json').name)}|{read('../Secret/secret.txt')}|{read('link.txt')}|{read('linkdir/secret.txt')}|{read('hard.txt')}|{read('/etc/hostname')}|{String($file.exists('link.txt'))}|{String($file.exists('data.json'))});"; + store.create("Widget", main, None).unwrap(); + store.write_file("Widget", "lib/label.js", b"function label(name) { return 'name=' + name; }").unwrap(); + store.write_file("Widget", "data.json", b"{\"name\":\"widget\"}").unwrap(); + let secret = fixture.temporary.new_file("library/Secret/secret.txt", "TOP-SECRET"); + symlink(&fixture.in_library("Widget/link.txt"), &secret); + symlink(&fixture.in_library("Widget/linkdir"), secret.parent().unwrap()); + hard_link(&fixture.in_library("Widget/hard.txt"), &secret); + + let files = store.files("Widget").unwrap(); + let prepared = compiler().prepare(&files, &PrepareOptions::default()).expect("prepare"); + let request = ExecutionRequest::new(&prepared, &files); + let result = ScriptExecutor::new(scripts()).execute(&request); + let rendered = result.tree.unwrap_or_else(|| panic!("no tree: {:?}", result.error)); + assert!(rendered.contains(r#""children":["name=widget","|","","|","","|","","|","","|","","|","false","|","true"]"#), "{rendered}"); + assert!(!rendered.contains("TOP-SECRET")); +} + +// Permissions + +#[test] +fn everything_the_store_writes_is_private_to_the_user() { + let fixture = Fixture::with_widget(); + let store = &fixture.store; + store.write_file("Widget", "src/deep/util.js", b"util").unwrap(); + store.write_file("Widget", "main.jsx", b"replaced").unwrap(); + store.duplicate("Widget").unwrap(); + fixture.import(&package_zip("Imported", "Imported", &[("data/a.json", "{}")])).unwrap(); + assert_eq!(fixture.names(), ["Imported", "Widget", "Widget Remix"]); + fn check(directory: &Path) { + for entry in std::fs::read_dir(directory).unwrap() { + let path = entry.unwrap().path(); + if path.is_dir() { + assert_eq!(mode(&path), 0o700, "{path:?}"); + check(&path); + } else { + assert_eq!(mode(&path), 0o600, "{path:?}"); + } + } + } + check(&fixture.library); + assert_eq!(mode(&fixture.library), 0o700); + assert_eq!(mode(&fixture.staging), 0o700); +} + +#[test] +fn a_file_the_user_cannot_read_is_not_a_package_file() { + let fixture = Fixture::with_widget(); + if permissions_are_not_enforced(&fixture.temporary) { + eprintln!("SKIPPED a_file_the_user_cannot_read_is_not_a_package_file: running as root, file modes are not enforced"); + return; + } + let store = &fixture.store; + store.write_file("Widget", "locked.js", b"locked").unwrap(); + set_mode(&fixture.in_library("Widget/locked.js"), 0o000); + let files = store.files("Widget").unwrap(); + assert!(!files.exists("locked.js")); + assert_eq!(files.read_bytes("locked.js"), None); + assert_code("file_not_found", store.read_file("Widget", "locked.js")); + // It still counts as a file of the directory, so an export or a copy fails instead of silently dropping it. + assert_code("file_too_large", store.export_archive("Widget", &mut Vec::new())); + assert_code("unsafe_file", store.duplicate("Widget")); + // A directory that cannot be searched hides its files the same way. + store.write_file("Widget", "sealed/inner.js", b"inner").unwrap(); + set_mode(&fixture.in_library("Widget/sealed"), 0o000); + assert!(!files.exists("sealed/inner.js")); + assert_code("unsafe_file", store.write_file("Widget", "sealed/new.js", b"owned")); + assert_code("unsafe_file", validate_tree(&fixture.in_library("Widget"), &default_limits())); + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn directories_that_cannot_be_written_fail_cleanly() { + let fixture = Fixture::with_widget(); + if permissions_are_not_enforced(&fixture.temporary) { + eprintln!("SKIPPED directories_that_cannot_be_written_fail_cleanly: running as root, file modes are not enforced"); + return; + } + let store = &fixture.store; + // A read-only library: nothing can be published, and nothing stays in staging. + set_mode(&fixture.library, 0o500); + assert_code("storage_unavailable", store.create("Second", SOURCE, None)); + let error = store.import_archive(&mut &package_zip("W", "Second", &[])[..], None, None).expect_err("read-only library"); + assert_eq!(error.code, "storage_unavailable", "{error}"); + assert_code("storage_unavailable", store.duplicate("Widget")); + assert_code("storage_unavailable", store.rename("Widget", "Renamed")); + assert_eq!(fixture.names(), ["Widget"]); + assert!(tree(&fixture.staging).is_empty()); + set_mode(&fixture.library, 0o700); + + // A read-only package directory: the old content stays, and no temporary file is left. + set_mode(&fixture.in_library("Widget"), 0o500); + assert_code("storage_unavailable", store.write_file("Widget", "main.jsx", b"owned")); + assert_code("unsafe_file", store.write_file("Widget", "new/file.js", b"owned")); + assert_eq!(store.read_file("Widget", "main.jsx").unwrap(), SOURCE.as_bytes()); + assert_eq!(tree(&fixture.in_library("Widget")), ["main.jsx", "widget.json"]); + set_mode(&fixture.in_library("Widget"), 0o700); + + // A read-only staging directory: nothing can be prepared, so nothing is installed. + set_mode(&fixture.staging, 0o500); + assert_code("storage_unavailable", store.create("Second", SOURCE, None)); + assert_code("storage_unavailable", store.import_archive(&mut &widget_zip()[..], None, None)); + assert_code("storage_unavailable", store.duplicate("Widget")); + assert_eq!(fixture.names(), ["Widget"]); + set_mode(&fixture.staging, 0o700); +} + +#[test] +fn delete_removes_directories_that_lost_their_permissions() { + let fixture = Fixture::with_widget(); + let store = &fixture.store; + store.write_file("Widget", "locked/deep/a.js", b"a").unwrap(); + store.write_file("Widget", "sealed/b.js", b"b").unwrap(); + set_mode(&fixture.in_library("Widget/locked/deep"), 0o500); + set_mode(&fixture.in_library("Widget/locked"), 0o500); + set_mode(&fixture.in_library("Widget/sealed"), 0o000); + store.delete("Widget").unwrap(); + assert!(!fixture.in_library("Widget").exists()); + assert!(fixture.names().is_empty()); + assert!(tree(&fixture.staging).is_empty()); +} + +#[test] +fn extra_files_cannot_smuggle_unsafe_paths_into_an_export() { + let fixture = Fixture::with_widget(); + for path in ["../escape.txt", "/abs.txt", "a\\b", ".hidden", "__Build/x.js", "src/../x.js", "C:x"] { + let extras: BTreeMap> = [(path.to_owned(), b"x".to_vec())].into(); + let mut output = Vec::new(); + assert_code("invalid_path", archive::export(&fixture.in_library("Widget"), "Widget", &mut output, &extras, &default_limits())); + assert!(output.is_empty()); + } +} diff --git a/Linux/crates/sw-core/tests/packages_store.rs b/Linux/crates/sw-core/tests/packages_store.rs new file mode 100644 index 00000000..3f7aa19b --- /dev/null +++ b/Linux/crates/sw-core/tests/packages_store.rs @@ -0,0 +1,601 @@ +//! The library of installed packages. Ported from `PackageStoreTests.cs`. + +#[path = "packages_support/mod.rs"] +mod support; + +use std::path::PathBuf; + +use support::{RawEntry, SOURCE, SYMBOLIC_LINK, TemporaryFolder, assert_code, deflated_zip, forge_declared_size, hard_link, manifest, package_zip, raw_zip, symlink, tree, widget_zip, write_file}; +use sw_core::PackageFiles; +use sw_core::json::{ObjectExt, parse_object}; +use sw_core::packages::limits::{MAX_FILE_BYTES, MAX_FILE_COUNT, MAX_METADATA_BYTES, MAX_SCRIPT_BYTES}; +use sw_core::packages::{PackageError, PackageInfo, PackageStore, archive}; + +struct Fixture { + temporary: TemporaryFolder, + library: PathBuf, + staging: PathBuf, + store: PackageStore, +} + +impl Fixture { + fn new() -> Self { + let temporary = TemporaryFolder::new(); + let library = temporary.path("library"); + let staging = temporary.path("staging"); + Self { store: PackageStore::new(&library, &staging), temporary, library, staging } + } + + fn names(&self) -> Vec { + self.store.list().unwrap().into_iter().map(|package| package.name).collect() + } + + fn staging_tree(&self) -> Vec { + tree(&self.staging) + } + + fn import(&self, archive: &[u8], suggested: Option<&str>) -> Result { + self.store.import_archive(&mut &archive[..], suggested, None) + } + + fn export(&self, name: &str) -> Result, PackageError> { + let mut output = Vec::new(); + self.store.export_archive(name, &mut output).map(|()| output) + } + + fn in_library(&self, path: &str) -> PathBuf { + self.library.join(path) + } + + fn write_in_library(&self, path: &str, text: &str) { + write_file(&self.in_library(path), text.as_bytes()); + } + + fn read_text(&self, name: &str, path: &str) -> String { + String::from_utf8(self.store.read_file(name, path).unwrap()).unwrap() + } + + fn find(&self, name: &str) -> PackageInfo { + self.store.list().unwrap().into_iter().find(|package| package.name == name).unwrap() + } +} + +fn is_local_id(id: &str) -> bool { + id.strip_prefix("local.").is_some_and(|uuid| uuid.len() == 36 && uuid.bytes().all(|byte| byte == b'-' || byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))) +} + +// create / list + +#[test] +fn create_writes_source_and_default_manifest_atomically() { + let fixture = Fixture::new(); + let created = fixture.store.create("My Widget", SOURCE, None).unwrap(); + assert_eq!(created.name, "My Widget"); + assert_eq!(created.directory, fixture.library.join("My Widget")); + assert!(created.has_manifest); + assert_eq!(created.manifest_name.as_deref(), Some("My Widget")); + assert_eq!(created.manifest_version.as_deref(), Some("1.0.0")); + assert!(is_local_id(created.manifest_id.as_deref().unwrap()), "{:?}", created.manifest_id); + assert_eq!(std::fs::read_to_string(created.directory.join("main.jsx")).unwrap(), SOURCE); + let manifest_text = std::fs::read_to_string(created.directory.join("widget.json")).unwrap(); + assert!(!manifest_text.contains('\r')); + let manifest = parse_object(&manifest_text).unwrap(); + assert_eq!(manifest.opt_i32("formatVersion", 0), 2); + assert_eq!(manifest.opt_string("runtimeVersion"), "1.0"); + assert_eq!(manifest.opt_string("entry"), "main.jsx"); + assert!(manifest.opt_array("permissions").unwrap().is_empty()); + assert_eq!(tree(&created.directory), ["main.jsx", "widget.json"]); + assert_eq!(fixture.store.list().unwrap(), [created]); + assert!(fixture.staging_tree().is_empty()); +} + +#[test] +fn generated_manifests_use_line_feeds_and_keep_unicode() { + let fixture = Fixture::new(); + let created = fixture.store.create("阅读记录", SOURCE, None).unwrap(); + let text = std::fs::read_to_string(created.directory.join("widget.json")).unwrap(); + assert!(!text.contains('\r')); + assert!(text.ends_with("]\n}\n"), "{text}"); + assert!(text.contains("\"name\": \"阅读记录\""), "{text}"); + assert_eq!( + parse_object(&text).unwrap().keys().collect::>(), + ["formatVersion", "id", "name", "version", "runtimeVersion", "entry", "supportedFamilies", "permissions", "networkDomains", "plugins", "actions", "controls"] + ); +} + +#[test] +fn create_honors_a_supplied_manifest_and_its_entry() { + let fixture = Fixture::new(); + let manifest = manifest("Custom").replace("\"main.jsx\"", "\"src/app.jsx\""); + let created = fixture.store.create("Custom", SOURCE, Some(&manifest)).unwrap(); + assert_eq!(created.manifest_id.as_deref(), Some("com.example.widget")); + assert_eq!(created.manifest_version.as_deref(), Some("1.2.3")); + assert_eq!(std::fs::read_to_string(created.directory.join("widget.json")).unwrap(), manifest); + assert_eq!(std::fs::read_to_string(created.directory.join("src/app.jsx")).unwrap(), SOURCE); +} + +#[test] +fn create_fails_closed_and_leaves_nothing_behind() { + let fixture = Fixture::new(); + let store = &fixture.store; + store.create("Widget", SOURCE, None).unwrap(); + assert_code("package_exists", store.create("Widget", "other", None)); + assert_code("package_exists", store.create("WIDGET", "other", None)); + let too_long = "a".repeat(81); + for name in ["", " ", "..", "../escape", "a/b", "a\\b", ".hidden", "__Build", "C:x", &too_long] { + assert_code("invalid_name", store.create(name, SOURCE, None)); + } + assert_code("invalid_manifest", store.create("Bad", SOURCE, Some("{not json"))); + assert_code("invalid_manifest", store.create("Bad", SOURCE, Some("[]"))); + assert_code("invalid_manifest", store.create("Bad", SOURCE, Some("{\"name\":\"Bad\"}"))); + assert_code("invalid_manifest", store.create("Bad", SOURCE, Some("{\"entry\":\"main.jsx\",\"entry\":\"other.jsx\"}"))); + for entry in ["../main.jsx", "/main.jsx", "a\\\\b.jsx", "main.txt", "widget.json", ".hidden.js", "__Build/main.js", "C:/main.jsx"] { + assert_code("invalid_entry", store.create("Bad", SOURCE, Some(&format!("{{\"entry\":\"{entry}\"}}")))); + } + assert_code("file_too_large", store.create("Big", &"a".repeat(MAX_SCRIPT_BYTES + 1), None)); + assert_code("file_too_large", store.create("Big", SOURCE, Some(&format!("{{\"entry\":\"main.jsx\",\"description\":\"{}\"}}", "d".repeat(MAX_METADATA_BYTES))))); + assert!(std::fs::read_to_string(fixture.in_library("Widget/main.jsx")).unwrap().contains("Hello")); + assert_eq!(fixture.names(), ["Widget"]); + assert_eq!(tree(&fixture.library), ["Widget/", "Widget/main.jsx", "Widget/widget.json"]); + assert!(fixture.staging_tree().is_empty()); +} + +#[test] +fn list_reports_only_real_package_directories() { + let fixture = Fixture::new(); + let store = &fixture.store; + store.create("Beta", SOURCE, None).unwrap(); + store.create("Alpha", SOURCE, None).unwrap(); + fixture.write_in_library("Legacy/main.jsx", SOURCE); + fixture.write_in_library("Broken/widget.json", "{not json"); + fixture.write_in_library("Typed/widget.json", "{\"id\":7,\"name\":[\"x\"],\"version\":\"2.0.0\"}"); + fixture.write_in_library("WrongCase/Widget.JSON", &manifest("WrongCase")); + std::fs::create_dir_all(fixture.in_library(".hidden")).unwrap(); + std::fs::create_dir_all(fixture.in_library("__Build")).unwrap(); + fixture.write_in_library("stray.txt", "file"); + symlink(&fixture.in_library("Linked"), &fixture.in_library("Alpha")); + + assert_eq!(fixture.names(), ["Alpha", "Beta", "Broken", "Legacy", "Typed", "WrongCase"]); + assert!(!fixture.find("Legacy").has_manifest); + assert_eq!(fixture.find("Legacy").manifest_name, None); + assert!(fixture.find("Broken").has_manifest); + assert_eq!(fixture.find("Broken").manifest_id, None); + assert_eq!(fixture.find("Typed").manifest_id, None); + assert_eq!(fixture.find("Typed").manifest_name, None); + assert_eq!(fixture.find("Typed").manifest_version.as_deref(), Some("2.0.0")); + assert!(fixture.find("WrongCase").has_manifest); + assert_eq!(fixture.find("WrongCase").manifest_name, None); + assert_code("package_not_found", store.files("Linked")); + assert_code("package_not_found", store.read_file("Linked", "main.jsx")); + assert_code("package_not_found", store.delete("Linked")); + assert_code("package_not_found", store.files("ALPHA")); +} + +#[test] +fn library_root_must_not_be_a_link() { + let fixture = Fixture::new(); + let real = fixture.temporary.new_folder("real"); + let alias = fixture.temporary.path("alias"); + symlink(&alias, &real); + let linked = PackageStore::new(&alias, &fixture.staging); + assert_code("storage_unavailable", linked.list()); + assert_code("storage_unavailable", linked.create("Widget", SOURCE, None)); + assert_code("storage_unavailable", linked.import_archive(&mut &widget_zip()[..], None, None)); + assert!(tree(&real).is_empty()); + assert!(fixture.staging_tree().is_empty()); +} + +// import + +#[test] +fn import_installs_under_the_manifest_name_not_the_archive_name() { + let fixture = Fixture::new(); + let installed = fixture.import(&package_zip("ArchiveFolder", "Manifest Name", &[("src/a.js", "a")]), Some("FileName")).unwrap(); + assert_eq!(installed.name, "Manifest Name"); + assert_eq!(installed.manifest_name.as_deref(), Some("Manifest Name")); + assert_eq!(installed.manifest_id.as_deref(), Some("com.example.widget")); + assert_eq!(installed.manifest_version.as_deref(), Some("1.2.3")); + assert_eq!(fixture.store.files("Manifest Name").unwrap().paths(), ["main.jsx", "src/a.js", "widget.json"]); + assert_eq!(fixture.names(), ["Manifest Name"]); + assert!(fixture.staging_tree().is_empty()); +} + +#[test] +fn import_picks_a_unique_name_on_collision() { + let fixture = Fixture::new(); + assert_eq!(fixture.import(&widget_zip(), None).unwrap().name, "Widget"); + assert_eq!(fixture.import(&widget_zip(), None).unwrap().name, "Widget (1)"); + assert_eq!(fixture.import(&widget_zip(), None).unwrap().name, "Widget (2)"); + assert_eq!(fixture.import(&package_zip("Widget", "WIDGET", &[]), None).unwrap().name, "WIDGET (3)"); + assert_eq!(parse_object(&fixture.read_text("Widget (1)", "widget.json")).unwrap().opt_string("name"), "Widget"); + assert_eq!(fixture.names(), ["WIDGET (3)", "Widget", "Widget (1)", "Widget (2)"]); +} + +#[test] +fn unique_name_shortens_the_stem_and_skips_case_and_normalization_aliases() { + let fixture = Fixture::new(); + let store = &fixture.store; + assert_eq!(store.unique_name("Fresh").unwrap(), "Fresh"); + let long_name = "a".repeat(80); + store.create(&long_name, SOURCE, None).unwrap(); + assert_eq!(store.unique_name(&long_name).unwrap(), "a".repeat(76) + " (1)"); + store.create("Caf\u{E9}", SOURCE, None).unwrap(); + assert_eq!(store.unique_name("Caf\u{E9}").unwrap(), "Caf\u{E9} (1)"); + assert_eq!(store.unique_name("CAF\u{C9}").unwrap(), "CAF\u{C9} (1)"); + assert_eq!(store.unique_name("Cafe\u{301}").unwrap(), "Cafe\u{301} (1)"); + store.create("Caf\u{E9} (1)", SOURCE, None).unwrap(); + assert_eq!(store.unique_name("Caf\u{E9}").unwrap(), "Caf\u{E9} (2)"); + assert_code("invalid_name", store.unique_name("../x")); + // Compared in upper case: the Kelvin sign is the letter K. + store.create("Kelvin", SOURCE, None).unwrap(); + assert_code("package_exists", store.create("\u{212A}elvin", SOURCE, None)); + assert_eq!(store.unique_name("\u{212A}elvin").unwrap(), "\u{212A}elvin (1)"); +} + +#[test] +fn import_migrates_legacy_packages() { + let fixture = Fixture::new(); + let meta = "{\"description\":\"Old\",\"category\":\"Tools\",\"tags\":[\"a\",\"b\"],\"featured\":true,\"icon\":5}"; + let nested = fixture.import(&raw_zip(&[RawEntry::new("Old Widget/main.jsx", SOURCE), RawEntry::new("Old Widget/meta.json", meta)]), Some("ignored")).unwrap(); + assert_eq!(nested.name, "Old Widget"); + assert!(nested.has_manifest); + assert_eq!(nested.manifest_id.as_deref(), Some("legacy.old-widget")); + let manifest = parse_object(&fixture.read_text("Old Widget", "widget.json")).unwrap(); + assert_eq!(manifest.opt_string("description"), "Old"); + assert_eq!(manifest.opt_string("category"), "Tools"); + assert_eq!(manifest.opt_array("tags").unwrap().len(), 2); + assert!(!manifest.has("featured")); + assert!(!manifest.has("icon")); + assert_eq!(manifest.opt_string("entry"), "main.jsx"); + + assert_eq!(fixture.import(&raw_zip(&[RawEntry::new("main.jsx", SOURCE), RawEntry::new("meta.json", "{broken")]), Some("Shared File")).unwrap().name, "Shared File"); + assert_eq!(fixture.import(&raw_zip(&[RawEntry::new("main.jsx", SOURCE)]), Some("../bad")).unwrap().name, "Imported Widget"); + assert_eq!(fixture.import(&raw_zip(&[RawEntry::new("main.jsx", SOURCE)]), None).unwrap().name, "Imported Widget (1)"); +} + +#[test] +fn rejected_imports_leave_nothing_in_library_or_staging() { + let fixture = Fixture::new(); + fixture.store.create("Existing", SOURCE, None).unwrap(); + let before = tree(&fixture.library); + let main = || RawEntry::new("W/main.jsx", SOURCE); + let with_manifest = |content: &[u8]| raw_zip(&[RawEntry::new("W/widget.json", content), main()]); + let long_root = format!("{}/main.jsx", "n".repeat(81)); + let rejected: Vec<(&str, &[&str], Vec)> = vec![ + ("traversal", &["archive_unsafe_path"], raw_zip(&[main(), RawEntry::new("../Existing/main.jsx", "owned")])), + ("absolute", &["archive_unsafe_path"], raw_zip(&[RawEntry::new("/W/main.jsx", SOURCE)])), + ("backslash", &["archive_unsafe_path"], raw_zip(&[RawEntry::new("W\\main.jsx", SOURCE)])), + ("symlink", &["archive_symlink"], raw_zip(&[main(), RawEntry::new("W/link", "../Existing").mode(SYMBOLIC_LINK)])), + ("collision", &["archive_path_collision"], raw_zip(&[main(), RawEntry::new("W/MAIN.jsx", SOURCE)])), + ("entries", &["archive_too_many_entries"], raw_zip(&(0..501).map(|index| RawEntry::new(&format!("W/f{index}.js"), "x")).collect::>())), + ("file size", &["archive_file_too_large"], raw_zip(&[main().declared_size(MAX_FILE_BYTES + 1)])), + // Both codes: see `assert_lying_size_rejected`. + ("bomb", &["archive_size_mismatch", "archive_corrupt"], forge_declared_size(&deflated_zip(&[("W/main.jsx", SOURCE.as_bytes()), ("W/bomb.bin", &vec![0; 8 * 1024 * 1024])]), 1, 8)), + ("no package", &["archive_no_package"], raw_zip(&[RawEntry::new("A/main.jsx", SOURCE), RawEntry::new("B/main.jsx", SOURCE)])), + ("wrong-case manifest", &["invalid_manifest_case"], raw_zip(&[RawEntry::new("W/Widget.json", manifest("W")), main()])), + ("malformed manifest", &["invalid_manifest"], with_manifest(b"{bad")), + ("non-object manifest", &["invalid_manifest"], with_manifest(b"[1]")), + ("non-UTF-8 manifest", &["invalid_manifest"], with_manifest(&[0x7B, 0xC3, 0x28, 0x7D])), + ("nameless manifest", &["invalid_manifest"], with_manifest(b"{\"name\":5}")), + ("empty manifest", &["invalid_manifest"], with_manifest(b"")), + ("manifest with trailing text", &["invalid_manifest"], with_manifest(b"{\"name\":\"W\"} trailing")), + ("manifest with a comment", &["invalid_manifest"], with_manifest(b"{\"name\":\"W\" /* c */}")), + ("manifest with a duplicate key", &["invalid_manifest"], with_manifest(b"{\"name\":\"W\",\"name\":\"Existing\"}")), + ("oversized manifest", &["invalid_manifest"], with_manifest(format!("{{\"name\":\"W\",\"description\":\"{}\"}}", "d".repeat(MAX_METADATA_BYTES)).as_bytes())), + ("unsafe manifest name", &["invalid_name"], package_zip("Widget", "../Existing", &[])), + ("reserved manifest name", &["invalid_name"], package_zip("Widget", "__Build", &[])), + ("legacy without entry", &["missing_entry"], raw_zip(&[RawEntry::new("W/other.jsx", SOURCE)])), + ("unsafe legacy name", &["invalid_name"], raw_zip(&[RawEntry::new(&long_root, SOURCE)])), + ]; + for (label, codes, archive) in rejected { + let error = fixture.import(&archive, Some("Suggested")).expect_err(label); + assert!(codes.contains(&error.code.as_str()), "{label}: {error}"); + assert_eq!(tree(&fixture.library), before, "{label}"); + assert!(fixture.staging_tree().is_empty(), "{label}"); + } +} + +#[test] +fn validation_hook_runs_before_publication_and_can_veto() { + let fixture = Fixture::new(); + let seen = std::cell::RefCell::new(Vec::new()); + let veto = |files: &dyn PackageFiles| -> Result<(), PackageError> { + *seen.borrow_mut() = files.paths(); + assert!(files.read_text("widget.json").unwrap().contains("\"runtimeVersion\"")); + Err(PackageError::new("unsupported_runtime", "Runtime 9.0 is incompatible with 1.0.")) + }; + assert_code("unsupported_runtime", fixture.store.import_archive(&mut &widget_zip()[..], None, Some(&veto))); + assert_eq!(*seen.borrow(), ["main.jsx", "widget.json"]); + assert!(fixture.names().is_empty()); + assert!(tree(&fixture.library).is_empty()); + assert!(fixture.staging_tree().is_empty()); +} + +#[test] +fn import_never_installs_build_caches_or_hidden_files() { + let fixture = Fixture::new(); + let archive = raw_zip(&[ + RawEntry::new("W/widget.json", manifest("Clean")), + RawEntry::new("W/main.jsx", SOURCE), + RawEntry::new("W/__Build/main.js", "planted"), + RawEntry::new("W/src/__Build/deep.js", "planted"), + RawEntry::new("W/src/ok.js", "ok"), + RawEntry::new("W/.env", "SECRET=1"), + RawEntry::new("__MACOSX/W/._main.jsx", "fork"), + ]); + assert_eq!(fixture.import(&archive, None).unwrap().name, "Clean"); + assert_eq!(tree(&fixture.in_library("Clean")), ["main.jsx", "src/", "src/ok.js", "widget.json"]); +} + +#[test] +fn concurrent_imports_get_names_and_staging_directories_of_their_own() { + let fixture = Fixture::new(); + let archive = widget_zip(); + let mut installed: Vec = std::thread::scope(|scope| { + let workers: Vec<_> = (0..12).map(|_| scope.spawn(|| fixture.import(&archive, None).unwrap().name)).collect(); + workers.into_iter().map(|worker| worker.join().unwrap()).collect() + }); + installed.sort_unstable(); + let mut expected: Vec = std::iter::once("Widget".to_owned()).chain((1..12).map(|index| format!("Widget ({index})"))).collect(); + expected.sort_unstable(); + assert_eq!(installed, expected); + assert_eq!(fixture.names(), expected); + for name in &expected { + assert_eq!(tree(&fixture.in_library(name)), ["main.jsx", "widget.json"], "{name}"); + } + assert!(fixture.staging_tree().is_empty()); +} + +// export + +#[test] +fn export_then_import_round_trips_the_package() { + let fixture = Fixture::new(); + let store = &fixture.store; + store.create("Round Trip", SOURCE, None).unwrap(); + store.write_file("Round Trip", "src/lib/util.js", b"export default 1;").unwrap(); + store.write_file("Round Trip", "image/logo.png", &(0..4096).map(|index| (index * 7) as u8).collect::>()).unwrap(); + fixture.write_in_library("Round Trip/__Build/main.js", "compiled"); + fixture.write_in_library("Round Trip/.DS_Store", "junk"); + symlink(&fixture.in_library("Round Trip/link.js"), &fixture.in_library("Round Trip/main.jsx")); + + let archive_bytes = fixture.export("Round Trip").unwrap(); + assert_eq!(fixture.export("Round Trip").unwrap(), archive_bytes); + assert_eq!( + archive::preflight(&archive_bytes, &Default::default()).unwrap(), + ["Round Trip/image/logo.png", "Round Trip/main.jsx", "Round Trip/src/lib/util.js", "Round Trip/widget.json"] + ); + let copy = fixture.import(&archive_bytes, None).unwrap(); + assert_eq!(copy.name, "Round Trip (1)"); + let original = store.files("Round Trip").unwrap(); + let imported = store.files(©.name).unwrap(); + assert_eq!(original.paths(), imported.paths()); + for path in original.paths() { + assert_eq!(original.read_bytes(&path), imported.read_bytes(&path), "{path}"); + } + assert_eq!(fixture.find("Round Trip").manifest_id, copy.manifest_id); + assert!(fixture.staging_tree().is_empty()); +} + +#[test] +fn export_migrates_legacy_packages_without_touching_the_library() { + let fixture = Fixture::new(); + fixture.write_in_library("Legacy/main.jsx", SOURCE); + fixture.write_in_library("Legacy/meta.json", "{\"description\":\"Old\"}"); + let archive_bytes = fixture.export("Legacy").unwrap(); + assert_eq!(archive::preflight(&archive_bytes, &Default::default()).unwrap(), ["Legacy/main.jsx", "Legacy/meta.json", "Legacy/widget.json"]); + assert!(!fixture.in_library("Legacy/widget.json").exists()); + std::fs::remove_dir_all(fixture.in_library("Legacy")).unwrap(); + let imported = fixture.import(&archive_bytes, None).unwrap(); + assert_eq!(imported.name, "Legacy"); + assert_eq!(imported.manifest_id.as_deref(), Some("legacy.legacy")); + assert_eq!(parse_object(&fixture.read_text("Legacy", "widget.json")).unwrap().opt_string("description"), "Old"); +} + +#[test] +fn export_refuses_invalid_packages() { + let fixture = Fixture::new(); + fixture.write_in_library("Broken/widget.json", "{bad"); + fixture.write_in_library("Broken/main.jsx", SOURCE); + fixture.write_in_library("Cased/WIDGET.json", &manifest("Cased")); + std::fs::create_dir_all(fixture.in_library("Empty")).unwrap(); + let mut output = Vec::new(); + assert_code("invalid_manifest", fixture.store.export_archive("Broken", &mut output)); + assert_code("invalid_manifest_case", fixture.store.export_archive("Cased", &mut output)); + assert_code("missing_entry", fixture.store.export_archive("Empty", &mut output)); + assert_code("package_not_found", fixture.store.export_archive("Missing", &mut output)); + assert_code("invalid_name", fixture.store.export_archive("../Broken", &mut output)); + assert!(output.is_empty()); +} + +// duplicate / rename / delete + +#[test] +fn duplicate_copies_files_and_refreshes_identity() { + let fixture = Fixture::new(); + let store = &fixture.store; + let original = store.create("Widget", SOURCE, None).unwrap(); + store.write_file("Widget", "src/a.js", b"a").unwrap(); + fixture.write_in_library("Widget/__Build/main.js", "compiled"); + fixture.write_in_library("Widget/.DS_Store", "junk"); + + let copy = store.duplicate("Widget").unwrap(); + assert_eq!(copy.name, "Widget Remix"); + assert_eq!(copy.manifest_name.as_deref(), Some("Widget Remix")); + assert_ne!(original.manifest_id, copy.manifest_id); + assert!(is_local_id(copy.manifest_id.as_deref().unwrap())); + assert_eq!(tree(©.directory), ["main.jsx", "src/", "src/a.js", "widget.json"]); + assert_eq!(fixture.read_text("Widget Remix", "main.jsx"), SOURCE); + assert_eq!(fixture.find("Widget").manifest_name.as_deref(), Some("Widget")); + assert_eq!(store.duplicate("Widget").unwrap().name, "Widget Remix (1)"); + assert!(fixture.staging_tree().is_empty()); +} + +#[test] +fn duplicate_keeps_legacy_packages_legacy_and_rejects_unsafe_sources() { + let fixture = Fixture::new(); + let store = &fixture.store; + fixture.write_in_library("Legacy/main.jsx", SOURCE); + let copy = store.duplicate("Legacy").unwrap(); + assert_eq!(copy.name, "Legacy Remix"); + assert!(!copy.has_manifest); + assert_eq!(tree(©.directory), ["main.jsx"]); + + let secret = fixture.temporary.new_file("secret.txt", ""); + symlink(&fixture.in_library("Legacy/link.txt"), &secret); + assert_code("unsafe_file", store.duplicate("Legacy")); + fixture.write_in_library("Broken/widget.json", "{bad"); + assert_code("invalid_manifest", store.duplicate("Broken")); + assert_code("package_not_found", store.duplicate("Missing")); + assert_eq!(fixture.names(), ["Broken", "Legacy", "Legacy Remix"]); + assert!(fixture.staging_tree().is_empty()); +} + +#[test] +fn rename_moves_the_directory_and_refuses_collisions() { + let fixture = Fixture::new(); + let store = &fixture.store; + store.create("Widget", SOURCE, None).unwrap(); + store.create("Other", SOURCE, None).unwrap(); + let renamed = store.rename("Widget", "阅读记录").unwrap(); + assert_eq!(renamed.name, "阅读记录"); + assert_eq!(renamed.manifest_name.as_deref(), Some("Widget")); + assert_eq!(fixture.names(), ["Other", "阅读记录"]); + assert_eq!(store.rename("阅读记录", "阅读记录").unwrap().name, "阅读记录"); + assert_code("package_exists", store.rename("阅读记录", "Other")); + assert_code("package_exists", store.rename("阅读记录", "OTHER")); + assert_code("package_exists", store.rename("Other", "other")); + assert_code("invalid_name", store.rename("Other", "../escape")); + assert_code("invalid_name", store.rename("Other", "__Build")); + assert_code("package_not_found", store.rename("Widget", "Again")); + assert_eq!(fixture.names(), ["Other", "阅读记录"]); +} + +#[test] +fn delete_removes_the_package_without_following_links() { + let fixture = Fixture::new(); + let store = &fixture.store; + store.create("Widget", SOURCE, None).unwrap(); + let keep = fixture.temporary.new_folder("keep"); + std::fs::write(keep.join("precious.txt"), "keep me").unwrap(); + symlink(&fixture.in_library("Widget/link"), &keep); + symlink(&fixture.in_library("Widget/file-link"), &keep.join("precious.txt")); + store.delete("Widget").unwrap(); + assert!(fixture.names().is_empty()); + assert!(!fixture.in_library("Widget").exists()); + assert_eq!(std::fs::read_to_string(keep.join("precious.txt")).unwrap(), "keep me"); + assert!(fixture.staging_tree().is_empty()); + assert_code("package_not_found", store.delete("Widget")); + assert_code("invalid_name", store.delete("..")); +} + +// write_file / read_file + +#[test] +fn write_file_replaces_content_atomically() { + let fixture = Fixture::new(); + let store = &fixture.store; + store.create("Widget", SOURCE, None).unwrap(); + store.write_file("Widget", "main.jsx", b"new source").unwrap(); + store.write_file("Widget", "src/deep/util.js", b"util").unwrap(); + store.write_file("Widget", "src/deep/util.js", b"util 2").unwrap(); + assert_eq!(fixture.read_text("Widget", "main.jsx"), "new source"); + assert_eq!(fixture.read_text("Widget", "src/deep/util.js"), "util 2"); + // No temporary file may remain. + assert_eq!(tree(&fixture.in_library("Widget")), ["main.jsx", "src/", "src/deep/", "src/deep/util.js", "widget.json"]); +} + +#[test] +fn write_file_rejects_unsafe_paths_and_keeps_old_content() { + let fixture = Fixture::new(); + let store = &fixture.store; + store.create("Widget", SOURCE, None).unwrap(); + std::fs::create_dir_all(fixture.in_library("Sibling")).unwrap(); + let before = tree(&fixture.library); + let absolute = fixture.in_library("Sibling/x.js").to_string_lossy().into_owned(); + let too_deep = support::nested("d", 17); + let too_long = "x".repeat(256); + let unsafe_paths = [ + "", + "/", + "..", + "../Sibling/main.jsx", + "src/../../Sibling/x.js", + "src/../main.jsx", + "./main.jsx", + "/main.jsx", + &absolute, + "src\\x.js", + "..\\Sibling\\x.js", + "main\0.jsx", + "C:x.js", + ".hidden", + "src/.hidden/x.js", + "__Build/main.js", + "src/__BUILD/main.js", + &too_deep, + &too_long, + ]; + for path in unsafe_paths { + assert_code("invalid_path", store.write_file("Widget", path, b"owned")); + } + assert_code("invalid_manifest_case", store.write_file("Widget", "Widget.json", b"{}")); + assert_code("unsafe_file", store.write_file("Widget", "MAIN.jsx", b"owned")); + assert_code("unsafe_file", store.write_file("Widget", "main.jsx/child.js", b"owned")); + assert_code("file_too_large", store.write_file("Widget", "big.bin", &vec![0; MAX_FILE_BYTES as usize + 1])); + assert_code("file_too_large", store.write_file("Widget", "widget.json", &vec![0; MAX_METADATA_BYTES + 1])); + assert_code("package_not_found", store.write_file("Missing", "main.jsx", b"x")); + assert_code("invalid_name", store.write_file("../Sibling", "main.jsx", b"x")); + assert_eq!(tree(&fixture.library), before); + assert_eq!(fixture.read_text("Widget", "main.jsx"), SOURCE); +} + +#[test] +fn write_file_never_writes_through_links() { + let fixture = Fixture::new(); + let store = &fixture.store; + store.create("Widget", SOURCE, None).unwrap(); + let outside = fixture.temporary.new_folder("outside"); + let victim = outside.join("victim.txt"); + std::fs::write(&victim, "original").unwrap(); + symlink(&fixture.in_library("Widget/link.txt"), &victim); + symlink(&fixture.in_library("Widget/linkdir"), &outside); + symlink(&fixture.in_library("Widget/dangling.txt"), &outside.join("created.txt")); + hard_link(&fixture.in_library("Widget/hard.txt"), &victim); + + for path in ["link.txt", "linkdir/victim.txt", "linkdir/new.txt", "linkdir/sub/new.txt", "dangling.txt", "hard.txt"] { + assert_code("unsafe_file", store.write_file("Widget", path, b"owned")); + } + assert_eq!(std::fs::read_to_string(&victim).unwrap(), "original"); + assert_eq!(tree(&outside), ["victim.txt"]); + for path in ["link.txt", "linkdir/victim.txt", "dangling.txt", "hard.txt"] { + assert_code("file_not_found", store.read_file("Widget", path)); + } +} + +#[test] +fn write_file_enforces_the_entry_limit() { + let fixture = Fixture::new(); + let store = &fixture.store; + store.create("Widget", SOURCE, None).unwrap(); + for index in 0..MAX_FILE_COUNT - 2 { + fixture.write_in_library(&format!("Widget/f{index}.txt"), "x"); + } + assert_code("too_many_entries", store.write_file("Widget", "one-more.txt", b"x")); + store.write_file("Widget", "f0.txt", b"replaced").unwrap(); + assert_eq!(fixture.read_text("Widget", "f0.txt"), "replaced"); +} + +#[test] +fn read_file_reports_missing_and_unsafe_paths() { + let fixture = Fixture::new(); + let store = &fixture.store; + store.create("Widget", SOURCE, None).unwrap(); + assert_eq!(fixture.read_text("Widget", "main.jsx"), SOURCE); + assert_code("file_not_found", store.read_file("Widget", "missing.js")); + assert_code("file_not_found", store.read_file("Widget", "MAIN.JSX")); + assert_code("invalid_path", store.read_file("Widget", "../Widget/main.jsx")); + assert_code("invalid_path", store.read_file("Widget", "/etc/hosts")); + assert_code("invalid_path", store.read_file("Widget", "a\\b")); + assert_code("package_not_found", store.read_file("Missing", "main.jsx")); +} diff --git a/Linux/crates/sw-core/tests/packages_support/mod.rs b/Linux/crates/sw-core/tests/packages_support/mod.rs new file mode 100644 index 00000000..3fa03acf --- /dev/null +++ b/Linux/crates/sw-core/tests/packages_support/mod.rs @@ -0,0 +1,382 @@ +//! Fixtures shared by the package tests: hand-written ZIP archives in which +//! every header field an attacker controls can be forged, a temporary folder, +//! links, and a listing of a directory tree. Ported from `ZipFixtures.cs`. + +#![allow(dead_code)] + +use std::collections::BTreeMap; +use std::io::Write; +use std::path::{Path, PathBuf}; + +use sw_core::packages::PackageError; + +/// One hand-written ZIP entry. +#[derive(Clone)] +pub struct RawEntry { + pub name: String, + pub data: Vec, + pub external_attributes: u32, + pub declared_size: Option, + pub declared_compressed_size: Option, + pub flags: u16, + pub method: u16, + pub name_bytes: Option>, +} + +/// Unix mode of a regular file in the high half of the external attributes. +pub const REGULAR_FILE: u32 = 0x81A4 << 16; +pub const SYMBOLIC_LINK: u32 = 0xA1FF << 16; + +impl RawEntry { + pub fn new(name: &str, data: impl Into>) -> Self { + Self { name: name.to_owned(), data: data.into(), external_attributes: REGULAR_FILE, declared_size: None, declared_compressed_size: None, flags: 0x0800, method: 0, name_bytes: None } + } + + pub fn mode(mut self, external_attributes: u32) -> Self { + self.external_attributes = external_attributes; + self + } + + pub fn declared_size(mut self, size: u64) -> Self { + self.declared_size = Some(size); + self + } + + pub fn declared_compressed_size(mut self, size: u64) -> Self { + self.declared_compressed_size = Some(size); + self + } + + pub fn flags(mut self, flags: u16) -> Self { + self.flags = flags; + self + } + + pub fn method(mut self, method: u16) -> Self { + self.method = method; + self + } + + pub fn name_bytes(mut self, bytes: &[u8]) -> Self { + self.name_bytes = Some(bytes.to_vec()); + self + } +} + +/// An entry holding "x". +pub fn entry(name: &str) -> RawEntry { + RawEntry::new(name, "x") +} + +/// CRC-32 computed bit by bit, independently of the implementation under test. +pub fn crc32(bytes: &[u8]) -> u32 { + let mut state = 0xFFFF_FFFFu32; + for byte in bytes { + state ^= u32::from(*byte); + for _ in 0..8 { + state = if state & 1 != 0 { 0xEDB8_8320 ^ (state >> 1) } else { state >> 1 }; + } + } + !state +} + +/// ZIP written byte by byte, like `makeStoredZIP` in the Swift tests. The +/// data of an entry is written as given, whatever its method says. +pub fn raw_zip(entries: &[RawEntry]) -> Vec { + raw_zip_with_checksums(entries, &entries.iter().map(|entry| crc32(&entry.data)).collect::>()) +} + +fn raw_zip_with_checksums(entries: &[RawEntry], checksums: &[u32]) -> Vec { + let mut locals = Vec::new(); + let mut central = Vec::new(); + for (entry, crc) in entries.iter().zip(checksums) { + let name = entry.name_bytes.clone().unwrap_or_else(|| entry.name.clone().into_bytes()); + let size = entry.declared_size.unwrap_or(entry.data.len() as u64) as u32; + let compressed = entry.declared_compressed_size.unwrap_or(entry.data.len() as u64) as u32; + let offset = locals.len() as u32; + let mut shared = Vec::new(); + for field in [20u16, entry.flags, entry.method, 0, 0x21] { + shared.extend_from_slice(&field.to_le_bytes()); + } + for field in [*crc, compressed, size] { + shared.extend_from_slice(&field.to_le_bytes()); + } + shared.extend_from_slice(&(name.len() as u16).to_le_bytes()); + shared.extend_from_slice(&0u16.to_le_bytes()); + + locals.extend_from_slice(&0x0403_4B50u32.to_le_bytes()); + locals.extend_from_slice(&shared); + locals.extend_from_slice(&name); + locals.extend_from_slice(&entry.data); + + central.extend_from_slice(&0x0201_4B50u32.to_le_bytes()); + central.extend_from_slice(&0x031Eu16.to_le_bytes()); + central.extend_from_slice(&shared); + // Comment length, disk number and internal attributes stay zero. + central.extend_from_slice(&[0; 6]); + central.extend_from_slice(&entry.external_attributes.to_le_bytes()); + central.extend_from_slice(&offset.to_le_bytes()); + central.extend_from_slice(&name); + } + let mut archive = locals; + let central_offset = archive.len() as u32; + archive.extend_from_slice(¢ral); + archive.extend_from_slice(&0x0605_4B50u32.to_le_bytes()); + archive.extend_from_slice(&[0; 4]); + archive.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + archive.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + archive.extend_from_slice(&(central.len() as u32).to_le_bytes()); + archive.extend_from_slice(¢ral_offset.to_le_bytes()); + archive.extend_from_slice(&[0; 2]); + archive +} + +/// ZIP whose entries are deflated; the headers state the true sizes and checksums. +pub fn deflated_zip(files: &[(&str, &[u8])]) -> Vec { + let mut entries = Vec::new(); + let mut checksums = Vec::new(); + for (name, data) in files { + let mut encoder = flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::best()); + encoder.write_all(data).unwrap(); + entries.push(RawEntry::new(name, encoder.finish().unwrap()).method(8).declared_size(data.len() as u64)); + checksums.push(crc32(data)); + } + raw_zip_with_checksums(&entries, &checksums) +} + +fn u16_at(bytes: &[u8], index: usize) -> usize { + usize::from(u16::from_le_bytes([bytes[index], bytes[index + 1]])) +} + +fn u32_at(bytes: &[u8], index: usize) -> u32 { + u32::from_le_bytes([bytes[index], bytes[index + 1], bytes[index + 2], bytes[index + 3]]) +} + +/// Overwrites the uncompressed size the central directory declares for entry `index`. +pub fn forge_declared_size(archive: &[u8], index: usize, size: u32) -> Vec { + let mut bytes = archive.to_vec(); + let mut cursor = u32_at(&bytes, bytes.len() - 22 + 16) as usize; + for _ in 0..index { + cursor += 46 + u16_at(&bytes, cursor + 28) + u16_at(&bytes, cursor + 30) + u16_at(&bytes, cursor + 32); + } + assert_eq!(u32_at(&bytes, cursor), 0x0201_4B50); + bytes[cursor + 24..cursor + 28].copy_from_slice(&size.to_le_bytes()); + bytes +} + +/// Entry names and contents of an archive, read by a small reader of its own +/// (central directory, then each local header), for checking exports. +pub fn read_zip(archive: &[u8]) -> Vec<(String, Vec)> { + let end = archive.len() - 22; + assert_eq!(u32_at(archive, end), 0x0605_4B50, "end record"); + let mut cursor = u32_at(archive, end + 16) as usize; + let mut result = Vec::new(); + for _ in 0..u16_at(archive, end + 10) { + assert_eq!(u32_at(archive, cursor), 0x0201_4B50, "central header"); + let name_length = u16_at(archive, cursor + 28); + let name = String::from_utf8(archive[cursor + 46..cursor + 46 + name_length].to_vec()).unwrap(); + let compressed = u32_at(archive, cursor + 20) as usize; + let local = u32_at(archive, cursor + 42) as usize; + assert_eq!(u32_at(archive, local), 0x0403_4B50, "local header"); + let start = local + 30 + u16_at(archive, local + 26) + u16_at(archive, local + 28); + let data = &archive[start..start + compressed]; + let content = match u16_at(archive, cursor + 10) { + 0 => data.to_vec(), + 8 => { + let mut content = Vec::new(); + std::io::Read::read_to_end(&mut flate2::read::DeflateDecoder::new(data), &mut content).unwrap(); + content + } + method => panic!("unexpected method {method}"), + }; + assert_eq!(content.len(), u32_at(archive, cursor + 24) as usize, "declared size of {name}"); + assert_eq!(crc32(&content), u32_at(archive, cursor + 16), "checksum of {name}"); + result.push((name, content)); + cursor += 46 + name_length + u16_at(archive, cursor + 30) + u16_at(archive, cursor + 32); + } + result +} + +pub fn entry_names(archive: &[u8]) -> Vec { + read_zip(archive).into_iter().map(|(name, _)| name).collect() +} + +pub const SOURCE: &str = "$render(Hello);"; + +pub fn manifest(name: &str) -> String { + format!(r#"{{"formatVersion":2,"id":"com.example.widget","name":"{name}","version":"1.2.3","runtimeVersion":"1.0","entry":"main.jsx","supportedFamilies":["systemSmall"],"permissions":[],"networkDomains":[]}}"#) +} + +/// A well-formed package archive under one top-level directory. +pub fn package_zip(root: &str, name: &str, extra: &[(&str, &str)]) -> Vec { + let mut entries = vec![RawEntry::new(&format!("{root}/widget.json"), manifest(name)), RawEntry::new(&format!("{root}/main.jsx"), "$render(hi);")]; + entries.extend(extra.iter().map(|(path, text)| RawEntry::new(&format!("{root}/{path}"), *text))); + raw_zip(&entries) +} + +/// The archive most tests import: `Widget/widget.json` and `Widget/main.jsx`. +pub fn widget_zip() -> Vec { + package_zip("Widget", "Widget", &[]) +} + +#[track_caller] +pub fn assert_code(code: &str, result: Result) { + match result { + Err(error) => assert_eq!(error.code, code, "{}", error.message), + Ok(value) => panic!("expected {code}, but it succeeded with {value:?}"), + } +} + +/// A deflated entry whose real content is longer than the size the central +/// directory declares. The reader stops as soon as the content passes the +/// declared size and reports `archive_size_mismatch`; the Windows reader +/// stops at the declared size and fails the checksum instead, so both codes +/// are accepted there and here. +#[track_caller] +pub fn assert_lying_size_rejected(result: Result) { + let error = result.expect_err("a lying size must be rejected"); + assert!(["archive_size_mismatch", "archive_corrupt"].contains(&error.code.as_str()), "{error}"); +} + +/// Relative paths of everything below `root`, directories included (with a +/// trailing slash), for "nothing left behind" checks. Links are listed and +/// never followed. A missing root lists as empty. +pub fn tree(root: &Path) -> Vec { + fn walk(directory: &Path, prefix: &str, result: &mut Vec) { + let Ok(entries) = std::fs::read_dir(directory) else { return }; + for entry in entries { + let entry = entry.unwrap(); + let name = entry.file_name().to_string_lossy().into_owned(); + let is_directory = entry.file_type().unwrap().is_dir(); + result.push(format!("{prefix}{name}{}", if is_directory { "/" } else { "" })); + if is_directory { + walk(&entry.path(), &format!("{prefix}{name}/"), result); + } + } + } + let mut result = Vec::new(); + walk(root, "", &mut result); + result.sort_unstable(); + result +} + +/// A unique directory under the system temporary directory that is removed +/// when the test ends. The counterpart of JUnit's `TemporaryFolder`. +pub struct TemporaryFolder { + pub root: PathBuf, +} + +impl TemporaryFolder { + pub fn new() -> Self { + static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + let unique = format!("pkg-{}-{}-{}", std::process::id(), NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed), std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()); + // The canonical form: on macOS the temporary directory is reached through a link. + let base = std::fs::canonicalize(std::env::temp_dir()).unwrap(); + let root = base.join("scriptwidget-tests").join(unique); + std::fs::create_dir_all(&root).unwrap(); + Self { root } + } + + pub fn path(&self, relative: &str) -> PathBuf { + self.root.join(relative) + } + + pub fn new_folder(&self, relative: &str) -> PathBuf { + let path = self.path(relative); + std::fs::create_dir_all(&path).unwrap(); + path + } + + pub fn new_file(&self, relative: &str, text: &str) -> PathBuf { + let path = self.path(relative); + write_file(&path, text.as_bytes()); + path + } +} + +impl Drop for TemporaryFolder { + fn drop(&mut self) { + // Tests leave directories without permissions behind; open them up first. + fn unlock(directory: &Path) { + use std::os::unix::fs::PermissionsExt; + let _ = std::fs::set_permissions(directory, std::fs::Permissions::from_mode(0o700)); + for entry in std::fs::read_dir(directory).into_iter().flatten().flatten() { + if entry.file_type().is_ok_and(|kind| kind.is_dir()) { + unlock(&entry.path()); + } + } + } + unlock(&self.root); + let _ = std::fs::remove_dir_all(&self.root); + } +} + +/// Writes a file, creating the directories above it. +pub fn write_file(path: &Path, bytes: &[u8]) { + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(path, bytes).unwrap(); +} + +/// Makes `link` a symbolic link to `target`, which need not exist. +pub fn symlink(link: &Path, target: &Path) { + std::fs::create_dir_all(link.parent().unwrap()).unwrap(); + std::os::unix::fs::symlink(target, link).unwrap(); + assert!(std::fs::symlink_metadata(link).unwrap().file_type().is_symlink()); +} + +/// Makes `link` a second hard link to the file `existing`. +pub fn hard_link(link: &Path, existing: &Path) { + use std::os::unix::fs::MetadataExt; + std::fs::create_dir_all(link.parent().unwrap()).unwrap(); + std::fs::hard_link(existing, link).unwrap(); + assert!(std::fs::metadata(link).unwrap().nlink() >= 2); +} + +pub fn mode(path: &Path) -> u32 { + use std::os::unix::fs::MetadataExt; + std::fs::symlink_metadata(path).unwrap().mode() & 0o777 +} + +pub fn set_mode(path: &Path, mode: u32) { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)).unwrap(); +} + +/// True when the test runs as root, for whom file permissions do not hold. +pub fn permissions_are_not_enforced(scratch: &TemporaryFolder) -> bool { + let probe = scratch.new_file(".permission-probe", "x"); + set_mode(&probe, 0o000); + let readable = std::fs::read(&probe).is_ok(); + std::fs::remove_file(&probe).unwrap(); + readable +} + +/// Puts a Unix socket at `path`. A socket address holds about a hundred +/// bytes, so the directory is reached through a short link while binding. +pub fn make_socket(path: &Path) { + static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + let short = PathBuf::from(format!("/tmp/sw-{}-{}", std::process::id(), NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed))); + let _ = std::fs::remove_file(&short); + std::os::unix::fs::symlink(path.parent().unwrap(), &short).unwrap(); + let bound = std::os::unix::net::UnixListener::bind(short.join(path.file_name().unwrap())); + std::fs::remove_file(&short).unwrap(); + drop(bound.unwrap()); + use std::os::unix::fs::FileTypeExt; + assert!(std::fs::symlink_metadata(path).unwrap().file_type().is_socket()); +} + +/// Puts a FIFO at `path`. +pub fn make_fifo(path: &Path) { + let made = std::process::Command::new("mkfifo").arg(path).status().expect("mkfifo"); + assert!(made.success(), "mkfifo failed"); + use std::os::unix::fs::FileTypeExt; + assert!(std::fs::symlink_metadata(path).unwrap().file_type().is_fifo()); +} + +pub fn extras(files: &[(&str, &str)]) -> BTreeMap> { + files.iter().map(|(path, text)| (path.to_string(), text.as_bytes().to_vec())).collect() +} + +pub fn nested(name: &str, depth: usize) -> String { + vec![name; depth].join("/") +} diff --git a/Linux/crates/sw-core/tests/parity.rs b/Linux/crates/sw-core/tests/parity.rs new file mode 100644 index 00000000..49e7ab8b --- /dev/null +++ b/Linux/crates/sw-core/tests/parity.rs @@ -0,0 +1,137 @@ +//! Runs every bundled package that needs no live data through QuickJS and +//! compares the canonical element tree, byte for byte, with the one +//! JavaScriptCore produced (Web/preview-engine/test/parity/native-trees.json). +//! The Linux counterpart of Windows/tests/ScriptWidget.Core.Tests/ParityTests.cs. +//! +//! node Linux/scripts/build-shared-js.mjs +//! cargo test -p sw-core --test parity -- --nocapture +//! +//! This file holds a single test because it sets the time zone of the whole +//! process (the corpus was recorded in one zone): Cargo runs each test file +//! as its own program, and with one test nothing else reads the environment +//! or local time while it changes. The zone reaches JavaScript's `Date` +//! through the C library (TZ and tzset) and Intl through ICU, to which the +//! bridge passes the zone by name. + +use std::path::PathBuf; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +use serde_json::Value; +use sw_core::api::StorageModule; +use sw_core::compiler::PrepareOptions; +use sw_core::{ExecutionRequest, MemoryPackageFiles, NativeModule, PackageCompiler, ScriptExecutor, SharedScripts, intl}; + +fn linux_directory() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..") +} + +/// A stretch of text around a byte offset, cut at character boundaries. +fn window(text: &str, at: usize) -> &str { + let mut start = at.saturating_sub(60); + while !text.is_char_boundary(start) { + start -= 1; + } + let mut end = (at + 100).min(text.len()); + while !text.is_char_boundary(end) { + end += 1; + } + &text[start..end] +} + +fn clip(text: &str, length: usize) -> String { + text.chars().take(length).collect() +} + +#[test] +fn bundled_packages_produce_the_same_trees_as_javascriptcore() { + let corpus_file = linux_directory().join("tests/assets/parity-corpus.json"); + let corpus_text = std::fs::read_to_string(&corpus_file).unwrap_or_else(|error| panic!("{}: {error}. Run node Linux/scripts/build-shared-js.mjs.", corpus_file.display())); + let corpus: Value = serde_json::from_str(&corpus_text).expect("parity corpus is JSON"); + let time_zone = corpus["timeZone"].as_str().expect("corpus time zone"); + let locale = corpus["locale"].as_str().unwrap_or("en-US"); + let prelude = corpus["prelude"].as_str().unwrap_or(""); + let packages = corpus["corpus"].as_array().expect("corpus packages"); + + // SAFETY: the only test of this program, and no other thread exists yet + // that reads the environment or converts local time. + unsafe { intl::set_time_zone_override(Some(time_zone)) }; + intl::set_locale_override(Some(locale)); + + let scripts = Arc::new(SharedScripts::new(linux_directory().join("assets/scriptwidget")).expect("shared scripts")); + let compiler = PackageCompiler::new(scripts.clone()).expect("compiler thread"); + let executor = ScriptExecutor::new(scripts); + // Each package starts with empty storage, like the JavaScriptCore reference run. + let storage_directory = std::env::temp_dir().join(format!("scriptwidget-parity-storage-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&storage_directory); + let storage: Arc = Arc::new(StorageModule::new(&storage_directory)); + + let mut failures: Vec = Vec::new(); + let (mut compile, mut execute, mut slowest, mut first_compile) = (Duration::ZERO, Duration::ZERO, Duration::ZERO, None); + for (index, item) in packages.iter().enumerate() { + let name = item["name"].as_str().unwrap_or("?"); + let text = item["files"].as_object().expect("package files").iter().map(|(path, content)| (path.clone(), content.as_str().unwrap_or("").to_owned())); + let binaries = item["binaries"].as_array().into_iter().flatten().filter_map(|path| Some((path.as_str()?.to_owned(), Vec::new()))); + let files = MemoryPackageFiles::new(text, binaries); + let expected = item["expected"]["tree"].as_str(); + + let compile_start = Instant::now(); + let prepared = match compiler.prepare(&files, &PrepareOptions { prelude, ..Default::default() }) { + Ok(prepared) => prepared, + Err(error) => { + failures.push(format!("{name}: prepare failed [{}] {}", error.kind, clip(&error.message, 300))); + continue; + } + }; + // The first compile also loads Babel into a new context. + first_compile.get_or_insert(compile_start.elapsed()); + compile += compile_start.elapsed(); + + let mut request = ExecutionRequest::new(&prepared, &files); + request.modules = vec![storage.clone()]; + request.package_id = format!("parity-{index}"); + let result = executor.execute(&request); + execute += result.elapsed; + slowest = slowest.max(result.elapsed); + + let Some(expected) = expected else { + failures.push(format!("{name}: no native reference tree")); + continue; + }; + match result.tree.as_deref() { + Some(actual) if actual == expected => {} + Some(actual) => { + let at = actual.bytes().zip(expected.bytes()).take_while(|(left, right)| left == right).count(); + failures.push(format!("{name}: tree differs at byte {at}\n native: {}\n linux : {}", window(expected, at), window(actual, at))); + } + None => { + let error = result.error.as_ref(); + failures.push(format!("{name}: error [{}] {}", error.map_or("", |error| error.kind.as_str()), clip(error.map_or("", |error| error.message.as_str()), 400))); + } + } + } + let _ = std::fs::remove_dir_all(&storage_directory); + drop(compiler); + intl::set_locale_override(None); + // SAFETY: as above; the compiler thread has ended. + unsafe { intl::set_time_zone_override(None) }; + + let total = packages.len(); + let summary = format!( + "parity {}/{total}; ICU {}; {} {}; time zone {time_zone}; compile {} ms total (first {} ms, with loading Babel); execute {} ms total, slowest {} ms", + total - failures.len(), + intl::icu_version(), + std::env::consts::OS, + std::env::consts::ARCH, + compile.as_millis(), + first_compile.unwrap_or_default().as_millis(), + execute.as_millis(), + slowest.as_millis(), + ); + println!("{summary}"); + for failure in &failures { + println!(" {failure}"); + } + assert!(total > 0, "corpus is empty"); + assert!(failures.is_empty(), "{summary}\n{}", failures.join("\n")); +} diff --git a/Linux/crates/sw-core/tests/plugins_common/mod.rs b/Linux/crates/sw-core/tests/plugins_common/mod.rs new file mode 100644 index 00000000..00844fc3 --- /dev/null +++ b/Linux/crates/sw-core/tests/plugins_common/mod.rs @@ -0,0 +1,155 @@ +//! Shared by the `plugins_*` tests: the plugin of the Swift tests and +//! hand-written ZIP archives. Ported from +//! `Windows/tests/ScriptWidget.Core.Tests/Plugins/PluginFixtures.cs`. +#![allow(dead_code)] + +use std::io::Write; + +use serde_json::Value; +use sw_core::json::Object; +use sw_core::plugins::manifest::{self, PluginError, PluginErrorKind, PluginManifest}; + +pub const ID: &str = "dev.scriptwidget.test-source"; + +/// The plugin of `testRequestBuilderMapsPathQueryHeaderAndJSONBody` in the Swift tests. +pub const BASE: &str = r#" +{ + "formatVersion": 1, "id": "dev.scriptwidget.test-source", "name": "Test Source", "version": "1.0.0", + "summary": "Test", "symbol": "network", "author": "Tests", "license": "MIT", "minimumRuntimeVersion": "1.0", + "baseURL": "https://api.example.com", "hosts": ["api.example.com"], + "operations": [ + {"id": "update", "title": "Update", "method": "POST", "path": "/users/{user}", "responseType": "json", "parameters": [ + {"id": "user", "label": "User", "location": "path", "required": true}, + {"id": "page", "label": "Page", "location": "query", "required": false, "defaultValue": "1"}, + {"id": "X-Token", "label": "Token", "location": "header", "required": true, "defaultValue": null}, + {"id": "enabled", "label": "Enabled", "location": "jsonBody", "required": true} + ]}, + {"id": "status", "title": "Status", "method": "GET", "path": "/status", "responseType": "text", "parameters": []} + ] +} +"#; + +/// The base plugin after `mutate`, as bytes. +pub fn bytes_with(mutate: impl FnOnce(&mut Value)) -> Vec { + let mut root: Value = serde_json::from_str(BASE).expect("base plugin"); + mutate(&mut root); + root.to_string().into_bytes() +} + +pub fn bytes() -> Vec { + bytes_with(|_| {}) +} + +pub fn manifest_with(mutate: impl FnOnce(&mut Value)) -> PluginManifest { + manifest::decode(&bytes_with(mutate)).expect("a valid plugin") +} + +pub fn base_manifest() -> PluginManifest { + manifest_with(|_| {}) +} + +pub fn remove(value: &mut Value, key: &str) { + value.as_object_mut().expect("object").shift_remove(key); +} + +pub fn values(value: Value) -> Object { + value.as_object().cloned().expect("values") +} + +pub fn assert_rejected(expected: &str, mutate: impl FnOnce(&mut Value)) { + let error: PluginError = manifest::decode(&bytes_with(mutate)).expect_err("the manifest must be refused"); + assert_eq!(error.kind, PluginErrorKind::InvalidManifest); + assert!(error.message.contains(expected), "Expected \"{expected}\" in \"{}\"", error.message); +} + +/// One hand-written ZIP entry; every header field an attacker controls can be forged. +#[derive(Clone)] +pub struct RawEntry { + pub name: String, + pub data: Vec, + pub external_attributes: u32, + pub flags: u16, + pub method: u16, + /// Deflate the data before storing it. + pub deflate: bool, +} + +impl RawEntry { + pub fn new(name: &str, data: &[u8]) -> Self { + Self { name: name.into(), data: data.to_vec(), external_attributes: 0x81A4 << 16, flags: 0x0800, method: 0, deflate: false } + } + + pub fn deflated(name: &str, data: &[u8]) -> Self { + Self { method: 8, deflate: true, ..Self::new(name, data) } + } + + pub fn directory(name: &str) -> Self { + Self { external_attributes: 0x41ED << 16, ..Self::new(name, b"") } + } +} + +/// A ZIP written byte by byte, like `makeStoredZIP` in the Swift tests. +pub fn raw_zip(entries: &[RawEntry]) -> Vec { + let mut locals: Vec = Vec::new(); + let mut central: Vec = Vec::new(); + for entry in entries { + let stored = if entry.deflate { + let mut encoder = flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::best()); + encoder.write_all(&entry.data).unwrap(); + encoder.finish().unwrap() + } else { + entry.data.clone() + }; + let crc = sw_core::plugins::files::crc32(&entry.data); + let offset = locals.len() as u32; + let shared: Vec = [20u16.to_le_bytes(), entry.flags.to_le_bytes(), entry.method.to_le_bytes(), 0u16.to_le_bytes(), 0x21u16.to_le_bytes()].concat(); + let sizes: Vec = [crc.to_le_bytes(), (stored.len() as u32).to_le_bytes(), (entry.data.len() as u32).to_le_bytes()].concat(); + locals.extend_from_slice(&0x0403_4B50u32.to_le_bytes()); + locals.extend_from_slice(&shared); + locals.extend_from_slice(&sizes); + locals.extend_from_slice(&(entry.name.len() as u16).to_le_bytes()); + locals.extend_from_slice(&0u16.to_le_bytes()); + locals.extend_from_slice(entry.name.as_bytes()); + locals.extend_from_slice(&stored); + + central.extend_from_slice(&0x0201_4B50u32.to_le_bytes()); + central.extend_from_slice(&0x031Eu16.to_le_bytes()); + central.extend_from_slice(&shared); + central.extend_from_slice(&sizes); + central.extend_from_slice(&(entry.name.len() as u16).to_le_bytes()); + central.extend_from_slice(&[0u8; 8]); + central.extend_from_slice(&entry.external_attributes.to_le_bytes()); + central.extend_from_slice(&offset.to_le_bytes()); + central.extend_from_slice(entry.name.as_bytes()); + } + let mut output = locals.clone(); + output.extend_from_slice(¢ral); + output.extend_from_slice(&0x0605_4B50u32.to_le_bytes()); + output.extend_from_slice(&[0u8; 4]); + output.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + output.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + output.extend_from_slice(&(central.len() as u32).to_le_bytes()); + output.extend_from_slice(&(locals.len() as u32).to_le_bytes()); + output.extend_from_slice(&0u16.to_le_bytes()); + output +} + +/// Overwrites the uncompressed size the central directory declares for entry `index`. +pub fn forge_declared_size(archive: &[u8], index: usize, size: u32) -> Vec { + let mut bytes = archive.to_vec(); + let u16_at = |bytes: &[u8], at: usize| u16::from_le_bytes([bytes[at], bytes[at + 1]]) as usize; + let end = bytes.len() - 22; + let mut cursor = u32::from_le_bytes([bytes[end + 16], bytes[end + 17], bytes[end + 18], bytes[end + 19]]) as usize; + for _ in 0..index { + cursor += 46 + u16_at(&bytes, cursor + 28) + u16_at(&bytes, cursor + 30) + u16_at(&bytes, cursor + 32); + } + assert_eq!(&bytes[cursor..cursor + 4], &0x0201_4B50u32.to_le_bytes()); + bytes[cursor + 24..cursor + 28].copy_from_slice(&size.to_le_bytes()); + bytes +} + +/// Entry names and contents of an archive, in order. +pub fn read_zip(archive: &[u8]) -> Vec<(String, Vec)> { + let entries = sw_core::plugins::files::validate_archive(archive).expect("a valid archive"); + entries.iter().map(|entry| (entry.name.clone(), sw_core::plugins::files::inflate(archive, entry, 1 << 24).expect("entry data"))).collect() +} diff --git a/Linux/crates/sw-core/tests/plugins_manifest.rs b/Linux/crates/sw-core/tests/plugins_manifest.rs new file mode 100644 index 00000000..4ed05aa2 --- /dev/null +++ b/Linux/crates/sw-core/tests/plugins_manifest.rs @@ -0,0 +1,254 @@ +//! The strict decoder and validator of `plugin.json`. Ported from +//! `Windows/tests/ScriptWidget.Core.Tests/Plugins/PluginValidatorTests.cs`. + +mod plugins_common; + +use plugins_common::*; +use serde_json::{Value, json}; +use sw_core::plugins::manifest::{self, MAXIMUM_MANIFEST_BYTES, PluginManifest, PluginParameter, PluginParameterLocation, PluginResponseType, decode, is_valid_domain_declaration, validate}; +use sw_core::plugins::store::built_ins; + +fn message(data: &[u8]) -> String { + decode(data).expect_err("the manifest must be refused").message +} + +// Swift: testBuiltInPluginsValidate +#[test] +fn built_in_plugins_validate() { + assert_eq!(built_ins().len(), 2); + for plugin in built_ins() { + assert_eq!(validate(plugin), []); + } +} + +#[test] +fn built_in_plugins_survive_encoding_and_decoding() { + for plugin in built_ins() { + assert_eq!(&decode(plugin.to_json().as_bytes()).unwrap(), plugin); + } + let manifest = base_manifest(); + assert_eq!(decode(manifest.to_json().as_bytes()).unwrap(), manifest); + // The written form is the one Swift writes: sorted keys, two spaces, slashes as they are. + let text = built_ins()[1].to_json(); + assert!(text.starts_with("{\n \"author\": \"GitHub\",\n \"baseURL\": \"https://api.github.com\",\n \"formatVersion\": 1,\n \"hosts\": [\"api.github.com\"],\n"), "{text}"); + assert!(text.contains(" {\"defaultValue\": \"everettjf\", \"id\": \"owner\", \"label\": \"Owner\", \"location\": \"path\", \"required\": true},\n"), "{text}"); + assert!(text.ends_with(" \"version\": \"1.0.0\"\n}\n"), "{text}"); + assert!(manifest.to_json().contains("{\"id\": \"user\", \"label\": \"User\", \"location\": \"path\", \"required\": true}"), "an absent default is left out"); + assert!(manifest.to_json().contains(" \"parameters\": [],\n")); +} + +#[test] +fn decodes_every_field() { + let manifest = base_manifest(); + assert_eq!(manifest.id, ID); + assert_eq!(manifest.base_url, "https://api.example.com"); + assert_eq!(manifest.hosts, ["api.example.com"]); + let update = &manifest.operations[0]; + assert_eq!(update.method, "POST"); + assert_eq!(update.response_type, PluginResponseType::Json); + assert_eq!(update.parameters[1], PluginParameter { id: "page".into(), label: "Page".into(), location: PluginParameterLocation::Query, required: false, default_value: Some("1".into()) }); + assert_eq!(update.parameters[2].default_value, None); + assert_eq!(manifest.operations[1].response_type, PluginResponseType::Text); + // A byte order mark is not part of the document. + assert_eq!(decode(&[&[0xEF, 0xBB, 0xBF][..], &bytes()].concat()).unwrap(), manifest); +} + +// Swift: testManifestDecoderFailsClosedAndRejectsHTTP +#[test] +fn unknown_keys_fail_closed_at_every_level() { + assert_rejected("plugin.json contains unknown fields.", |root| root["unexpected"] = json!(true)); + assert_rejected("plugin.json contains unknown fields.", |root| root["nativeLibrary"] = json!("libplugin.so")); + assert_rejected("plugin.json contains unknown fields.", |root| root["permissions"] = json!(["network"])); + assert_rejected("Plugin operation contains unknown fields.", |root| root["operations"][0]["script"] = json!("fetch()")); + assert_rejected("Plugin operation contains unknown fields.", |root| root["operations"][0]["parameters"][0]["secret"] = json!("x")); +} + +#[test] +fn http_base_url_is_rejected() { + let unsafe_plugin = PluginManifest { base_url: "http://api.open-meteo.com".into(), ..built_ins()[0].clone() }; + assert!(!validate(&unsafe_plugin).is_empty()); + assert_rejected("baseURL must be an HTTPS URL without credentials, query, or fragment.", |root| root["baseURL"] = json!("http://api.example.com")); +} + +#[test] +fn base_url_rules() { + let message = "baseURL must be an HTTPS URL without credentials, query, or fragment."; + for url in [ + "ftp://api.example.com", "https://user:pw@api.example.com", "https://user@api.example.com", "https://api.example.com?key=1", "https://api.example.com/#top", "api.example.com", "https:///path", "", + "https://api example.com", "file:///etc/hosts", + ] { + assert_rejected(message, |root| root["baseURL"] = json!(url)); + } + assert_rejected("baseURL host must be included in hosts.", |root| root["baseURL"] = json!("https://other.example.com")); + // A port and a base path are allowed. + assert_eq!(manifest_with(|root| root["baseURL"] = json!("https://api.example.com:8443/v2")).base_url, "https://api.example.com:8443/v2"); +} + +/// Spellings a lenient URL parser would accept or reinterpret are not a base URL. +#[test] +fn base_urls_that_only_a_lenient_parser_accepts_are_rejected() { + let message = "baseURL must be an HTTPS URL without credentials, query, or fragment."; + for url in [ + "https://api.example.com\\@evil.example.net", "https:\\\\api.example.com", "https:api.example.com", "https://api_internal.example.com", "https://api.example.com:port", "https://api.example.com/a b", + "https://api.example.com/%zz", "https://api.example.com?", "https://api.example.com#", "https://@api.example.com", "https://\u{4f8b}\u{3048}.example.com", " https://api.example.com", "https://api.example.com\n", + "https://api.example.com\t", "HTTPS://api.example.com/\u{0}", "https://2130706433", + ] { + assert_rejected(message, |root| root["baseURL"] = json!(url)); + } +} + +#[test] +fn versions_fail_closed() { + assert_rejected("Unsupported Data Source Plugin format 2.", |root| root["formatVersion"] = json!(2)); + assert_rejected("Unsupported Data Source Plugin format 0.", |root| root["formatVersion"] = json!(0)); + assert_rejected("\"formatVersion\" is missing or has the wrong type", |root| root["formatVersion"] = json!("1")); + assert_rejected("\"formatVersion\" is missing or has the wrong type", |root| root["formatVersion"] = json!(1.5)); + assert_rejected("\"formatVersion\" is missing or has the wrong type", |root| root["formatVersion"] = json!(true)); + assert_rejected("\"formatVersion\" is missing or has the wrong type", |root| root["formatVersion"] = json!(5_000_000_000u64)); + assert_rejected("\"formatVersion\" is missing or has the wrong type", |root| remove(root, "formatVersion")); + assert_rejected("Plugin requires unsupported runtime 2.0.", |root| root["minimumRuntimeVersion"] = json!("2.0")); + assert_rejected("Plugin requires unsupported runtime 1.6.", |root| root["minimumRuntimeVersion"] = json!("1.6")); + assert_rejected("Plugin version must use semantic versioning.", |root| root["version"] = json!("1.0")); + assert_rejected("Plugin version must use semantic versioning.", |root| root["version"] = json!("v1.0.0")); + assert_eq!(manifest_with(|root| root["version"] = json!("1.0.0-beta.1")).version, "1.0.0-beta.1"); + // 1.0 is the whole number 1, as for Swift's decoder. + assert_eq!(decode(String::from_utf8(bytes()).unwrap().replace("\"formatVersion\":1", "\"formatVersion\":1.0").as_bytes()).unwrap(), base_manifest()); +} + +#[test] +fn identifier_and_name_rules() { + let long = "a".repeat(129); + for id in ["ab", "-abc", "has space", "a/b/c", long.as_str(), "abc\n"] { + assert_rejected("Plugin id must be a stable 3–128 character identifier.", |root| root["id"] = json!(id)); + } + assert_rejected("Plugin name must contain 1–80 characters.", |root| root["name"] = json!(" ")); + assert_rejected("Plugin name must contain 1–80 characters.", |root| root["name"] = json!("n".repeat(81))); + assert_eq!(manifest_with(|root| root["name"] = json!("n".repeat(80))).name.len(), 80); + // Characters as a person counts them: a family emoji is one, an accent does not add one. + assert_eq!(manifest_with(|root| root["name"] = json!("👨\u{200d}👩\u{200d}👧".repeat(80))).name.chars().count(), 400); + assert_rejected("Plugin name must contain 1–80 characters.", |root| root["name"] = json!("👨\u{200d}👩\u{200d}👧".repeat(81))); + assert!(decode(&bytes_with(|root| root["name"] = json!("e\u{301}".repeat(80)))).is_ok()); +} + +#[test] +fn private_and_local_hosts_are_rejected() { + for host in ["localhost", "127.0.0.1", "10.0.0.5", "192.168.1.1", "172.16.0.1", "printer.local"] { + assert_rejected(&format!("Plugin host must be a public Internet host: {host}."), |root| { + root["baseURL"] = json!(format!("https://{host}")); + root["hosts"] = json!([host]); + }); + } + assert_rejected("Plugin host must be a public Internet host: *.local.", |root| root["hosts"] = json!(["api.example.com", "*.local"])); + assert_rejected("baseURL", |root| { + root["baseURL"] = json!("https://[::1]"); + root["hosts"] = json!(["api.example.com"]); + }); +} + +#[test] +fn host_declaration_rules() { + assert_rejected("A plugin must declare 1–20 HTTPS hosts.", |root| root["hosts"] = json!([])); + assert_rejected("A plugin must declare 1–20 HTTPS hosts.", |root| { + root["hosts"] = Value::Array((0..21).map(|index| if index == 0 { json!("api.example.com") } else { json!(format!("h{index}.example.com")) }).collect()); + }); + assert_rejected("Plugin hosts must be unique.", |root| root["hosts"] = json!(["api.example.com", "api.example.com"])); + for host in ["https://api.example.com", "api.example.com/path", "api.example.com:443", "API.example.com", ".example.com", "example.com.", "api.*.com", "*example.com", "*.*.example.com", "exa mple.com", ""] { + assert_rejected(&format!("Invalid plugin host declaration: {host}."), |root| root["hosts"] = json!(["api.example.com", host])); + } + assert_rejected("\"hosts\" is missing or has the wrong type", |root| root["hosts"] = json!(["api.example.com", 7])); + assert_rejected("\"hosts\" is missing or has the wrong type", |root| root["hosts"] = json!("api.example.com")); + assert_eq!(manifest_with(|root| root["hosts"] = json!(["*.example.com"])).hosts, ["*.example.com"]); +} + +#[test] +fn operation_rules() { + assert_rejected("A plugin must contain 1–50 operations.", |root| root["operations"] = json!([])); + assert_rejected("A plugin must contain 1–50 operations.", |root| { + let status = root["operations"][1].clone(); + root["operations"] = Value::Array( + (0..=50) + .map(|index| { + let mut copy = status.clone(); + copy["id"] = json!(format!("status-{index}")); + copy + }) + .collect(), + ); + }); + assert_rejected("Operation ids must be unique.", |root| root["operations"][1]["id"] = json!("UPDATE")); + assert_rejected("Operation id is invalid: a b.", |root| root["operations"][1]["id"] = json!("a b")); + for method in ["get", "HEAD", "OPTIONS", "CONNECT", "TRACE", ""] { + assert_rejected("Operation status has an unsupported HTTP method.", |root| root["operations"][1]["method"] = json!(method)); + } + let path_message = "Operation path must be an absolute URL path without traversal, query, or fragment."; + for path in ["", "status", "/a/../b", "/status?x=1", "/status#top", "//evil.example.com/.."] { + assert_rejected(path_message, |root| root["operations"][1]["path"] = json!(path)); + } + assert_rejected("\"responseType\" is missing or has the wrong type", |root| root["operations"][1]["responseType"] = json!("xml")); + assert_rejected("\"title\" is missing or has the wrong type", |root| remove(&mut root["operations"][1], "title")); + assert_rejected("plugin.json contains unknown fields.", |root| root["operations"] = json!("none")); + assert_rejected("plugin.json contains unknown fields.", |root| root["operations"] = json!([1])); + assert_rejected("Plugin operation contains unknown fields.", |root| root["operations"][1]["parameters"] = json!("none")); +} + +#[test] +fn parameter_declaration_rules() { + assert_rejected("Operation status has too many parameters.", |root| { + root["operations"][1]["parameters"] = Value::Array((0..=30).map(|index| json!({ "id": format!("param-{index}"), "label": "P", "location": "query", "required": false })).collect()); + }); + assert_rejected("Operation parameter ids must be unique.", |root| root["operations"][0]["parameters"][1]["id"] = json!("ENABLED")); + assert_rejected("Invalid parameter id: p!.", |root| root["operations"][0]["parameters"][1]["id"] = json!("p!")); + assert_rejected("Path parameter page has no matching placeholder.", |root| root["operations"][0]["parameters"][1]["location"] = json!("path")); + assert_rejected("Placeholder user must be a path parameter.", |root| root["operations"][0]["parameters"][0]["location"] = json!("query")); + assert_rejected("Path placeholder team has no declared parameter.", |root| root["operations"][0]["path"] = json!("/teams/{team}/users/{user}")); + assert_rejected("\"location\" is missing or has the wrong type", |root| root["operations"][0]["parameters"][1]["location"] = json!("cookie")); + assert_rejected("\"required\" is missing or has the wrong type", |root| root["operations"][0]["parameters"][1]["required"] = json!("yes")); + assert_rejected("\"required\" is missing or has the wrong type", |root| remove(&mut root["operations"][0]["parameters"][1], "required")); + assert_rejected("\"defaultValue\" is missing or has the wrong type", |root| root["operations"][0]["parameters"][1]["defaultValue"] = json!(1)); + assert_rejected("\"label\" is missing or has the wrong type", |root| root["operations"][0]["parameters"][1]["label"] = json!(5)); +} + +#[test] +fn every_issue_is_reported() { + let data = bytes_with(|root| { + root["id"] = json!("x"); + root["version"] = json!("1"); + root["formatVersion"] = json!(3); + }); + assert_eq!(message(&data), "Unsupported Data Source Plugin format 3.\nPlugin id must be a stable 3–128 character identifier.\nPlugin version must use semantic versioning."); +} + +#[test] +fn oversize_and_malformed_documents_are_rejected() { + let padded = bytes_with(|root| root["summary"] = json!("s".repeat(MAXIMUM_MANIFEST_BYTES))); + assert_eq!(message(&padded), "plugin.json exceeds 256 KiB."); + assert_eq!(message(b"[]"), "plugin.json must be an object."); + assert_eq!(message(b"\"plugin\""), "plugin.json must be an object."); + assert_eq!(message(&[0x7B, 0xFF, 0x7D]), "plugin.json must be UTF-8 JSON."); + let valid = String::from_utf8(bytes()).unwrap(); + let replace_first_brace = |replacement: &str| valid.replacen('{', replacement, 1); + for text in [ + String::new(), + "{".to_owned(), + format!("{valid} trailing"), + format!("{valid}{valid}"), + valid.replace("\"formatVersion\"", "formatVersion"), + valid.replace('"', "'"), + replace_first_brace("{ // comment\n"), + replace_first_brace("{\"id\":\"dev.scriptwidget.first\","), + valid.replace("\"formatVersion\":1", "\"formatVersion\":1e999"), + ] { + assert_eq!(message(text.as_bytes()), "plugin.json is not valid JSON.", "{text}"); + } +} + +#[test] +fn domain_declarations_match_swift() { + assert!(is_valid_domain_declaration("api.example.com")); + assert!(is_valid_domain_declaration("*.example.com")); + assert!(!is_valid_domain_declaration("Api.example.com")); + assert!(!is_valid_domain_declaration("api.example.com:8443")); + assert!(!is_valid_domain_declaration("a*.example.com")); + assert!(!is_valid_domain_declaration("")); + assert_eq!(manifest::RUNTIME_API_VERSION, "1.0"); +} diff --git a/Linux/crates/sw-core/tests/plugins_module.rs b/Linux/crates/sw-core/tests/plugins_module.rs new file mode 100644 index 00000000..b346204a --- /dev/null +++ b/Linux/crates/sw-core/tests/plugins_module.rs @@ -0,0 +1,447 @@ +//! `$dataSource.request` as the script bridge calls it. Ported from +//! `Windows/tests/ScriptWidget.Core.Tests/Plugins/DataSourceModuleTests.cs`. + +mod api_common; +mod plugins_common; + +use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use api_common::{BLOCKED, FakeResponse, FakeTransport, Package, render, temporary_directory, url, widget_manifest}; +use plugins_common::*; +use serde_json::{Value, json}; +use sw_core::api::fetch::{self, Cancel, FetchError, FetchModule, Resolver, Response, Url}; +use sw_core::plugins::manifest::{PluginManifest, PluginOperation}; +use sw_core::plugins::module::{self, DataSourceExecutor, DataSourceModule, DataSourceTransport, PackageScope}; +use sw_core::plugins::{PluginStore, requests}; +use sw_core::{Completion, ExecutionRequest, NativeModule}; + +#[derive(Debug, Clone)] +struct Call { + method: String, + url: Url, + headers: Vec<(String, String)>, + body: Option>, + timeout: u64, +} + +type Respond = Box bool) -> Result + Send + Sync>; + +/// Offline transport: records each call and answers with `respond`. +struct Fake { + respond: Mutex, + calls: Mutex>, +} + +impl Fake { + fn respond(&self, respond: impl Fn(&Call, &dyn Fn(&Url) -> bool) -> Result + Send + Sync + 'static) { + *self.respond.lock().unwrap() = Box::new(respond); + } + + fn calls(&self) -> Vec { + self.calls.lock().unwrap().clone() + } +} + +fn response(status: u16, body: &str) -> Result { + Ok(Response { status, body: body.as_bytes().to_vec(), headers: HashMap::new() }) +} + +struct Fixture { + temporary: PathBuf, + store: Arc, + transport: Arc, + plugin: PluginManifest, +} + +impl Fixture { + fn new() -> Self { + let temporary = temporary_directory("datasource"); + let store = Arc::new(PluginStore::new(temporary.join("Plugins"))); + store.install(&mut &bytes()[..]).expect("the test plugin"); + let transport = Arc::new(Fake { respond: Mutex::new(Box::new(|_, _| response(200, "{\"ok\":true}"))), calls: Mutex::default() }); + Self { temporary, store, transport, plugin: base_manifest() } + } + + /// The module over the fake transport, running the request on the calling thread. + fn module(&self) -> DataSourceModule { + let fake = self.transport.clone(); + let transport: DataSourceTransport = Arc::new(move |method, url, headers, body, timeout, allow_redirect, _: Option| { + let call = Call { method: method.to_owned(), url: url.clone(), headers: headers.to_vec(), body: body.map(<[u8]>::to_vec), timeout }; + fake.calls.lock().unwrap().push(call.clone()); + (fake.respond.lock().unwrap())(&call, allow_redirect) + }); + DataSourceModule::with_parts(self.store.clone(), transport, direct()) + } + + /// Starts the operation; `None` when no answer arrived. + fn run_with(&self, module: &DataSourceModule, request: &ExecutionRequest<'_>, payload: Option<&Value>) -> Option<(bool, Value)> { + let (done, answers) = Completion::detached(request.session.clone()); + module.start(request, DataSourceModule::OPERATION, payload.and_then(Value::as_object), done); + let (_, ok, json) = answers.recv_timeout(Duration::from_millis(200)).ok()?; + assert!(answers.try_recv().is_err(), "done must be called exactly once"); + Some((ok, serde_json::from_str(&json.expect("an answer")).expect("answer JSON"))) + } + + fn run(&self, package: &Package, payload: Value) -> (bool, Value) { + self.run_with(&self.module(), &package.request(), Some(&payload)).expect("an answer") + } + + fn assert_rejected(&self, expected: &str, outcome: (bool, Value)) { + assert_eq!(outcome, (false, json!(expected))); + assert_eq!(self.transport.calls().len(), 0, "a rejected request must not reach the network"); + } +} + +impl Drop for Fixture { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.temporary); + } +} + +fn direct() -> DataSourceExecutor { + Arc::new(|work| work()) +} + +fn widget(plugins: Option<&[&str]>, permissions: &[&str], domains: Option<&[&str]>) -> Package { + let mut manifest = json!({ "formatVersion": 2, "permissions": permissions }); + if let Some(plugins) = plugins { + manifest["plugins"] = json!(plugins); + } + if let Some(domains) = domains { + manifest["networkDomains"] = json!(domains); + } + Package::with_manifest(manifest) +} + +fn default_widget() -> Package { + widget(Some(&[ID]), &["network"], Some(&["api.example.com"])) +} + +fn with_domains(domains: &[&str]) -> Package { + widget(Some(&[ID]), &["network"], Some(domains)) +} + +fn payload(plugin: &str, operation: &str, parameters: Value) -> Value { + json!({ "pluginID": plugin, "operationID": operation, "parameters": parameters }) +} + +fn status() -> Value { + payload(ID, "status", json!({})) +} + +#[test] +fn exposes_the_data_source_global_and_operation() { + let fixture = Fixture::new(); + let module = fixture.module(); + assert_eq!(module.apis(), ["$dataSource"]); + assert!(module.handles("datasource.request")); + assert!(!module.handles("http.request")); +} + +#[test] +fn resolves_json_and_text_responses() { + let fixture = Fixture::new(); + fixture.transport.respond(|_, _| response(200, "plain text")); + assert_eq!(fixture.run(&default_widget(), status()), (true, json!("plain text"))); + let calls = fixture.transport.calls(); + assert_eq!((calls[0].url.as_str(), calls[0].method.as_str(), calls[0].timeout), ("https://api.example.com/status", "GET", DataSourceModule::TIMEOUT_MILLISECONDS)); + + fixture.transport.respond(|_, _| response(200, "{\"name\":\"Ada\",\"tags\":[1,2]}")); + let parameters = json!({ "user": "ada l", "X-Token": "t", "enabled": true, "undeclared": "x" }); + let (ok, value) = fixture.run(&default_widget(), payload(ID, "update", parameters)); + assert!(ok); + assert_eq!(value["name"], "Ada"); + let call = &fixture.transport.calls()[1]; + assert_eq!((call.url.as_str(), call.method.as_str()), ("https://api.example.com/users/ada%20l?page=1", "POST")); + assert!(call.headers.contains(&("X-Token".to_owned(), "t".to_owned()))); + assert_eq!(call.body.as_deref(), Some(&b"{\"enabled\":true}"[..])); +} + +#[test] +fn built_in_plugins_work_when_declared() { + let fixture = Fixture::new(); + let id = "app.scriptwidget.datasource.github"; + let package = widget(Some(&[id]), &["network"], Some(&["api.github.com"])); + assert!(fixture.run(&package, json!({ "pluginID": id, "operationID": "repository" })).0); + assert_eq!(fixture.transport.calls()[0].url.as_str(), "https://api.github.com/repos/everettjf/scriptwidget"); +} + +#[test] +fn undeclared_plugin_is_rejected() { + let fixture = Fixture::new(); + let message = format!("Plugin {ID} is not declared in widget.json"); + fixture.assert_rejected(&message, fixture.run(&widget(Some(&["dev.scriptwidget.other"]), &["network"], Some(&["api.example.com"])), status())); + fixture.assert_rejected(&message, fixture.run(&widget(None, &["network"], Some(&["api.example.com"])), status())); + fixture.assert_rejected(&message, fixture.run(&widget(Some(&[]), &["network"], Some(&["api.example.com"])), status())); + // A legacy package has no manifest and therefore declares nothing, even if stale manifest data is present. + fixture.assert_rejected(&message, fixture.run(&Package::legacy(), status())); + let mut stale = default_widget(); + stale.prepared.has_manifest = false; + fixture.assert_rejected(&message, fixture.run(&stale, status())); + // Installing a plugin grants nothing: the built-in is installed but not declared. + fixture.assert_rejected("Plugin app.scriptwidget.datasource.github is not declared in widget.json", fixture.run(&default_widget(), payload("app.scriptwidget.datasource.github", "repository", json!({})))); +} + +#[test] +fn missing_network_permission_is_rejected() { + let fixture = Fixture::new(); + fixture.assert_rejected("Plugin requests require the network permission", fixture.run(&widget(Some(&[ID]), &[], Some(&["api.example.com"])), status())); + fixture.assert_rejected("Plugin requests require the network permission", fixture.run(&widget(Some(&[ID]), &["location"], Some(&["api.example.com"])), status())); +} + +#[test] +fn host_missing_from_network_domains_is_rejected() { + let fixture = Fixture::new(); + let message = "Data source host is not declared in widget.json networkDomains."; + fixture.assert_rejected(message, fixture.run(&with_domains(&["other.example.com"]), status())); + fixture.assert_rejected(message, fixture.run(&with_domains(&[]), status())); + fixture.assert_rejected(message, fixture.run(&widget(Some(&[ID]), &["network"], None), status())); + fixture.assert_rejected(message, fixture.run(&with_domains(&["*.api.example.com"]), status())); + assert!(fixture.run(&with_domains(&["*.example.com"]), status()).0); +} + +#[test] +fn host_missing_from_plugin_hosts_is_rejected() { + let fixture = Fixture::new(); + let package = with_domains(&["api.example.com", "evil.example.net"]); + let scope = PackageScope::of(&package.request()); + assert_eq!(module::destination_error(&scope, &fixture.plugin, &url("https://evil.example.net/status")), Some("Plugin request escaped its declared hosts.")); + assert_eq!(module::destination_error(&scope, &fixture.plugin, &url("https://api.example.com/status")), None); + // The package allows the host, the plugin does not declare it: a tampered manifest is still refused. + let tampered = PluginManifest { base_url: "https://evil.example.net".into(), ..fixture.plugin.clone() }; + assert_eq!(requests::prepare(&tampered, "status", &values(json!({}))).unwrap_err().message, "Plugin request escaped its declared hosts."); +} + +#[test] +fn http_and_private_destinations_are_rejected() { + let fixture = Fixture::new(); + let package = with_domains(&["api.example.com", "localhost", "10.0.0.1"]); + let scope = PackageScope::of(&package.request()); + let destination = |plugin: &PluginManifest, address: &str| module::destination_error(&scope, plugin, &url(address)); + assert_eq!(destination(&fixture.plugin, "http://api.example.com/status"), Some("Data source requests require HTTPS")); + assert_eq!(destination(&fixture.plugin, "ftp://api.example.com/status"), Some("Data source requests require HTTPS")); + assert_eq!(destination(&fixture.plugin, "file:///status"), Some("Data source requests require HTTPS")); + assert_eq!(destination(&fixture.plugin, "https://user:pw@api.example.com/status"), Some("URL credentials are not allowed")); + let local = PluginManifest { hosts: vec!["api.example.com".into(), "localhost".into(), "10.0.0.1".into()], ..fixture.plugin.clone() }; + assert_eq!(destination(&local, "https://localhost/status"), Some(BLOCKED)); + assert_eq!(destination(&local, "https://10.0.0.1/status"), Some(BLOCKED)); + assert_eq!(destination(&local, "https://167772161/status"), Some(BLOCKED), "10.0.0.1 written as one number"); +} + +#[test] +fn timeline_projections_and_live_activities_are_rejected() { + let fixture = Fixture::new(); + let timeline = "[timeline_read_only] Future entries cannot make live service requests"; + fixture.assert_rejected(timeline, fixture.run(&default_widget().environments(&[("widget-size", "medium"), ("timeline-is-projection", "true")]), status())); + let mut compiled = default_widget(); + compiled.prepared.timeline_environment = vec![("timeline-is-projection".into(), "true".into())]; + fixture.assert_rejected(timeline, fixture.run(&compiled, status())); + let live = "[live_activity_unavailable] Live Activity presentations cannot request service data; update their state from the app"; + fixture.assert_rejected(live, fixture.run(&default_widget().environments(&[("widget-size", "live-activity")]), status())); + fixture.assert_rejected(live, fixture.run(&default_widget().environments(&[("widget-size", "dynamic-island")]), status())); + assert!(fixture.run(&default_widget().environments(&[("widget-size", "medium"), ("timeline-is-projection", "false")]), status()).0); +} + +#[test] +fn a_finished_execution_is_rejected() { + let fixture = Fixture::new(); + let package = default_widget(); + let request = package.request(); + assert_eq!(module::presentation_error(&request), None); + request.session.finish(); + assert_eq!(module::presentation_error(&request), Some("Execution session is no longer active")); + // Nobody is left to hear the rejection, and nothing is sent. + assert_eq!(fixture.run_with(&fixture.module(), &request, Some(&status())), None); + assert_eq!(fixture.transport.calls().len(), 0); +} + +#[test] +fn the_request_budget_is_shared_with_fetch() { + let fixture = Fixture::new(); + let module = fixture.module(); + let package = default_widget(); + let request = package.request(); + for _ in 0..fetch::MAXIMUM_REQUESTS { + assert!(fixture.run_with(&module, &request, Some(&status())).unwrap().0); + } + assert_eq!(fixture.run_with(&module, &request, Some(&status())), Some((false, json!("Network request budget exceeded")))); + assert_eq!(fixture.transport.calls().len(), fetch::MAXIMUM_REQUESTS as usize); + + // Requests made by fetch count against the same budget. + let shared = package.request(); + for _ in 0..fetch::MAXIMUM_REQUESTS { + shared.session.add_network_request(); + } + assert_eq!(fixture.run_with(&module, &shared, Some(&status())), Some((false, json!("Network request budget exceeded")))); + assert_eq!(fixture.transport.calls().len(), fetch::MAXIMUM_REQUESTS as usize); +} + +#[test] +fn rejected_requests_do_not_consume_the_budget() { + let fixture = Fixture::new(); + let module = fixture.module(); + let package = default_widget(); + let request = package.request(); + for job in [payload(ID, "missing", json!({})), payload(ID, "update", json!({})), payload("dev.scriptwidget.other", "status", json!({}))] { + assert!(!fixture.run_with(&module, &request, Some(&job)).unwrap().0); + } + let undeclared_host = with_domains(&["other.example.com"]); + let other = undeclared_host.request(); + assert!(!fixture.run_with(&module, &other, Some(&status())).unwrap().0); + assert_eq!((request.session.network_requests(), other.session.network_requests()), (0, 0)); +} + +#[test] +fn redirects_are_restricted_to_declared_https_hosts() { + let fixture = Fixture::new(); + let decisions: Arc>> = Arc::default(); + let seen = decisions.clone(); + fixture.transport.respond(move |_, allow_redirect| { + for target in ["https://api.example.com/moved", "https://evil.example.net/x", "https://cdn.example.com/x", "http://api.example.com/moved", "https://localhost/x", "https://user@api.example.com/x"] { + seen.lock().unwrap().push((target.to_owned(), allow_redirect(&url(target)))); + } + if !allow_redirect(&url("https://evil.example.net/x")) { + return Err(FetchError::failed("Redirect target is not allowed")); + } + response(200, "{}") + }); + let outcome = fixture.run(&with_domains(&["*.example.com", "evil.example.net"]), status()); + assert_eq!(outcome, (false, json!("Redirect target is not allowed"))); + let expected = [ + ("https://api.example.com/moved", true), + ("https://evil.example.net/x", false), // allowed by the package, not declared by the plugin + ("https://cdn.example.com/x", false), // allowed by the package wildcard, not declared by the plugin + ("http://api.example.com/moved", false), // downgrade + ("https://localhost/x", false), + ("https://user@api.example.com/x", false), + ]; + assert_eq!(*decisions.lock().unwrap(), expected.map(|(target, allowed)| (target.to_owned(), allowed))); + + // A host the plugin declares but the package does not allow is refused as well. + let wide = PluginManifest { hosts: vec!["api.example.com".into(), "cdn.example.org".into()], ..fixture.plugin.clone() }; + let narrow = default_widget(); + let request = narrow.request(); + let scope = PackageScope::of(&request); + assert!(!module::allows_redirect(&scope, &wide, &url("https://cdn.example.org/x"))); + assert!(module::allows_redirect(&scope, &wide, &url("https://api.example.com/x"))); + request.session.finish(); + assert!(!module::allows_redirect(&scope, &wide, &url("https://api.example.com/x"))); +} + +#[test] +fn uninstalled_plugin_and_bad_requests_are_rejected() { + let fixture = Fixture::new(); + let other = "dev.scriptwidget.other"; + fixture.assert_rejected(&format!("Plugin {other} is not installed"), fixture.run(&widget(Some(&[other]), &["network"], Some(&["api.example.com"])), payload(other, "status", json!({})))); + fixture.assert_rejected(&format!("Operation missing was not found in {ID}."), fixture.run(&default_widget(), payload(ID, "missing", json!({})))); + fixture.assert_rejected("Missing required parameter user.", fixture.run(&default_widget(), payload(ID, "update", json!({})))); + let needs_ids = "Data source requests need a plugin id and an operation id"; + fixture.assert_rejected(needs_ids, fixture.run(&default_widget(), json!({ "pluginID": 7, "operationID": "status" }))); + fixture.assert_rejected(needs_ids, fixture.run(&default_widget(), json!({ "pluginID": ID }))); + let package = default_widget(); + assert_eq!(fixture.run_with(&fixture.module(), &package.request(), None), Some((false, json!(needs_ids)))); + let (done, answers) = Completion::detached(package.request().session.clone()); + fixture.module().start(&package.request(), "datasource.other", status().as_object(), done); + assert_eq!(answers.recv_timeout(Duration::from_secs(1)).unwrap().2.as_deref(), Some("\"Unsupported operation datasource.other\"")); +} + +#[test] +fn response_failures_use_the_swift_messages() { + let fixture = Fixture::new(); + let text = |job: Value| fixture.run(&default_widget(), job).1; + fixture.transport.respond(|_, _| response(500, "{\"secret\":\"body\"}")); + assert_eq!(text(status()), "Data source request failed"); + fixture.transport.respond(|_, _| Ok(Response { status: 200, body: vec![0xFF], headers: HashMap::new() })); + assert_eq!(text(status()), "Data source returned non-UTF-8 text"); + fixture.transport.respond(|_, _| response(200, "")); + assert_eq!(text(payload(ID, "update", json!({ "user": "u", "X-Token": "t", "enabled": 1 }))), "Data source returned invalid JSON"); + fixture.transport.respond(|_, _| Ok(Response { status: 200, body: vec![0; fetch::MAXIMUM_RESPONSE_BYTES + 1], headers: HashMap::new() })); + assert_eq!(text(status()), "Data source response exceeds 2 MiB."); + fixture.transport.respond(|_, _| Err(FetchError::failed(BLOCKED))); + assert_eq!(text(status()), BLOCKED); + // Text from the network stack may quote the URL and its parameter values; it is not forwarded. + fixture.transport.respond(|call, _| Err(FetchError::Transport(format!("failed to connect to {}?token=secret", call.url)))); + assert_eq!(text(status()), "Data source request failed"); + fixture.transport.respond(|_, _| Err(FetchError::failed(""))); + assert_eq!(text(status()), "Data source request failed"); +} + +#[test] +fn results_are_dropped_after_the_execution_ends() { + let fixture = Fixture::new(); + let package = default_widget(); + let request = package.request(); + let session = request.session.clone(); + fixture.transport.respond(move |_, _| { + session.finish(); + response(200, "ok") + }); + assert_eq!(fixture.run_with(&fixture.module(), &request, Some(&status())), None); + assert_eq!(fixture.transport.calls().len(), 1); +} + +/// The same rules over the real `fetch` module, with the HTTP exchange and the +/// resolver replaced. A plugin request never leaves HTTPS, its declared hosts +/// or public addresses. +#[test] +fn the_fetch_transport_keeps_plugin_requests_on_declared_public_https_hosts() { + let fixture = Fixture::new(); + let location: Arc> = Arc::new(Mutex::new("https://api.example.com/moved".into())); + let target = location.clone(); + let exchange = FakeTransport::new(move |request| if request.path() == "/status" { FakeResponse::redirect(302, Some(&target.lock().unwrap())) } else { FakeResponse::text(200, "moved") }); + let address: Arc> = Arc::new(Mutex::new("93.184.216.34".into())); + let answer = address.clone(); + let resolver: Resolver = Arc::new(move |host: &str| match host { + "api.example.com" => Ok(vec![answer.lock().unwrap().parse().unwrap()]), + _ => Err(std::io::Error::other("unknown host")), + }); + let fetch = Arc::new(FetchModule::with_parts(resolver, exchange.clone())); + let module = || { + let fetch = fetch.clone(); + let transport: DataSourceTransport = Arc::new(move |method, url, headers, body, timeout, allow_redirect, cancel| fetch.perform(method, url, headers, body, timeout, allow_redirect, cancel)); + DataSourceModule::with_parts(fixture.store.clone(), transport, direct()) + }; + let run = |package: &Package| fixture.run_with(&module(), &package.request(), Some(&status())).unwrap(); + + assert_eq!(run(&default_widget()), (true, json!("moved"))); + let requests = exchange.requests(); + assert_eq!(requests.iter().map(|request| request.url.as_str()).collect::>(), ["https://api.example.com/status", "https://api.example.com/moved"]); + assert_eq!(requests[0].header("User-Agent"), Some("ScriptWidget-DataSource/1")); + + for to in ["http://api.example.com/moved", "https://evil.example.net/x", "https://cdn.example.com/x", "https://127.0.0.1/x", "https://user:pw@api.example.com/x", "file:///etc/passwd"] { + exchange.clear(); + *location.lock().unwrap() = to.to_owned(); + assert_eq!(run(&with_domains(&["*.example.com", "evil.example.net", "127.0.0.1"])), (false, json!("Redirect target is not allowed")), "{to}"); + assert_eq!(exchange.count(), 1); + } + + // The declared host itself resolving to a private address is refused before anything is sent. + exchange.clear(); + *address.lock().unwrap() = "10.0.0.7".into(); + assert_eq!(run(&default_widget()), (false, json!(BLOCKED))); + assert_eq!(exchange.count(), 0); +} + +/// From a script, through the JavaScript installer and the bridge. +#[test] +fn scripts_reach_a_declared_plugin_and_nothing_else() { + let fixture = Fixture::new(); + fixture.transport.respond(|call, _| response(200, &format!("{{\"path\":\"{}\"}}", call.url.path()))); + let script = r#" + $dataSource.request("dev.scriptwidget.test-source", "update", { user: "ada", "X-Token": "t", enabled: true }).then( + value => $render({value.path}), error => $render({"error: " + String(error)})) + "#; + let manifest = |plugins: &str, domains: &str| widget_manifest(r#"["network"]"#, domains).replace(r#""plugins":[]"#, &format!(r#""plugins":{plugins}"#)); + let declared = manifest(r#"["dev.scriptwidget.test-source"]"#, r#"["api.example.com"]"#); + assert_eq!(render(script, Some(&declared), vec![Arc::new(fixture.module())], &[]), "/users/ada"); + let undeclared = render(script, Some(&manifest("[]", r#"["api.example.com"]"#)), vec![Arc::new(fixture.module())], &[]); + assert!(undeclared.starts_with("error: ") && undeclared.contains("not declared"), "{undeclared}"); + let wrong_host = render(script, Some(&manifest(r#"["dev.scriptwidget.test-source"]"#, r#"["example.org"]"#)), vec![Arc::new(fixture.module())], &[]); + assert!(wrong_host.starts_with("error: "), "{wrong_host}"); + assert_eq!(fixture.transport.calls().len(), 1); + // The operation is one the plugin declares; the store's types stay what they were. + let _: &PluginOperation = &fixture.plugin.operations[0]; +} diff --git a/Linux/crates/sw-core/tests/plugins_requests.rs b/Linux/crates/sw-core/tests/plugins_requests.rs new file mode 100644 index 00000000..f5dec302 --- /dev/null +++ b/Linux/crates/sw-core/tests/plugins_requests.rs @@ -0,0 +1,216 @@ +//! Building a plugin request from script values, and mapping the response. +//! Ported from +//! `Windows/tests/ScriptWidget.Core.Tests/Plugins/DataSourceRequestBuilderTests.cs`. + +mod plugins_common; + +use plugins_common::*; +use serde_json::{Value, json}; +use sw_core::plugins::manifest::{PluginErrorKind, PluginManifest, PluginOperation}; +use sw_core::plugins::requests::{self, PreparedDataSourceRequest, map_response, prepare}; +use sw_core::plugins::store::built_ins; + +fn github() -> &'static PluginManifest { + built_ins().iter().find(|plugin| plugin.id == "app.scriptwidget.datasource.github").unwrap() +} + +fn weather() -> &'static PluginManifest { + built_ins().iter().find(|plugin| plugin.id == "app.scriptwidget.datasource.open-meteo").unwrap() +} + +fn body(prepared: &PreparedDataSourceRequest) -> Value { + serde_json::from_slice(prepared.body.as_deref().expect("a body")).expect("body JSON") +} + +fn failure(plugin: &PluginManifest, operation: &str, given: Value) -> (PluginErrorKind, String) { + let error = prepare(plugin, operation, &values(given)).expect_err("the request must be refused"); + (error.kind, error.message) +} + +// Swift: testRequestBuilderMapsPathQueryHeaderAndJSONBody +#[test] +fn maps_path_query_header_and_json_body() { + let prepared = prepare(&base_manifest(), "update", &values(json!({ "user": "hello world", "X-Token": "secret", "enabled": true }))).unwrap(); + assert_eq!(prepared.url.as_str(), "https://api.example.com/users/hello%20world?page=1"); + assert_eq!(prepared.method, "POST"); + assert_eq!(prepared.header("X-Token"), Some("secret")); + assert_eq!(prepared.header("User-Agent"), Some("ScriptWidget-DataSource/1")); + assert_eq!(prepared.header("Content-Type"), Some("application/json")); + assert_eq!(body(&prepared)["enabled"], true); +} + +#[test] +fn defaults_fill_missing_values_and_supplied_values_win() { + assert_eq!(prepare(github(), "repository", &values(json!({}))).unwrap().url.as_str(), "https://api.github.com/repos/everettjf/scriptwidget"); + assert_eq!(prepare(github(), "repository", &values(json!({ "owner": "quickjs-ng", "repo": "quickjs" }))).unwrap().url.as_str(), "https://api.github.com/repos/quickjs-ng/quickjs"); + let forecast = prepare(weather(), "forecast", &values(json!({ "latitude": 48.85, "longitude": 2, "undeclared": "ignored" }))).unwrap(); + assert_eq!(forecast.url.as_str(), "https://api.open-meteo.com/v1/forecast?latitude=48.85&longitude=2¤t=temperature_2m,weather_code&timezone=auto"); + assert_eq!(forecast.body, None); + assert_eq!(forecast.headers, [("User-Agent".to_owned(), "ScriptWidget-DataSource/1".to_owned())]); +} + +#[test] +fn path_values_cannot_change_the_path_structure() { + let path = |owner: Value| prepare(github(), "repository", &values(json!({ "owner": owner, "repo": "r" }))).map(|prepared| prepared.url.path().to_owned()).map_err(|error| error.message); + assert_eq!(path(json!("a/b")).unwrap(), "/repos/a%2Fb/r"); + assert_eq!(path(json!("a?x=1#f")).unwrap(), "/repos/a%3Fx=1%23f/r"); + assert_eq!(path(json!("{repo}")).unwrap(), "/repos/%7Brepo%7D/r"); + assert_eq!(path(json!("你好%")).unwrap(), "/repos/%E4%BD%A0%E5%A5%BD%25/r"); + assert_eq!(path(json!("a-b_c.d~e!$&'()*+,=:@")).unwrap(), "/repos/a-b_c.d~e!$&'()*+,=:@/r"); + assert_eq!(path(json!("a;b c\\d")).unwrap(), "/repos/a%3Bb%20c%5Cd/r"); + assert_eq!(path(json!(true)).unwrap(), "/repos/1/r"); + assert_eq!(path(json!(42)).unwrap(), "/repos/42/r"); + for value in ["..", "."] { + assert_eq!(path(json!(value)).unwrap_err(), "Could not encode owner."); + } + assert_eq!(path(json!({ "a": 1 })).unwrap_err(), "Could not encode owner."); + assert_eq!(path(json!([1])).unwrap_err(), "Could not encode owner."); + // An encoded separator or dot segment stays one segment in the URL that is sent. + assert_eq!(path(json!("../../admin")).unwrap(), "/repos/..%2F..%2Fadmin/r"); + assert_eq!(path(json!("%2E%2E")).unwrap(), "/repos/%252E%252E/r"); + assert_eq!(path(json!("...")).unwrap(), "/repos/.../r"); + assert_eq!(path(json!("a\r\nHost: evil")).unwrap(), "/repos/a%0D%0AHost:%20evil/r"); +} + +#[test] +fn query_values_cannot_add_query_items() { + let query = |value: Value| { + let prepared = prepare(weather(), "forecast", &values(json!({ "timezone": value }))).unwrap(); + let query = prepared.url.query().unwrap_or("").to_owned(); + query.split_once("timezone=").map(|(_, rest)| rest.to_owned()) + }; + assert_eq!(query(json!("a&b=c#d")).as_deref(), Some("a%26b%3Dc%23d")); + assert_eq!(query(json!("Europe/Paris")).as_deref(), Some("Europe/Paris")); + assert_eq!(query(json!("a b+c")).as_deref(), Some("a%20b+c")); + assert_eq!(query(json!("€")).as_deref(), Some("%E2%82%AC")); + assert_eq!(query(json!(false)).as_deref(), Some("0")); + assert_eq!(query(json!(true)).as_deref(), Some("1")); + assert_eq!(query(json!(1.5)).as_deref(), Some("1.5")); + // Numbers as the script bridge delivers them: JSON text. + let parsed = |text: &str| serde_json::from_str::(text).unwrap(); + assert_eq!(query(parsed("2.0")).as_deref(), Some("2")); + assert_eq!(query(parsed("1e-6")).as_deref(), Some("0.000001")); + assert_eq!(query(parsed("1e20")).as_deref(), Some("100000000000000000000")); + assert_eq!(query(parsed("\"x\"")).as_deref(), Some("x")); + // An empty optional value is omitted; null falls back to the default. + assert_eq!(query(json!("")), None); + assert_eq!(query(Value::Null).as_deref(), Some("auto")); + assert_eq!(prepare(weather(), "forecast", &values(json!({}))).unwrap().url.query().unwrap().split_once("timezone=").unwrap().1, "auto"); +} + +#[test] +fn missing_required_parameters_are_reported() { + let plugin = base_manifest(); + assert_eq!(failure(&plugin, "update", json!({ "X-Token": "t", "enabled": true })).1, "Missing required parameter user."); + assert_eq!(failure(&plugin, "update", json!({ "user": "u", "enabled": true })).1, "Missing required parameter X-Token."); + assert_eq!(failure(&plugin, "update", json!({ "user": "u", "X-Token": "t", "enabled": "" })), (PluginErrorKind::InvalidParameters, "Missing required parameter enabled.".to_owned())); + // A supplied empty string does not fall back to the default (Swift behavior). + assert_eq!(failure(weather(), "forecast", json!({ "latitude": "" })).1, "Missing required parameter latitude."); +} + +#[test] +fn unknown_operation_is_not_found() { + assert_eq!(failure(&base_manifest(), "missing", json!({})), (PluginErrorKind::NotFound, format!("Operation missing was not found in {ID}."))); +} + +#[test] +fn header_values_cannot_inject_headers() { + let plugin = base_manifest(); + for token in ["a\r\nX-Evil: 1", "a\nb", "a\0b", "a\u{7f}b"] { + assert_eq!(failure(&plugin, "update", json!({ "user": "u", "enabled": true, "X-Token": token })).1, "Could not encode X-Token."); + } + assert_eq!(failure(&plugin, "update", json!({ "user": "u", "enabled": true, "X-Token": [] })).1, "Could not encode X-Token."); +} + +#[test] +fn reserved_headers_are_set_by_the_runtime() { + let custom = manifest_with(|root| { + root["operations"][0]["parameters"][2]["id"] = json!("user-agent"); + root["operations"][0]["parameters"].as_array_mut().unwrap().push(json!({ "id": "content-type", "label": "Type", "location": "header", "required": false })); + }); + let prepared = prepare(&custom, "update", &values(json!({ "user": "u", "user-agent": "Mozilla", "content-type": "text/plain", "enabled": 1 }))).unwrap(); + assert_eq!(prepared.headers, [("User-Agent".to_owned(), "ScriptWidget-DataSource/1".to_owned()), ("Content-Type".to_owned(), "application/json".to_owned())]); +} + +#[test] +fn json_body_keeps_structured_values() { + let plugin = base_manifest(); + let prepared = prepare(&plugin, "update", &values(json!({ "user": "u", "X-Token": "t", "enabled": { "tags": ["a", "b"] } }))).unwrap(); + assert_eq!(body(&prepared)["enabled"]["tags"][1], "b"); + let number = prepare(&plugin, "update", &values(json!({ "user": "u", "X-Token": "t", "enabled": 3 }))).unwrap(); + assert_eq!(number.body.as_deref(), Some(&b"{\"enabled\":3}"[..])); + let text = prepare(&plugin, "update", &values(json!({ "user": "u", "X-Token": "t", "enabled": "yes" }))).unwrap(); + assert_eq!(text.body.as_deref(), Some(&b"{\"enabled\":\"yes\"}"[..])); +} + +#[test] +fn base_path_and_port_are_preserved() { + let versioned = manifest_with(|root| root["baseURL"] = json!("https://api.example.com:8443/v2")); + assert_eq!(prepare(&versioned, "status", &values(json!({}))).unwrap().url.as_str(), "https://api.example.com:8443/v2/status"); + let slash = manifest_with(|root| root["baseURL"] = json!("https://api.example.com/")); + assert_eq!(prepare(&slash, "status", &values(json!({}))).unwrap().url.as_str(), "https://api.example.com/status"); +} + +#[test] +fn requests_cannot_leave_the_declared_hosts() { + let plugin = base_manifest(); + let with_base = |base: &str| PluginManifest { base_url: base.into(), ..plugin.clone() }; + // Not reachable through the validator; the builder still refuses a tampered manifest. + assert_eq!(failure(&with_base("https://evil.example.net"), "status", json!({})), (PluginErrorKind::Permission, "Plugin request escaped its declared hosts.".to_owned())); + assert_eq!(failure(&with_base("http://api.example.com"), "status", json!({})).1, "Plugin request escaped its declared hosts."); + assert_eq!(failure(&with_base("https://api.example.com@evil.example.net"), "status", json!({})).1, "Plugin request escaped its declared hosts."); + assert_eq!(failure(&with_base("not a url"), "status", json!({})).1, "Invalid plugin baseURL."); + let with_path = |path: &str| PluginManifest { operations: vec![PluginOperation { path: path.into(), ..plugin.operations[1].clone() }], ..plugin.clone() }; + assert_eq!(failure(&with_path("/x/{gone}"), "status", json!({})).1, "A path parameter is unresolved."); + // A backslash, which a browser's parser reads as a slash, never reaches it. + assert_eq!(failure(&with_base("https://api.example.com\\@evil.example.net"), "status", json!({})).1, "Invalid plugin baseURL."); + assert_eq!(failure(&with_path("/\\evil.example.net/x"), "status", json!({})).1, "Invalid plugin baseURL."); + // A path that starts a new authority stays a path of the declared host. + let double = prepare(&with_path("//evil.example.net/x"), "status", &values(json!({}))).unwrap(); + assert_eq!((double.url.host_str(), double.url.path()), (Some("api.example.com"), "//evil.example.net/x")); +} + +// MARK: - Response mapping + +#[test] +fn json_responses_pass_through_unchanged() { + let plugin = base_manifest(); + let operation = &plugin.operations[0]; + let body = r#"{"temperature": 21.50, "tags": ["a", null], "big": 12345678901234567890}"#; + assert_eq!(map_response(operation, 200, body.as_bytes()).unwrap(), body); + assert_eq!(map_response(operation, 204, &[&[0xEF, 0xBB, 0xBF][..], b"[1,2]"].concat()).unwrap(), "[1,2]"); + assert_eq!(map_response(operation, 200, br#"{"a":1,"a":2}"#).unwrap(), r#"{"a":1,"a":2}"#); +} + +#[test] +fn invalid_json_responses_are_rejected() { + let plugin = base_manifest(); + let operation = &plugin.operations[0]; + for body in ["", "", "{\"a\":}", "{} trailing", "\"text\"", "42", "null", "{'a':1}"] { + let error = map_response(operation, 200, body.as_bytes()).unwrap_err(); + assert_eq!((error.kind, error.message.as_str()), (PluginErrorKind::Network, "Data source returned invalid JSON"), "{body}"); + } + assert_eq!(map_response(operation, 200, &[0x7B, 0xC3, 0x28, 0x7D]).unwrap_err().message, "Data source returned invalid JSON"); +} + +#[test] +fn text_responses_become_json_strings() { + let plugin = base_manifest(); + let operation = &plugin.operations[1]; + assert_eq!(map_response(operation, 200, "line 1\n\"quoted\" é".as_bytes()).unwrap(), "\"line 1\\n\\\"quoted\\\" é\""); + assert_eq!(map_response(operation, 200, b"").unwrap(), "\"\""); + assert_eq!(map_response(operation, 200, &[0xFF, 0xFE]).unwrap_err().message, "Data source returned non-UTF-8 text"); + // The text is a JavaScript literal as well: these two characters end a line there. + assert_eq!(map_response(operation, 200, "a\u{2028}b".as_bytes()).unwrap(), "\"a\\u2028b\""); +} + +#[test] +fn failed_and_oversize_responses_are_rejected() { + let plugin = base_manifest(); + for status in [199, 301, 404, 500] { + assert_eq!(map_response(&plugin.operations[0], status, b"{}").unwrap_err().message, "Data source request failed"); + } + let oversize = vec![b'a'; requests::MAXIMUM_RESPONSE_BYTES + 1]; + assert_eq!(map_response(&plugin.operations[1], 200, &oversize).unwrap_err().message, "Data source response exceeds 2 MiB."); + assert_eq!(requests::MAXIMUM_RESPONSE_BYTES, 2 * 1024 * 1024); +} diff --git a/Linux/crates/sw-core/tests/plugins_store.rs b/Linux/crates/sw-core/tests/plugins_store.rs new file mode 100644 index 00000000..1c28a881 --- /dev/null +++ b/Linux/crates/sw-core/tests/plugins_store.rs @@ -0,0 +1,385 @@ +//! Installing, listing, exporting and deleting plugins. Ported from +//! `Windows/tests/ScriptWidget.Core.Tests/Plugins/PluginStoreTests.cs`. + +mod plugins_common; + +use std::os::unix::fs::PermissionsExt; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; + +use plugins_common::*; +use serde_json::json; +use sw_core::plugins::manifest::{self, PluginErrorKind, PluginManifest}; +use sw_core::plugins::store::{self, PluginStore, built_ins}; + +struct Fixture { + temporary: PathBuf, + root: PathBuf, + store: PluginStore, +} + +impl Fixture { + fn new() -> Self { + static COUNTER: AtomicU64 = AtomicU64::new(0); + let temporary = std::env::temp_dir().join(format!("scriptwidget-test-plugins-{}-{}", std::process::id(), COUNTER.fetch_add(1, Ordering::SeqCst))); + let _ = std::fs::remove_dir_all(&temporary); + std::fs::create_dir_all(&temporary).unwrap(); + let root = temporary.join("Plugins"); + Self { store: PluginStore::new(&root), root, temporary } + } + + fn install(&self, bytes: &[u8]) -> Result { + self.store.install(&mut &bytes[..]) + } + + /// Installed plugin directories; the hidden staging directory must be empty. + fn installed(&self) -> Vec { + assert_eq!(names(&self.root.join(".staging")), [] as [&str; 0]); + names(&self.root).into_iter().filter(|name| name != ".staging").collect() + } + + fn assert_import_rejected(&self, expected: &str, archive: &[u8]) { + let error = self.install(archive).expect_err("the import must be refused"); + assert!(error.message.contains(expected), "Expected \"{expected}\" in \"{}\"", error.message); + assert_eq!(self.installed(), [] as [&str; 0]); + assert_eq!(self.store.manifest(ID), None); + } +} + +impl Drop for Fixture { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.temporary); + } +} + +fn names(directory: &Path) -> Vec { + let mut names: Vec = std::fs::read_dir(directory).into_iter().flatten().map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()).collect(); + names.sort_unstable(); + names +} + +fn entry(name: &str, data: &[u8]) -> RawEntry { + RawEntry::new(name, data) +} + +fn mode(path: &Path) -> u32 { + std::fs::symlink_metadata(path).unwrap().permissions().mode() & 0o777 +} + +#[test] +fn lists_built_ins_when_nothing_is_installed() { + let fixture = Fixture::new(); + let listed = fixture.store.list(); + assert_eq!(listed.iter().map(|plugin| plugin.manifest.name.as_str()).collect::>(), ["GitHub Public API", "Open-Meteo"]); + assert!(listed.iter().all(|plugin| plugin.built_in && plugin.readme.as_deref().is_some_and(|readme| readme.starts_with("# ")))); + assert!(fixture.store.manifest("app.scriptwidget.datasource.open-meteo").is_some()); + assert_eq!(fixture.store.manifest("dev.unknown.plugin"), None); + assert!(!fixture.root.exists(), "listing must not create the directory"); +} + +#[test] +fn installs_bare_plugin_json() { + let fixture = Fixture::new(); + assert_eq!(fixture.install(&bytes()).unwrap().id, ID); + assert_eq!(fixture.installed(), ["test-source"]); + assert_eq!(names(&fixture.root.join("test-source")), ["plugin.json"]); + let listed = fixture.store.list(); + let plugin = listed.iter().find(|plugin| plugin.manifest.id == ID).unwrap(); + assert!(!plugin.built_in); + assert_eq!(plugin.readme, None); + assert_eq!(listed.len(), built_ins().len() + 1); + assert_eq!(fixture.store.manifest(ID), Some(base_manifest())); + // Everything the store writes is private to the user. + assert_eq!(mode(&fixture.root), 0o700); + assert_eq!(mode(&fixture.root.join("test-source")), 0o700); + assert_eq!(mode(&fixture.root.join("test-source/plugin.json")), 0o600); +} + +#[test] +fn installs_archive_with_files_at_the_root() { + let fixture = Fixture::new(); + fixture.install(&raw_zip(&[entry("plugin.json", &bytes()), entry("README.md", b"# Test\n\nHello")])).unwrap(); + assert_eq!(names(&fixture.root.join("test-source")), ["README.md", "plugin.json"]); + assert_eq!(fixture.store.list().iter().find(|plugin| plugin.manifest.id == ID).unwrap().readme.as_deref(), Some("# Test\n\nHello")); +} + +#[test] +fn installs_archive_with_one_directory_and_ignores_hidden_files() { + let fixture = Fixture::new(); + fixture + .install(&raw_zip(&[ + RawEntry::directory("Test/"), + RawEntry::deflated("Test/plugin.json", &bytes()), + RawEntry::deflated("Test/README.md", b"docs"), + RawEntry::deflated("Test/.DS_Store", &[1]), + RawEntry::deflated("__MACOSX/Test/._plugin.json", &[2]), + RawEntry::deflated(".hidden", &[3]), + ])) + .unwrap(); + assert_eq!(names(&fixture.root.join("test-source")), ["README.md", "plugin.json"]); +} + +#[test] +fn duplicate_ids_are_refused() { + let fixture = Fixture::new(); + fixture.install(&bytes()).unwrap(); + assert_eq!(fixture.install(&bytes()).unwrap_err().message, format!("Plugin {ID} is already installed.")); + assert_eq!(fixture.installed(), ["test-source"]); + let built_in = &built_ins()[0]; + assert_eq!(fixture.install(built_in.to_json().as_bytes()).unwrap_err().message, format!("Plugin {} is already installed.", built_in.id)); +} + +#[test] +fn directory_names_are_slugs_and_never_collide() { + let fixture = Fixture::new(); + fixture.install(&bytes_with(|root| root["name"] = json!("../../Evil Name!"))).unwrap(); + fixture + .install(&bytes_with(|root| { + root["name"] = json!("evil name"); + root["id"] = json!("dev.scriptwidget.second"); + })) + .unwrap(); + fixture + .install(&bytes_with(|root| { + root["name"] = json!("数据"); + root["id"] = json!("dev.scriptwidget.third"); + })) + .unwrap(); + // Names that mean something to a shell or to other systems are plain directory names here. + fixture + .install(&bytes_with(|root| { + root["name"] = json!("CON"); + root["id"] = json!("dev.scriptwidget.fourth"); + })) + .unwrap(); + fixture + .install(&bytes_with(|root| { + root["name"] = json!(".staging"); + root["id"] = json!("dev.scriptwidget.fifth"); + })) + .unwrap(); + assert_eq!(fixture.installed(), ["con", "evil-name", "evil-name-2", "plugin", "staging"]); + assert_eq!(names(&fixture.temporary), ["Plugins"]); +} + +#[test] +fn invalid_manifests_are_not_installed() { + let fixture = Fixture::new(); + fixture.assert_import_rejected("plugin.json contains unknown fields.", &bytes_with(|root| root["unexpected"] = json!(true))); + fixture.assert_import_rejected("baseURL must be an HTTPS URL", &raw_zip(&[entry("plugin.json", &bytes_with(|root| root["baseURL"] = json!("http://api.example.com")))])); + fixture.assert_import_rejected("plugin.json is not valid JSON.", b"not json"); + fixture.assert_import_rejected("Archive must contain one Data Source Plugin 1.0 package.", b""); +} + +#[test] +fn oversize_input_is_rejected() { + let fixture = Fixture::new(); + fixture.assert_import_rejected("A plugin package may not exceed 512 KiB.", &vec![b' '; store::MAXIMUM_PACKAGE_BYTES + 1]); + fixture.assert_import_rejected("A plugin package may not exceed 512 KiB.", &raw_zip(&[entry("plugin.json", &bytes()), entry("README.md", &vec![0u8; store::MAXIMUM_PACKAGE_BYTES])])); + // A highly compressible README fits the archive limit but not the expanded limit. + fixture.assert_import_rejected("README.md exceeds 512 KiB.", &raw_zip(&[RawEntry::deflated("plugin.json", &bytes()), RawEntry::deflated("README.md", &vec![b'a'; store::MAXIMUM_README_BYTES + 1])])); + fixture.assert_import_rejected("plugin.json exceeds 256 KiB.", &raw_zip(&[RawEntry::deflated("plugin.json", &vec![b' '; manifest::MAXIMUM_MANIFEST_BYTES + 1])])); + // A reader that never ends is cut off at the limit. + let error = fixture.store.install(&mut std::io::repeat(b' ')).unwrap_err(); + assert_eq!(error.message, "A plugin package may not exceed 512 KiB."); +} + +#[test] +fn only_plugin_json_and_readme_are_allowed() { + let fixture = Fixture::new(); + let manifest = bytes(); + let only = "A plugin package allows only plugin.json and optional README.md."; + fixture.assert_import_rejected(only, &raw_zip(&[entry("plugin.json", &manifest), entry("main.js", b"fetch()")])); + fixture.assert_import_rejected(only, &raw_zip(&[entry("plugin.json", &manifest), entry("lib/native.so", b"x")])); + fixture.assert_import_rejected(only, &raw_zip(&[entry("Test/plugin.json", &manifest), entry("Test/extra/plugin.json", &manifest)])); + fixture.assert_import_rejected(only, &raw_zip(&[entry("Test/README.md", b"no manifest")])); + fixture.assert_import_rejected(only, &raw_zip(&[entry("Test/Plugin.json", &manifest)])); + fixture.assert_import_rejected(only, &raw_zip(&[entry("plugin.json", &manifest), entry("readme.md", b"wrong case")])); + fixture.assert_import_rejected(only, &raw_zip(&[entry("plugin.json", &manifest), RawEntry::directory("README.md/")])); + let package = "Archive must contain one Data Source Plugin 1.0 package."; + fixture.assert_import_rejected(package, &raw_zip(&[entry("A/plugin.json", &manifest), entry("B/plugin.json", &manifest)])); + fixture.assert_import_rejected(package, &raw_zip(&[entry("README.md", b"no package")])); + fixture.assert_import_rejected("README.md must be UTF-8 text.", &raw_zip(&[entry("plugin.json", &manifest), entry("README.md", &[0xFF])])); +} + +#[test] +fn unsafe_archives_are_rejected_before_anything_is_written() { + let fixture = Fixture::new(); + let manifest = bytes(); + let plugin = entry("plugin.json", &manifest); + let with = |other: RawEntry| raw_zip(&[plugin.clone(), other]); + for name in ["../README.md", "/README.md", "Test\\README.md", "C:README.md", "a/./README.md", "a\u{202e}b", "a\nb"] { + fixture.assert_import_rejected("unsafe or non-UTF-8 path", &with(entry(name, b"x"))); + } + let mut not_utf8 = with(entry("README.mX", b"x")); + let at = not_utf8.windows(9).position(|window| window == b"README.mX").unwrap(); + let positions: Vec = not_utf8.windows(9).enumerate().filter(|(_, window)| *window == b"README.mX").map(|(index, _)| index + 8).collect(); + assert!(positions.len() == 2 && positions[0] == at + 8); + for position in positions { + not_utf8[position] = 0xFF; + } + fixture.assert_import_rejected("unsafe or non-UTF-8 path", ¬_utf8); + fixture.assert_import_rejected("Symbolic links are not allowed", &with(RawEntry { external_attributes: 0xA1FF << 16, ..entry("README.md", b"/etc/hosts") })); + fixture.assert_import_rejected("Only regular files and directories", &with(RawEntry { external_attributes: 0x1000 << 16, ..entry("README.md", b"") })); + fixture.assert_import_rejected("duplicate or case-colliding paths", &with(entry("PLUGIN.JSON", &manifest))); + fixture.assert_import_rejected("duplicate or case-colliding paths", &with(entry("plugin.json", &manifest))); + fixture.assert_import_rejected("duplicate or case-colliding paths", &raw_zip(&[plugin.clone(), entry(".caf\u{e9}", b"1"), entry(".cafe\u{301}", b"2")])); + fixture.assert_import_rejected("duplicate or case-colliding paths", &raw_zip(&[plugin.clone(), entry("README.md", b"file"), entry("README.md/inner", b"and directory")])); + fixture.assert_import_rejected("Encrypted package entries", &raw_zip(&[RawEntry { flags: 0x0801, ..plugin.clone() }])); + fixture.assert_import_rejected("Unsupported ZIP compression method", &raw_zip(&[RawEntry { method: 12, ..plugin.clone() }])); + fixture.assert_import_rejected("no valid ZIP central directory", b"PK\x03\x04 truncated"); + let mut trailing = raw_zip(std::slice::from_ref(&plugin)); + trailing.extend_from_slice(b"extra"); + fixture.assert_import_rejected("malformed ZIP footer", &trailing); + let many: Vec = (0..501).map(|index| entry(&format!(".f{index}"), b"")).collect(); + fixture.assert_import_rejected("Archive must contain 1–500 entries.", &raw_zip(&many)); + assert_eq!(names(&fixture.temporary), [] as [&str; 0]); +} + +#[test] +fn forged_sizes_and_corrupt_entries_are_rejected() { + let fixture = Fixture::new(); + let manifest = bytes(); + let archive = raw_zip(&[RawEntry::deflated("plugin.json", &manifest)]); + let corrupt = "The plugin archive is corrupt."; + fixture.assert_import_rejected(corrupt, &forge_declared_size(&archive, 0, manifest.len() as u32 - 1)); + fixture.assert_import_rejected(corrupt, &forge_declared_size(&archive, 0, manifest.len() as u32 + 1)); + let mut stored = raw_zip(&[entry("plugin.json", &manifest)]); + stored[40 + "plugin.json".len()] += 1; + fixture.assert_import_rejected(corrupt, &stored); + let stored = raw_zip(&[entry("plugin.json", &manifest)]); + fixture.assert_import_rejected(corrupt, &forge_declared_size(&stored, 0, manifest.len() as u32 - 1)); + // A local header that is not where the directory says it is. + let mut misplaced = raw_zip(&[entry("plugin.json", &manifest)]); + misplaced[2] = 9; + fixture.assert_import_rejected(corrupt, &misplaced); + // Truncated deflate data. + let mut cut = raw_zip(&[RawEntry::deflated("plugin.json", &manifest)]); + for byte in &mut cut[45..60] { + *byte = 0xFF; + } + fixture.assert_import_rejected(corrupt, &cut); +} + +#[test] +fn export_round_trips_through_import() { + let fixture = Fixture::new(); + fixture.install(&raw_zip(&[entry("plugin.json", &bytes()), entry("README.md", b"# Docs")])).unwrap(); + let exported = fixture.store.export(ID).unwrap(); + let second = PluginStore::new(fixture.temporary.join("Other")); + assert_eq!(second.install(&mut &exported[..]).unwrap().id, ID); + assert_eq!(second.list().iter().find(|plugin| plugin.manifest.id == ID).unwrap().readme.as_deref(), Some("# Docs")); + assert_eq!(second.manifest(ID), Some(base_manifest())); + // The same plugin always yields the same bytes. + assert_eq!(fixture.store.export(ID).unwrap(), exported); + assert_eq!(second.export(ID).unwrap(), exported); +} + +#[test] +fn export_includes_only_the_plugin_files() { + let fixture = Fixture::new(); + fixture.install(&bytes()).unwrap(); + std::fs::write(fixture.root.join("test-source/secret.txt"), "not part of the plugin").unwrap(); + assert_eq!(read_zip(&fixture.store.export(ID).unwrap()).iter().map(|(name, _)| name.as_str()).collect::>(), ["plugin.json"]); +} + +#[test] +fn built_ins_export_but_cannot_be_deleted() { + let fixture = Fixture::new(); + let built_in = &built_ins()[1]; + // A built-in id is already present, so the export is checked by decoding it. + let entries = read_zip(&fixture.store.export(&built_in.id).unwrap()); + assert_eq!(entries.iter().map(|(name, _)| name.as_str()).collect::>(), ["plugin.json", "README.md"]); + assert_eq!(&manifest::decode(&entries[0].1).unwrap(), built_in); + let delete = |id: &str| fixture.store.delete(id).unwrap_err(); + assert_eq!((delete(&built_in.id).kind, delete(&built_in.id).message.as_str()), (PluginErrorKind::NotFound, "Built-in plugins cannot be deleted.")); + assert_eq!(delete("dev.unknown.plugin").message, "Plugin not found."); + assert_eq!(fixture.store.export("dev.unknown.plugin").unwrap_err().message, "Plugin not found."); +} + +#[test] +fn delete_removes_the_plugin() { + let fixture = Fixture::new(); + fixture.install(&bytes()).unwrap(); + fixture.store.delete(ID).unwrap(); + assert_eq!(fixture.installed(), [] as [&str; 0]); + assert_eq!(fixture.store.manifest(ID), None); + assert_eq!(fixture.store.list().len(), built_ins().len()); + fixture.install(&bytes()).unwrap(); + assert_eq!(fixture.installed(), ["test-source"]); +} + +#[test] +fn tampered_or_linked_directories_are_not_loaded() { + let fixture = Fixture::new(); + let (root, store) = (&fixture.root, &fixture.store); + fixture.install(&bytes()).unwrap(); + let file = root.join("test-source/plugin.json"); + std::fs::write(&file, bytes_with(|root| root["baseURL"] = json!("http://api.example.com"))).unwrap(); + assert_eq!(store.manifest(ID), None, "a stored manifest is validated on every read"); + std::fs::write(&file, bytes()).unwrap(); + assert!(store.manifest(ID).is_some()); + + // A link in the store is neither a plugin directory nor a plugin file. + const LINKED: &str = "dev.scriptwidget.linked"; + let outside = fixture.temporary.join("outside"); + std::fs::create_dir(&outside).unwrap(); + let linked = outside.join("plugin.json"); + std::fs::write(&linked, bytes_with(|root| root["id"] = json!(LINKED))).unwrap(); + // The file itself is a valid plugin when it sits in a real directory of the store. + std::fs::create_dir(root.join("control")).unwrap(); + std::fs::copy(&linked, root.join("control/plugin.json")).unwrap(); + assert!(store.manifest(LINKED).is_some()); + std::fs::remove_dir_all(root.join("control")).unwrap(); + + std::os::unix::fs::symlink(&outside, root.join("linked")).unwrap(); + assert_eq!(store.manifest(LINKED), None, "a linked directory"); + std::fs::create_dir(root.join("real")).unwrap(); + std::os::unix::fs::symlink(&linked, root.join("real/plugin.json")).unwrap(); + assert_eq!(store.manifest(LINKED), None, "a linked file"); + std::fs::remove_file(root.join("real/plugin.json")).unwrap(); + std::fs::hard_link(&linked, root.join("real/plugin.json")).unwrap(); + assert_eq!(store.manifest(LINKED), None, "a file with a second name outside the store"); + assert!(!store.list().iter().any(|plugin| plugin.manifest.id == LINKED)); + assert_eq!(store.export(LINKED).unwrap_err().message, "Plugin not found."); + // A named pipe in place of the manifest is not waited on. + std::fs::remove_file(root.join("real/plugin.json")).unwrap(); + let pipe = std::ffi::CString::new(root.join("real/plugin.json").to_str().unwrap()).unwrap(); + // SAFETY: the path is a NUL-terminated string that lives until the call returns. + assert_eq!(unsafe { libc::mkfifo(pipe.as_ptr(), 0o600) }, 0); + assert_eq!(store.manifest(LINKED), None, "a pipe"); + // Another spelling of the manifest's name beside it makes the directory ambiguous on some file systems. + std::fs::remove_file(root.join("real/plugin.json")).unwrap(); + std::fs::copy(&linked, root.join("real/plugin.json")).unwrap(); + assert!(store.manifest(LINKED).is_some()); + if std::fs::write(root.join("real/PLUGIN.JSON"), bytes()).is_ok() && names(&root.join("real")).len() == 2 { + assert_eq!(store.manifest(LINKED), None, "a case alias"); + } + + // The store's own directory may not be a link either. + let elsewhere = fixture.temporary.join("elsewhere"); + std::fs::create_dir(&elsewhere).unwrap(); + std::os::unix::fs::symlink(&elsewhere, fixture.temporary.join("LinkedStore")).unwrap(); + let through_link = PluginStore::new(fixture.temporary.join("LinkedStore")); + assert_eq!(through_link.install(&mut &bytes()[..]).unwrap_err().kind, PluginErrorKind::Storage); + assert_eq!(names(&elsewhere), [] as [&str; 0]); +} + +#[test] +fn list_is_sorted_by_name_across_built_in_and_installed_plugins() { + let fixture = Fixture::new(); + fixture.install(&bytes_with(|root| root["name"] = json!("alpha"))).unwrap(); + fixture + .install(&bytes_with(|root| { + root["name"] = json!("Hub"); + root["id"] = json!("dev.scriptwidget.second"); + })) + .unwrap(); + assert_eq!(fixture.store.list().iter().map(|plugin| plugin.manifest.name.as_str()).collect::>(), ["alpha", "GitHub Public API", "Hub", "Open-Meteo"]); + // An installed plugin with a built-in's id cannot be imported, but one put there by hand replaces it in the list once. + let directory = fixture.root.join("manual"); + std::fs::create_dir(&directory).unwrap(); + std::fs::write(directory.join("plugin.json"), built_ins()[0].to_json()).unwrap(); + let listed = fixture.store.list(); + assert_eq!(listed.iter().filter(|plugin| plugin.manifest.id == built_ins()[0].id).map(|plugin| plugin.built_in).collect::>(), [false]); +} diff --git a/Linux/crates/sw-core/tests/runtime.rs b/Linux/crates/sw-core/tests/runtime.rs new file mode 100644 index 00000000..8c9ce59e --- /dev/null +++ b/Linux/crates/sw-core/tests/runtime.rs @@ -0,0 +1,185 @@ +//! The compiler, the executor and the native APIs working together on small +//! packages. Needs the assets of `node Linux/scripts/build-shared-js.mjs`. + +use std::path::PathBuf; +use std::sync::{Arc, OnceLock}; + +use sw_core::api::{StorageModule, system}; +use sw_core::compiler::PrepareOptions; +use sw_core::{ExecutionRequest, ExecutionResult, MemoryPackageFiles, NativeModule, PackageCompiler, ScriptExecutor, SharedScripts}; + +fn scripts() -> Arc { + static SCRIPTS: OnceLock> = OnceLock::new(); + SCRIPTS.get_or_init(|| Arc::new(SharedScripts::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets/scriptwidget")).expect("shared scripts"))).clone() +} + +fn compiler() -> &'static PackageCompiler { + static COMPILER: OnceLock = OnceLock::new(); + COMPILER.get_or_init(|| PackageCompiler::new(scripts()).expect("compiler thread")) +} + +fn package(files: &[(&str, &str)]) -> MemoryPackageFiles { + MemoryPackageFiles::new(files.iter().map(|(path, text)| (path.to_string(), text.to_string())), []) +} + +struct Run<'a> { + files: &'a MemoryPackageFiles, + modules: Vec>, + package_id: &'a str, + environments: &'a [(&'a str, &'a str)], + timeout_milliseconds: u64, +} + +impl<'a> Run<'a> { + fn new(files: &'a MemoryPackageFiles) -> Self { + Self { files, modules: Vec::new(), package_id: "test", environments: &[], timeout_milliseconds: 5000 } + } + + fn execute(self) -> ExecutionResult { + let prepared = compiler().prepare(self.files, &PrepareOptions::default()).expect("prepare"); + let mut request = ExecutionRequest::new(&prepared, self.files); + request.system = Some(system::system_info()); + request.modules = self.modules; + request.package_id = self.package_id.to_owned(); + request.timeout_milliseconds = self.timeout_milliseconds; + request.environments.extend(self.environments.iter().map(|(name, value)| (name.to_string(), value.to_string()))); + ScriptExecutor::new(scripts()).execute(&request) + } +} + +fn tree(result: &ExecutionResult) -> &str { + result.tree.as_deref().unwrap_or_else(|| panic!("no tree: {:?}", result.error)) +} + +/// A directory of its own for one test's storage. +fn storage_directory(name: &str) -> PathBuf { + let directory = std::env::temp_dir().join(format!("scriptwidget-test-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&directory); + directory +} + +#[test] +fn renders_jsx_to_the_canonical_tree() { + let files = package(&[("main.jsx", "$render(Hello 你好 🌤️);")]); + assert_eq!( + tree(&Run::new(&files).execute()), + r#"{"$element":"vstack","props":null,"children":[{"$element":"text","props":{"font":"title"},"children":["Hello 你好 🌤️"]}]}"# + ); +} + +#[test] +fn the_platform_is_linux() { + let files = package(&[("main.jsx", "$render({$system.platform()});")]); + assert!(tree(&Run::new(&files).execute()).contains(r#""children":["linux"]"#)); +} + +#[test] +fn imports_and_package_files_stay_inside_the_package() { + let files = package(&[ + ("main.jsx", "$import('lib/label.js'); $render({label($file.readJSON('data.json').name)}|{$file.read('../../etc/passwd')}|{String($file.exists('/etc/passwd'))});"), + ("lib/label.js", "function label(name) { return 'name=' + name; }"), + ("data.json", r#"{"name":"widget"}"#), + ]); + let result = Run::new(&files).execute(); + assert!(tree(&result).contains(r#""children":["name=widget","|","","|","false"]"#), "{}", tree(&result)); +} + +#[test] +fn storage_persists_per_package_and_is_bounded() { + let directory = storage_directory("storage"); + let module = Arc::new(StorageModule::new(&directory)); + let run = |package_id: &str, code: &str| { + let files = package(&[("main.jsx", code)]); + let result = Run { modules: vec![module.clone()], package_id, ..Run::new(&files) }.execute(); + tree(&result).to_owned() + }; + let write = "$render({String($storage.setString('count', '7'))},{String($storage.setJSON('record', {a: 1}))},{String($storage.setString('big', 'x'.repeat(300000)))});"; + assert!(run("first", write).contains(r#"["true",",","true",",","false"]"#)); + let read = "$render({$storage.getString('count')}|{JSON.stringify($storage.getJSON('record'))}|{$storage.keys().join(',')});"; + assert!(run("first", read).contains(r#"["7","|","{\"a\":1}","|","count,record"]"#)); + // Another package sees none of it. + assert!(run("second", read).contains(r#"["","|","{}","|",""]"#)); + assert_eq!(module.get_string("first", "count"), "7"); + + let cas = "$render({String($storage.compareAndSetString('count', '1', '2'))},{String($storage.compareAndSetString('count', '7', '8'))},{$storage.getString('count')});"; + assert!(run("first", cas).contains(r#"["false",",","true",",","8"]"#)); + + // Files are private to the user and named by a hash, not by the package. + use std::os::unix::fs::PermissionsExt; + let entries: Vec<_> = std::fs::read_dir(&directory).unwrap().map(|entry| entry.unwrap()).collect(); + assert_eq!(entries.len(), 1); + assert_eq!(entries[0].metadata().unwrap().permissions().mode() & 0o777, 0o600); + assert!(!entries[0].file_name().to_string_lossy().contains("first")); + let _ = std::fs::remove_dir_all(&directory); +} + +#[test] +fn timeline_projections_cannot_write_storage() { + let directory = storage_directory("projection"); + let module = Arc::new(StorageModule::new(&directory)); + let files = package(&[("main.jsx", "$render({String($storage.setString('key', 'value'))});")]); + let result = Run { modules: vec![module.clone()], environments: &[("timeline-is-projection", "true")], ..Run::new(&files) }.execute(); + assert!(tree(&result).contains(r#"["false"]"#), "{}", tree(&result)); + assert!(module.snapshot("test").is_empty()); + let _ = std::fs::remove_dir_all(&directory); +} + +#[test] +fn an_endless_loop_ends_with_a_resource_limit_error() { + let files = package(&[("main.jsx", "for (;;) {}")]); + let result = Run { timeout_milliseconds: 300, ..Run::new(&files) }.execute(); + assert_eq!(result.tree, None); + assert_eq!(result.error.map(|error| error.kind).as_deref(), Some("resourceLimit")); + assert!(result.elapsed.as_millis() < 3000, "{:?}", result.elapsed); +} + +#[test] +fn runaway_allocation_ends_with_a_resource_limit_error() { + let files = package(&[("main.jsx", "const all = []; for (;;) all.push(new Array(100000).fill(1));")]); + let result = Run::new(&files).execute(); + assert_eq!(result.tree, None); + let error = result.error.expect("error"); + assert_eq!(error.kind, "resourceLimit", "{}", error.message); +} + +#[test] +fn package_code_cannot_reach_the_native_entry_points() { + let files = package(&[( + "main.jsx", + "$render({[typeof __swNative, typeof __swEvaluate, typeof __swBoot, typeof __swKernel, Object.getOwnPropertyNames(globalThis).filter((name) => name.startsWith('__sw')).map((name) => name.replace(/[0-9a-f]{32}$/, '')).join('+')].join(',')});", + )]); + // The driver the host calls is the only name left. It is not enumerable, and it can only + // pump, resolve and time out the execution that is already running. + let result = Run::new(&files).execute(); + assert!(tree(&result).contains(r#"["undefined,undefined,undefined,undefined,__sw_"]"#), "{}", tree(&result)); +} + +#[test] +fn unsupported_apis_say_so() { + let files = package(&[("main.jsx", "$render({String($health.isAvailable())}); $health.stepCountToday();")]); + let result = Run::new(&files).execute(); + match (&result.tree, &result.error) { + (_, Some(error)) => assert!(error.message.contains("is not supported on Linux"), "{}", error.message), + (Some(tree), None) => assert!(tree.contains(r#"["false"]"#), "{tree}"), + (None, None) => panic!("neither tree nor error"), + } +} + +#[test] +fn a_malformed_manifest_fails_closed() { + let files = package(&[("main.jsx", "$render(hi);"), ("widget.json", "{ not json")]); + let error = compiler().prepare(&files, &PrepareOptions::default()).expect_err("a damaged manifest must not be read as a legacy package"); + assert!(!error.kind.is_empty()); + let report = compiler().validate_manifest("{ not json", &["main.jsx".into(), "widget.json".into()]).expect("validator"); + assert_eq!(report.get("ok").and_then(|value| value.as_bool()), Some(false)); +} + +#[test] +fn intl_formats_with_the_system_icu() { + let files = package(&[( + "main.jsx", + "const d = new Date(Date.UTC(2026, 0, 15, 20, 5));\n$render({[new Intl.NumberFormat('en-US').format(1234567.891), new Intl.NumberFormat('de-DE', {style: 'currency', currency: 'EUR'}).format(12.5), d.toLocaleDateString('en-US', {timeZone: 'UTC', dateStyle: 'medium'}), new Intl.DateTimeFormat('en-US', {timeZone: 'Asia/Tokyo', hour: '2-digit', minute: '2-digit'}).format(d), new Intl.PluralRules('en-US').select(1), new Intl.RelativeTimeFormat('en', {numeric: 'auto'}).format(-1, 'day'), new Intl.ListFormat('en', {type: 'conjunction'}).format(['a', 'b', 'c']), 'a'.localeCompare('b', 'en')].join(';')});", + )]); + let result = Run::new(&files).execute(); + assert!(tree(&result).contains("1,234,567.891;12,50\u{a0}€;Jan 15, 2026;05:05 AM;one;yesterday;a, b, and c;-1"), "{}", tree(&result)); +} diff --git a/Linux/crates/sw-core/tests/timeline_planner.rs b/Linux/crates/sw-core/tests/timeline_planner.rs new file mode 100644 index 00000000..f273e00b --- /dev/null +++ b/Linux/crates/sw-core/tests/timeline_planner.rs @@ -0,0 +1,133 @@ +//! The schedule of a package's `timeline.dailyTimes`, evaluated by the +//! planner shared with every platform. The cases are those of +//! `Android/core/src/androidTest/.../TimelineTest.kt`, plus daylight-saving +//! changes and the plan cache. + +use std::path::PathBuf; +use std::sync::{Arc, Mutex, MutexGuard}; + +use serde_json::Value; +use sw_core::api::support::iso_dates; +use sw_core::json::Object; +use sw_core::{SharedScripts, TimelinePlanner, TimelineState, intl}; + +/// The zone override is one for the process: the tests of this file take turns. +fn turn() -> MutexGuard<'static, ()> { + static TURN: Mutex<()> = Mutex::new(()); + TURN.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) +} + +fn planner() -> TimelinePlanner { + TimelinePlanner::new(Arc::new(SharedScripts::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets/scriptwidget")).expect("shared scripts"))) +} + +fn manifest(times: &str, zone: Option<&str>) -> Object { + let zone = zone.map(|zone| format!(r#","timeZone":"{zone}""#)).unwrap_or_default(); + match serde_json::from_str(&format!(r#"{{"timeline":{{"dailyTimes":{times}{zone}}}}}"#)).expect("manifest") { + Value::Object(manifest) => manifest, + _ => unreachable!(), + } +} + +fn at(text: &str) -> i64 { + iso_dates::parse(text).expect("instant") +} + +fn environment<'a>(state: &'a TimelineState, name: &str) -> Option<&'a str> { + state.environments.iter().find(|(known, _)| known == name).map(|(_, value)| value.as_str()) +} + +const LOS_ANGELES: Option<&str> = Some("America/Los_Angeles"); + +#[test] +fn reports_the_current_slot_and_the_next_transition() { + let _turn = turn(); + // 2026-03-10 15:30 UTC is 08:30 in Los Angeles (daylight time): the 06:00 slot, next change at 12:00 local. + let state = planner().state_at(Some(&manifest(r#"["06:00","12:00","18:00","22:00"]"#, LOS_ANGELES)), at("2026-03-10T15:30:00Z")).unwrap(); + assert_eq!(environment(&state, "timeline-slot"), Some("0")); + assert_eq!(environment(&state, "timeline-is-projection"), Some("false")); + assert_eq!(environment(&state, "timeline-time-zone"), Some("America/Los_Angeles")); + assert_eq!(environment(&state, "timeline-date"), Some("2026-03-10T15:30:00.000Z")); + assert_eq!(state.next_transition_milliseconds, Some(at("2026-03-10T19:00:00Z"))); +} + +#[test] +fn before_the_first_time_of_the_day_the_last_slot_still_applies() { + let _turn = turn(); + // 03:00 local. + let state = planner().state_at(Some(&manifest(r#"["06:00","22:00"]"#, LOS_ANGELES)), at("2026-03-10T10:00:00Z")).unwrap(); + assert_eq!(environment(&state, "timeline-slot"), Some("1")); + assert_eq!(state.next_transition_milliseconds, Some(at("2026-03-10T13:00:00Z"))); +} + +#[test] +fn uses_the_zone_of_the_machine_when_none_is_declared() { + let _turn = turn(); + // SAFETY: the tests of this file run one at a time (see `turn`), and no other thread of this process uses local time. + unsafe { intl::set_time_zone_override(Some("Asia/Shanghai")) }; + // 08:30 in Shanghai. + let state = planner().state_at(Some(&manifest(r#"["09:00"]"#, None)), at("2026-03-10T00:30:00Z")); + // SAFETY: as above. + unsafe { intl::set_time_zone_override(None) }; + let state = state.unwrap(); + assert_eq!(environment(&state, "timeline-time-zone"), Some("Asia/Shanghai")); + assert_eq!(state.next_transition_milliseconds, Some(at("2026-03-10T01:00:00Z"))); +} + +#[test] +fn packages_without_a_valid_schedule_have_no_timeline() { + let _turn = turn(); + let planner = planner(); + assert_eq!(planner.state(None), None); + assert_eq!(planner.state(Some(&Object::new())), None); + assert_eq!(planner.state(Some(&manifest(r#"["25:00"]"#, LOS_ANGELES))), None); + assert_eq!(planner.state(Some(&manifest(r#"["06:00"]"#, Some("Not/AZone")))), None); + assert_eq!(planner.state(Some(&manifest(r#"["12:00","06:00"]"#, LOS_ANGELES))), None); + assert_eq!(planner.state(Some(&manifest(r#"["01:00","02:00","03:00","04:00","05:00"]"#, LOS_ANGELES))), None); + // A schedule is data: text in it is never run. + assert_eq!(planner.state(Some(&manifest(r#"["06:00\"); throw new Error(\"x"]"#, LOS_ANGELES))), None); + let mut not_an_object = Object::new(); + not_an_object.insert("timeline".into(), Value::String("daily".into())); + assert_eq!(planner.state(Some(¬_an_object)), None); +} + +#[test] +fn daylight_saving_changes_move_the_transitions_with_the_wall_clock() { + let _turn = turn(); + let planner = planner(); + let schedule = manifest(r#"["02:30","12:00"]"#, LOS_ANGELES); + // 8 March 2026: clocks go from 02:00 to 03:00, so 02:30 does not exist; the slot starts at the next valid minute. + let spring = planner.state_at(Some(&schedule), at("2026-03-08T09:00:00Z")).unwrap(); // 01:00 PST + assert_eq!(environment(&spring, "timeline-slot"), Some("1")); + assert_eq!(spring.next_transition_milliseconds, Some(at("2026-03-08T10:00:00Z"))); // 03:00 PDT + // 1 November 2026: 01:00 to 02:00 happens twice; 12:00 local is 20:00 UTC afterwards, not 19:00. + let autumn = planner.state_at(Some(&schedule), at("2026-11-01T12:00:00Z")).unwrap(); // 04:00 PST + assert_eq!(environment(&autumn, "timeline-slot"), Some("0")); + assert_eq!(autumn.next_transition_milliseconds, Some(at("2026-11-01T20:00:00Z"))); + // A zone whose offset is not a whole hour (+05:45). + let kathmandu = planner.state_at(Some(&manifest(r#"["09:00"]"#, Some("Asia/Kathmandu"))), at("2026-03-10T03:00:00Z")).unwrap(); // 08:45 local + assert_eq!(kathmandu.next_transition_milliseconds, Some(at("2026-03-10T03:15:00Z"))); +} + +#[test] +fn a_plan_is_reused_until_its_next_transition_or_for_an_hour() { + let _turn = turn(); + let planner = planner(); + let schedule = manifest(r#"["06:00","12:00"]"#, LOS_ANGELES); + let first = planner.state_at(Some(&schedule), at("2026-03-10T18:30:00Z")).unwrap(); // 11:30 local + assert_eq!(first.next_transition_milliseconds, Some(at("2026-03-10T19:00:00Z"))); + // Twenty minutes later the same plan answers: its date is the moment it was made. + let reused = planner.state_at(Some(&schedule), at("2026-03-10T18:50:00Z")).unwrap(); + assert_eq!(reused, first); + // At the transition the plan is made again and the slot changes. + let after = planner.state_at(Some(&schedule), at("2026-03-10T19:00:00Z")).unwrap(); + assert_eq!(environment(&after, "timeline-slot"), Some("1")); + assert_eq!(environment(&after, "timeline-date"), Some("2026-03-10T19:00:00.000Z")); + // Far from a transition a plan is kept for an hour at most. + let evening = planner.state_at(Some(&schedule), at("2026-03-10T20:00:00Z")).unwrap(); + assert_eq!(planner.state_at(Some(&schedule), at("2026-03-10T20:59:59Z")).unwrap(), evening); + assert_ne!(planner.state_at(Some(&schedule), at("2026-03-10T21:00:00Z")).unwrap(), evening); + // Another schedule has its own plan. + let other = planner.state_at(Some(&manifest(r#"["06:00","13:00"]"#, LOS_ANGELES)), at("2026-03-10T18:50:00Z")).unwrap(); + assert_eq!(other.next_transition_milliseconds, Some(at("2026-03-10T20:00:00Z"))); +} diff --git a/Linux/crates/sw-host/Cargo.toml b/Linux/crates/sw-host/Cargo.toml new file mode 100644 index 00000000..8d9e6380 --- /dev/null +++ b/Linux/crates/sw-host/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "sw-host" +description = "The ScriptWidget background service for Linux: schedules, renders and places desktop widgets, and serves the display protocol" +version.workspace = true +edition.workspace = true +license.workspace = true +publish.workspace = true + +[dependencies] +sw-core.workspace = true +sw-render.workspace = true +# The element tree nests deeper than serde_json reads by default. +serde_json = { workspace = true, features = ["unbounded_depth"] } +serde.workspace = true +sha2.workspace = true +getrandom.workspace = true +libc.workspace = true +url.workspace = true diff --git a/Linux/crates/sw-host/src/client.rs b/Linux/crates/sw-host/src/client.rs new file mode 100644 index 00000000..4c488d25 --- /dev/null +++ b/Linux/crates/sw-host/src/client.rs @@ -0,0 +1,98 @@ +//! The client side of the socket, used by the `scriptwidget` command line +//! and by the management window. + +use std::io::{BufRead, BufReader, Write}; +use std::os::unix::net::UnixStream; +use std::path::Path; +use std::time::Duration; + +use serde_json::Value; + +use crate::protocol::{Refusal, Request}; + +/// How long a command waits for the service's answer. +const REPLY_TIMEOUT: Duration = Duration::from_secs(15); +/// A reply or an event line longer than this is not from the service. +const MAXIMUM_REPLY_BYTES: u64 = 4 * 1024 * 1024; + +#[derive(Debug)] +pub enum ClientError { + /// Nothing is listening: the service is not running. + NotRunning, + /// The connection ended before the answer came. + Lost, + /// The service refused the request. + Refused { code: String, message: String }, +} + +fn connect(socket: &Path) -> Result { + UnixStream::connect(socket).map_err(|_| ClientError::NotRunning) +} + +fn read_line(reader: &mut impl BufRead) -> Option { + let mut line = String::new(); + match std::io::Read::take(reader, MAXIMUM_REPLY_BYTES).read_line(&mut line) { + Ok(count) if count > 0 && line.ends_with('\n') => Some(line.trim_end().to_owned()), + _ => None, + } +} + +/// Sends one request and returns the service's reply object when it says `"ok": true`. +pub fn request(socket: &Path, request: &Request) -> Result { + let mut stream = connect(socket)?; + let _ = stream.set_read_timeout(Some(REPLY_TIMEOUT)); + let _ = stream.set_write_timeout(Some(REPLY_TIMEOUT)); + let mut line = request.line(); + line.push('\n'); + stream.write_all(line.as_bytes()).map_err(|_| ClientError::Lost)?; + let reply: Value = read_line(&mut BufReader::new(stream)).and_then(|line| serde_json::from_str(&line).ok()).ok_or(ClientError::Lost)?; + if reply["ok"] == true { + return Ok(reply); + } + let error = &reply["error"]; + Err(ClientError::Refused { code: error["code"].as_str().unwrap_or("request.invalid").to_owned(), message: error["message"].as_str().unwrap_or("The service refused the request.").to_owned() }) +} + +/// How an event stream ended. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum WatchEnd { + /// The service was not running. + NotRunning, + /// The service said `bye`. + Bye, + /// The connection ended without a `bye`. + Lost, + /// `on_event` returned false. + Stopped, +} + +/// Connects as a watcher and calls `on_event` with every event line, until the stream ends or `on_event` returns false. +pub fn watch(socket: &Path, mut on_event: impl FnMut(&str) -> bool) -> WatchEnd { + let Ok(mut stream) = connect(socket) else { return WatchEnd::NotRunning }; + let mut line = Request::Watch.line(); + line.push('\n'); + if stream.write_all(line.as_bytes()).is_err() { + return WatchEnd::Lost; + } + let mut reader = BufReader::new(stream); + while let Some(line) = read_line(&mut reader) { + let Ok(event) = serde_json::from_str::(&line) else { return WatchEnd::Lost }; + // A refusal instead of events (too many connections) ends the stream like a lost connection. + if event.get("event").is_none() { + return WatchEnd::Lost; + } + if !on_event(&line) { + return WatchEnd::Stopped; + } + if event["event"] == "bye" { + return WatchEnd::Bye; + } + } + WatchEnd::Lost +} + +impl From for ClientError { + fn from(refusal: Refusal) -> Self { + Self::Refused { code: refusal.code.to_owned(), message: refusal.message } + } +} diff --git a/Linux/crates/sw-host/src/clock.rs b/Linux/crates/sw-host/src/clock.rs new file mode 100644 index 00000000..1c3ae075 --- /dev/null +++ b/Linux/crates/sw-host/src/clock.rs @@ -0,0 +1,85 @@ +//! The two clocks the service schedules with, behind a trait so that tests +//! can move time by hand. + +use std::sync::atomic::{AtomicI64, AtomicU64, Ordering}; + +pub trait Clock: Send + Sync { + /// Milliseconds that only go forward, for deadlines. On Linux this + /// counts the time the machine was asleep, so a deadline that passed + /// during sleep is overdue at once when the machine wakes. + fn now(&self) -> u64; + /// Milliseconds since 1970 on the wall clock, which the user and the network can set. + fn wall(&self) -> i64; +} + +pub struct SystemClock { + #[cfg(not(target_os = "linux"))] + started: std::time::Instant, +} + +impl SystemClock { + pub fn new() -> Self { + Self { + #[cfg(not(target_os = "linux"))] + started: std::time::Instant::now(), + } + } +} + +impl Default for SystemClock { + fn default() -> Self { + Self::new() + } +} + +impl Clock for SystemClock { + #[cfg(target_os = "linux")] + fn now(&self) -> u64 { + let mut time = libc::timespec { tv_sec: 0, tv_nsec: 0 }; + // SAFETY: `time` is a valid timespec to write to; CLOCK_BOOTTIME exists on every kernel Rust supports. + unsafe { libc::clock_gettime(libc::CLOCK_BOOTTIME, &mut time) }; + (time.tv_sec as u64) * 1000 + (time.tv_nsec as u64) / 1_000_000 + } + + #[cfg(not(target_os = "linux"))] + fn now(&self) -> u64 { + self.started.elapsed().as_millis() as u64 + } + + fn wall(&self) -> i64 { + std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |elapsed| elapsed.as_millis() as i64) + } +} + +/// A clock that stands still until it is moved, for tests. +pub struct ManualClock { + now: AtomicU64, + wall: AtomicI64, +} + +impl ManualClock { + pub fn new(wall_milliseconds: i64) -> Self { + Self { now: AtomicU64::new(1_000_000), wall: AtomicI64::new(wall_milliseconds) } + } + + /// Lets time pass: both clocks move. + pub fn advance(&self, milliseconds: u64) { + self.now.fetch_add(milliseconds, Ordering::SeqCst); + self.wall.fetch_add(milliseconds as i64, Ordering::SeqCst); + } + + /// Sets the wall clock, as a user or a time server would: only it moves. + pub fn set_wall(&self, milliseconds: i64) { + self.wall.store(milliseconds, Ordering::SeqCst); + } +} + +impl Clock for ManualClock { + fn now(&self) -> u64 { + self.now.load(Ordering::SeqCst) + } + + fn wall(&self) -> i64 { + self.wall.load(Ordering::SeqCst) + } +} diff --git a/Linux/crates/sw-host/src/frames.rs b/Linux/crates/sw-host/src/frames.rs new file mode 100644 index 00000000..120e95a8 --- /dev/null +++ b/Linux/crates/sw-host/src/frames.rs @@ -0,0 +1,113 @@ +//! The pictures the display shows: PNG files in +//! `$XDG_RUNTIME_DIR/scriptwidget/frames/`. +//! +//! A picture is written under a hidden temporary name and renamed, so the +//! display never reads half a file. Every picture gets a name that was never +//! used before, in this run of the service or an earlier one: a display that +//! is told the same path again does not load it again. The name is +//! `--.png`, or `---.png` +//! for the frames of an animation, where `` is random for each start of +//! the service. + +use std::io::Write; +use std::os::unix::fs::OpenOptionsExt; +use std::path::{Path, PathBuf}; + +pub struct FrameStore { + directory: PathBuf, + run: String, +} + +impl FrameStore { + /// Opens the directory, which must exist (see `RuntimePaths::prepare`), and removes what an earlier run left in it. + pub fn new(directory: PathBuf) -> std::io::Result { + let mut token = [0u8; 4]; + getrandom::fill(&mut token).map_err(|error| std::io::Error::other(error.to_string()))?; + let store = Self { directory, run: token.iter().map(|byte| format!("{byte:02x}")).collect() }; + store.clear()?; + Ok(store) + } + + pub fn directory(&self) -> &Path { + &self.directory + } + + /// Removes every picture. Only regular files directly in the directory are touched; nothing is followed. + pub fn clear(&self) -> std::io::Result<()> { + for entry in std::fs::read_dir(&self.directory)? { + let entry = entry?; + if entry.file_type().is_ok_and(|kind| kind.is_file() || kind.is_symlink()) { + let _ = std::fs::remove_file(entry.path()); + } + } + Ok(()) + } + + /// Where a job writes frame `index` before the picture is published. `job` is unique in this run. + pub fn temporary(&self, job: u64, index: usize) -> PathBuf { + self.directory.join(format!(".job-{}-{job}-{index}.tmp", self.run)) + } + + /// The published name of a picture of `id`. `frame` is `None` for a picture that does not move. + pub fn published(&self, id: &str, version: u64, frame: Option) -> PathBuf { + self.directory.join(match frame { + Some(frame) => format!("{id}-{}-{version}-{frame}.png", self.run), + None => format!("{id}-{}-{version}.png", self.run), + }) + } + + /// Writes a new private file; fails when anything already has the name. + pub fn write(&self, path: &Path, bytes: &[u8]) -> std::io::Result<()> { + let mut file = std::fs::OpenOptions::new().write(true).create_new(true).custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC).mode(0o600).open(path)?; + file.write_all(bytes).inspect_err(|_| { + let _ = std::fs::remove_file(path); + }) + } + + pub fn publish(&self, temporary: &Path, published: &Path) -> std::io::Result<()> { + std::fs::rename(temporary, published) + } + + pub fn remove(&self, paths: &[PathBuf]) { + for path in paths { + // Only files of this directory, whatever the caller kept. + if path.parent() == Some(self.directory.as_path()) { + let _ = std::fs::remove_file(path); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn names_are_new_in_every_run_and_old_pictures_are_removed() { + let directory = std::env::temp_dir().join(format!("sw-host-frames-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&directory); + std::fs::create_dir_all(&directory).unwrap(); + let first = FrameStore::new(directory.clone()).unwrap(); + let temporary = first.temporary(7, 0); + assert!(temporary.file_name().unwrap().to_str().unwrap().starts_with('.')); + first.write(&temporary, b"png").unwrap(); + assert!(first.write(&temporary, b"again").is_err(), "a name is written once"); + use std::os::unix::fs::MetadataExt; + assert_eq!(std::fs::metadata(&temporary).unwrap().mode() & 0o777, 0o600); + let published = first.published("0123456789abcdef", 1, None); + first.publish(&temporary, &published).unwrap(); + assert!(published.is_file() && !temporary.exists()); + assert_ne!(first.published("0123456789abcdef", 1, Some(0)), published); + + let second = FrameStore::new(directory.clone()).unwrap(); + assert!(!published.exists(), "a new run starts with an empty directory"); + assert_ne!(second.published("0123456789abcdef", 1, None), published, "and never reuses a name"); + // Removing only ever touches this directory. + let outside = directory.parent().unwrap().join(format!("sw-host-frames-outside-{}", std::process::id())); + std::fs::write(&outside, b"keep").unwrap(); + second.remove(&[outside.clone()]); + assert!(outside.exists()); + std::fs::remove_file(outside).unwrap(); + std::fs::remove_dir_all(directory).unwrap(); + } +} diff --git a/Linux/crates/sw-host/src/launcher.rs b/Linux/crates/sw-host/src/launcher.rs new file mode 100644 index 00000000..6a9d8441 --- /dev/null +++ b/Linux/crates/sw-host/src/launcher.rs @@ -0,0 +1,144 @@ +//! What the service starts on the user's behalf: a browser or a mail program +//! for a link, and the management window. Both go through one trait so that +//! tests can record what would have been started instead of starting it. +//! +//! A link comes from a package and is not trusted. Only `http`, `https` and +//! `mailto` are opened: a file path or a `file:` URL would run programs, and +//! many registered handlers were never meant to take input from untrusted +//! content (the same rule as `Windows/src/ScriptWidget.App/SystemIntegration.cs`). +//! Programs are started with an argument vector, never through a shell. + +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +/// Longer text is not a link. +pub const MAXIMUM_LINK_LENGTH: usize = 4096; + +/// What a link of a widget may do. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum LinkTarget { + /// Open this URL: the canonical form of the parsed text, never the text itself. + Open(String), + /// A `scriptwidget:` link: show the management window. Nothing of the link is passed on. + Application, +} + +/// Decides what a link may do, or `None` when it is refused. +pub fn checked_link(text: &str) -> Option { + if text.trim().is_empty() || text.len() > MAXIMUM_LINK_LENGTH { + return None; + } + let url = url::Url::parse(text.trim()).ok()?; + match url.scheme() { + "scriptwidget" => Some(LinkTarget::Application), + "http" | "https" if url.host_str().is_some_and(|host| !host.is_empty()) => Some(LinkTarget::Open(url.as_str().to_owned())), + "mailto" => Some(LinkTarget::Open(url.as_str().to_owned())), + _ => None, + } +} + +pub trait Launcher: Send + Sync { + /// Opens a URL that [`checked_link`] accepted. Returns false when nothing could be started. + fn open_link(&self, url: &str) -> bool; + /// Shows the management window, on the package with this name when one is given. Returns false when it is not installed. + fn open_studio(&self, package: Option<&str>) -> bool; +} + +/// Starts `xdg-open` and `scriptwidget-studio`. +pub struct SystemLauncher; + +/// Starts a program detached from the service's input and output, and collects it when it ends. +fn start(mut command: Command) -> bool { + command.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()); + match command.spawn() { + Ok(mut child) => { + // No zombie stays behind, and nothing waits for a program the user may keep open for hours. + let _ = std::thread::Builder::new().name("scriptwidget-child".into()).spawn(move || { + let _ = child.wait(); + }); + true + } + Err(_) => false, + } +} + +/// The management window program: beside this program, or on the search path. +pub fn studio_program() -> Option { + const NAME: &str = "scriptwidget-studio"; + let executable = |path: &Path| { + use std::os::unix::fs::PermissionsExt; + std::fs::metadata(path).is_ok_and(|metadata| metadata.is_file() && metadata.permissions().mode() & 0o111 != 0) + }; + let beside = std::env::current_exe().ok().and_then(|program| Some(program.parent()?.join(NAME))); + let on_path = std::env::var_os("PATH").into_iter().flat_map(|path| std::env::split_paths(&path).collect::>()).filter(|directory| directory.is_absolute()).map(|directory| directory.join(NAME)); + beside.into_iter().chain(on_path).find(|candidate| executable(candidate)) +} + +impl Launcher for SystemLauncher { + fn open_link(&self, url: &str) -> bool { + // Checked again here, so that nothing reaches xdg-open that the rule above did not produce. + match checked_link(url) { + Some(LinkTarget::Open(canonical)) if canonical == url => { + let mut command = Command::new("xdg-open"); + command.arg(canonical); + start(command) + } + _ => false, + } + } + + fn open_studio(&self, package: Option<&str>) -> bool { + let Some(program) = studio_program() else { return false }; + let mut command = Command::new(program); + if let Some(package) = package { + command.arg("--edit").arg(package); + } + start(command) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_web_and_mail_links_are_opened() { + assert_eq!(checked_link("https://example.com/a b?q=1"), Some(LinkTarget::Open("https://example.com/a%20b?q=1".into()))); + assert_eq!(checked_link(" HTTP://Example.COM "), Some(LinkTarget::Open("http://example.com/".into()))); + assert_eq!(checked_link("mailto:someone@example.com?subject=Hi"), Some(LinkTarget::Open("mailto:someone@example.com?subject=Hi".into()))); + assert_eq!(checked_link("scriptwidget://open?package=x"), Some(LinkTarget::Application)); + for refused in [ + "", + " ", + "example.com", + "/etc/passwd", + "file:///etc/passwd", + "FILE:///etc/passwd", + "javascript:alert(1)", + "data:text/html,", + "ssh://host/", + "smb://host/share", + "vscode://file/etc/passwd", + "steam://run/1", + "tel:+1234", + "http://", + "--version", + "-a https://example.com", + "x-scheme-handler/http", + ] { + assert_eq!(checked_link(refused), None, "{refused:?}"); + } + assert_eq!(checked_link(&format!("https://example.com/{}", "a".repeat(MAXIMUM_LINK_LENGTH))), None); + } + + #[test] + fn an_opened_url_never_starts_like_an_option() { + // Whatever the text, what is handed to xdg-open begins with one of three schemes. + for text in ["https://example.com", "http://-x.example.com/", "mailto:-x@example.com", "https://example.com/ --flag"] { + if let Some(LinkTarget::Open(url)) = checked_link(text) { + assert!(url.starts_with("http://") || url.starts_with("https://") || url.starts_with("mailto:"), "{url}"); + assert!(!url.contains(' '), "{url}"); + } + } + } +} diff --git a/Linux/crates/sw-host/src/lib.rs b/Linux/crates/sw-host/src/lib.rs new file mode 100644 index 00000000..43541423 --- /dev/null +++ b/Linux/crates/sw-host/src/lib.rs @@ -0,0 +1,40 @@ +//! The ScriptWidget background service for Linux (plan, sections 4.4 and +//! 4.8; the protocol is written down in `docs/linux/protocol.md`). +//! +//! The service opens no window. It runs each placed widget's script on a +//! schedule, draws it to PNG files under `$XDG_RUNTIME_DIR/scriptwidget/frames/`, +//! and tells every connected display which widget has a new picture, where +//! it goes, how large it is and which areas can be tapped. A display sends +//! back numbers: which region was tapped, where a widget was dragged to. +//! +//! It is a library so that the tests can run a whole service inside the test +//! process, and so that there is one installed program: `scriptwidget host +//! run` in `sw-cli` is the service, and the same crate gives the command line +//! its client side. + +pub mod client; +pub mod clock; +pub mod frames; +pub mod launcher; +pub mod paths; +pub mod placements; +pub mod protocol; +pub mod schedule; +pub mod server; +pub mod service; +pub mod session; +pub mod surfaces; +pub mod worker; + +pub use clock::{Clock, ManualClock, SystemClock}; +pub use launcher::{Launcher, SystemLauncher}; +pub use paths::RuntimePaths; +pub use protocol::{Refusal, Request, Switch}; +pub use service::{Controller, Service, ServiceOptions, StartError}; +pub use session::SessionEvent; + +/// One line for the journal. Never a credential, a response body or the content of a user's file: +/// identifiers, error kinds and paths of the service's own files only. +pub fn log(message: &str) { + eprintln!("scriptwidget-host: {message}"); +} diff --git a/Linux/crates/sw-host/src/paths.rs b/Linux/crates/sw-host/src/paths.rs new file mode 100644 index 00000000..4bd8fbbb --- /dev/null +++ b/Linux/crates/sw-host/src/paths.rs @@ -0,0 +1,114 @@ +//! Where the service meets its clients: `$XDG_RUNTIME_DIR/scriptwidget/`, +//! with the socket, the lock that keeps a second service from starting, and +//! the pictures. +//! +//! The runtime directory is the one place the session gives a user alone +//! (the XDG specification: owned by the user, mode 0700, gone at log-out). +//! There is no fallback to `/tmp`: without it the service does not start. + +use std::ffi::OsString; +use std::io; +use std::os::unix::fs::{DirBuilderExt, MetadataExt, PermissionsExt}; +use std::path::{Path, PathBuf}; + +const APPLICATION: &str = "scriptwidget"; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct RuntimePaths { + directory: PathBuf, +} + +impl RuntimePaths { + /// The paths under the runtime directory of this session, or an error when the session has none. + pub fn from_environment() -> io::Result { + Self::locate(|name| std::env::var_os(name)) + } + + pub fn locate(variable: impl Fn(&str) -> Option) -> io::Result { + match variable("XDG_RUNTIME_DIR").map(PathBuf::from).filter(|path| path.is_absolute()) { + Some(runtime) => Ok(Self::under(&runtime)), + None => Err(io::Error::new(io::ErrorKind::NotFound, "XDG_RUNTIME_DIR does not name an absolute directory; ScriptWidget needs the runtime directory of a login session")), + } + } + + /// The paths under a given runtime directory. + pub fn under(runtime_directory: &Path) -> Self { + Self { directory: runtime_directory.join(APPLICATION) } + } + + pub fn directory(&self) -> &Path { + &self.directory + } + + pub fn socket(&self) -> PathBuf { + self.directory.join("host.sock") + } + + pub fn lock(&self) -> PathBuf { + self.directory.join("host.lock") + } + + pub fn frames(&self) -> PathBuf { + self.directory.join("frames") + } + + /// Creates the directory and the one for the pictures, both for the user + /// alone, and refuses anything that is not that: a link, a directory of + /// another user, or one that others can enter. + pub fn prepare(&self) -> io::Result<()> { + // SAFETY: geteuid has no preconditions and cannot fail. + let user = unsafe { libc::geteuid() }; + for directory in [self.directory.clone(), self.frames()] { + match std::fs::DirBuilder::new().mode(0o700).create(&directory) { + Ok(()) => {} + Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {} + Err(error) => return Err(io::Error::new(error.kind(), format!("{}: {error}", directory.display()))), + } + let metadata = std::fs::symlink_metadata(&directory)?; + if !metadata.is_dir() || metadata.uid() != user { + return Err(io::Error::new(io::ErrorKind::PermissionDenied, format!("{} is not a directory of this user", directory.display()))); + } + if metadata.mode() & 0o077 != 0 { + // Ours, but opened up by something: close it again before anything is put inside. + std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o700))?; + } + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn needs_an_absolute_runtime_directory() { + let located = |value: Option<&str>| RuntimePaths::locate(|_| value.map(OsString::from)); + assert_eq!(located(Some("/run/user/1000")).unwrap().socket(), Path::new("/run/user/1000/scriptwidget/host.sock")); + assert_eq!(located(Some("/run/user/1000")).unwrap().frames(), Path::new("/run/user/1000/scriptwidget/frames")); + assert!(located(None).is_err()); + assert!(located(Some("")).is_err()); + assert!(located(Some("run/user")).is_err()); + } + + #[test] + fn prepares_private_directories_and_refuses_a_link() { + let root = std::env::temp_dir().join(format!("sw-host-paths-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + let paths = RuntimePaths::under(&root); + paths.prepare().unwrap(); + assert_eq!(std::fs::metadata(paths.directory()).unwrap().mode() & 0o777, 0o700); + assert_eq!(std::fs::metadata(paths.frames()).unwrap().mode() & 0o777, 0o700); + // Opened up by something else: closed again. + std::fs::set_permissions(paths.frames(), std::fs::Permissions::from_mode(0o755)).unwrap(); + paths.prepare().unwrap(); + assert_eq!(std::fs::metadata(paths.frames()).unwrap().mode() & 0o777, 0o700); + // A link where the pictures should be is never followed. + std::fs::remove_dir(paths.frames()).unwrap(); + std::fs::create_dir(root.join("elsewhere")).unwrap(); + std::os::unix::fs::symlink(root.join("elsewhere"), paths.frames()).unwrap(); + assert!(paths.prepare().is_err()); + std::fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/Linux/crates/sw-host/src/placements.rs b/Linux/crates/sw-host/src/placements.rs new file mode 100644 index 00000000..bc30d10c --- /dev/null +++ b/Linux/crates/sw-host/src/placements.rs @@ -0,0 +1,345 @@ +//! What the service remembers between runs: the widgets on the desktop +//! (`widgets.json` in the data directory), what the display last said about +//! screens and theme (`display.json` in the cache directory), and the one +//! setting it reads (`refreshMinutes` in `settings.json`). Ported from +//! `Windows/src/ScriptWidget.Core/Hosting/WidgetPlacements.cs`. +//! +//! The files are the user's, but they are still read as untrusted text: +//! anything that is not a valid entry is dropped, never repaired by guessing. + +use std::collections::BTreeMap; +use std::io::Write; +use std::os::unix::fs::OpenOptionsExt; +use std::path::{Path, PathBuf}; +use std::time::SystemTime; + +use serde_json::{Map, Value, json}; + +use crate::protocol::{self, MAXIMUM_OFFSET, MAXIMUM_SCALE, MAXIMUM_SCREEN_LENGTH, MINIMUM_SCALE}; + +pub const MAXIMUM_WIDGETS: usize = 64; +pub const MAXIMUM_SCREENS: usize = 16; +pub const DEFAULT_REFRESH_MINUTES: u64 = 15; +pub const MINIMUM_REFRESH_MINUTES: u64 = 5; +pub const MAXIMUM_REFRESH_MINUTES: u64 = 24 * 60; +/// Larger files are not ours. +const MAXIMUM_FILE_BYTES: u64 = 256 * 1024; + +/// One widget on the desktop: which package it shows, at what size, and where. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Placement { + pub id: String, + /// The installation identity of the package, which stays the same when the package is renamed. + pub package: String, + pub family: String, + /// The display's name of the screen; empty until the display has placed the widget. + pub screen: String, + /// Offset from the screen's top-left corner in logical pixels. + pub x: i64, + pub y: i64, +} + +/// True for the form `sw_core::library` gives an installation identity. +pub fn is_installation(text: &str) -> bool { + (8..=64).contains(&text.len()) && text.bytes().all(|byte| byte.is_ascii_alphanumeric() || byte == b'-') +} + +impl Placement { + fn to_json(&self) -> Value { + json!({ "id": self.id, "package": self.package, "family": self.family, "screen": self.screen, "x": self.x, "y": self.y }) + } + + fn from_json(item: &Map) -> Option { + let text = |name: &str| item.get(name).and_then(Value::as_str); + let id = text("id").filter(|id| protocol::is_identifier(id))?; + let package = text("package").filter(|package| is_installation(package))?; + let family = text("family").filter(|family| protocol::is_family(family)).unwrap_or("medium"); + let screen = text("screen").filter(|screen| protocol::is_screen_name(screen)).unwrap_or(""); + let offset = |name: &str| item.get(name).and_then(Value::as_i64).unwrap_or(0).clamp(-MAXIMUM_OFFSET, MAXIMUM_OFFSET); + Some(Self { id: id.to_owned(), package: package.to_owned(), family: family.to_owned(), screen: screen.to_owned(), x: offset("x"), y: offset("y") }) + } +} + +/// Reads a small JSON object from a regular file, never through a link. +fn read_object(file: &Path) -> Option> { + use std::io::Read; + let opened = std::fs::OpenOptions::new().read(true).custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC | libc::O_NONBLOCK).open(file).ok()?; + if !opened.metadata().ok()?.is_file() { + return None; + } + let mut text = String::new(); + opened.take(MAXIMUM_FILE_BYTES + 1).read_to_string(&mut text).ok()?; + if text.len() as u64 > MAXIMUM_FILE_BYTES { + return None; + } + match serde_json::from_str(&text).ok()? { + Value::Object(object) => Some(object), + _ => None, + } +} + +/// Writes beside the file and renames over it: a reader sees the old or the new content, and a link at the name is replaced, not followed. +fn replace(file: &Path, value: &Value) -> std::io::Result<()> { + let mut temporary = file.as_os_str().to_owned(); + temporary.push(format!(".{}.tmp", std::process::id())); + let temporary = PathBuf::from(temporary); + let _ = std::fs::remove_file(&temporary); + let written = (|| { + let mut output = std::fs::OpenOptions::new().write(true).create_new(true).custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC).mode(0o600).open(&temporary)?; + output.write_all(serde_json::to_string_pretty(value).unwrap_or_default().as_bytes())?; + output.write_all(b"\n")?; + output.sync_all()?; + std::fs::rename(&temporary, file) + })(); + written.inspect_err(|_| { + let _ = std::fs::remove_file(&temporary); + }) +} + +/// The saved list of desktop widgets. +pub struct Placements { + file: PathBuf, + items: Vec, +} + +impl Placements { + pub fn load(file: PathBuf) -> Self { + let mut items: Vec = Vec::new(); + if let Some(root) = read_object(&file) + && let Some(Value::Array(widgets)) = root.get("widgets") + { + for placement in widgets.iter().filter_map(Value::as_object).filter_map(Placement::from_json) { + if items.len() < MAXIMUM_WIDGETS && items.iter().all(|existing| existing.id != placement.id) { + items.push(placement); + } + } + } + Self { file, items } + } + + pub fn all(&self) -> &[Placement] { + &self.items + } + + pub fn find(&self, id: &str) -> Option<&Placement> { + self.items.iter().find(|item| item.id == id) + } + + /// Adds a widget and saves. `None` when the limit is reached. + pub fn add(&mut self, placement: Placement) -> Option<&Placement> { + if self.items.len() >= MAXIMUM_WIDGETS || self.find(&placement.id).is_some() { + return None; + } + self.items.push(placement); + self.save(); + self.items.last() + } + + pub fn remove(&mut self, id: &str) -> bool { + let before = self.items.len(); + self.items.retain(|item| item.id != id); + let removed = self.items.len() != before; + if removed { + self.save(); + } + removed + } + + /// Changes one placement and saves when something changed. + pub fn update(&mut self, id: &str, change: impl FnOnce(&mut Placement)) -> Option<&Placement> { + let index = self.items.iter().position(|item| item.id == id)?; + let before = self.items[index].clone(); + change(&mut self.items[index]); + if self.items[index] != before { + self.save(); + } + self.items.get(index) + } + + fn save(&self) { + let value = json!({ "version": 1, "widgets": self.items.iter().map(Placement::to_json).collect::>() }); + if let Err(error) = replace(&self.file, &value) { + crate::log(&format!("the widget list could not be saved: {error}")); + } + } +} + +/// A screen as the display reported it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct ScreenInfo { + /// Pixels per logical pixel, in thousandths. + pub scale: i64, + pub width: i64, + pub height: i64, +} + +impl ScreenInfo { + pub fn factor(&self) -> f32 { + self.scale as f32 / 1000.0 + } +} + +/// What the display last reported, kept so that a restarted service draws at +/// the right scale and in the right theme before the display reports again. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct DisplayMemory { + pub dark: bool, + pub screens: BTreeMap, +} + +impl DisplayMemory { + pub fn load(file: &Path) -> Self { + let mut memory = Self::default(); + let Some(root) = read_object(file) else { return memory }; + memory.dark = root.get("dark").and_then(Value::as_bool).unwrap_or(false); + if let Some(Value::Object(screens)) = root.get("screens") { + for (name, value) in screens { + let number = |member: &str, minimum: i64, maximum: i64| value.get(member).and_then(Value::as_i64).filter(|number| (minimum..=maximum).contains(number)); + if memory.screens.len() < MAXIMUM_SCREENS + && protocol::is_screen_name(name) + && let (Some(scale), Some(width), Some(height)) = (number("scale", MINIMUM_SCALE, MAXIMUM_SCALE), number("width", 1, MAXIMUM_SCREEN_LENGTH), number("height", 1, MAXIMUM_SCREEN_LENGTH)) + { + memory.screens.insert(name.clone(), ScreenInfo { scale, width, height }); + } + } + } + memory + } + + pub fn save(&self, file: &Path) { + let screens: Map = self.screens.iter().map(|(name, screen)| (name.clone(), json!({ "scale": screen.scale, "width": screen.width, "height": screen.height }))).collect(); + if let Err(error) = replace(file, &json!({ "dark": self.dark, "screens": screens })) { + crate::log(&format!("the display state could not be saved: {error}")); + } + } +} + +/// `refreshMinutes` of `settings.json`: how often a widget without a +/// schedule of its own runs again. The file is read again when it changes, +/// so the management window only has to write it. +pub struct Settings { + file: PathBuf, + seen: Option<(SystemTime, u64)>, + refresh_minutes: u64, +} + +impl Settings { + pub fn new(file: PathBuf) -> Self { + Self { file, seen: None, refresh_minutes: DEFAULT_REFRESH_MINUTES } + } + + pub fn refresh_minutes(&mut self) -> u64 { + let stamp = std::fs::metadata(&self.file).ok().and_then(|metadata| Some((metadata.modified().ok()?, metadata.len()))); + if stamp != self.seen || stamp.is_none() { + self.seen = stamp; + self.refresh_minutes = read_object(&self.file) + .and_then(|root| root.get("refreshMinutes").and_then(Value::as_i64)) + .map_or(DEFAULT_REFRESH_MINUTES, |minutes| minutes.clamp(MINIMUM_REFRESH_MINUTES as i64, MAXIMUM_REFRESH_MINUTES as i64) as u64); + } + self.refresh_minutes + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn scratch(name: &str) -> PathBuf { + let directory = std::env::temp_dir().join(format!("sw-host-placements-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&directory); + std::fs::create_dir_all(&directory).unwrap(); + directory + } + + fn placement(id: &str) -> Placement { + Placement { id: id.into(), package: "0123456789abcdef".into(), family: "small".into(), screen: "eDP-1".into(), x: 40, y: -12 } + } + + #[test] + fn placements_survive_a_reload() { + let directory = scratch("reload"); + let file = directory.join("widgets.json"); + let mut placements = Placements::load(file.clone()); + assert!(placements.all().is_empty()); + placements.add(placement("00000000000000aa")).unwrap(); + placements.add(placement("00000000000000bb")).unwrap(); + assert!(placements.add(placement("00000000000000aa")).is_none(), "an identifier is used once"); + placements.update("00000000000000bb", |item| { + item.family = "large".into(); + item.x = 300; + }); + assert!(placements.remove("00000000000000aa")); + assert!(!placements.remove("00000000000000aa")); + use std::os::unix::fs::MetadataExt; + assert_eq!(std::fs::metadata(&file).unwrap().mode() & 0o777, 0o600); + let reloaded = Placements::load(file); + assert_eq!(reloaded.all(), [Placement { family: "large".into(), x: 300, ..placement("00000000000000bb") }]); + std::fs::remove_dir_all(directory).unwrap(); + } + + #[test] + fn entries_that_are_not_valid_are_dropped() { + let directory = scratch("invalid"); + let file = directory.join("widgets.json"); + std::fs::write( + &file, + r#"{"version":1,"widgets":[ + {"id":"00000000000000aa","package":"0123456789abcdef","family":"huge","screen":"../../x","x":99999999,"y":"7"}, + {"id":"../../etc/passwd","package":"0123456789abcdef"}, + {"id":"00000000000000bb","package":"../Packages/x"}, + {"id":"00000000000000aa","package":"0123456789abcdef"}, + "text", 7, null]}"#, + ) + .unwrap(); + let placements = Placements::load(file.clone()); + assert_eq!(placements.all(), [Placement { id: "00000000000000aa".into(), package: "0123456789abcdef".into(), family: "medium".into(), screen: String::new(), x: MAXIMUM_OFFSET, y: 0 }]); + for text in ["", "[]", "{\"widgets\":7}", "not json"] { + std::fs::write(&file, text).unwrap(); + assert!(Placements::load(file.clone()).all().is_empty(), "{text}"); + } + // A link is not followed. + std::fs::write(directory.join("real.json"), r#"{"widgets":[{"id":"00000000000000aa","package":"0123456789abcdef"}]}"#).unwrap(); + std::fs::remove_file(&file).unwrap(); + std::os::unix::fs::symlink(directory.join("real.json"), &file).unwrap(); + assert!(Placements::load(file).all().is_empty()); + std::fs::remove_dir_all(directory).unwrap(); + } + + #[test] + fn no_more_widgets_than_the_limit() { + let directory = scratch("limit"); + let mut placements = Placements::load(directory.join("widgets.json")); + for index in 0..MAXIMUM_WIDGETS { + assert!(placements.add(placement(&format!("{index:016x}"))).is_some()); + } + assert!(placements.add(placement("ffffffffffffffff")).is_none()); + std::fs::remove_dir_all(directory).unwrap(); + } + + #[test] + fn the_display_state_and_the_setting_are_read_within_their_limits() { + let directory = scratch("display"); + let file = directory.join("display.json"); + let mut memory = DisplayMemory::default(); + memory.dark = true; + memory.screens.insert("eDP-1".into(), ScreenInfo { scale: 2000, width: 1440, height: 900 }); + memory.save(&file); + assert_eq!(DisplayMemory::load(&file), memory); + std::fs::write(&file, r#"{"dark":"yes","screens":{"a b":{"scale":1000,"width":1,"height":1},"DP-1":{"scale":1,"width":1,"height":1},"DP-2":{"scale":1250,"width":2560,"height":1440}}}"#).unwrap(); + let loaded = DisplayMemory::load(&file); + assert!(!loaded.dark); + assert_eq!(loaded.screens.keys().collect::>(), ["DP-2"]); + assert_eq!(loaded.screens["DP-2"].factor(), 1.25); + + let file = directory.join("settings.json"); + let mut settings = Settings::new(file.clone()); + assert_eq!(settings.refresh_minutes(), 15); + for (text, minutes) in [("{\"refreshMinutes\":30}", 30), ("{\"refreshMinutes\":1}", 5), ("{\"refreshMinutes\":100000}", 1440), ("{\"refreshMinutes\":\"x\"}", 15), ("broken", 15)] { + std::fs::write(&file, text).unwrap(); + // The file is read again when its time or size changed; make sure one of them did. + settings.seen = None; + assert_eq!(settings.refresh_minutes(), minutes, "{text}"); + } + std::fs::remove_dir_all(directory).unwrap(); + } +} diff --git a/Linux/crates/sw-host/src/protocol.rs b/Linux/crates/sw-host/src/protocol.rs new file mode 100644 index 00000000..3e06eff6 --- /dev/null +++ b/Linux/crates/sw-host/src/protocol.rs @@ -0,0 +1,443 @@ +//! The words of the display protocol (`docs/linux/protocol.md`): the +//! requests a client may send over the socket, the replies, and the events +//! `scriptwidget watch` prints. +//! +//! A request is one line of JSON that names only package names, widget +//! identifiers, integers and fixed words. Everything is validated here, on +//! both sides: the command line refuses a bad argument before it connects, +//! and the service refuses it again, because any program of the user can +//! write to the socket. + +use serde_json::{Map, Value, json}; +use sw_core::packages::names; + +/// The version in `hello`. A display that does not know it shows "update ScriptWidget" and parses nothing else. +pub const PROTOCOL: u32 = 1; +/// A request line, without its line feed, is at most this long. +pub const MAXIMUM_LINE_BYTES: usize = 4096; +/// The words `menu` accepts. Nothing else is ever looked up. +pub const MENU_WORDS: [&str; 7] = ["resize-small", "resize-medium", "resize-large", "resize-extraLarge", "edit", "remove", "refresh"]; +pub const FAMILIES: [&str; 4] = sw_core::library::FAMILIES; +/// Offsets are kept inside this range, as on Windows. +pub const MAXIMUM_OFFSET: i64 = 100_000; +/// A display reports its scale in thousandths: 1000 is 1, 1500 is 1.5. +pub const MINIMUM_SCALE: i64 = 500; +pub const MAXIMUM_SCALE: i64 = 8000; +pub const MAXIMUM_SCREEN_LENGTH: i64 = 32_768; +const IDENTIFIER_LENGTH: usize = 16; +const MAXIMUM_SCREEN_NAME: usize = 64; + +/// Why a request was refused: a fixed code and a sentence for a person. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Refusal { + pub code: &'static str, + pub message: String, +} + +impl Refusal { + pub fn new(code: &'static str, message: impl Into) -> Self { + Self { code, message: message.into() } + } + + pub fn invalid(message: impl Into) -> Self { + Self::new("request.invalid", message) + } + + pub fn reply(&self) -> Value { + json!({ "ok": false, "error": { "code": self.code, "message": self.message } }) + } +} + +/// A new widget identifier: 16 lower-case hexadecimal digits from the system's random source. +pub fn new_identifier() -> std::io::Result { + let mut bytes = [0u8; IDENTIFIER_LENGTH / 2]; + getrandom::fill(&mut bytes).map_err(|error| std::io::Error::other(error.to_string()))?; + Ok(bytes.iter().map(|byte| format!("{byte:02x}")).collect()) +} + +/// True for the form [`new_identifier`] produces; nothing else is ever looked up or used in a file name. +pub fn is_identifier(text: &str) -> bool { + text.len() == IDENTIFIER_LENGTH && text.bytes().all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)) +} + +/// A display's own name for an output, such as `eDP-1`: letters, digits, `.`, `_`, `:` and `-`. +pub fn is_screen_name(text: &str) -> bool { + !text.is_empty() && text.len() <= MAXIMUM_SCREEN_NAME && text.bytes().all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b':' | b'-')) +} + +pub fn is_family(text: &str) -> bool { + FAMILIES.contains(&text) +} + +/// `on`, `off` or `toggle`, for `arrange` and `peek`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Switch { + On, + Off, + Toggle, +} + +impl Switch { + pub fn parse(word: &str) -> Option { + match word { + "on" => Some(Self::On), + "off" => Some(Self::Off), + "toggle" => Some(Self::Toggle), + _ => None, + } + } + + pub fn word(self) -> &'static str { + match self { + Self::On => "on", + Self::Off => "off", + Self::Toggle => "toggle", + } + } + + pub fn apply(self, current: bool) -> bool { + match self { + Self::On => true, + Self::Off => false, + Self::Toggle => !current, + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Request { + /// Turns the connection into the event stream. + Watch, + Status, + /// `package` is the name of an installed package. + Add { package: String, family: Option }, + Remove { id: String }, + Widgets, + /// One widget, the widgets of one package, or all of them. + Refresh { id: Option, package: Option }, + Peek(Switch), + Arrange(Switch), + /// `version` is the picture the region number belongs to; without it, the current one. + Tap { id: String, n: u32, version: Option }, + Move { id: String, screen: String, x: i64, y: i64 }, + /// `scale` in thousandths; `width` and `height` in logical pixels. + Screen { name: String, scale: i64, width: i64, height: i64 }, + Theme { dark: bool }, + Menu { id: String, word: String }, + /// The display says that the session was locked or unlocked. + Session { locked: bool }, +} + +fn text<'a>(object: &'a Map, name: &str) -> Result<&'a str, Refusal> { + object.get(name).and_then(Value::as_str).ok_or_else(|| Refusal::invalid(format!("\"{name}\" must be a string"))) +} + +fn optional_text<'a>(object: &'a Map, name: &str) -> Result, Refusal> { + match object.get(name) { + None => Ok(None), + Some(value) => value.as_str().map(Some).ok_or_else(|| Refusal::invalid(format!("\"{name}\" must be a string"))), + } +} + +fn integer(object: &Map, name: &str) -> Result { + // Only what JSON writes as an integer: 1.0 and 1e3 are refused. + object.get(name).and_then(Value::as_i64).ok_or_else(|| Refusal::invalid(format!("\"{name}\" must be an integer"))) +} + +fn word<'a>(object: &'a Map, name: &str, allowed: &[&str]) -> Result<&'a str, Refusal> { + let value = text(object, name)?; + if allowed.contains(&value) { Ok(value) } else { Err(Refusal::invalid(format!("\"{name}\" must be one of: {}", allowed.join(", ")))) } +} + +fn identifier(object: &Map) -> Result { + let value = text(object, "id")?; + if is_identifier(value) { Ok(value.to_owned()) } else { Err(Refusal::invalid("\"id\" is not a widget identifier")) } +} + +fn package(value: &str) -> Result { + if names::is_valid(value) { Ok(value.to_owned()) } else { Err(Refusal::invalid("\"package\" is not a package name")) } +} + +fn family(value: &str) -> Result { + if is_family(value) { Ok(value.to_owned()) } else { Err(Refusal::invalid("\"family\" must be small, medium, large or extraLarge")) } +} + +fn in_range(name: &str, value: i64, minimum: i64, maximum: i64) -> Result { + if (minimum..=maximum).contains(&value) { Ok(value) } else { Err(Refusal::invalid(format!("\"{name}\" must be between {minimum} and {maximum}"))) } +} + +impl Request { + /// Checks the values of a request built from command-line arguments, by the rules a received line is read with. + pub fn checked(self) -> Result { + Self::parse(&self.line()) + } + + /// Reads one received line. Unknown operations, unknown members, wrong types and values out of range are refused. + pub fn parse(line: &str) -> Result { + if line.len() > MAXIMUM_LINE_BYTES { + return Err(Refusal::new("request.too_long", "The request is too long.")); + } + let Ok(Value::Object(object)) = serde_json::from_str::(line) else { + return Err(Refusal::invalid("A request is one JSON object on one line.")); + }; + let operation = text(&object, "op")?; + let members: &[&str] = match operation { + "watch" | "status" | "widgets" => &[], + "add" => &["package", "family"], + "remove" => &["id"], + "refresh" => &["id", "package"], + "peek" | "arrange" => &["state"], + "tap" => &["id", "n", "version"], + "move" => &["id", "screen", "x", "y"], + "screen" => &["name", "scale", "width", "height"], + "theme" => &["mode"], + "menu" => &["id", "word"], + "session" => &["state"], + _ => return Err(Refusal::invalid("Unknown operation.")), + }; + if let Some(unknown) = object.keys().find(|key| *key != "op" && !members.contains(&key.as_str())) { + // The name is the sender's own text: say which one without repeating more than a word of it. + let shown: String = unknown.chars().filter(|character| character.is_ascii_alphanumeric()).take(24).collect(); + return Err(Refusal::invalid(format!("Unknown member \"{shown}\"."))); + } + Ok(match operation { + "watch" => Self::Watch, + "status" => Self::Status, + "widgets" => Self::Widgets, + "add" => Self::Add { package: package(text(&object, "package")?)?, family: optional_text(&object, "family")?.map(family).transpose()? }, + "remove" => Self::Remove { id: identifier(&object)? }, + "refresh" => { + let id = if object.contains_key("id") { Some(identifier(&object)?) } else { None }; + let package = optional_text(&object, "package")?.map(package).transpose()?; + if id.is_some() && package.is_some() { + return Err(Refusal::invalid("Name a widget or a package, not both.")); + } + Self::Refresh { id, package } + } + "peek" | "arrange" => { + let switch = Switch::parse(word(&object, "state", &["on", "off", "toggle"])?).unwrap_or(Switch::Toggle); + if operation == "peek" { Self::Peek(switch) } else { Self::Arrange(switch) } + } + "tap" => Self::Tap { + id: identifier(&object)?, + n: in_range("n", integer(&object, "n")?, 0, 4095)? as u32, + version: if object.contains_key("version") { Some(in_range("version", integer(&object, "version")?, 1, i64::MAX)? as u64) } else { None }, + }, + "move" => { + let screen = text(&object, "screen")?; + if !is_screen_name(screen) { + return Err(Refusal::invalid("\"screen\" is not a screen name")); + } + // Clamped, as on Windows: a widget dragged far away comes back to the edge of the range. + let offset = |name: &str| integer(&object, name).map(|value| value.clamp(-MAXIMUM_OFFSET, MAXIMUM_OFFSET)); + Self::Move { id: identifier(&object)?, screen: screen.to_owned(), x: offset("x")?, y: offset("y")? } + } + "screen" => { + let name = text(&object, "name")?; + if !is_screen_name(name) { + return Err(Refusal::invalid("\"name\" is not a screen name")); + } + Self::Screen { + name: name.to_owned(), + scale: in_range("scale", integer(&object, "scale")?, MINIMUM_SCALE, MAXIMUM_SCALE)?, + width: in_range("width", integer(&object, "width")?, 1, MAXIMUM_SCREEN_LENGTH)?, + height: in_range("height", integer(&object, "height")?, 1, MAXIMUM_SCREEN_LENGTH)?, + } + } + "theme" => Self::Theme { dark: word(&object, "mode", &["dark", "light"])? == "dark" }, + "menu" => Self::Menu { id: identifier(&object)?, word: word(&object, "word", &MENU_WORDS)?.to_owned() }, + _ => Self::Session { locked: word(&object, "state", &["locked", "unlocked"])? == "locked" }, + }) + } + + /// The request as the one line that is sent, without the line feed. + pub fn line(&self) -> String { + let value = match self { + Self::Watch => json!({ "op": "watch" }), + Self::Status => json!({ "op": "status" }), + Self::Widgets => json!({ "op": "widgets" }), + Self::Add { package, family } => { + let mut value = json!({ "op": "add", "package": package }); + if let Some(family) = family { + value["family"] = json!(family); + } + value + } + Self::Remove { id } => json!({ "op": "remove", "id": id }), + Self::Refresh { id, package } => { + let mut value = json!({ "op": "refresh" }); + if let Some(id) = id { + value["id"] = json!(id); + } + if let Some(package) = package { + value["package"] = json!(package); + } + value + } + Self::Peek(switch) => json!({ "op": "peek", "state": switch.word() }), + Self::Arrange(switch) => json!({ "op": "arrange", "state": switch.word() }), + Self::Tap { id, n, version } => { + let mut value = json!({ "op": "tap", "id": id, "n": n }); + if let Some(version) = version { + value["version"] = json!(version); + } + value + } + Self::Move { id, screen, x, y } => json!({ "op": "move", "id": id, "screen": screen, "x": x, "y": y }), + Self::Screen { name, scale, width, height } => json!({ "op": "screen", "name": name, "scale": scale, "width": width, "height": height }), + Self::Theme { dark } => json!({ "op": "theme", "mode": if *dark { "dark" } else { "light" } }), + Self::Menu { id, word } => json!({ "op": "menu", "id": id, "word": word }), + Self::Session { locked } => json!({ "op": "session", "state": if *locked { "locked" } else { "unlocked" } }), + }; + value.to_string() + } +} + +/// A display scale as text ("2", "1.5", "1.25") in thousandths, or `None` when it is not a plain decimal number. +pub fn scale_thousandths(text: &str) -> Option { + let (whole, fraction) = text.split_once('.').unwrap_or((text, "")); + if whole.is_empty() || whole.len() > 2 || fraction.len() > 6 || !whole.bytes().chain(fraction.bytes()).all(|byte| byte.is_ascii_digit()) { + return None; + } + let mut thousandths = whole.parse::().ok()? * 1000; + let digits: Vec = fraction.bytes().map(|byte| i64::from(byte - b'0')).collect(); + for (index, weight) in [100, 10, 1].into_iter().enumerate() { + thousandths += digits.get(index).copied().unwrap_or(0) * weight; + } + // 1.3333 is a third more than 1.333: round on the fourth digit. + if digits.get(3).copied().unwrap_or(0) >= 5 { + thousandths += 1; + } + Some(thousandths) +} + +// Events + +pub fn hello(dark: bool, arrange: bool, peek: bool) -> Value { + json!({ "event": "hello", "protocol": PROTOCOL, "dark": dark, "arrange": arrange, "peek": peek, "service": env!("CARGO_PKG_VERSION") }) +} + +pub fn mode(arrange: bool, peek: bool) -> Value { + json!({ "event": "mode", "arrange": arrange, "peek": peek }) +} + +pub fn removed(id: &str) -> Value { + json!({ "event": "remove", "id": id }) +} + +pub fn bye(reason: &str) -> Value { + json!({ "event": "bye", "reason": reason }) +} + +#[cfg(test)] +mod tests { + use super::*; + + const ID: &str = "0123456789abcdef"; + + #[test] + fn identifiers_are_sixteen_lower_case_hexadecimal_digits() { + let id = new_identifier().unwrap(); + assert!(is_identifier(&id), "{id}"); + assert_ne!(id, new_identifier().unwrap()); + for bad in ["", "0123456789abcde", "0123456789abcdef0", "0123456789ABCDEF", "0123456789abcdeg", "../../etc/passwd0", "0123456789abcde/", "0123456789abcde\n"] { + assert!(!is_identifier(bad), "{bad:?}"); + } + } + + #[test] + fn every_request_survives_its_own_line() { + let requests = [ + Request::Watch, + Request::Status, + Request::Widgets, + Request::Add { package: "Daily Quote".into(), family: None }, + Request::Add { package: "时钟".into(), family: Some("extraLarge".into()) }, + Request::Remove { id: ID.into() }, + Request::Refresh { id: None, package: None }, + Request::Refresh { id: Some(ID.into()), package: None }, + Request::Refresh { id: None, package: Some("Clock".into()) }, + Request::Peek(Switch::Toggle), + Request::Arrange(Switch::On), + Request::Tap { id: ID.into(), n: 3, version: None }, + Request::Tap { id: ID.into(), n: 0, version: Some(7) }, + Request::Move { id: ID.into(), screen: "eDP-1".into(), x: -20, y: 400 }, + Request::Screen { name: "HDMI-A-1".into(), scale: 1500, width: 2560, height: 1440 }, + Request::Theme { dark: true }, + Request::Menu { id: ID.into(), word: "resize-extraLarge".into() }, + Request::Session { locked: true }, + ]; + for request in requests { + let line = request.line(); + assert!(!line.contains('\n')); + assert_eq!(Request::parse(&line), Ok(request.clone()), "{line}"); + assert_eq!(request.clone().checked(), Ok(request)); + } + } + + #[test] + fn what_is_not_a_known_request_is_refused() { + let id = format!("\"id\":\"{ID}\""); + let lines = [ + String::new(), + "watch".into(), + "[]".into(), + "\"watch\"".into(), + "{}".into(), + "{\"op\":7}".into(), + "{\"op\":\"run\"}".into(), + "{\"op\":\"exec\",\"command\":\"rm -rf ~\"}".into(), + "{\"op\":\"watch\",\"extra\":1}".into(), + "{\"op\":\"add\"}".into(), + "{\"op\":\"add\",\"package\":\"../x\"}".into(), + "{\"op\":\"add\",\"package\":\"/etc\"}".into(), + "{\"op\":\"add\",\"package\":\".hidden\"}".into(), + "{\"op\":\"add\",\"package\":\"A\",\"family\":\"huge\"}".into(), + "{\"op\":\"add\",\"package\":\"A\",\"path\":\"/tmp/x\"}".into(), + "{\"op\":\"remove\",\"id\":\"../../../etc/passwd\"}".into(), + "{\"op\":\"remove\",\"id\":7}".into(), + format!("{{\"op\":\"refresh\",{id},\"package\":\"A\"}}"), + format!("{{\"op\":\"tap\",{id}}}"), + format!("{{\"op\":\"tap\",{id},\"n\":-1}}"), + format!("{{\"op\":\"tap\",{id},\"n\":1.5}}"), + format!("{{\"op\":\"tap\",{id},\"n\":\"1\"}}"), + format!("{{\"op\":\"tap\",{id},\"n\":99999}}"), + format!("{{\"op\":\"tap\",{id},\"n\":1,\"version\":0}}"), + format!("{{\"op\":\"move\",{id},\"screen\":\"a b\",\"x\":1,\"y\":1}}"), + format!("{{\"op\":\"move\",{id},\"screen\":\"../x\",\"x\":1,\"y\":1}}"), + format!("{{\"op\":\"move\",{id},\"screen\":\"eDP-1\",\"x\":\"1\",\"y\":1}}"), + "{\"op\":\"screen\",\"name\":\"eDP-1\",\"scale\":100,\"width\":10,\"height\":10}".into(), + "{\"op\":\"screen\",\"name\":\"eDP-1\",\"scale\":1000,\"width\":0,\"height\":10}".into(), + "{\"op\":\"screen\",\"name\":\"\",\"scale\":1000,\"width\":10,\"height\":10}".into(), + "{\"op\":\"theme\",\"mode\":\"blue\"}".into(), + format!("{{\"op\":\"menu\",{id},\"word\":\"delete-everything\"}}"), + format!("{{\"op\":\"menu\",{id},\"word\":\"resize-huge\"}}"), + "{\"op\":\"peek\",\"state\":\"maybe\"}".into(), + "{\"op\":\"session\",\"state\":\"asleep\"}".into(), + ]; + for line in lines { + let refusal = Request::parse(&line).expect_err(&line); + assert_eq!(refusal.code, "request.invalid", "{line}"); + } + let long = format!("{{\"op\":\"add\",\"package\":\"{}\"}}", "a".repeat(MAXIMUM_LINE_BYTES)); + assert_eq!(Request::parse(&long).unwrap_err().code, "request.too_long"); + } + + #[test] + fn offsets_are_clamped_and_scales_are_read_as_thousandths() { + let line = format!("{{\"op\":\"move\",\"id\":\"{ID}\",\"screen\":\"DP-1\",\"x\":-99999999,\"y\":99999999}}"); + assert_eq!(Request::parse(&line), Ok(Request::Move { id: ID.into(), screen: "DP-1".into(), x: -MAXIMUM_OFFSET, y: MAXIMUM_OFFSET })); + for (text, thousandths) in [("1", 1000), ("2", 2000), ("1.5", 1500), ("1.25", 1250), ("1.3333", 1333), ("1.6667", 1667), ("0.5", 500), ("1.", 1000)] { + assert_eq!(scale_thousandths(text), Some(thousandths), "{text}"); + } + for text in ["", ".5", "-1", "1e3", "1,5", "abc", "1.5x", "100", "1.2345678"] { + assert_eq!(scale_thousandths(text), None, "{text}"); + } + } + + #[test] + fn an_unknown_member_is_named_without_repeating_what_was_sent() { + let refusal = Request::parse("{\"op\":\"watch\",\"\":1}").unwrap_err(); + assert_eq!(refusal.message, "Unknown member \"scriptalert1script\"."); + } +} diff --git a/Linux/crates/sw-host/src/schedule.rs b/Linux/crates/sw-host/src/schedule.rs new file mode 100644 index 00000000..85c9b6cd --- /dev/null +++ b/Linux/crates/sw-host/src/schedule.rs @@ -0,0 +1,201 @@ +//! When a widget is next due: the pure arithmetic of the refresh schedule, +//! with every time passed in, so that it is tested without waiting. +//! +//! The rules are those of the Windows host (`AppHost.cs`): a script runs +//! again after the refresh interval or at its next timeline transition, +//! whichever is first; a tree with a running timer is drawn again every +//! second and one with a clock time every minute, without running the script. + +use serde_json::Value; + +/// How often a tree must be drawn again because a `` in it changes by itself. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub enum Tick { + /// `style` is `timer`, `relative` or `offset`: the text changes every second. + Second, + /// `style` is `time`: the text changes every minute. + Minute, +} + +/// The fastest tick any `` of a tree needs, or `None` when the picture only changes when the script runs. +pub fn tick_of(tree: &str) -> Option { + if !tree.contains("\"date\"") { + return None; + } + // The tree nests deeper than serde_json reads by default; its depth is bounded by the runtime contract. + let mut deserializer = serde_json::Deserializer::from_str(tree); + deserializer.disable_recursion_limit(); + let parsed: Value = serde::Deserialize::deserialize(&mut deserializer).ok()?; + let mut found = None; + let mut pending = vec![&parsed]; + while let Some(node) = pending.pop() { + match node { + Value::Object(item) => { + if item.get("$element").and_then(Value::as_str) == Some("date") { + let tick = match item.get("props").and_then(|props| props.get("style")).and_then(Value::as_str).unwrap_or("date") { + "timer" | "relative" | "offset" => Some(Tick::Second), + "time" => Some(Tick::Minute), + _ => None, + }; + found = match (found, tick) { + (Some(a), Some(b)) => Some(std::cmp::min::(a, b)), + (a, b) => a.or(b), + }; + } + pending.extend(item.get("children")); + } + Value::Array(items) => pending.extend(items), + _ => {} + } + } + found +} + +/// Milliseconds until the picture changes: just after the next full second or minute of the wall clock. +pub fn tick_delay(tick: Tick, wall_milliseconds: i64) -> u64 { + let (period, margin) = match tick { + Tick::Second => (1000, 15), + Tick::Minute => (60_000, 50), + }; + (period - wall_milliseconds.rem_euclid(period) + margin) as u64 +} + +/// Milliseconds until the script runs again: the refresh interval, or the +/// next timeline transition when that comes first (but not sooner than a second). +pub fn run_delay(refresh_minutes: u64, next_transition_wall: Option, wall_milliseconds: i64) -> u64 { + let interval = refresh_minutes * 60_000; + match next_transition_wall { + Some(transition) => interval.min((transition - wall_milliseconds).max(1000) as u64), + None => interval, + } +} + +/// The frames of a widget with animated GIFs: `(start, duration)` in +/// milliseconds for each picture, in order. One GIF gives its own frames. +/// Several GIFs give a picture for every moment one of them changes, over the +/// length of the longest; the shorter ones repeat within it. When that would +/// be more than `maximum` pictures, only the longest GIF moves. +/// +/// `delays` has one list per GIF: how long each of its frames is shown. +pub fn gif_frames(delays: &[Vec], maximum: usize) -> Vec<(u64, u32)> { + let duration = |slot: &Vec| slot.iter().map(|delay| u64::from(*delay)).sum::(); + let animated: Vec<&Vec> = delays.iter().filter(|slot| slot.len() > 1 && duration(slot) > 0).collect(); + let Some(longest) = animated.iter().copied().max_by_key(|slot| duration(slot)) else { return Vec::new() }; + let total = duration(longest); + let boundaries_of = |slot: &Vec| { + let mut boundaries = Vec::new(); + let mut time = 0u64; + 'outer: loop { + for delay in slot { + if time >= total || boundaries.len() > maximum { + break 'outer; + } + boundaries.push(time); + time += u64::from((*delay).max(1)); + } + } + boundaries + }; + let mut boundaries: Vec = animated.iter().flat_map(|slot| boundaries_of(slot)).collect(); + boundaries.sort_unstable(); + boundaries.dedup(); + if boundaries.len() > maximum { + boundaries = boundaries_of(longest); + boundaries.truncate(maximum); + } + let ends = boundaries.iter().skip(1).copied().chain([total]); + boundaries.iter().copied().zip(ends).map(|(start, end)| (start, (end - start).min(u64::from(u32::MAX)) as u32)).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn tree(styles: &[Option<&str>]) -> String { + let dates: Vec = styles + .iter() + .map(|style| match style { + Some(style) => format!(r#"{{"$element":"date","props":{{"style":"{style}","date":"2026-01-01T00:00:00Z"}},"children":[]}}"#), + None => r#"{"$element":"date","props":{"date":"2026-01-01T00:00:00Z"},"children":[]}"#.to_owned(), + }) + .collect(); + format!(r#"{{"$element":"vstack","props":null,"children":[{{"$element":"hstack","props":null,"children":[{}]}}]}}"#, dates.join(",")) + } + + #[test] + fn a_timer_ticks_each_second_and_a_clock_each_minute() { + assert_eq!(tick_of(&tree(&[])), None); + assert_eq!(tick_of(&tree(&[None])), None, "a plain date changes when the script runs"); + assert_eq!(tick_of(&tree(&[Some("date")])), None); + assert_eq!(tick_of(&tree(&[Some("time")])), Some(Tick::Minute)); + for style in ["timer", "relative", "offset"] { + assert_eq!(tick_of(&tree(&[Some(style)])), Some(Tick::Second), "{style}"); + } + assert_eq!(tick_of(&tree(&[Some("time"), Some("timer"), None])), Some(Tick::Second), "the fastest wins"); + assert_eq!(tick_of(&tree(&[Some("timer"), Some("time")])), Some(Tick::Second)); + // Text that only mentions a date is not a date element. + assert_eq!(tick_of(r#"{"$element":"text","props":{"style":"timer"},"children":["\"date\""]}"#), None); + assert_eq!(tick_of("{\"date\" not json"), None); + } + + #[test] + fn a_deeply_nested_tree_is_read() { + let depth = 400; + let deep = format!("{}{}{}", r#"{"$element":"vstack","props":null,"children":["#.repeat(depth), tree(&[Some("timer")]), "]}".repeat(depth)); + assert_eq!(tick_of(&deep), Some(Tick::Second)); + } + + #[test] + fn ticks_follow_the_wall_clock() { + assert_eq!(tick_delay(Tick::Second, 1_700_000_000_000), 1015); + assert_eq!(tick_delay(Tick::Second, 1_700_000_000_400), 615); + assert_eq!(tick_delay(Tick::Second, 1_700_000_000_999), 16); + // 1_700_000_040_000 is a full minute. + assert_eq!(tick_delay(Tick::Minute, 1_700_000_040_000), 60_050); + assert_eq!(tick_delay(Tick::Minute, 1_700_000_040_000 + 20_000), 40_050); + assert_eq!(tick_delay(Tick::Minute, 1_700_000_040_000 + 59_999), 51); + assert_eq!(tick_delay(Tick::Second, -1), 16, "before 1970 the arithmetic still holds"); + } + + #[test] + fn the_script_runs_at_the_interval_or_the_next_transition() { + let now = 1_700_000_000_000; + assert_eq!(run_delay(15, None, now), 900_000); + assert_eq!(run_delay(5, None, now), 300_000); + assert_eq!(run_delay(15, Some(now + 60_000), now), 60_000); + assert_eq!(run_delay(15, Some(now + 3_600_000), now), 900_000, "a far transition does not delay the refresh"); + assert_eq!(run_delay(15, Some(now + 10), now), 1000, "never a busy loop"); + assert_eq!(run_delay(15, Some(now - 5000), now), 1000, "a transition that has passed runs soon, once"); + } + + #[test] + fn one_gif_gives_its_own_frames() { + assert_eq!(gif_frames(&[vec![100, 250, 100]], 60), [(0, 100), (100, 250), (350, 100)]); + assert_eq!(gif_frames(&[], 60), []); + assert_eq!(gif_frames(&[vec![100]], 60), [], "a still image has no frames"); + assert_eq!(gif_frames(&[vec![100], vec![50, 50]], 60), [(0, 50), (50, 50)]); + } + + #[test] + fn several_gifs_share_one_cycle() { + // 300 ms in three frames, and 200 ms in two: the short one starts again at 200. + assert_eq!(gif_frames(&[vec![100, 100, 100], vec![150, 50]], 60), [(0, 100), (100, 50), (150, 50), (200, 100)]); + // The same GIF twice changes nothing. + assert_eq!(gif_frames(&[vec![100, 100], vec![100, 100]], 60), [(0, 100), (100, 100)]); + let frames = gif_frames(&[vec![100, 100, 100], vec![150, 50]], 60); + assert_eq!(frames.iter().map(|(_, duration)| u64::from(*duration)).sum::(), 300, "the cycle is as long as the longest GIF"); + } + + #[test] + fn no_more_pictures_than_the_limit() { + // Two GIFs that never change together: 60 + 59 moments, over the limit, so only the longest moves. + let long: Vec = vec![100; 60]; + let odd: Vec = vec![101; 59]; + let frames = gif_frames(&[long.clone(), odd], 60); + assert_eq!(frames.len(), 60); + assert!(frames.iter().all(|(_, duration)| *duration == 100)); + assert_eq!(gif_frames(&[vec![20; 200]], 60).len(), 60); + // A zero delay cannot stall the loop. + assert_eq!(gif_frames(&[vec![0, 100]], 60), [(0, 1), (1, 99)]); + } +} diff --git a/Linux/crates/sw-host/src/server.rs b/Linux/crates/sw-host/src/server.rs new file mode 100644 index 00000000..38df79f8 --- /dev/null +++ b/Linux/crates/sw-host/src/server.rs @@ -0,0 +1,317 @@ +//! The socket: `$XDG_RUNTIME_DIR/scriptwidget/host.sock`, for this user +//! alone. One line of JSON in, one line of JSON out; after `watch` the +//! connection carries events until either side closes it. +//! +//! Three things keep other users and runaway clients out: the directory and +//! the socket are private to the user, the user identifier of every peer is +//! asked from the kernel and compared with ours, and lines, connections and +//! waiting times are all bounded. Nothing received is executed or used as a +//! path; `protocol::Request::parse` decides what a line may say. + +use std::io::{Read, Write}; +use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; +use std::os::unix::net::{UnixListener, UnixStream}; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use std::sync::mpsc::{Receiver, Sender, SyncSender, sync_channel}; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +/// Hands a message to the service's main loop; false when the loop is gone. +pub type Deliver = Arc bool + Send + Sync>; + +use serde_json::Value; + +use crate::paths::RuntimePaths; +use crate::protocol::{MAXIMUM_LINE_BYTES, Refusal, Request}; + +/// More connections than this at once are refused; a display holds one or two. +pub const MAXIMUM_CONNECTIONS: usize = 32; +/// Events that may wait for one watcher; one that falls further behind is dropped. +pub const WATCHER_QUEUE: usize = 1024; +/// A connection that sends this many lines that are not requests is closed. +const MAXIMUM_REFUSALS: usize = 8; +const REQUEST_TIMEOUT: Duration = Duration::from_secs(10); +const WRITE_TIMEOUT: Duration = Duration::from_secs(5); + +/// What a connection asks of the service's main loop. +pub enum Inbound { + Request { request: Request, reply: SyncSender }, + /// A new watcher: the main loop queues `hello` and the current state on `events`, then every event that follows. + Watch { watcher: u64, events: SyncSender> }, + WatcherGone { watcher: u64 }, +} + +/// Holds the lock that makes this the only service of the session; dropping it removes the socket. +pub struct Listening { + listener: UnixListener, + socket: PathBuf, + _lock: std::fs::File, +} + +impl Listening { + /// A second handle on the listening socket, for the thread that accepts. + pub fn listener(&self) -> std::io::Result { + self.listener.try_clone() + } + + pub fn socket(&self) -> &Path { + &self.socket + } +} + +impl Drop for Listening { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.socket); + } +} + +#[derive(Debug)] +pub enum BindError { + /// Another service holds the lock. + AlreadyRunning, + Io(std::io::Error), +} + +impl From for BindError { + fn from(error: std::io::Error) -> Self { + Self::Io(error) + } +} + +/// Takes the lock and binds the socket. A socket file left by a service that +/// crashed is removed: holding the lock proves that no service is using it. +pub fn bind(paths: &RuntimePaths) -> Result { + paths.prepare()?; + let lock = std::fs::OpenOptions::new().read(true).write(true).create(true).truncate(false).custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC).mode(0o600).open(paths.lock())?; + use std::os::fd::AsRawFd; + // SAFETY: the descriptor is open for the duration of the call. + if unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } != 0 { + let error = std::io::Error::last_os_error(); + return Err(if error.kind() == std::io::ErrorKind::WouldBlock { BindError::AlreadyRunning } else { BindError::Io(error) }); + } + let socket = paths.socket(); + match std::fs::symlink_metadata(&socket) { + // Whatever is at the name, a link included, is removed itself; nothing is followed. + Ok(metadata) if !metadata.is_dir() => std::fs::remove_file(&socket)?, + Ok(_) => return Err(BindError::Io(std::io::Error::new(std::io::ErrorKind::AlreadyExists, format!("{} is a directory", socket.display())))), + Err(_) => {} + } + let listener = UnixListener::bind(&socket)?; + // The directory already keeps everyone else out; the mode says the same of the socket itself. + std::fs::set_permissions(&socket, std::fs::Permissions::from_mode(0o600))?; + Ok(Listening { listener, socket, _lock: lock }) +} + +/// The user the kernel says is at the other end of a connection. +pub fn peer_user(stream: &UnixStream) -> std::io::Result { + use std::os::fd::AsRawFd; + #[cfg(any(target_os = "linux", target_os = "android"))] + { + let mut credentials = libc::ucred { pid: 0, uid: u32::MAX, gid: u32::MAX }; + let mut length = std::mem::size_of::() as libc::socklen_t; + // SAFETY: `credentials` and `length` are valid for writing and describe each other's size. + let result = unsafe { libc::getsockopt(stream.as_raw_fd(), libc::SOL_SOCKET, libc::SO_PEERCRED, (&raw mut credentials).cast(), &raw mut length) }; + if result != 0 || length as usize != std::mem::size_of::() { + return Err(std::io::Error::last_os_error()); + } + Ok(credentials.uid) + } + #[cfg(not(any(target_os = "linux", target_os = "android")))] + { + let (mut user, mut group) = (u32::MAX, u32::MAX); + // SAFETY: both pointers are valid for writing. + if unsafe { libc::getpeereid(stream.as_raw_fd(), &raw mut user, &raw mut group) } != 0 { + return Err(std::io::Error::last_os_error()); + } + Ok(user) + } +} + +/// Reads lines of at most [`MAXIMUM_LINE_BYTES`] bytes. +struct Lines { + stream: UnixStream, + buffer: Vec, +} + +enum Line { + Text(String), + /// Too long, or not UTF-8: nothing more is read from this connection. + Unreadable(&'static str), + Closed, +} + +impl Lines { + fn next(&mut self) -> Line { + loop { + if let Some(end) = self.buffer.iter().position(|byte| *byte == b'\n') { + let line: Vec = self.buffer.drain(..=end).collect(); + return match String::from_utf8(line) { + Ok(text) => Line::Text(text.trim_end_matches(['\n', '\r']).to_owned()), + Err(_) => Line::Unreadable("request.invalid"), + }; + } + if self.buffer.len() > MAXIMUM_LINE_BYTES { + return Line::Unreadable("request.too_long"); + } + let mut chunk = [0u8; 1024]; + match self.stream.read(&mut chunk) { + Ok(0) | Err(_) => return Line::Closed, + Ok(count) => self.buffer.extend_from_slice(&chunk[..count]), + } + } + } +} + +fn send(stream: &mut UnixStream, value: &Value) -> bool { + let mut line = value.to_string(); + line.push('\n'); + stream.write_all(line.as_bytes()).is_ok() +} + +struct Shared { + deliver: Deliver, + connections: AtomicUsize, + next_watcher: AtomicUsize, + expected_user: u32, + stopping: AtomicBool, + /// Told when the writer of a watcher has written its last line. + drained: Mutex>, +} + +/// Accepts connections until [`Acceptor::stop`]. +pub struct Acceptor { + shared: Arc, + socket: PathBuf, + /// One message for every watcher whose last line has been written. + pub drained: Receiver<()>, +} + +impl Acceptor { + pub fn start(listener: UnixListener, socket: PathBuf, deliver: Deliver, expected_user: u32) -> std::io::Result { + let (drained_sender, drained) = std::sync::mpsc::channel(); + let shared = Arc::new(Shared { + deliver, + connections: AtomicUsize::new(0), + next_watcher: AtomicUsize::new(1), + expected_user, + stopping: AtomicBool::new(false), + drained: Mutex::new(drained_sender), + }); + let accepting = shared.clone(); + std::thread::Builder::new().name("scriptwidget-accept".into()).spawn(move || { + for stream in listener.incoming() { + if accepting.stopping.load(Ordering::SeqCst) { + break; + } + let Ok(stream) = stream else { continue }; + if accepting.connections.fetch_add(1, Ordering::SeqCst) >= MAXIMUM_CONNECTIONS { + accepting.connections.fetch_sub(1, Ordering::SeqCst); + let mut stream = stream; + let _ = stream.set_write_timeout(Some(WRITE_TIMEOUT)); + send(&mut stream, &Refusal::new("service.busy", "Too many connections.").reply()); + continue; + } + let shared = accepting.clone(); + let spawned = std::thread::Builder::new().name("scriptwidget-client".into()).spawn(move || { + serve(stream, &shared); + shared.connections.fetch_sub(1, Ordering::SeqCst); + }); + if spawned.is_err() { + accepting.connections.fetch_sub(1, Ordering::SeqCst); + } + } + })?; + Ok(Self { shared, socket, drained }) + } + + /// Stops accepting. Connections that are open end by themselves when their peer closes or the main loop is gone. + pub fn stop(&self) { + self.shared.stopping.store(true, Ordering::SeqCst); + // Wakes the thread out of `accept`. + let _ = UnixStream::connect(&self.socket); + } +} + +fn serve(mut stream: UnixStream, shared: &Arc) { + let _ = stream.set_write_timeout(Some(WRITE_TIMEOUT)); + let _ = stream.set_read_timeout(Some(REQUEST_TIMEOUT)); + match peer_user(&stream) { + Ok(user) if user == shared.expected_user => {} + _ => { + send(&mut stream, &Refusal::new("peer.refused", "This socket serves one user only.").reply()); + return; + } + } + let Ok(reading) = stream.try_clone() else { return }; + let mut lines = Lines { stream: reading, buffer: Vec::new() }; + let mut refusals = 0; + loop { + let text = match lines.next() { + Line::Text(text) => text, + Line::Unreadable(code) => { + send(&mut stream, &Refusal::new(code, "A request is one line of JSON, at most 4096 bytes long.").reply()); + return; + } + Line::Closed => return, + }; + if text.is_empty() { + continue; + } + let request = match Request::parse(&text) { + Ok(request) => request, + Err(refusal) => { + refusals += 1; + if !send(&mut stream, &refusal.reply()) || refusals >= MAXIMUM_REFUSALS { + return; + } + continue; + } + }; + if request == Request::Watch { + watch(stream, lines, shared); + return; + } + let (reply, answer) = sync_channel(1); + if !(shared.deliver)(Inbound::Request { request, reply }) { + return; + } + match answer.recv_timeout(REQUEST_TIMEOUT) { + Ok(value) if send(&mut stream, &value) => {} + _ => return, + } + } +} + +/// Carries events to one watcher. A second thread writes, so that this one +/// can notice the peer closing while no event is waiting. +fn watch(stream: UnixStream, mut lines: Lines, shared: &Arc) { + let watcher = shared.next_watcher.fetch_add(1, Ordering::SeqCst) as u64; + let (events, queue) = sync_channel::>(WATCHER_QUEUE); + let drained = shared.drained.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).clone(); + let mut writing = stream; + let writer = std::thread::Builder::new().name("scriptwidget-watch".into()).spawn(move || { + for line in queue { + if writing.write_all(line.as_bytes()).and_then(|()| writing.write_all(b"\n")).is_err() { + break; + } + } + // Everything queued has been written (or the peer is gone): the reader wakes and the connection ends. + let _ = writing.shutdown(std::net::Shutdown::Both); + let _ = drained.send(()); + }); + if writer.is_err() || !(shared.deliver)(Inbound::Watch { watcher, events }) { + return; + } + // A watcher has nothing more to say; what it sends is read and dropped until it closes. + let _ = lines.stream.set_read_timeout(None); + let mut discard = [0u8; 256]; + while matches!(lines.stream.read(&mut discard), Ok(count) if count > 0) {} + (shared.deliver)(Inbound::WatcherGone { watcher }); +} + +/// For callers that only need the path check of a socket file in tests and diagnostics. +pub fn is_socket(path: &Path) -> bool { + use std::os::unix::fs::FileTypeExt; + std::fs::symlink_metadata(path).is_ok_and(|metadata| metadata.file_type().is_socket()) +} diff --git a/Linux/crates/sw-host/src/service.rs b/Linux/crates/sw-host/src/service.rs new file mode 100644 index 00000000..f0698f50 --- /dev/null +++ b/Linux/crates/sw-host/src/service.rs @@ -0,0 +1,923 @@ +//! The service itself: one loop that owns all state. +//! +//! Threads and channels, no asynchronous runtime: the service does a handful +//! of things at human speed, and every one of them is a blocking call that a +//! thread expresses directly. +//! +//! - The **main loop** (this file) owns the widgets, the schedule and the +//! watchers. It handles one message at a time and never does anything +//! slow: no script, no drawing, no waiting for a client. +//! - The **worker** (`worker.rs`) runs scripts and draws, one widget at a time. +//! - The **socket** (`server.rs`) has a thread that accepts, and one thread +//! for each connection (two for a watcher). +//! - Small threads wait for signals, for logind and for the clock (`session.rs`). +//! +//! Between refreshes every thread sleeps in a blocking call: the main loop +//! in a wait for its next deadline, which is 15 minutes away when no widget +//! shows a clock. + +use std::path::{Path, PathBuf}; +use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender, SyncSender, channel}; +use std::sync::{Arc, Mutex}; +use std::thread::JoinHandle; +use std::time::Duration; + +use serde_json::{Map, Value, json}; +use sw_core::api::system::Screen; +use sw_core::library::Library; +use sw_core::{DataPaths, Resources, SharedScripts, WidgetRuntime}; +use sw_render::{HitRegion, WidgetRenderer}; + +use crate::clock::{Clock, SystemClock}; +use crate::frames::FrameStore; +use crate::launcher::{Launcher, LinkTarget, SystemLauncher, checked_link}; +use crate::paths::RuntimePaths; +use crate::placements::{DisplayMemory, MAXIMUM_SCREENS, Placement, Placements, ScreenInfo, Settings}; +use crate::protocol::{self, Refusal, Request}; +use crate::schedule; +use crate::server::{self, Acceptor, BindError, Inbound, Listening}; +use crate::session::SessionEvent; +use crate::worker::{Action, Job, Kept, Outcome, Worker, WorkerMessage, interaction_text}; + +/// A picture that was replaced stays on disk this long, so that a display that is still loading it finds it. +pub const DEFAULT_FRAME_GRACE: u64 = 5000; +/// The layout engine is freed when nothing was drawn for this long. +const RELEASE_AFTER: u64 = 2 * 60_000; +/// Waking from sleep is reported by more than one source; they count as one within this time. +const REFRESH_ALL_DEBOUNCE: u64 = 2000; +/// The wall clock moved against the steady one by more than this: it was set. +const CLOCK_JUMP: i64 = 5000; +/// Room for the JSON of the deepest element tree the runtime contract allows. +const WORKER_STACK: usize = 64 * 1024 * 1024; +/// More tappable areas than this in one picture are not told to the display. +pub const MAXIMUM_REGIONS: usize = 256; + +pub struct ServiceOptions { + pub runtime: RuntimePaths, + pub data: DataPaths, + pub resources: Resources, + pub clock: Arc, + pub launcher: Arc, + /// The only user whose connections are served; `None` is the user of this process. + pub expected_user: Option, + /// See [`DEFAULT_FRAME_GRACE`]. + pub frame_grace: u64, +} + +impl ServiceOptions { + pub fn new(runtime: RuntimePaths, data: DataPaths, resources: Resources) -> Self { + Self { runtime, data, resources, clock: Arc::new(SystemClock::new()), launcher: Arc::new(SystemLauncher), expected_user: None, frame_grace: DEFAULT_FRAME_GRACE } + } +} + +#[derive(Debug)] +pub enum StartError { + /// A service of this session already holds the lock. + AlreadyRunning, + Io(std::io::Error), +} + +impl std::fmt::Display for StartError { + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::AlreadyRunning => write!(formatter, "the ScriptWidget service is already running in this session"), + Self::Io(error) => write!(formatter, "{error}"), + } + } +} + +impl From for StartError { + fn from(error: std::io::Error) -> Self { + Self::Io(error) + } +} + +enum Message { + Client(Inbound), + Outcome(Box), + Session(SessionEvent), + /// Look at the clock again; for tests that move it by hand. + Poke, + Stop(String), +} + +/// Talks to a running service from other threads of the same process. +#[derive(Clone)] +pub struct Controller(Sender); + +impl Controller { + pub fn session(&self, event: SessionEvent) { + let _ = self.0.send(Message::Session(event)); + } + + pub fn poke(&self) { + let _ = self.0.send(Message::Poke); + } + + /// Asks the service to say `bye` with this reason and end. + pub fn stop(&self, reason: &str) { + let _ = self.0.send(Message::Stop(reason.to_owned())); + } +} + +/// A running service. +pub struct Service { + controller: Controller, + thread: Option>, + socket: PathBuf, +} + +impl Service { + /// Takes the lock, binds the socket, starts the threads and begins drawing the saved widgets. + pub fn start(options: ServiceOptions) -> Result { + let listening = match server::bind(&options.runtime) { + Ok(listening) => listening, + Err(BindError::AlreadyRunning) => return Err(StartError::AlreadyRunning), + Err(BindError::Io(error)) => return Err(StartError::Io(error)), + }; + let frames = Arc::new(FrameStore::new(options.runtime.frames())?); + let scripts = Arc::new(SharedScripts::new(&options.resources.runtime)?); + let runtime = WidgetRuntime::new(scripts.clone(), &options.data)?; + let renderer = WidgetRenderer::new(scripts)?; + let templates = options.resources.templates.clone(); + let worker = Worker::new(runtime, renderer, Library::new(&options.data, templates.clone()), frames.clone()); + + let (sender, messages) = channel::(); + let (jobs, job_queue) = channel::(); + let outcomes = sender.clone(); + std::thread::Builder::new().name("scriptwidget-worker".into()).stack_size(WORKER_STACK).spawn(move || { + worker.serve(job_queue, |outcome| { + let _ = outcomes.send(Message::Outcome(Box::new(outcome))); + }); + })?; + + let deliver = Mutex::new(sender.clone()); + let deliver: server::Deliver = Arc::new(move |inbound| deliver.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).send(Message::Client(inbound)).is_ok()); + // SAFETY: geteuid has no preconditions and cannot fail. + let user = options.expected_user.unwrap_or_else(|| unsafe { libc::geteuid() }); + let acceptor = Acceptor::start(listening.listener()?, listening.socket().to_owned(), deliver, user)?; + + let socket = listening.socket().to_owned(); + let display_file = options.data.cache().join("display.json"); + let mut host = Host { + clock: options.clock, + launcher: options.launcher, + frames, + library: Library::new(&options.data, templates), + placements: Placements::load(options.data.widgets()), + settings: Settings::new(options.data.settings()), + display: DisplayMemory::load(&display_file), + display_file, + widgets: Vec::new(), + arrange: false, + peek: false, + peek_before_arrange: None, + locked_by_logind: false, + locked_by_display: false, + watchers: Vec::new(), + jobs, + next_token: 1, + retired: Vec::new(), + frame_grace: options.frame_grace, + clock_offset: 0, + last_refresh_all: None, + last_job: 0, + renderer_live: false, + }; + let thread = std::thread::Builder::new().name("scriptwidget-host".into()).spawn(move || host.run(messages, acceptor, listening))?; + Ok(Self { controller: Controller(sender), thread: Some(thread), socket }) + } + + pub fn socket(&self) -> &Path { + &self.socket + } + + pub fn controller(&self) -> Controller { + self.controller.clone() + } + + /// Waits until the service has ended. + pub fn wait(mut self) { + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } + + /// Stops the service and waits until the socket is gone. + pub fn stop(self, reason: &str) { + self.controller.stop(reason); + self.wait(); + } +} + +impl Drop for Service { + fn drop(&mut self) { + if let Some(thread) = self.thread.take() { + self.controller.stop("shutdown"); + let _ = thread.join(); + } + } +} + +/// A tappable area of a published picture, in logical pixels from the widget's top-left corner. +struct Region { + x: i64, + y: i64, + width: i64, + height: i64, + /// What the shared renderer recorded for the element. It comes from the package; it is validated when it is used. + interaction: Map, +} + +/// The picture of a widget that the display has been told about. +struct Published { + version: u64, + hash: [u8; 32], + width: u32, + height: u32, + scale: f32, + /// The files in order with their durations; one entry for a picture that does not move. + frames: Vec<(PathBuf, u32)>, + regions: Arc>, +} + +struct Pending { + run: bool, + action: Option, +} + +struct Widget { + id: String, + version: u64, + published: Option, + /// The action tables of pictures that were replaced, for a tap that names its version, each with the time it is forgotten. + previous: Vec<(u64, Arc>, u64)>, + /// The token of the job that is running for this widget. + busy: Option, + /// What was asked for while that job ran. + pending: Option, + kept: Option>, + /// When the script runs again, on the steady clock. + next_run: Option, + /// When the same tree is drawn again because a date in it changed. + next_redraw: Option, + /// How often the script has run; for the list and for tests. + runs: u64, +} + +impl Widget { + fn new(id: &str) -> Self { + Self { id: id.to_owned(), version: 0, published: None, previous: Vec::new(), busy: None, pending: None, kept: None, next_run: None, next_redraw: None, runs: 0 } + } +} + +struct Host { + clock: Arc, + launcher: Arc, + frames: Arc, + library: Library, + placements: Placements, + settings: Settings, + display: DisplayMemory, + display_file: PathBuf, + widgets: Vec, + arrange: bool, + peek: bool, + /// What `peek` was before arranging turned it on; `None` when not arranging or when the user set it since. + peek_before_arrange: Option, + locked_by_logind: bool, + locked_by_display: bool, + watchers: Vec<(u64, SyncSender>)>, + jobs: Sender, + next_token: u64, + /// Files of replaced pictures, with the time they are deleted. + retired: Vec<(u64, Vec)>, + frame_grace: u64, + /// Wall clock minus steady clock when last looked at; a change means the wall clock was set. + clock_offset: i64, + last_refresh_all: Option, + last_job: u64, + /// True while the worker may hold a layout engine. + renderer_live: bool, +} + +fn ok() -> Value { + json!({ "ok": true }) +} + +fn regions_of(hit_regions: Vec, width: u32, height: u32) -> Vec { + // A display reads at most this many regions of a widget. + hit_regions + .into_iter() + .take(MAXIMUM_REGIONS) + .map(|region| { + let clamp = |value: f32, maximum: u32| (value as i64).clamp(0, i64::from(maximum)); + let (left, top) = (clamp(region.bounds.left.floor(), width), clamp(region.bounds.top.floor(), height)); + let (right, bottom) = (clamp(region.bounds.right.ceil(), width), clamp(region.bounds.bottom.ceil(), height)); + Region { x: left, y: top, width: (right - left).max(0), height: (bottom - top).max(0), interaction: region.interaction } + }) + .collect() +} + +impl Host { + fn locked(&self) -> bool { + self.locked_by_logind || self.locked_by_display + } + + fn widget(&mut self, id: &str) -> Option<&mut Widget> { + self.widgets.iter_mut().find(|widget| widget.id == id) + } + + /// The screen a placement is drawn for: its own when the display has reported it, otherwise the first one known. + fn screen_of(&self, placement: &Placement) -> ScreenInfo { + self.display.screens.get(&placement.screen).or_else(|| self.display.screens.values().next()).copied().unwrap_or(ScreenInfo { scale: 1000, width: 1920, height: 1080 }) + } + + // The loop + + fn run(&mut self, messages: Receiver, acceptor: Acceptor, listening: Listening) { + self.clock_offset = self.clock.wall() - self.clock.now() as i64; + self.last_job = self.clock.now(); + for placement in self.placements.all().to_vec() { + self.widgets.push(Widget::new(&placement.id)); + self.queue(&placement.id, true, None); + } + crate::log(&format!("listening on {}", listening.socket().display())); + let reason = loop { + self.on_time(); + let message = match self.next_deadline() { + Some(deadline) => match messages.recv_timeout(Duration::from_millis(deadline.saturating_sub(self.clock.now()).max(1))) { + Ok(message) => message, + Err(RecvTimeoutError::Timeout) => continue, + Err(RecvTimeoutError::Disconnected) => break "shutdown".to_owned(), + }, + None => match messages.recv() { + Ok(message) => message, + Err(_) => break "shutdown".to_owned(), + }, + }; + match message { + Message::Client(Inbound::Request { request, reply }) => { + let answer = self.handle(request).unwrap_or_else(|refusal| refusal.reply()); + let _ = reply.try_send(answer); + } + Message::Client(Inbound::Watch { watcher, events }) => self.add_watcher(watcher, events), + Message::Client(Inbound::WatcherGone { watcher }) => self.watchers.retain(|(id, _)| *id != watcher), + Message::Outcome(outcome) => self.apply(*outcome), + Message::Session(event) => self.session(event), + Message::Poke => {} + Message::Stop(reason) => break reason, + } + }; + + // Every watcher gets `bye` as its last line; the socket goes away before the process does. + self.broadcast(&protocol::bye(&reason)); + while acceptor.drained.try_recv().is_ok() {} + let watchers = std::mem::take(&mut self.watchers).len(); + let deadline = std::time::Instant::now() + Duration::from_secs(1); + for _ in 0..watchers { + if acceptor.drained.recv_timeout(deadline.saturating_duration_since(std::time::Instant::now())).is_err() { + break; + } + } + acceptor.stop(); + drop(listening); + let _ = self.frames.clear(); + crate::log(&format!("stopped ({reason})")); + } + + /// Does what is due now. + fn on_time(&mut self) { + let now = self.clock.now(); + // The wall clock was set while nothing reported it (the fallback where the kernel does not tell). + let offset = self.clock.wall() - now as i64; + if (offset - self.clock_offset).abs() > CLOCK_JUMP { + self.session(SessionEvent::ClockChanged); + } + let frames = self.frames.clone(); + self.retired.retain(|(at, files)| { + if *at <= now { + frames.remove(files); + } + *at > now + }); + for widget in &mut self.widgets { + widget.previous.retain(|(_, _, forgotten)| *forgotten > now); + } + // While the session is locked nothing runs and nothing is drawn. + if !self.locked() { + let due: Vec<(String, bool)> = self + .widgets + .iter() + .filter(|widget| widget.busy.is_none()) + .filter_map(|widget| { + if widget.next_run.is_some_and(|at| at <= now) { + Some((widget.id.clone(), true)) + } else if widget.next_redraw.is_some_and(|at| at <= now) { + Some((widget.id.clone(), false)) + } else { + None + } + }) + .collect(); + for (id, run) in due { + self.queue(&id, run, None); + } + } + if self.renderer_live && self.widgets.iter().all(|widget| widget.busy.is_none()) && now >= self.last_job + RELEASE_AFTER { + let _ = self.jobs.send(WorkerMessage::Release); + self.renderer_live = false; + } + } + + /// The next moment something is due, or `None` when only a message can bring work. + fn next_deadline(&self) -> Option { + let idle = self.widgets.iter().all(|widget| widget.busy.is_none()); + let retiring = self.retired.iter().map(|(at, _)| *at); + let forgetting = self.widgets.iter().flat_map(|widget| widget.previous.iter().map(|(_, _, forgotten)| *forgotten)); + let scheduled = self.widgets.iter().filter(|widget| widget.busy.is_none() && !self.locked()).flat_map(|widget| [widget.next_run, widget.next_redraw]).flatten(); + let release = (self.renderer_live && idle).then_some(self.last_job + RELEASE_AFTER); + retiring.chain(forgetting).chain(scheduled).chain(release).min() + } + + // Jobs + + /// Starts a job for a widget, or notes it for when the running one is done. + /// `run` runs the script; without it the last tree is drawn again. + fn queue(&mut self, id: &str, run: bool, action: Option) { + let Some(placement) = self.placements.find(id).cloned() else { return }; + let screen = self.screen_of(&placement); + let dark = self.display.dark; + let token = self.next_token; + let Some(widget) = self.widget(id) else { return }; + if widget.busy.is_some() { + let pending = widget.pending.get_or_insert(Pending { run: false, action: None }); + pending.run |= run || action.is_some(); + if action.is_some() { + pending.action = action; + } + return; + } + let run = run || action.is_some(); + if run { + widget.next_run = None; + } + widget.next_redraw = None; + widget.busy = Some(token); + let job = Job { + token, + id: id.to_owned(), + package: placement.package, + family: placement.family, + dark, + scale: screen.factor(), + screen: Screen { width: screen.width as f64, height: screen.height as f64, scale: f64::from(screen.factor()) }, + action, + kept: if run { None } else { widget.kept.clone() }, + current: widget.published.as_ref().map(|published| published.hash), + }; + self.next_token += 1; + self.renderer_live = true; + let _ = self.jobs.send(WorkerMessage::Job(Box::new(job))); + } + + /// Shows a finished job and schedules the next one. + fn apply(&mut self, outcome: Outcome) { + let now = self.clock.now(); + let wall = self.clock.wall(); + self.last_job = now; + let temporaries = |outcome: &Outcome| outcome.picture.as_ref().map(|picture| picture.frames.iter().map(|(file, _)| file.clone()).collect::>()).unwrap_or_default(); + if !self.widgets.iter().any(|widget| widget.id == outcome.id && widget.busy == Some(outcome.token)) { + // The widget was removed while its job ran. + self.frames.remove(&temporaries(&outcome)); + return; + } + let refresh_minutes = self.settings.refresh_minutes(); + let frames = self.frames.clone(); + let grace = self.frame_grace; + let mut retired = None; + let mut changed = false; + let Some(widget) = self.widget(&outcome.id) else { return }; + widget.busy = None; + if let Some(picture) = outcome.picture { + let version = widget.version + 1; + let moving = picture.frames.len() > 1; + let mut published = Vec::new(); + let mut failed = false; + for (index, (temporary, duration)) in picture.frames.iter().enumerate() { + let name = frames.published(&widget.id, version, moving.then_some(index)); + failed |= frames.publish(temporary, &name).is_err(); + published.push((name, *duration)); + } + if failed { + crate::log(&format!("the picture of widget {} could not be published", widget.id)); + frames.remove(&picture.frames.iter().map(|(file, _)| file.clone()).chain(published.iter().map(|(file, _)| file.clone())).collect::>()); + } else { + widget.version = version; + let regions = Arc::new(regions_of(picture.regions, picture.width, picture.height)); + let new = Published { version, hash: picture.hash, width: picture.width, height: picture.height, scale: picture.scale, frames: published, regions }; + if let Some(old) = widget.published.replace(new) { + widget.previous.push((old.version, old.regions, now + grace)); + retired = Some((now + grace, old.frames.into_iter().map(|(file, _)| file).collect::>())); + } + changed = true; + } + } + if let Some(kept) = outcome.kept { + if outcome.ran { + widget.runs += 1; + widget.next_run = Some(now + schedule::run_delay(refresh_minutes, kept.next_transition, wall)); + } + widget.next_redraw = kept.tick.map(|tick| now + schedule::tick_delay(tick, wall)); + widget.kept = Some(kept); + } + let pending = widget.pending.take(); + self.retired.extend(retired); + if changed && let Some(event) = self.widget_event(&outcome.id) { + self.broadcast(&event); + } + if let Some(pending) = pending { + self.queue(&outcome.id, pending.run, pending.action); + } + } + + fn refresh_all(&mut self) { + let ids: Vec = self.widgets.iter().map(|widget| widget.id.clone()).collect(); + for id in ids { + self.queue(&id, true, None); + } + } + + fn session(&mut self, event: SessionEvent) { + let was_locked = self.locked(); + let now = self.clock.now(); + match event { + SessionEvent::Locked(locked) => self.locked_by_logind = locked, + SessionEvent::DisplayLocked(locked) => self.locked_by_display = locked, + SessionEvent::Sleeping => {} + SessionEvent::Resumed | SessionEvent::ClockChanged => { + self.clock_offset = self.clock.wall() - now as i64; + // Everything may be stale: time passed that no timer counted, or the time itself changed. + // While locked the unlock does it. + if !self.locked() && self.last_refresh_all.is_none_or(|last| now >= last + REFRESH_ALL_DEBOUNCE) { + self.last_refresh_all = Some(now); + self.refresh_all(); + } + } + } + if was_locked && !self.locked() { + self.last_refresh_all = Some(now); + self.refresh_all(); + } + } + + // Watchers + + fn broadcast(&mut self, event: &Value) { + let line: Arc = Arc::from(event.to_string()); + // A watcher that is this far behind is dropped; its connection closes and the display connects again. + self.watchers.retain(|(_, events)| events.try_send(line.clone()).is_ok()); + } + + /// A watcher that connects later receives the whole current state before anything else. + fn add_watcher(&mut self, watcher: u64, events: SyncSender>) { + let mut lines = vec![protocol::hello(self.display.dark, self.arrange, self.peek)]; + lines.extend(self.widgets.iter().filter_map(|widget| self.widget_event(&widget.id))); + if lines.into_iter().all(|line| events.try_send(Arc::from(line.to_string())).is_ok()) { + self.watchers.push((watcher, events)); + } + } + + /// The `widget` event for the picture now published, or `None` when the widget has none yet. + fn widget_event(&self, id: &str) -> Option { + let placement = self.placements.find(id)?; + let widget = self.widgets.iter().find(|widget| widget.id == id)?; + let published = widget.published.as_ref()?; + let path = |file: &PathBuf| file.display().to_string(); + let mut event = json!({ + "event": "widget", + "id": id, + "screen": placement.screen, + "x": placement.x, + "y": placement.y, + "width": published.width, + "height": published.height, + "image": path(&published.frames.first()?.0), + "version": published.version, + "regions": published.regions.iter().enumerate().map(|(n, region)| json!({ "n": n, "x": region.x, "y": region.y, "width": region.width, "height": region.height })).collect::>(), + }); + if published.frames.len() > 1 { + event["frames"] = published.frames.iter().map(|(file, duration)| json!({ "image": path(file), "ms": duration })).collect(); + } + event["family"] = json!(placement.family); + let families = widget.kept.as_ref().map(|kept| kept.families.clone()).filter(|families| !families.is_empty()); + event["families"] = json!(families.unwrap_or_else(|| protocol::FAMILIES.iter().map(|family| (*family).to_owned()).collect())); + Some(event) + } + + // Requests + + fn handle(&mut self, request: Request) -> Result { + let unknown = || Refusal::new("widget.unknown", "No widget has this identifier."); + match request { + // The socket layer turns `watch` into a stream before it gets here. + Request::Watch => Err(Refusal::invalid("watch is not a request with a reply.")), + Request::Status => Ok(json!({ + "ok": true, + "protocol": protocol::PROTOCOL, + "service": env!("CARGO_PKG_VERSION"), + "pid": std::process::id(), + "widgets": self.widgets.len(), + "watchers": self.watchers.len(), + "dark": self.display.dark, + "arrange": self.arrange, + "peek": self.peek, + "locked": self.locked(), + "refreshMinutes": self.settings.refresh_minutes(), + "screens": self.display.screens.iter().map(|(name, screen)| (name.clone(), json!({ "scale": screen.scale, "width": screen.width, "height": screen.height }))).collect::>(), + })), + Request::Widgets => { + let widgets: Vec = self + .widgets + .iter() + .filter_map(|widget| { + let placement = self.placements.find(&widget.id)?; + let (width, height) = sw_render::family_size(&placement.family)?; + let kept = widget.kept.as_deref(); + Some(json!({ + "id": widget.id, + "package": kept.map(|kept| kept.name.clone()).filter(|name| !name.is_empty()), + "installation": placement.package, + "installed": kept.is_none_or(|kept| kept.installed), + "family": placement.family, + "screen": placement.screen, + "x": placement.x, + "y": placement.y, + "width": width as u32, + "height": height as u32, + "version": widget.version, + "image": widget.published.as_ref().and_then(|published| published.frames.first()).map(|(file, _)| file.display().to_string()), + "frames": widget.published.as_ref().map_or(0, |published| published.frames.len()), + "regions": widget.published.as_ref().map_or(0, |published| published.regions.len()), + "scale": widget.published.as_ref().map(|published| published.scale), + "runs": widget.runs, + "busy": widget.busy.is_some(), + "error": kept.and_then(|kept| kept.error.clone()), + })) + }) + .collect(); + Ok(json!({ "ok": true, "widgets": widgets })) + } + Request::Add { package, family } => { + let installed = self.library.find(&package).ok_or_else(|| Refusal::new("package.unknown", "No installed package has this name. scriptwidget list shows them."))?; + let families = installed.families(); + let family = match family { + Some(family) if families.contains(&family.as_str()) => family, + Some(_) => return Err(Refusal::new("family.unsupported", format!("This package supports: {}.", families.join(", ")))), + None => (*families.iter().find(|family| **family == "medium").or(families.first()).unwrap_or(&"medium")).to_owned(), + }; + let id = protocol::new_identifier().map_err(|_| Refusal::new("service.internal", "No random source."))?; + // Each new widget a little further down and right, so that they do not hide each other. + let step = 40 + 28 * (self.placements.all().len() as i64 % 8); + let placement = Placement { id: id.clone(), package: installed.installation.clone(), family: family.clone(), screen: String::new(), x: step, y: step }; + if self.placements.add(placement).is_none() { + return Err(Refusal::new("widgets.limit", "The desktop already holds the most widgets it can.")); + } + self.widgets.push(Widget::new(&id)); + self.queue(&id, true, None); + Ok(json!({ "ok": true, "id": id, "package": installed.name, "family": family })) + } + Request::Remove { id } => { + if !self.remove(&id) { + return Err(unknown()); + } + Ok(ok()) + } + Request::Refresh { id, package } => { + let ids: Vec = match (id, package) { + (Some(id), _) => vec![self.widget(&id).ok_or_else(unknown)?.id.clone()], + (None, Some(package)) => { + let installed = self.library.find(&package).ok_or_else(|| Refusal::new("package.unknown", "No installed package has this name."))?; + self.placements.all().iter().filter(|placement| placement.package == installed.installation).map(|placement| placement.id.clone()).collect() + } + (None, None) => self.widgets.iter().map(|widget| widget.id.clone()).collect(), + }; + for id in &ids { + self.queue(id, true, None); + } + Ok(json!({ "ok": true, "refreshed": ids.len() })) + } + Request::Peek(switch) => { + let peek = switch.apply(self.peek); + // Said during arranging, it is the user's choice and stays when arranging ends. + self.peek_before_arrange = None; + self.set_mode(self.arrange, peek); + Ok(json!({ "ok": true, "arrange": self.arrange, "peek": self.peek })) + } + Request::Arrange(switch) => { + let arrange = switch.apply(self.arrange); + // On the bottom layer a drag ends where the pointer reaches a window, so arranging raises the + // widgets above the windows; ending it puts them back where they were. + let mut peek = self.peek; + if arrange && !self.arrange { + self.peek_before_arrange = Some(self.peek); + peek = true; + } else if !arrange && self.arrange { + peek = self.peek_before_arrange.take().unwrap_or(self.peek); + } + self.set_mode(arrange, peek); + Ok(json!({ "ok": true, "arrange": self.arrange, "peek": self.peek })) + } + Request::Tap { id, n, version } => self.tap(&id, n as usize, version), + Request::Move { id, screen, x, y } => { + let family = self.placements.find(&id).ok_or_else(unknown)?.family.clone(); + let (mut x, mut y) = (x, y); + // On a screen whose size is known the widget stays on it; the display does the snapping. + if let (Some(known), Some((width, height))) = (self.display.screens.get(&screen), sw_render::family_size(&family)) { + x = x.clamp(0, (known.width - width as i64).max(0)); + y = y.clamp(0, (known.height - height as i64).max(0)); + } + self.placements.update(&id, |placement| { + placement.screen = screen.clone(); + placement.x = x; + placement.y = y; + }); + self.redraw_if_scale_changed(&id); + if let Some(event) = self.widget_event(&id) { + self.broadcast(&event); + } + Ok(json!({ "ok": true, "screen": screen, "x": x, "y": y })) + } + Request::Screen { name, scale, width, height } => { + if !self.display.screens.contains_key(&name) && self.display.screens.len() >= MAXIMUM_SCREENS { + return Err(Refusal::new("screens.limit", "Too many screens.")); + } + let screen = ScreenInfo { scale, width, height }; + if self.display.screens.insert(name, screen) != Some(screen) { + self.display.save(&self.display_file); + let ids: Vec = self.widgets.iter().map(|widget| widget.id.clone()).collect(); + for id in ids { + self.redraw_if_scale_changed(&id); + } + } + Ok(ok()) + } + Request::Theme { dark } => { + if self.display.dark != dark { + self.display.dark = dark; + self.display.save(&self.display_file); + self.refresh_all(); + } + Ok(json!({ "ok": true, "dark": dark })) + } + Request::Menu { id, word } => { + let placement = self.placements.find(&id).ok_or_else(unknown)?.clone(); + match word.as_str() { + "remove" => { + self.remove(&id); + } + "refresh" => self.queue(&id, true, None), + "edit" => { + // The name is looked up from the installation; nothing of the request is passed on. + let installed = self.library.find_by_installation(&placement.package).ok_or_else(|| Refusal::new("package.unknown", "The package of this widget is no longer installed."))?; + if !self.launcher.open_studio(Some(&installed.name)) { + return Err(Refusal::new("studio.unavailable", "scriptwidget-studio is not installed.")); + } + } + resize => { + let family = resize.strip_prefix("resize-").filter(|family| protocol::is_family(family)).ok_or_else(|| Refusal::invalid("Unknown menu word."))?; + if let Some(installed) = self.library.find_by_installation(&placement.package) + && !installed.families().contains(&family) + { + return Err(Refusal::new("family.unsupported", format!("This package supports: {}.", installed.families().join(", ")))); + } + if placement.family != family { + self.placements.update(&id, |placement| placement.family = family.to_owned()); + self.queue(&id, true, None); + } else if let Some(event) = self.widget_event(&id) { + // The display waits for an answer to a resize: the size it already has. + self.broadcast(&event); + } + } + } + Ok(ok()) + } + Request::Session { locked } => { + self.session(SessionEvent::DisplayLocked(locked)); + Ok(json!({ "ok": true, "locked": self.locked() })) + } + } + } + + fn set_mode(&mut self, arrange: bool, peek: bool) { + if (arrange, peek) != (self.arrange, self.peek) { + self.arrange = arrange; + self.peek = peek; + self.broadcast(&protocol::mode(arrange, peek)); + } + } + + fn redraw_if_scale_changed(&mut self, id: &str) { + let Some(placement) = self.placements.find(id) else { return }; + let wanted = self.screen_of(placement).factor(); + let drawn = self.widgets.iter().find(|widget| widget.id == id).and_then(|widget| widget.published.as_ref()).map(|published| published.scale); + if drawn.is_some_and(|drawn| (drawn - wanted).abs() > 0.001) { + self.queue(id, false, None); + } + } + + fn remove(&mut self, id: &str) -> bool { + if !self.placements.remove(id) { + return false; + } + let now = self.clock.now(); + if let Some(index) = self.widgets.iter().position(|widget| widget.id == id) { + let widget = self.widgets.remove(index); + if let Some(published) = widget.published { + self.retired.push((now + self.frame_grace, published.frames.into_iter().map(|(file, _)| file).collect())); + } + } + self.broadcast(&protocol::removed(id)); + true + } + + /// Runs what region `n` of a picture stands for. The number selects an + /// entry of the service's own table; nothing else of the request is used. + fn tap(&mut self, id: &str, n: usize, version: Option) -> Result { + let widget = self.widgets.iter().find(|widget| widget.id == id).ok_or_else(|| Refusal::new("widget.unknown", "No widget has this identifier."))?; + let published = widget.published.as_ref().ok_or_else(|| Refusal::new("region.unknown", "This widget has no picture yet."))?; + let regions = match version { + None => published.regions.clone(), + Some(version) if version == published.version => published.regions.clone(), + Some(version) => widget.previous.iter().find(|(previous, _, _)| *previous == version).map(|(_, regions, _)| regions.clone()).ok_or_else(|| Refusal::new("version.stale", "That picture is no longer on screen."))?, + }; + let interaction = ®ions.get(n).ok_or_else(|| Refusal::new("region.unknown", "This picture has no region with that number."))?.interaction; + let text = |name: &str| interaction_text(interaction, name); + match text("type").as_deref() { + Some("link") => match text("url").as_deref().and_then(checked_link) { + Some(LinkTarget::Open(url)) => { + if !self.launcher.open_link(&url) { + return Err(Refusal::new("link.unavailable", "The link could not be opened: xdg-open did not start.")); + } + Ok(json!({ "ok": true, "action": "link" })) + } + Some(LinkTarget::Application) => { + if !self.launcher.open_studio(None) { + return Err(Refusal::new("studio.unavailable", "scriptwidget-studio is not installed.")); + } + Ok(json!({ "ok": true, "action": "application" })) + } + None => Err(Refusal::new("link.refused", "Only http, https and mailto links are opened.")), + }, + Some("button") if text("action").is_some_and(|action| action.eq_ignore_ascii_case("reload")) => { + self.queue(id, true, None); + Ok(json!({ "ok": true, "action": "reload" })) + } + Some("button") => { + self.queue(id, true, Some(Action { action_id: text("actionID"), handler: text("handler"), state_key: None, value: None })); + Ok(json!({ "ok": true, "action": "button" })) + } + Some("toggle") => { + let on = interaction.get("on").and_then(Value::as_bool).unwrap_or(false); + self.queue(id, true, Some(Action { action_id: text("actionID"), handler: text("handler"), state_key: text("stateKey"), value: Some(!on) })); + Ok(json!({ "ok": true, "action": "toggle", "value": !on })) + } + _ => Err(Refusal::new("region.unknown", "This region does nothing.")), + } + } +} + +/// Runs the service for this session until it is told to stop: the body of +/// `scriptwidget host run`. Returns the exit code. +pub fn run_process(data: Option<&Path>) -> i32 { + let fail = |message: String| { + eprintln!("error: [service.start] {message}"); + 2 + }; + let runtime = match RuntimePaths::from_environment() { + Ok(runtime) => runtime, + Err(error) => return fail(error.to_string()), + }; + let data = match DataPaths::new(data) { + Ok(data) => data, + Err(error) => return fail(format!("The data folder cannot be used: {error}")), + }; + let resources = match Resources::locate() { + Ok(resources) => resources, + Err(error) => return fail(error.to_string()), + }; + let service = match Service::start(ServiceOptions::new(runtime, data, resources)) { + Ok(service) => service, + Err(error) => return fail(error.to_string()), + }; + let controller = service.controller(); + if let Err(error) = crate::session::on_termination(move || controller.stop("shutdown")) { + crate::log(&format!("signals cannot be handled: {error}")); + } + let controller = service.controller(); + crate::session::watch_logind(move |event| controller.session(event)); + let controller = service.controller(); + crate::session::watch_clock(move |event| controller.session(event)); + service.wait(); + 0 +} diff --git a/Linux/crates/sw-host/src/session.rs b/Linux/crates/sw-host/src/session.rs new file mode 100644 index 00000000..3ef1d5ad --- /dev/null +++ b/Linux/crates/sw-host/src/session.rs @@ -0,0 +1,286 @@ +//! What the session tells the service: locked and unlocked, going to sleep +//! and awake again, the wall clock set, and the request to stop. +//! +//! - **Lock and sleep** come from logind's D-Bus signals, read from a child +//! `gdbus monitor --system --dest org.freedesktop.login1` (an argument +//! vector, no shell). That needs no D-Bus library in this program; `gdbus` +//! is part of GLib, which every desktop has. Without it, lock is only known +//! when the display reports it (`scriptwidget session locked`), and +//! waking is still noticed through the clock, below. +//! - **The clock**: a timer on the realtime clock that the kernel cancels +//! whenever that clock is set. The kernel does the same when the machine +//! wakes from sleep, so this is also how waking is noticed when logind +//! says nothing. +//! - **Stopping**: `SIGTERM` and `SIGINT` write one byte to a pipe that a +//! thread reads; the handler does nothing else. +//! +//! None of these threads polls: each one sleeps in a read until there is news. + +use std::collections::BTreeSet; +use std::io::{BufRead, BufReader}; +use std::process::{Command, Stdio}; +use std::sync::atomic::{AtomicI32, Ordering}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum SessionEvent { + /// logind says the session is locked or unlocked. + Locked(bool), + /// The display says the session is locked or unlocked. + DisplayLocked(bool), + /// The machine is about to sleep. + Sleeping, + /// The machine woke up. + Resumed, + /// The wall clock was set, or the machine woke up. + ClockChanged, +} + +/// What one line of `gdbus monitor` says. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum LogindSignal { + /// `PrepareForSleep`: true before sleep, false after. + PrepareForSleep(bool), + /// `Lock`, `Unlock` or a change of `LockedHint` on the session with this identifier. + Lock { session: String, locked: bool }, +} + +/// The session identifier in a logind object path. systemd escapes every +/// byte that is not a letter or a digit, and a leading digit, as `_` and two +/// hexadecimal digits: session `2` is `/org/freedesktop/login1/session/_32`. +fn session_of(path: &str) -> Option { + let label = path.strip_prefix("/org/freedesktop/login1/session/")?; + let bytes = label.as_bytes(); + let mut text = Vec::new(); + let mut index = 0; + while index < bytes.len() { + if bytes[index] == b'_' { + let code = std::str::from_utf8(bytes.get(index + 1..index + 3)?).ok()?; + text.push(u8::from_str_radix(code, 16).ok()?); + index += 3; + } else if bytes[index].is_ascii_alphanumeric() { + text.push(bytes[index]); + index += 1; + } else { + return None; + } + } + String::from_utf8(text).ok().filter(|id| !id.is_empty() && id.len() <= 64) +} + +/// Reads one line of `gdbus monitor --system --dest org.freedesktop.login1`, such as +/// `/org/freedesktop/login1: org.freedesktop.login1.Manager.PrepareForSleep (false,)`. +pub fn parse_logind_line(line: &str) -> Option { + let (path, rest) = line.trim().split_once(": ")?; + let (member, arguments) = rest.split_once(' ').unwrap_or((rest, "")); + match member { + "org.freedesktop.login1.Manager.PrepareForSleep" if path == "/org/freedesktop/login1" => match arguments.trim() { + "(true,)" => Some(LogindSignal::PrepareForSleep(true)), + "(false,)" => Some(LogindSignal::PrepareForSleep(false)), + _ => None, + }, + "org.freedesktop.login1.Session.Lock" => Some(LogindSignal::Lock { session: session_of(path)?, locked: true }), + "org.freedesktop.login1.Session.Unlock" => Some(LogindSignal::Lock { session: session_of(path)?, locked: false }), + "org.freedesktop.DBus.Properties.PropertiesChanged" if arguments.starts_with("('org.freedesktop.login1.Session',") => { + let session = session_of(path)?; + if arguments.contains("'LockedHint': ") { + Some(LogindSignal::Lock { session, locked: true }) + } else if arguments.contains("'LockedHint': ") { + Some(LogindSignal::Lock { session, locked: false }) + } else { + None + } + } + _ => None, + } +} + +/// The logind sessions of this user, from `loginctl show-user --property=Sessions --value`. +fn own_sessions() -> BTreeSet { + // SAFETY: geteuid has no preconditions and cannot fail. + let user = unsafe { libc::geteuid() }; + let output = Command::new("loginctl").args(["show-user", &user.to_string(), "--property=Sessions", "--value"]).stdin(Stdio::null()).stderr(Stdio::null()).output(); + output.ok().filter(|output| output.status.success()).map(|output| String::from_utf8_lossy(&output.stdout).split_whitespace().map(str::to_owned).collect()).unwrap_or_default() +} + +/// Watches logind on a thread of its own and reports what concerns this +/// user's sessions. Returns at once; when `gdbus` is missing or ends, the +/// thread tries again a few times and then gives up, and the service runs on +/// without these signals. +pub fn watch_logind(report: impl Fn(SessionEvent) + Send + 'static) { + let _ = std::thread::Builder::new().name("scriptwidget-logind".into()).spawn(move || { + for attempt in 0..5 { + if attempt > 0 { + std::thread::sleep(std::time::Duration::from_secs(30)); + } + let mut command = Command::new("gdbus"); + command.args(["monitor", "--system", "--dest", "org.freedesktop.login1"]).stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::null()); + #[cfg(target_os = "linux")] + { + use std::os::unix::process::CommandExt; + // SAFETY: prctl is async-signal-safe and touches no memory of this process; it runs in the child before exec. + // The child ends when this thread does, so no monitor is left behind by a service that was killed. + unsafe { + command.pre_exec(|| { + libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM); + Ok(()) + }) + }; + } + let Ok(mut child) = command.spawn() else { + crate::log("gdbus is not installed: lock and sleep are not reported by logind"); + return; + }; + let Some(output) = child.stdout.take() else { return }; + let mut sessions = own_sessions(); + for line in BufReader::new(output).lines() { + let Ok(line) = line else { break }; + match parse_logind_line(&line) { + Some(LogindSignal::PrepareForSleep(true)) => report(SessionEvent::Sleeping), + Some(LogindSignal::PrepareForSleep(false)) => report(SessionEvent::Resumed), + Some(LogindSignal::Lock { session, locked }) => { + // A session that started after this service did is asked for once. + if !sessions.contains(&session) { + sessions = own_sessions(); + } + // Another user's session on the same machine is not ours to follow. + if sessions.contains(&session) { + report(SessionEvent::Locked(locked)); + } + } + None => {} + } + } + let _ = child.kill(); + let _ = child.wait(); + } + }); +} + +/// Reports every time the wall clock is set and every time the machine wakes +/// from sleep, on a thread of its own. Linux only; elsewhere the service +/// notices a jump of the wall clock the next time it wakes for something else. +#[cfg(target_os = "linux")] +pub fn watch_clock(report: impl Fn(SessionEvent) + Send + 'static) { + const TFD_TIMER_ABSTIME: libc::c_int = 1; + const TFD_TIMER_CANCEL_ON_SET: libc::c_int = 2; + let _ = std::thread::Builder::new().name("scriptwidget-clock".into()).spawn(move || { + // SAFETY: plain system calls on a descriptor this thread owns; every buffer is valid for its length. + unsafe { + let timer = libc::timerfd_create(libc::CLOCK_REALTIME, libc::TFD_CLOEXEC); + if timer < 0 { + return; + } + loop { + // A time that never comes: the read below only returns when the kernel cancels the timer. + let far = libc::itimerspec { it_interval: libc::timespec { tv_sec: 0, tv_nsec: 0 }, it_value: libc::timespec { tv_sec: libc::time_t::MAX / 2, tv_nsec: 0 } }; + if libc::timerfd_settime(timer, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &far, std::ptr::null_mut()) != 0 { + break; + } + let mut expirations = 0u64; + let read = libc::read(timer, (&raw mut expirations).cast(), 8); + if read < 0 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ECANCELED) { + report(SessionEvent::ClockChanged); + } else if read < 0 && std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted { + continue; + } else { + break; + } + } + libc::close(timer); + } + }); +} + +#[cfg(not(target_os = "linux"))] +pub fn watch_clock(_report: impl Fn(SessionEvent) + Send + 'static) {} + +static SIGNAL_PIPE: AtomicI32 = AtomicI32::new(-1); + +extern "C" fn on_signal(_signal: libc::c_int) { + let pipe = SIGNAL_PIPE.load(Ordering::Relaxed); + if pipe >= 0 { + // SAFETY: write is async-signal-safe; the descriptor stays open for the life of the process. + unsafe { libc::write(pipe, [1u8].as_ptr().cast(), 1) }; + } +} + +/// Calls `stop` once, on a thread of its own, when the process receives `SIGTERM` or `SIGINT`. +pub fn on_termination(stop: impl FnOnce() + Send + 'static) -> std::io::Result<()> { + let mut ends = [0 as libc::c_int; 2]; + // SAFETY: `ends` has room for the two descriptors; the handler only writes to a pipe. + unsafe { + if libc::pipe(ends.as_mut_ptr()) != 0 { + return Err(std::io::Error::last_os_error()); + } + for end in ends { + libc::fcntl(end, libc::F_SETFD, libc::FD_CLOEXEC); + } + SIGNAL_PIPE.store(ends[1], Ordering::SeqCst); + let handler = on_signal as extern "C" fn(libc::c_int) as *const () as libc::sighandler_t; + libc::signal(libc::SIGTERM, handler); + libc::signal(libc::SIGINT, handler); + } + let read_end = ends[0]; + std::thread::Builder::new().name("scriptwidget-signal".into()).spawn(move || { + let mut byte = [0u8; 1]; + loop { + // SAFETY: `byte` is valid for one byte; the descriptor is open. + let count = unsafe { libc::read(read_end, byte.as_mut_ptr().cast(), 1) }; + if count == 1 { + break; + } + if count < 0 && std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted { + continue; + } + return; + } + stop(); + })?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_the_lines_gdbus_prints() { + let lock = |session: &str, locked| Some(LogindSignal::Lock { session: session.into(), locked }); + assert_eq!(parse_logind_line("/org/freedesktop/login1: org.freedesktop.login1.Manager.PrepareForSleep (true,)"), Some(LogindSignal::PrepareForSleep(true))); + assert_eq!(parse_logind_line("/org/freedesktop/login1: org.freedesktop.login1.Manager.PrepareForSleep (false,)\n"), Some(LogindSignal::PrepareForSleep(false))); + assert_eq!(parse_logind_line("/org/freedesktop/login1/session/_32: org.freedesktop.login1.Session.Lock ()"), lock("2", true)); + assert_eq!(parse_logind_line("/org/freedesktop/login1/session/_32: org.freedesktop.login1.Session.Unlock ()"), lock("2", false)); + assert_eq!(parse_logind_line("/org/freedesktop/login1/session/c1: org.freedesktop.login1.Session.Lock ()"), lock("c1", true)); + assert_eq!(parse_logind_line("/org/freedesktop/login1/session/_3142: org.freedesktop.login1.Session.Lock ()"), lock("142", true)); + assert_eq!( + parse_logind_line("/org/freedesktop/login1/session/_34: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', {'LockedHint': }, @as [])"), + lock("4", true) + ); + assert_eq!( + parse_logind_line("/org/freedesktop/login1/session/_34: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', {'IdleHint': , 'LockedHint': }, @as [])"), + lock("4", false) + ); + } + + #[test] + fn other_lines_say_nothing() { + for line in [ + "", + "Monitoring signals from all objects owned by org.freedesktop.login1", + "The name org.freedesktop.login1 is owned by :1.4", + "/org/freedesktop/login1: org.freedesktop.login1.Manager.SessionNew ('7', objectpath '/org/freedesktop/login1/session/_37')", + "/org/freedesktop/login1: org.freedesktop.login1.Manager.PrepareForShutdown (true,)", + "/org/freedesktop/login1: org.freedesktop.login1.Manager.PrepareForSleep (maybe,)", + // The signal on an object that is not the manager, or not a session. + "/somewhere/else: org.freedesktop.login1.Manager.PrepareForSleep (true,)", + "/org/freedesktop/login1/seat/seat0: org.freedesktop.login1.Session.Lock ()", + "/org/freedesktop/login1/session/: org.freedesktop.login1.Session.Lock ()", + "/org/freedesktop/login1/session/_3: org.freedesktop.login1.Session.Lock ()", + "/org/freedesktop/login1/session/a/b: org.freedesktop.login1.Session.Lock ()", + "/org/freedesktop/login1/session/_34: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', {'IdleHint': }, @as [])", + "/org/freedesktop/login1/user/_1000: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.User', {'LockedHint': }, @as [])", + ] { + assert_eq!(parse_logind_line(line), None, "{line}"); + } + } +} diff --git a/Linux/crates/sw-host/src/surfaces.rs b/Linux/crates/sw-host/src/surfaces.rs new file mode 100644 index 00000000..a1ff15a3 --- /dev/null +++ b/Linux/crates/sw-host/src/surfaces.rs @@ -0,0 +1,255 @@ +//! The `actions` and `controls` a package manifest declares, read with the +//! limits of the shared manifest validator. Ported from +//! `Windows/src/ScriptWidget.Core/Surfaces/SurfaceManifest.cs`. +//! +//! A tap can select a declared action; it can never name an arbitrary +//! function. Anything malformed is an error: the service never runs code +//! from a manifest it does not fully accept. + +use serde_json::{Map, Value}; +use sw_core::packages::names::grapheme_count; + +pub const MAXIMUM_ACTIONS: usize = 16; +pub const MAXIMUM_CONTROLS: usize = 8; + +/// One entry of `actions`: a JavaScript function the system may run by its identifier. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct DeclaredAction { + pub id: String, + pub function: String, +} + +/// The validated `actions` of one manifest, and whether it may use storage. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct PackageSurfaces { + pub actions: Vec, + pub has_storage_permission: bool, +} + +impl PackageSurfaces { + /// The declared action with this identifier, compared without regard to case as on iOS. + pub fn action(&self, id: &str) -> Option<&DeclaredAction> { + let mut matches = self.actions.iter().filter(|action| action.id.eq_ignore_ascii_case(id)); + match (matches.next(), matches.next()) { + (Some(action), None) if !id.is_empty() => Some(action), + _ => None, + } + } +} + +/// The code of the shared validator's issue for what is wrong. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SurfaceError(pub String); + +fn matches(text: &str, first: impl Fn(u8) -> bool, rest: impl Fn(u8) -> bool, maximum: usize) -> bool { + let bytes = text.as_bytes(); + !bytes.is_empty() && bytes.len() <= maximum && first(bytes[0]) && bytes[1..].iter().all(|byte| rest(*byte)) +} + +/// `^[A-Za-z0-9][A-Za-z0-9.-]{0,63}$` +fn is_short_id(text: &str) -> bool { + matches(text, |byte| byte.is_ascii_alphanumeric(), |byte| byte.is_ascii_alphanumeric() || byte == b'.' || byte == b'-', 64) +} + +/// `^[A-Za-z0-9][A-Za-z0-9.-]{0,79}$` +fn is_symbol(text: &str) -> bool { + matches(text, |byte| byte.is_ascii_alphanumeric(), |byte| byte.is_ascii_alphanumeric() || byte == b'.' || byte == b'-', 80) +} + +/// True when `name` is a JavaScript function identifier a manifest or a control may name: `^[A-Za-z_$][A-Za-z0-9_$]{0,63}$`. +pub fn is_valid_function(name: &str) -> bool { + matches(name, |byte| byte.is_ascii_alphabetic() || byte == b'_' || byte == b'$', |byte| byte.is_ascii_alphanumeric() || byte == b'_' || byte == b'$', 64) +} + +/// True when `key` may hold a toggle's state in package storage: `^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$`. +pub fn is_valid_state_key(key: &str) -> bool { + matches(key, |byte| byte.is_ascii_alphanumeric(), |byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-'), 128) +} + +fn fail(code: &str) -> Result { + Err(SurfaceError(code.to_owned())) +} + +/// The objects of an optional array member; unknown keys and other shapes are refused. +fn objects<'a>(manifest: &'a Map, field: &str, allowed: &[&str]) -> Result>, SurfaceError> { + match manifest.get(field) { + None | Some(Value::Null) => Ok(Vec::new()), + Some(Value::Array(items)) => items + .iter() + .map(|item| match item { + Value::Object(object) if object.keys().all(|key| allowed.contains(&key.as_str())) => Ok(object), + Value::Object(_) => fail(&format!("unknown_{field}_fields")), + _ => fail(&format!("malformed_{field}")), + }) + .collect(), + Some(_) => fail(&format!("malformed_{field}")), + } +} + +fn required<'a>(item: &'a Map, key: &str, field: &str) -> Result<&'a str, SurfaceError> { + item.get(key).and_then(Value::as_str).ok_or_else(|| SurfaceError(format!("malformed_{field}"))) +} + +fn optional<'a>(item: &'a Map, key: &str, field: &str) -> Result, SurfaceError> { + match item.get(key) { + None | Some(Value::Null) => Ok(None), + Some(Value::String(text)) => Ok(Some(text)), + Some(_) => fail(&format!("malformed_{field}")), + } +} + +fn unique(ids: &[&str]) -> bool { + let mut lowered: Vec = ids.iter().map(|id| id.to_ascii_lowercase()).collect(); + lowered.sort(); + lowered.windows(2).all(|pair| pair[0] != pair[1]) +} + +/// Validates and returns the declared surfaces. No manifest (a legacy package) declares none. +pub fn parse(manifest: Option<&Map>) -> Result { + let Some(manifest) = manifest else { return Ok(PackageSurfaces::default()) }; + let storage = manifest.get("permissions").and_then(Value::as_array).is_some_and(|items| items.iter().any(|item| item.as_str() == Some("storage"))); + + let items = objects(manifest, "actions", &["id", "title", "description", "systemImage", "function"])?; + if items.len() > MAXIMUM_ACTIONS { + return fail("too_many_actions"); + } + let mut actions = Vec::new(); + for item in items { + let id = required(item, "id", "actions")?; + if !is_short_id(id) { + return fail("invalid_action_id"); + } + let title = required(item, "title", "actions")?; + if title.trim().is_empty() || grapheme_count(title) > 40 { + return fail("invalid_action_title"); + } + if optional(item, "description", "actions")?.is_some_and(|description| grapheme_count(description) > 160) { + return fail("invalid_action_description"); + } + if !is_symbol(required(item, "systemImage", "actions")?) { + return fail("invalid_action_image"); + } + let function = required(item, "function", "actions")?; + if !is_valid_function(function) { + return fail("invalid_action_function"); + } + actions.push(DeclaredAction { id: id.to_owned(), function: function.to_owned() }); + } + if !unique(&actions.iter().map(|action| action.id.as_str()).collect::>()) { + return fail("duplicate_actions"); + } + + // Linux shows no controls, but a manifest whose controls are malformed is not accepted in part. + let items = objects(manifest, "controls", &["id", "type", "title", "subtitle", "systemImage", "action", "actionID", "stateKey"])?; + if items.len() > MAXIMUM_CONTROLS { + return fail("too_many_controls"); + } + let mut control_ids = Vec::new(); + for item in items { + let id = required(item, "id", "controls")?; + if !is_short_id(id) { + return fail("invalid_control_id"); + } + let kind = required(item, "type", "controls")?; + if kind != "button" && kind != "toggle" { + return fail("malformed_controls"); + } + let title = required(item, "title", "controls")?; + if title.trim().is_empty() || grapheme_count(title) > 40 { + return fail("invalid_control_title"); + } + if optional(item, "subtitle", "controls")?.is_some_and(|subtitle| grapheme_count(subtitle) > 80) { + return fail("invalid_control_subtitle"); + } + if !is_symbol(required(item, "systemImage", "controls")?) { + return fail("invalid_control_image"); + } + match (optional(item, "action", "controls")?, optional(item, "actionID", "controls")?) { + (Some(_), Some(_)) => return fail("ambiguous_control_action"), + (Some(legacy), None) if !is_valid_function(legacy) => return fail("invalid_control_action"), + (Some(_), None) => {} + (None, Some(action)) if !actions.iter().any(|declared| declared.id.eq_ignore_ascii_case(action)) => return fail("missing_control_action"), + (None, Some(_)) => {} + (None, None) => return fail("missing_control_action"), + } + let key = optional(item, "stateKey", "controls")?; + if kind == "toggle" { + if !key.is_some_and(is_valid_state_key) { + return fail("invalid_control_state"); + } + if !storage { + return fail("control_storage"); + } + } else if key.is_some() { + return fail("button_control_state"); + } + control_ids.push(id); + } + if !unique(&control_ids) { + return fail("duplicate_controls"); + } + Ok(PackageSurfaces { actions, has_storage_permission: storage }) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn manifest(text: &str) -> Map { + serde_json::from_str(text).unwrap() + } + + fn code(text: &str) -> String { + parse(Some(&manifest(text))).expect_err(text).0 + } + + const ACTION: &str = r#"{"id":"count.up","title":"Count","systemImage":"plus.circle","function":"countUp"}"#; + + #[test] + fn reads_declared_actions() { + let surfaces = parse(Some(&manifest(&format!(r#"{{"permissions":["storage"],"actions":[{ACTION}]}}"#)))).unwrap(); + assert!(surfaces.has_storage_permission); + assert_eq!(surfaces.action("COUNT.UP").map(|action| action.function.as_str()), Some("countUp")); + assert_eq!(surfaces.action("other"), None); + assert_eq!(surfaces.action(""), None); + assert_eq!(parse(None).unwrap(), PackageSurfaces::default()); + assert_eq!(parse(Some(&manifest("{}"))).unwrap(), PackageSurfaces::default()); + } + + #[test] + fn a_malformed_declaration_refuses_the_whole_manifest() { + assert_eq!(code(r#"{"actions":{}}"#), "malformed_actions"); + assert_eq!(code(r#"{"actions":["x"]}"#), "malformed_actions"); + assert_eq!(code(r#"{"actions":[{"id":"a","title":"A","systemImage":"x","function":"f","extra":1}]}"#), "unknown_actions_fields"); + assert_eq!(code(r#"{"actions":[{"id":"-a","title":"A","systemImage":"x","function":"f"}]}"#), "invalid_action_id"); + assert_eq!(code(r#"{"actions":[{"id":"a","title":" ","systemImage":"x","function":"f"}]}"#), "invalid_action_title"); + assert_eq!(code(r#"{"actions":[{"id":"a","title":"A","systemImage":"x y","function":"f"}]}"#), "invalid_action_image"); + assert_eq!(code(r#"{"actions":[{"id":"a","title":"A","systemImage":"x","function":"f()"}]}"#), "invalid_action_function"); + assert_eq!(code(r#"{"actions":[{"id":"a","title":"A","systemImage":"x","function":"a.b"}]}"#), "invalid_action_function"); + assert_eq!(code(r#"{"actions":[{"id":"a","title":"A","systemImage":"x"}]}"#), "malformed_actions"); + assert_eq!(code(&format!(r#"{{"actions":[{ACTION},{}]}}"#, ACTION.replace("count.up", "Count.Up"))), "duplicate_actions"); + let many = vec![ACTION; 17].iter().enumerate().map(|(index, action)| action.replace("count.up", &format!("a{index}"))).collect::>().join(","); + assert_eq!(code(&format!(r#"{{"actions":[{many}]}}"#)), "too_many_actions"); + // Controls are not shown on Linux, but they are part of what is accepted. + assert_eq!(code(&format!(r#"{{"actions":[{ACTION}],"controls":[{{"id":"c","type":"toggle","title":"T","systemImage":"x","actionID":"count.up","stateKey":"k"}}]}}"#)), "control_storage"); + assert_eq!(code(r#"{"controls":[{"id":"c","type":"button","title":"T","systemImage":"x","actionID":"nothing"}]}"#), "missing_control_action"); + assert_eq!(code(r#"{"controls":[{"id":"c","type":"slider","title":"T","systemImage":"x","action":"f"}]}"#), "malformed_controls"); + assert!(parse(Some(&manifest(&format!(r#"{{"permissions":["storage"],"actions":[{ACTION}],"controls":[{{"id":"c","type":"toggle","title":"T","systemImage":"x","actionID":"count.up","stateKey":"k"}}]}}"#)))).is_ok()); + } + + #[test] + fn function_names_and_state_keys() { + for name in ["f", "_private", "$x", "countUp2"] { + assert!(is_valid_function(name), "{name}"); + } + for name in ["", "2x", "a.b", "a b", "f()", "eval('x')", &"a".repeat(65)] { + assert!(!is_valid_function(name), "{name}"); + } + for key in ["k", "lights.on", "a_b-c"] { + assert!(is_valid_state_key(key), "{key}"); + } + for key in ["", ".k", "a/b", "a b", &"k".repeat(129)] { + assert!(!is_valid_state_key(key), "{key}"); + } + } +} diff --git a/Linux/crates/sw-host/src/worker.rs b/Linux/crates/sw-host/src/worker.rs new file mode 100644 index 00000000..f002c839 --- /dev/null +++ b/Linux/crates/sw-host/src/worker.rs @@ -0,0 +1,345 @@ +//! The one thread that runs package scripts and draws pictures, one widget +//! at a time, as the refresh thread of the Windows host does +//! (`Windows/src/ScriptWidget.App/AppHost.cs`). The service's main loop hands +//! it jobs and gets outcomes back; it never waits for it, so a slow script +//! or a slow download holds up neither the socket nor the other requests. +//! +//! Package code runs here, inside this process, in a fresh QuickJS context +//! with bounded memory, stack and time (plan, section 6). + +use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::Arc; +use std::sync::mpsc::Receiver; + +use serde_json::{Map, Value}; +use sha2::{Digest, Sha256}; +use sw_core::api::system::Screen; +use sw_core::executor::ExecutionError; +use sw_core::library::{InstalledPackage, Library}; +use sw_core::{PackageFiles, RunRequest, WidgetRuntime}; +use sw_render::gif_cache::FrameCacheScope; +use sw_render::{HitRegion, RenderError, RenderOutput, RenderRequest, WidgetRenderer, rounded_corners}; + +use crate::frames::FrameStore; +use crate::schedule::{self, Tick}; +use crate::surfaces; + +/// A widget shows at most this many pictures in a cycle (plan 4.8). +pub const MAXIMUM_GIF_FRAMES: usize = sw_render::gif::MAXIMUM_FRAMES; +/// All pictures of one cycle together have at most this many pixels; a widget that would need more shows its first frame only. +pub const MAXIMUM_CYCLE_PIXELS: u64 = 32 * 1024 * 1024; + +/// What a tap asked for. Built by the service from its own action table, never from a request. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Action { + /// A declared action of the manifest (`actions[].id`). + pub action_id: Option, + /// The legacy `onClick="name"` function of the element. + pub handler: Option, + /// For a toggle: the storage key that holds its state, and the new state. + pub state_key: Option, + pub value: Option, +} + +/// What a finished run left behind: enough to draw the same tree again, at +/// another scale or a second later, without running the script. +pub struct Kept { + pub family: String, + pub dark: bool, + tree: String, + /// False when `tree` is a card of the service (an error, a missing package). + own_tree: bool, + files: Option>, + remote_images: HashMap, + /// When the package's timeline next changes slot, in milliseconds since 1970. + pub next_transition: Option, + pub tick: Option, + /// The error kind of the run, when it failed. + pub error: Option, + /// False when the package is no longer installed. + pub installed: bool, + /// The package's name and the sizes it supports, for the list and the menu. + pub name: String, + pub families: Vec, +} + +pub struct Job { + /// Unique in this run of the service; names the temporary files. + pub token: u64, + pub id: String, + /// Installation identity of the package. + pub package: String, + pub family: String, + pub dark: bool, + pub scale: f32, + pub screen: Screen, + /// Run this before the script runs again. + pub action: Option, + /// Draw this again instead of running the script, when it still fits the family and theme. + pub kept: Option>, + /// The picture now on screen; a job that produces the same one publishes nothing. + pub current: Option<[u8; 32]>, +} + +pub struct Picture { + pub hash: [u8; 32], + /// Logical size. + pub width: u32, + pub height: u32, + pub scale: f32, + /// The temporary files, in order, each with how long it is shown; one entry for a picture that does not move. + pub frames: Vec<(PathBuf, u32)>, + pub regions: Vec, +} + +pub struct Outcome { + pub token: u64, + pub id: String, + pub kept: Option>, + /// True when the script ran (and not only the old tree was drawn again). + pub ran: bool, + /// `None` when the picture is the one already on screen, or when nothing could be drawn. + pub picture: Option, +} + +pub enum WorkerMessage { + Job(Box), + /// Frees what only drawing needs; the next job creates it again. + Release, +} + +pub struct Worker { + runtime: WidgetRuntime, + renderer: WidgetRenderer, + library: Library, + frames: Arc, +} + +impl Worker { + pub fn new(runtime: WidgetRuntime, renderer: WidgetRenderer, library: Library, frames: Arc) -> Self { + Self { runtime, renderer, library, frames } + } + + /// Serves jobs until the sender is dropped. + pub fn serve(self, jobs: Receiver, finished: impl Fn(Outcome)) { + for message in jobs { + match message { + WorkerMessage::Job(job) => finished(self.execute(*job)), + WorkerMessage::Release => self.renderer.release(), + } + } + } + + fn execute(&self, job: Job) -> Outcome { + let reusable = job.action.is_none(); + let mut kept = job.kept.clone().filter(|kept| reusable && kept.installed && kept.family == job.family && kept.dark == job.dark); + let ran = kept.is_none(); + if kept.is_none() { + kept = Some(Arc::new(self.run(&job))); + } + let picture = kept.as_deref().and_then(|kept| match self.picture(&job, kept) { + Ok(picture) => picture, + Err(error) => { + crate::log(&format!("widget {} could not be drawn: {}", job.id, error.message)); + None + } + }); + Outcome { token: job.token, id: job.id, kept, ran, picture } + } + + /// Runs the tapped action, then the script. + fn run(&self, job: &Job) -> Kept { + let Some(installed) = self.library.find_by_installation(&job.package) else { + return Kept { + family: job.family.clone(), + dark: job.dark, + tree: WidgetRuntime::notice_tree("Widget not found", "Its package was removed. Remove this widget from the desktop."), + own_tree: false, + files: None, + remote_images: HashMap::new(), + next_transition: None, + tick: None, + error: None, + installed: false, + name: String::new(), + families: Vec::new(), + }; + }; + let files: Arc = Arc::new(installed.files()); + let request = |files: &Arc| RunRequest { + family: job.family.clone(), + dark_mode: job.dark, + build_directory: Some(installed.build_directory()), + screen: job.screen, + ..RunRequest::new(files.clone(), installed.installation.clone()) + }; + if let Some(action) = &job.action { + self.perform(&installed, action, request(&files)); + } + let request = request(&files); + let result = self.runtime.run(&request); + if let Some(error) = &result.execution.error { + // The kind only: a message may quote what the script handled. + crate::log(&format!("widget {}: {}", job.id, error.kind)); + } + let remote_images = match (&result.execution.tree, &result.remote_images) { + (Some(tree), Some(scope)) => self.runtime.fetch_remote_images(tree, scope), + _ => HashMap::new(), + }; + let (tree, own_tree) = match result.execution.tree { + Some(tree) => (tree, true), + None => (self.runtime.error_tree(result.execution.error.as_ref()), false), + }; + Kept { + family: job.family.clone(), + dark: job.dark, + tick: if own_tree { schedule::tick_of(&tree) } else { None }, + tree, + own_tree, + files: Some(files), + remote_images, + next_transition: result.next_timeline_transition, + error: result.execution.error.map(|error| error.kind), + installed: true, + name: installed.name.clone(), + families: installed.families().iter().map(|family| (*family).to_owned()).collect(), + } + } + + /// Runs what a tap selected. Ported from `AppHost.Perform` of the Windows host. + fn perform(&self, installed: &InstalledPackage, action: &Action, mut request: RunRequest) { + // A manifest that is present but unreadable fails closed: nothing is stored and nothing runs. + if installed.manifest_present && installed.manifest.is_none() { + crate::log("action refused: the manifest cannot be read"); + return; + } + let mut environments = vec![("action-source".to_owned(), "widget".to_owned())]; + if let Some(value) = action.value { + let text = if value { "true" } else { "false" }; + // The toggle's new state is saved first, then offered to the action as action-value. + if let Some(key) = action.state_key.as_deref().filter(|key| surfaces::is_valid_state_key(key)) + && installed.permissions().iter().any(|permission| permission == "storage") + { + self.runtime.storage().set_string(&installed.installation, key, text); + } + environments.push(("action-value".to_owned(), text.to_owned())); + environments.push(("control-value".to_owned(), text.to_owned())); + } + if let Some(id) = action.action_id.as_deref().filter(|id| !id.is_empty()) { + // Only an action the manifest declares can be selected by a tap. + let declared = match surfaces::parse(installed.manifest.as_ref()) { + Ok(surfaces) => surfaces.action(id).cloned(), + Err(error) => { + crate::log(&format!("action refused: {}", error.0)); + return; + } + }; + let Some(declared) = declared else { + crate::log("action refused: not declared in the manifest"); + return; + }; + environments.push(("action-id".to_owned(), declared.id.clone())); + request.action_id = Some(declared.id); + } else if let Some(function) = action.handler.as_deref().filter(|function| surfaces::is_valid_function(function)) { + request.call_function = Some(function.to_owned()); + } else { + return; + } + request.extra_environments = environments; + let result = self.runtime.run(&request); + crate::log(&format!("action of {}: {}", installed.installation, result.execution.error.map_or("ok".to_owned(), |error| error.kind))); + } + + /// One frame of `kept`: the package's tree, or a card that says why it could not be drawn. + fn draw(&self, kept: &Kept, scale: f32, gif_time: Option) -> Result { + let (width, height) = sw_render::family_size(&kept.family).unwrap_or((329.0, 170.0)); + let request = |tree: String| RenderRequest { family: kept.family.clone(), dark_mode: kept.dark, gif_time_milliseconds: gif_time, ..RenderRequest::new(tree, width, height, scale) }; + let drawn = self.renderer.render(RenderRequest { files: kept.files.clone(), remote_images: kept.remote_images.clone(), ..request(kept.tree.clone()) }); + let mut output = match drawn { + Ok(output) => output, + Err(error) if kept.own_tree => { + let message = if error.message.is_empty() { "Rendering failed".to_owned() } else { error.message }; + self.renderer.render(request(self.runtime.error_tree(Some(&ExecutionError { kind: "internalError".into(), message }))))? + } + Err(error) => return Err(error), + }; + rounded_corners::apply(&mut output.pixels, output.pixel_width as usize, output.pixel_height as usize, rounded_corners::DEFAULT_RADIUS * output.scale); + Ok(output) + } + + /// Draws every frame and writes the PNG files, unless the result is the picture already on screen. + fn picture(&self, job: &Job, kept: &Kept) -> Result, RenderError> { + let first = self.draw(kept, job.scale, None)?; + let (width, height) = sw_render::family_size(&kept.family).unwrap_or((329.0, 170.0)); + let mut digest = Sha256::new(); + digest.update(first.pixel_width.to_le_bytes()); + digest.update(first.pixel_height.to_le_bytes()); + digest.update(job.scale.to_le_bytes()); + for region in &first.hit_regions { + for edge in [region.bounds.left, region.bounds.top, region.bounds.right, region.bounds.bottom] { + digest.update(edge.to_le_bytes()); + } + digest.update(Value::Object(region.interaction.clone()).to_string().as_bytes()); + digest.update([0u8]); + } + + // The moments at which one of the GIFs changes; empty when nothing moves. + let mut sources: Vec<&str> = Vec::new(); + let mut delays: Vec> = Vec::new(); + for slot in &first.gif_slots { + if !sources.contains(&slot.source.as_str()) { + sources.push(&slot.source); + delays.push(slot.delays.clone()); + } + } + let mut moments = schedule::gif_frames(&delays, MAXIMUM_GIF_FRAMES); + if moments.len() as u64 * u64::from(first.pixel_width) * u64::from(first.pixel_height) > MAXIMUM_CYCLE_PIXELS { + moments.clear(); + } + + let fail = |error: std::io::Error| RenderError { message: format!("the picture could not be written: {error}") }; + if moments.len() < 2 { + digest.update(&first.pixels); + let hash: [u8; 32] = digest.finalize().into(); + if job.current == Some(hash) { + return Ok(None); + } + let file = self.frames.temporary(job.token, 0); + self.frames.write(&file, &first.encode_png()?).map_err(fail)?; + return Ok(Some(Picture { hash, width: width as u32, height: height as u32, scale: first.scale, frames: vec![(file, 0)], regions: first.hit_regions })); + } + + // Each frame is hashed, encoded and written before the next one is drawn, so only one bitmap is held at a time. + let _cache = FrameCacheScope::new(); + let mut files: Vec<(PathBuf, u32)> = Vec::new(); + let written = (|| { + for (index, (start, duration)) in moments.iter().enumerate() { + let output = if index == 0 { None } else { Some(self.draw(kept, job.scale, Some(*start))?) }; + let output = output.as_ref().unwrap_or(&first); + digest.update(&output.pixels); + digest.update(duration.to_le_bytes()); + let file = self.frames.temporary(job.token, index); + self.frames.write(&file, &output.encode_png()?).map_err(fail)?; + files.push((file, *duration)); + } + Ok(()) + })(); + let discard = |files: &[(PathBuf, u32)]| self.frames.remove(&files.iter().map(|(file, _)| file.clone()).collect::>()); + if let Err(error) = written { + discard(&files); + return Err(error); + } + let hash: [u8; 32] = digest.finalize().into(); + if job.current == Some(hash) { + discard(&files); + return Ok(None); + } + Ok(Some(Picture { hash, width: width as u32, height: height as u32, scale: first.scale, frames: files, regions: first.hit_regions })) + } +} + +/// The text member `name` of a region's interaction, when it is a non-empty string. +pub fn interaction_text(interaction: &Map, name: &str) -> Option { + interaction.get(name).and_then(Value::as_str).filter(|text| !text.is_empty()).map(str::to_owned) +} diff --git a/Linux/crates/sw-host/tests/service.rs b/Linux/crates/sw-host/tests/service.rs new file mode 100644 index 00000000..2250bfca --- /dev/null +++ b/Linux/crates/sw-host/tests/service.rs @@ -0,0 +1,831 @@ +//! Runs a whole service inside the test process, on a runtime directory and +//! a data directory of its own, and talks to it through its real socket. +//! +//! node Linux/scripts/build-shared-js.mjs +//! cargo test -p sw-host +//! +//! Nothing here opens a window, starts a browser or touches the user's own +//! service: links go to a launcher that only records them. + +use std::io::{BufRead, BufReader, Write}; +use std::os::unix::fs::{DirBuilderExt, MetadataExt}; +use std::os::unix::net::{UnixListener, UnixStream}; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use serde_json::{Value, json}; +use sw_core::library::Library; +use sw_core::{DataPaths, Resources}; +use sw_host::{Clock, Launcher, ManualClock, Request, RuntimePaths, Service, ServiceOptions, SessionEvent, StartError, Switch, SystemClock, client}; + +const WAIT: Duration = Duration::from_secs(30); +/// 2026-01-01 12:00:20 UTC. +const START: i64 = 1_767_268_820_000; + +#[derive(Default)] +struct Recorder { + links: Mutex>, + studio: Mutex>>, +} + +impl Launcher for Recorder { + fn open_link(&self, url: &str) -> bool { + self.links.lock().unwrap().push(url.to_owned()); + true + } + + fn open_studio(&self, package: Option<&str>) -> bool { + self.studio.lock().unwrap().push(package.map(str::to_owned)); + true + } +} + +/// One user's session: a runtime directory and a data directory, removed afterwards. +struct Session { + root: PathBuf, + runtime: RuntimePaths, + data: DataPaths, + launcher: Arc, + manual: Arc, + /// How long a replaced picture stays, in milliseconds. + grace: u64, +} + +impl Session { + fn new() -> Self { + static NEXT: AtomicU64 = AtomicU64::new(0); + // Short: the path of a socket is limited to about a hundred bytes. + let root = PathBuf::from("/tmp").join(format!("swh-{}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::SeqCst))); + let _ = std::fs::remove_dir_all(&root); + std::fs::DirBuilder::new().mode(0o700).create(&root).unwrap(); + std::fs::DirBuilder::new().mode(0o700).create(root.join("run")).unwrap(); + let data = DataPaths::new(Some(&root.join("data"))).unwrap(); + Self { runtime: RuntimePaths::under(&root.join("run")), data, root, launcher: Arc::default(), manual: Arc::new(ManualClock::new(START)), grace: 300 } + } + + fn options(&self) -> ServiceOptions { + let repository = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.."); + let resources = Resources { runtime: repository.join("Linux/assets/scriptwidget"), templates: None }; + let mut options = ServiceOptions::new(self.runtime.clone(), self.data.clone(), resources); + options.launcher = self.launcher.clone(); + options.frame_grace = self.grace; + options + } + + /// A service on the real clock. + fn start(&self) -> Service { + let mut options = self.options(); + options.clock = Arc::new(SystemClock::new()); + Service::start(options).expect("the service starts") + } + + /// A service whose clock only moves when the test moves it. + fn start_manual(&self) -> Service { + let mut options = self.options(); + options.clock = self.manual.clone(); + Service::start(options).expect("the service starts") + } + + fn library(&self) -> Library { + Library::new(&self.data, None) + } + + /// Installs a package with a Package 2.0 manifest; `extra` adds members to it. + fn install(&self, name: &str, source: &str, permissions: &str, extra: &str) { + let manifest = format!( + r#"{{"formatVersion":2,"id":"com.example.{}","name":"{name}","version":"1.0.0","runtimeVersion":"1.0","entry":"main.jsx","supportedFamilies":["systemSmall","systemMedium","systemLarge"],"permissions":[{permissions}],"networkDomains":[]{extra}}}"#, + name.to_lowercase().replace(' ', "-") + ); + self.library().create(name, source, Some(&manifest)).expect("the package installs"); + } + + fn socket(&self) -> PathBuf { + self.runtime.socket() + } + + fn ask(&self, request: Request) -> Value { + client::request(&self.socket(), &request).unwrap_or_else(|error| panic!("{request:?} failed: {error:?}")) + } + + fn refused(&self, request: Request) -> String { + match client::request(&self.socket(), &request) { + Err(client::ClientError::Refused { code, .. }) => code, + other => panic!("{request:?} should have been refused: {other:?}"), + } + } + + fn widgets(&self) -> Vec { + self.ask(Request::Widgets)["widgets"].as_array().cloned().unwrap() + } + + fn widget(&self, id: &str) -> Value { + self.widgets().into_iter().find(|widget| widget["id"] == id).unwrap_or_else(|| panic!("no widget {id}")) + } + + /// Waits until the widget satisfies `done`, and returns it. + #[track_caller] + fn until(&self, id: &str, what: &str, done: impl Fn(&Value) -> bool) -> Value { + let deadline = Instant::now() + WAIT; + loop { + let widget = self.widget(id); + if done(&widget) { + return widget; + } + assert!(Instant::now() < deadline, "timed out waiting for {what}: {widget}"); + std::thread::sleep(Duration::from_millis(20)); + } + } + + /// Adds a widget and waits for its first picture. + #[track_caller] + fn add(&self, package: &str, family: Option<&str>) -> String { + let id = self.ask(Request::Add { package: package.into(), family: family.map(str::to_owned) })["id"].as_str().unwrap().to_owned(); + self.until(&id, "the first picture", |widget| widget["version"].as_u64() >= Some(1) && widget["busy"] == false); + id + } + + /// Waits until the widget's script has run `runs` times and nothing is running. + #[track_caller] + fn runs(&self, id: &str, runs: u64) -> Value { + self.until(id, &format!("{runs} runs"), |widget| widget["runs"].as_u64() >= Some(runs) && widget["busy"] == false) + } + + fn frame_files(&self) -> Vec { + let mut names: Vec = std::fs::read_dir(self.runtime.frames()).unwrap().map(|entry| entry.unwrap().file_name().into_string().unwrap()).collect(); + names.sort(); + names + } +} + +impl Drop for Session { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.root); + } +} + +/// A connection that reads one JSON object a line. +struct Connection { + stream: UnixStream, + reader: BufReader, +} + +impl Connection { + fn open(socket: &Path) -> Self { + let stream = UnixStream::connect(socket).expect("the socket accepts"); + stream.set_read_timeout(Some(WAIT)).unwrap(); + Self { reader: BufReader::new(stream.try_clone().unwrap()), stream } + } + + fn watch(socket: &Path) -> Self { + let mut connection = Self::open(socket); + connection.send("{\"op\":\"watch\"}"); + connection + } + + fn send(&mut self, line: &str) { + // In one piece: the service may answer and close as soon as it has seen enough of a line. + self.stream.write_all(format!("{line}\n").as_bytes()).unwrap(); + } + + /// The next line, or `None` when the connection has ended. + fn line(&mut self) -> Option { + let mut line = String::new(); + match self.reader.read_line(&mut line) { + Ok(count) if count > 0 => Some(serde_json::from_str(&line).unwrap_or_else(|error| panic!("not JSON ({error}): {line}"))), + _ => None, + } + } + + #[track_caller] + fn next(&mut self) -> Value { + self.line().expect("another line") + } + + /// Reads events until one satisfies `wanted`. + #[track_caller] + fn event(&mut self, wanted: impl Fn(&Value) -> bool) -> Value { + loop { + let event = self.line().expect("the stream ended before the event came"); + if wanted(&event) { + return event; + } + } + } + + fn code(&mut self, line: &str) -> String { + self.send(line); + let reply = self.next(); + assert_eq!(reply["ok"], false, "{line} -> {reply}"); + reply["error"]["code"].as_str().unwrap().to_owned() + } +} + +fn png_size(path: &Path) -> (u32, u32) { + let bytes = std::fs::read(path).unwrap_or_else(|error| panic!("{}: {error}", path.display())); + assert_eq!(&bytes[..8], b"\x89PNG\r\n\x1a\n", "{} is a PNG", path.display()); + let number = |at: usize| u32::from_be_bytes(bytes[at..at + 4].try_into().unwrap()); + (number(16), number(20)) +} + +fn image(event: &Value) -> PathBuf { + PathBuf::from(event["image"].as_str().expect("an image path")) +} + +/// A GIF of solid frames from a palette of black, red, green and blue; delays in hundredths of a second. +fn gif(width: u16, height: u16, frames: &[(u8, u16)]) -> Vec { + let mut data = Vec::new(); + data.extend_from_slice(b"GIF89a"); + data.extend_from_slice(&width.to_le_bytes()); + data.extend_from_slice(&height.to_le_bytes()); + data.extend_from_slice(&[0x91, 0, 0]); + data.extend_from_slice(&[0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255]); + data.extend_from_slice(&[0x21, 0xFF, 0x0B]); + data.extend_from_slice(b"NETSCAPE2.0"); + data.extend_from_slice(&[3, 1, 0, 0, 0]); + for (color, delay) in frames { + data.extend_from_slice(&[0x21, 0xF9, 4, 0]); + data.extend_from_slice(&delay.to_le_bytes()); + data.extend_from_slice(&[0, 0]); + data.push(0x2C); + data.extend_from_slice(&[0, 0, 0, 0]); + data.extend_from_slice(&width.to_le_bytes()); + data.extend_from_slice(&height.to_le_bytes()); + data.push(0); + data.push(2); + let mut bits: Vec = Vec::new(); + let mut push = |code: u8| (0..3).for_each(|bit| bits.push(code >> bit & 1 == 1)); + for _ in 0..usize::from(width) * usize::from(height) { + push(4); + push(*color); + } + push(5); + let bytes: Vec = bits.chunks(8).map(|chunk| chunk.iter().enumerate().fold(0u8, |byte, (bit, set)| byte | (u8::from(*set) << bit))).collect(); + for block in bytes.chunks(255) { + data.push(block.len() as u8); + data.extend_from_slice(block); + } + data.push(0); + } + data.push(0x3B); + data +} + +const PLAIN: &str = r##"$render({$getenv("widget-size") + ($device.isdarkmode() ? " dark" : " light")});"##; + +/// A counter in storage: one button calls a function of the script, one names a declared action, +/// one names an action the manifest does not declare, one reloads; then a toggle and three links. +const CONTROLS: &str = r##" +function bump() { $storage.setString("count", String(Number($storage.getString("count") || "0") + 1)); } +function secret() { $storage.setString("count", "999"); } +const count = $storage.getString("count") || "0"; +const flag = $storage.getString("flag") === "true"; +$render( + + + + + + flag + web + file + mail + +); +function noop() {} +"##; + +const CONTROLS_ACTIONS: &str = r#","actions":[{"id":"bump.declared","title":"Bump","systemImage":"plus.circle","function":"bump"}]"#; + +fn install_controls(session: &Session) { + session.install("Controls", CONTROLS, "\"storage\"", CONTROLS_ACTIONS); +} + +#[test] +fn a_watcher_gets_hello_the_widgets_and_every_change() { + let session = Session::new(); + session.install("Plain", PLAIN, "", ""); + let service = session.start(); + assert_eq!(std::fs::metadata(session.socket()).unwrap().mode() & 0o777, 0o600); + assert_eq!(std::fs::metadata(session.runtime.directory()).unwrap().mode() & 0o777, 0o700); + assert_eq!(std::fs::metadata(session.runtime.frames()).unwrap().mode() & 0o777, 0o700); + + let mut first = Connection::watch(&session.socket()); + assert_eq!(first.next(), json!({ "event": "hello", "protocol": 1, "dark": false, "arrange": false, "peek": false, "service": env!("CARGO_PKG_VERSION") })); + + let added = session.ask(Request::Add { package: "Plain".into(), family: None }); + assert_eq!((added["package"].as_str(), added["family"].as_str()), (Some("Plain"), Some("medium"))); + let id = added["id"].as_str().unwrap().to_owned(); + assert!(sw_host::protocol::is_identifier(&id)); + + let widget = first.next(); + assert_eq!(widget["event"], "widget"); + assert_eq!(widget["id"], id.as_str()); + assert_eq!((widget["width"].as_u64(), widget["height"].as_u64(), widget["version"].as_u64()), (Some(329), Some(170), Some(1))); + assert_eq!((widget["screen"].as_str(), widget["x"].as_i64(), widget["y"].as_i64()), (Some(""), Some(40), Some(40))); + assert_eq!(widget["regions"], json!([])); + assert!(widget.get("frames").is_none(), "a picture that does not move has no frames"); + assert_eq!(widget["family"], "medium"); + assert_eq!(widget["families"], json!(["small", "medium", "large"])); + let picture = image(&widget); + assert!(picture.is_absolute() && picture.starts_with(session.runtime.frames())); + assert_eq!(png_size(&picture), (329, 170), "drawn at scale 1 until a screen says otherwise"); + assert_eq!(std::fs::metadata(&picture).unwrap().mode() & 0o777, 0o600); + assert!(picture.file_name().unwrap().to_str().unwrap().starts_with(&format!("{id}-"))); + + // A watcher that connects later gets the whole state first. + let mut second = Connection::watch(&session.socket()); + assert_eq!(second.next()["event"], "hello"); + assert_eq!(second.next(), widget); + assert_eq!(session.ask(Request::Status)["watchers"], 2); + + // Modes are service state, told to every watcher. + // Arranging raises the widgets above the windows and puts them back afterwards. + let answer = session.ask(Request::Arrange(Switch::On)); + assert_eq!((answer["arrange"].as_bool(), answer["peek"].as_bool()), (Some(true), Some(true))); + session.ask(Request::Arrange(Switch::On)); + session.ask(Request::Arrange(Switch::Toggle)); + session.ask(Request::Peek(Switch::Toggle)); + session.ask(Request::Arrange(Switch::Toggle)); + for watcher in [&mut first, &mut second] { + assert_eq!(watcher.next(), json!({ "event": "mode", "arrange": true, "peek": true })); + assert_eq!(watcher.next(), json!({ "event": "mode", "arrange": false, "peek": false }), "saying on twice is one change; peek is as it was before"); + assert_eq!(watcher.next(), json!({ "event": "mode", "arrange": false, "peek": true })); + assert_eq!(watcher.next(), json!({ "event": "mode", "arrange": true, "peek": true })); + } + let mut third = Connection::watch(&session.socket()); + assert_eq!(third.next(), json!({ "event": "hello", "protocol": 1, "dark": false, "arrange": true, "peek": true, "service": env!("CARGO_PKG_VERSION") })); + drop(third); + // Peek that was on before arranging stays on after it; peek set while arranging is the user's choice. + session.ask(Request::Arrange(Switch::Off)); + session.ask(Request::Arrange(Switch::On)); + session.ask(Request::Peek(Switch::Off)); + session.ask(Request::Arrange(Switch::Off)); + for watcher in [&mut first, &mut second] { + assert_eq!(watcher.next(), json!({ "event": "mode", "arrange": false, "peek": true })); + assert_eq!(watcher.next(), json!({ "event": "mode", "arrange": true, "peek": true })); + assert_eq!(watcher.next(), json!({ "event": "mode", "arrange": true, "peek": false })); + assert_eq!(watcher.next(), json!({ "event": "mode", "arrange": false, "peek": false })); + } + + // A move is told with the picture unchanged. + let moved = session.ask(Request::Move { id: id.clone(), screen: "DP-1".into(), x: -30, y: 500 }); + assert_eq!((moved["x"].as_i64(), moved["y"].as_i64()), (Some(-30), Some(500)), "an unknown screen has no edges to keep to"); + let event = first.next(); + assert_eq!((event["screen"].as_str(), event["x"].as_i64(), event["y"].as_i64(), event["version"].as_u64()), (Some("DP-1"), Some(-30), Some(500), Some(1))); + assert_eq!(event["image"], widget["image"]); + assert_eq!(second.next(), event); + + session.ask(Request::Remove { id: id.clone() }); + for watcher in [&mut first, &mut second] { + assert_eq!(watcher.next(), json!({ "event": "remove", "id": id })); + } + assert_eq!(session.refused(Request::Remove { id: id.clone() }), "widget.unknown"); + assert!(session.widgets().is_empty()); + + service.stop("shutdown"); + for watcher in [&mut first, &mut second] { + assert_eq!(watcher.next(), json!({ "event": "bye", "reason": "shutdown" })); + assert!(watcher.line().is_none(), "bye is the last line"); + } + assert!(!session.socket().exists(), "no socket is left behind"); + assert!(session.frame_files().is_empty(), "no picture is left behind"); +} + +#[test] +fn placements_survive_a_restart() { + let session = Session::new(); + session.install("Plain", PLAIN, "", ""); + session.install("Other", PLAIN, "", ""); + let service = session.start(); + let one = session.add("Plain", Some("small")); + let two = session.add("Other", None); + session.ask(Request::Screen { name: "eDP-1".into(), scale: 2000, width: 1440, height: 900 }); + session.ask(Request::Theme { dark: true }); + let moved = session.ask(Request::Move { id: one.clone(), screen: "eDP-1".into(), x: 5000, y: -7 }); + assert_eq!((moved["x"].as_i64(), moved["y"].as_i64()), (Some(1440 - 170), Some(0)), "kept on a screen whose size is known"); + session.ask(Request::Menu { id: two.clone(), word: "resize-large".into() }); + let before = session.until(&two, "the large picture", |widget| widget["height"] == 345 && widget["busy"] == false && widget["scale"] == 2.0); + let old_picture = before["image"].as_str().unwrap().to_owned(); + service.stop("shutdown"); + + let saved: Value = serde_json::from_str(&std::fs::read_to_string(session.data.widgets()).unwrap()).unwrap(); + assert_eq!(saved["version"], 1); + assert_eq!(saved["widgets"][0], json!({ "id": one, "package": session.library().find("Plain").unwrap().installation, "family": "small", "screen": "eDP-1", "x": 1270, "y": 0 })); + assert_eq!(std::fs::metadata(session.data.widgets()).unwrap().mode() & 0o777, 0o600); + + let service = session.start(); + let mut watcher = Connection::watch(&session.socket()); + assert_eq!(watcher.next()["dark"], true, "the theme is remembered"); + let widgets: Vec = [watcher.event(|event| event["event"] == "widget"), watcher.event(|event| event["event"] == "widget")].into(); + let found = |id: &str| widgets.iter().find(|widget| widget["id"] == id).unwrap_or_else(|| panic!("{id} is back")).clone(); + let (first, second) = (found(&one), found(&two)); + assert_eq!((first["screen"].as_str(), first["x"].as_i64(), first["y"].as_i64(), first["family"].as_str()), (Some("eDP-1"), Some(1270), Some(0), Some("small"))); + assert_eq!((second["family"].as_str(), second["width"].as_u64(), second["height"].as_u64()), (Some("large"), Some(329), Some(345))); + assert_eq!(png_size(&image(&first)), (340, 340), "the screen's scale is remembered too"); + assert_ne!(second["image"].as_str().unwrap(), old_picture, "a picture's path is never used twice, also across restarts"); + assert_eq!(session.widgets().len(), 2); + drop(service); +} + +#[test] +fn a_tap_runs_the_action_and_the_picture_changes() { + let mut session = Session::new(); + // Long enough for the tap below that names the picture before the current one. + session.grace = 2500; + install_controls(&session); + let _service = session.start(); + let mut watcher = Connection::watch(&session.socket()); + let id = session.add("Controls", Some("large")); + let first = watcher.event(|event| event["event"] == "widget"); + let regions = first["regions"].as_array().unwrap(); + assert_eq!(regions.len(), 8, "{first}"); + for (n, region) in regions.iter().enumerate() { + assert_eq!(region["n"], n); + assert!(region["width"].as_i64() > Some(0) && region["height"].as_i64() > Some(0), "{region}"); + assert!(region["x"].as_i64() >= Some(0) && region["x"].as_i64().unwrap() + region["width"].as_i64().unwrap() <= 329, "{region}"); + assert_eq!(region.as_object().unwrap().len(), 5, "a region is a number and a rectangle, nothing of the package: {region}"); + } + let storage = |key: &str| sw_core::api::StorageModule::new(session.data.storage()).get_string(&session.library().find("Controls").unwrap().installation, key); + + // The button with a function of the script. + assert_eq!(session.ask(Request::Tap { id: id.clone(), n: 0, version: None })["action"], "button"); + let second = watcher.event(|event| event["event"] == "widget"); + assert_eq!(second["version"], 2); + assert_ne!(second["image"], first["image"], "a new picture has a new path"); + assert_ne!(std::fs::read(image(&second)).unwrap(), std::fs::read(image(&first)).unwrap_or_default()); + assert_eq!(storage("count"), "1"); + + // The button with a declared action, tapped on the picture before: its table is still known. + assert_eq!(session.ask(Request::Tap { id: id.clone(), n: 1, version: Some(1) })["action"], "button"); + assert_eq!(watcher.event(|event| event["event"] == "widget")["version"], 3); + assert_eq!(storage("count"), "2"); + + // An action the manifest does not declare runs nothing; the script still runs again and draws the same picture. + let runs = session.widget(&id)["runs"].as_u64().unwrap(); + session.ask(Request::Tap { id: id.clone(), n: 2, version: None }); + let after = session.runs(&id, runs + 1); + assert_eq!(storage("count"), "2", "secret() was not called"); + assert_eq!(after["version"], 3, "the same picture is not published again"); + + // Reload runs the script again. + assert_eq!(session.ask(Request::Tap { id: id.clone(), n: 3, version: None })["action"], "reload"); + assert_eq!(session.runs(&id, runs + 2)["version"], 3); + + // The toggle saves its new state, then the picture is green. + let answer = session.ask(Request::Tap { id: id.clone(), n: 4, version: None }); + assert_eq!((answer["action"].as_str(), answer["value"].as_bool()), (Some("toggle"), Some(true))); + let toggled = watcher.event(|event| event["event"] == "widget"); + assert_eq!(toggled["version"], 4); + assert_eq!(storage("flag"), "true"); + let answer = session.ask(Request::Tap { id: id.clone(), n: 4, version: None }); + assert_eq!(answer["value"], false, "the table of the new picture knows the toggle is on"); + assert_eq!(watcher.event(|event| event["event"] == "widget")["version"], 5); + assert_eq!(storage("flag"), "false"); + + // Numbers that select nothing. + assert_eq!(session.refused(Request::Tap { id: id.clone(), n: 8, version: None }), "region.unknown"); + assert_eq!(session.refused(Request::Tap { id: id.clone(), n: 0, version: Some(99) }), "version.stale"); + assert_eq!(session.refused(Request::Tap { id: "0123456789abcdef".into(), n: 0, version: None }), "widget.unknown"); + + // Replaced pictures leave the directory after the grace time; the current one stays. + let current = session.widget(&id)["image"].as_str().unwrap().to_owned(); + let deadline = Instant::now() + WAIT; + while session.frame_files().len() > 1 { + assert!(Instant::now() < deadline, "old pictures are deleted: {:?}", session.frame_files()); + std::thread::sleep(Duration::from_millis(50)); + } + assert_eq!(session.frame_files(), [Path::new(¤t).file_name().unwrap().to_str().unwrap()]); + assert!(!image(&first).exists()); + assert_eq!(session.refused(Request::Tap { id, n: 0, version: Some(1) }), "version.stale", "and their tables are forgotten with them"); +} + +#[test] +fn links_open_only_web_and_mail_addresses() { + let session = Session::new(); + install_controls(&session); + let _service = session.start(); + let id = session.add("Controls", Some("large")); + let tap = |n: u32| Request::Tap { id: id.clone(), n, version: None }; + + assert_eq!(session.ask(tap(5))["action"], "link"); + assert_eq!(*session.launcher.links.lock().unwrap(), ["https://example.com/a%20page"], "the canonical form of the parsed address"); + assert_eq!(session.refused(tap(6)), "link.refused"); + assert_eq!(session.launcher.links.lock().unwrap().len(), 1, "a file address starts nothing"); + assert_eq!(session.ask(tap(7))["action"], "link"); + assert_eq!(session.launcher.links.lock().unwrap().last().map(String::as_str), Some("mailto:someone@example.com")); + assert_eq!(session.widget(&id)["runs"], 1, "a link does not run the script"); + + // The menu: edit names the installed package by its own name; the words are a fixed list. + session.ask(Request::Menu { id: id.clone(), word: "edit".into() }); + assert_eq!(*session.launcher.studio.lock().unwrap(), [Some("Controls".to_owned())]); + session.ask(Request::Menu { id: id.clone(), word: "refresh".into() }); + session.runs(&id, 2); + assert_eq!(session.refused(Request::Menu { id: id.clone(), word: "resize-extraLarge".into() }), "family.unsupported"); + let mut raw = Connection::open(&session.socket()); + assert_eq!(raw.code(&format!("{{\"op\":\"menu\",\"id\":\"{id}\",\"word\":\"run\"}}")), "request.invalid"); + session.ask(Request::Menu { id: id.clone(), word: "remove".into() }); + assert!(session.widgets().is_empty()); +} + +#[test] +fn what_is_not_a_request_is_refused() { + let session = Session::new(); + session.install("Plain", PLAIN, "", ""); + let _service = session.start(); + let id = session.add("Plain", None); + + let mut connection = Connection::open(&session.socket()); + for line in ["hello", "{}", "[1,2]", "{\"op\":\"shell\",\"command\":\"id\"}", "{\"op\":\"add\",\"package\":\"../../etc\"}", "{\"op\":\"remove\",\"id\":\"../../../tmp/x\"}", "{\"op\":\"tap\",\"id\":\"0123456789abcdef\",\"n\":\"0\"}"] { + assert_eq!(connection.code(line), "request.invalid", "{line}"); + } + assert_eq!(connection.code("{\"op\":\"tap\",\"id\":\"0123456789abcdef\",\"n\":0}"), "widget.unknown"); + assert_eq!(connection.code("{\"op\":\"add\",\"package\":\"Nothing Here\"}"), "package.unknown"); + assert_eq!(connection.code("{\"op\":\"add\",\"package\":\"Plain\",\"family\":\"extraLarge\"}"), "family.unsupported"); + // The eighth line that is not a request at all ends the connection. + assert_eq!(connection.code("x"), "request.invalid"); + assert!(connection.line().is_none()); + + // A connection keeps working after a refusal, and serves several requests. + let mut connection = Connection::open(&session.socket()); + assert_eq!(connection.code("nonsense"), "request.invalid"); + connection.send("{\"op\":\"status\"}"); + assert_eq!(connection.next()["ok"], true); + connection.send("{\"op\":\"widgets\"}"); + assert_eq!(connection.next()["widgets"][0]["id"], id.as_str()); + + // A line without an end is cut off at the limit. + let mut connection = Connection::open(&session.socket()); + connection.stream.write_all(&[b'a'; 5000]).unwrap(); + let reply = connection.next(); + assert_eq!(reply["error"]["code"], "request.too_long"); + assert!(connection.line().is_none(), "and the connection is closed"); + + // A long line that does end is refused as well. + let mut connection = Connection::open(&session.socket()); + assert_eq!(connection.code(&format!("{{\"op\":\"add\",\"package\":\"{}\"}}", "b".repeat(4200))), "request.too_long"); + + // Bytes that are not text. + let mut connection = Connection::open(&session.socket()); + connection.stream.write_all(&[0xff, 0xfe, 0x00, 0x80, b'\n']).unwrap(); + assert_eq!(connection.next()["error"]["code"], "request.invalid"); + assert!(connection.line().is_none()); + + // What a watcher sends after `watch` is ignored. + let mut watcher = Connection::watch(&session.socket()); + assert_eq!(watcher.next()["event"], "hello"); + assert_eq!(watcher.next()["event"], "widget"); + watcher.send("{\"op\":\"remove\",\"id\":\"0123456789abcdef\"}"); + watcher.send(&"z".repeat(10_000)); + session.ask(Request::Peek(Switch::On)); + assert_eq!(watcher.next()["event"], "mode"); + + // Nothing above disturbed the service. + assert_eq!(session.widgets().len(), 1); + assert_eq!(session.widget(&id)["version"], 1); +} + +#[test] +fn a_connection_of_another_user_is_refused() { + let session = Session::new(); + let mut options = session.options(); + // The kernel reports this process's user for the test's own connection; the service is told to expect someone else. + // SAFETY: geteuid has no preconditions and cannot fail. + options.expected_user = Some(unsafe { libc::geteuid() } + 1); + let _service = Service::start(options).unwrap(); + let mut connection = Connection::open(&session.socket()); + let reply = connection.next(); + assert_eq!(reply["error"]["code"], "peer.refused", "refused before anything is read"); + assert!(connection.line().is_none()); + assert!(matches!(client::request(&session.socket(), &Request::Status), Err(client::ClientError::Refused { code, .. }) if code == "peer.refused")); +} + +#[test] +fn one_service_a_session_and_a_stale_socket_is_replaced() { + let session = Session::new(); + let service = session.start(); + assert!(matches!(Service::start(session.options()), Err(StartError::AlreadyRunning))); + assert!(session.socket().exists(), "the refused start left the running service's socket alone"); + assert_eq!(session.ask(Request::Status)["ok"], true); + service.stop("shutdown"); + assert!(!session.socket().exists()); + assert!(matches!(client::request(&session.socket(), &Request::Status), Err(client::ClientError::NotRunning))); + + // What a crashed service leaves: a socket file nobody listens on, and pictures. + drop(UnixListener::bind(session.socket()).unwrap()); + assert!(session.socket().exists()); + std::fs::write(session.runtime.frames().join("0123456789abcdef-deadbeef-7.png"), b"old").unwrap(); + assert!(matches!(client::request(&session.socket(), &Request::Status), Err(client::ClientError::NotRunning))); + let _service = session.start(); + assert_eq!(session.ask(Request::Status)["ok"], true); + assert!(session.frame_files().is_empty(), "pictures of an earlier run are removed"); +} + +#[test] +fn no_more_connections_than_the_limit() { + let session = Session::new(); + let _service = session.start(); + let mut held: Vec = (0..sw_host::server::MAXIMUM_CONNECTIONS).map(|_| Connection::open(&session.socket())).collect(); + // Each of them is served. + held[0].send("{\"op\":\"status\"}"); + assert_eq!(held[0].next()["ok"], true); + let deadline = Instant::now() + WAIT; + loop { + // One too many is told so at once, without having asked anything. + let mut extra = Connection::open(&session.socket()); + extra.stream.set_read_timeout(Some(Duration::from_millis(500))).unwrap(); + if let Some(reply) = extra.line() { + assert_eq!(reply["error"]["code"], "service.busy"); + break; + } + // The accepting thread had not yet counted every held connection. + assert!(Instant::now() < deadline, "the limit is enforced"); + } + held.clear(); + let deadline = Instant::now() + WAIT; + while client::request(&session.socket(), &Request::Status).is_err() { + assert!(Instant::now() < deadline, "closed connections make room again"); + std::thread::sleep(Duration::from_millis(20)); + } +} + +#[test] +fn a_gif_becomes_frames_drawn_by_the_service() { + let session = Session::new(); + session.install("Animated", r##"$render(gif);"##, "", ""); + session.library().write_file("Animated", "image/spin.gif", &gif(8, 8, &[(1, 20), (2, 30), (3, 50)])).unwrap(); + session.install("Still", r##"$render();"##, "", ""); + session.library().write_file("Still", "image/one.gif", &gif(8, 8, &[(2, 10)])).unwrap(); + let _service = session.start(); + let mut watcher = Connection::watch(&session.socket()); + let id = session.add("Animated", Some("small")); + let event = watcher.event(|event| event["event"] == "widget"); + let frames = event["frames"].as_array().unwrap_or_else(|| panic!("frames: {event}")); + assert_eq!(frames.iter().map(|frame| frame["ms"].as_u64().unwrap()).collect::>(), [200, 300, 500]); + assert_eq!(frames[0]["image"], event["image"], "the picture is the first frame"); + let pictures: Vec> = frames.iter().map(|frame| std::fs::read(image(frame)).unwrap()).collect(); + for (index, frame) in frames.iter().enumerate() { + assert_eq!(png_size(&image(frame)), (170, 170), "every frame is the whole widget"); + assert!(image(frame).file_name().unwrap().to_str().unwrap().ends_with(&format!("-1-{index}.png"))); + } + assert!(pictures[0] != pictures[1] && pictures[1] != pictures[2] && pictures[0] != pictures[2], "red, green and blue"); + assert_eq!(session.frame_files().len(), 3); + assert_eq!(session.widget(&id)["frames"], 3); + + // Run again: the same frames are not published a second time. + session.ask(Request::Refresh { id: Some(id.clone()), package: None }); + assert_eq!(session.runs(&id, 2)["version"], 1); + assert_eq!(session.frame_files().len(), 3, "and the frames that were drawn to compare are gone"); + + // A GIF of one frame is a picture like any other. + let still = session.add("Still", Some("small")); + let event = watcher.event(|event| event["event"] == "widget" && event["id"] == still.as_str()); + assert!(event.get("frames").is_none()); +} + +#[test] +fn scripts_run_on_schedule() { + let session = Session::new(); + session.install("Plain", PLAIN, "", ""); + let service = session.start_manual(); + let controller = service.controller(); + let clock = session.manual.clone(); + let id = session.add("Plain", None); + assert_eq!(session.widget(&id)["runs"], 1); + assert_eq!(session.ask(Request::Status)["refreshMinutes"], 15); + // Moving the clock and looking again, with time for a run to start if one were due. + let pass = |milliseconds: u64| { + clock.advance(milliseconds); + controller.poke(); + std::thread::sleep(Duration::from_millis(150)); + }; + + pass(14 * 60_000); + assert_eq!(session.runs(&id, 1)["runs"], 1, "not before the interval"); + pass(60_000 + 1); + assert_eq!(session.runs(&id, 2)["runs"], 2, "every 15 minutes"); + assert_eq!(session.widget(&id)["version"], 1, "the same picture is not published again"); + pass(15 * 60_000 + 1); + session.runs(&id, 3); + + // The setting is read again when the file changes: between 5 minutes and a day. + std::fs::write(session.data.settings(), "{\"refreshMinutes\":5}").unwrap(); + assert_eq!(session.ask(Request::Status)["refreshMinutes"], 5); + pass(15 * 60_000 + 1); + session.runs(&id, 4); + pass(5 * 60_000 + 1); + session.runs(&id, 5); + + // While the session is locked nothing runs; unlocking runs everything once. + controller.session(SessionEvent::Locked(true)); + assert_eq!(session.ask(Request::Status)["locked"], true); + pass(60 * 60_000); + pass(60 * 60_000); + assert_eq!(session.runs(&id, 5)["runs"], 5, "paused while locked"); + controller.session(SessionEvent::Locked(false)); + session.runs(&id, 6); + std::thread::sleep(Duration::from_millis(150)); + assert_eq!(session.runs(&id, 6)["runs"], 6, "once, not once for the unlock and once for the overdue interval"); + + // The display may report the lock instead (a shell that locks without logind). + session.ask(Request::Session { locked: true }); + pass(60 * 60_000); + assert_eq!(session.runs(&id, 6)["runs"], 6); + session.ask(Request::Session { locked: false }); + session.runs(&id, 7); + + // Waking from sleep, as logind reports it, and as the clock shows it. + pass(3000); + controller.session(SessionEvent::Sleeping); + controller.session(SessionEvent::Resumed); + session.runs(&id, 8); + controller.session(SessionEvent::ClockChanged); + std::thread::sleep(Duration::from_millis(150)); + assert_eq!(session.runs(&id, 8)["runs"], 8, "two reports of one wake-up are one refresh"); + pass(3000); + // The wall clock is set an hour ahead and nothing reports it: the service notices the jump itself. + clock.set_wall(clock.wall() + 3_600_000); + controller.poke(); + session.runs(&id, 9); + + // A new theme runs the script again and changes the picture; the same theme does nothing. + session.ask(Request::Theme { dark: true }); + let dark = session.runs(&id, 10); + assert_eq!(dark["version"], 2); + session.ask(Request::Theme { dark: true }); + std::thread::sleep(Duration::from_millis(150)); + assert_eq!(session.runs(&id, 10)["runs"], 10); + + // A new scale draws the same tree again without running the script. + let mut watcher = Connection::watch(&session.socket()); + watcher.event(|event| event["event"] == "widget"); + session.ask(Request::Screen { name: "eDP-1".into(), scale: 1500, width: 1920, height: 1200 }); + let event = watcher.event(|event| event["event"] == "widget"); + assert_eq!((event["version"].as_u64(), event["width"].as_u64()), (Some(3), Some(329)), "the logical size stays"); + assert_eq!(png_size(&image(&event)), (494, 255)); + assert_eq!(session.runs(&id, 10)["runs"], 10); + assert_eq!(session.ask(Request::Status)["screens"]["eDP-1"], json!({ "scale": 1500, "width": 1920, "height": 1200 })); + + // On demand: one widget, a package, all. + session.ask(Request::Refresh { id: Some(id.clone()), package: None }); + session.runs(&id, 11); + assert_eq!(session.ask(Request::Refresh { id: None, package: Some("Plain".into()) })["refreshed"], 1); + session.runs(&id, 12); + assert_eq!(session.ask(Request::Refresh { id: None, package: None })["refreshed"], 1); + session.runs(&id, 13); + assert_eq!(session.refused(Request::Refresh { id: None, package: Some("Missing".into()) }), "package.unknown"); +} + +#[test] +fn a_running_timer_is_drawn_again_each_second_without_running_the_script() { + let session = Session::new(); + session.install("Timer", r##"$render();"##, "", ""); + session.install("Plain", PLAIN, "", ""); + let _service = session.start(); + let timer = session.add("Timer", Some("small")); + let plain = session.add("Plain", Some("small")); + let before = session.widget(&timer)["version"].as_u64().unwrap(); + std::thread::sleep(Duration::from_millis(3300)); + let after = session.widget(&timer); + let versions = after["version"].as_u64().unwrap() - before; + assert!((2..=4).contains(&versions), "one new picture a second: {versions} in 3.3 seconds"); + assert_eq!(after["runs"], 1, "the script ran once"); + let plain = session.widget(&plain); + assert_eq!((plain["version"].as_u64(), plain["runs"].as_u64()), (Some(1), Some(1)), "a widget without a timer is left alone"); + // The pictures of the seconds that passed do not pile up. + assert!(session.frame_files().len() <= 4, "{:?}", session.frame_files()); +} + +#[test] +fn a_package_that_fails_or_is_gone_still_has_a_picture() { + let session = Session::new(); + session.install("Broken", "throw new Error('no');", "", ""); + session.install("Plain", PLAIN, "", ""); + let _service = session.start(); + let broken = session.add("Broken", None); + let widget = session.widget(&broken); + assert_eq!(widget["error"], "scriptException"); + assert!(image(&widget).is_file(), "the error is drawn as a card"); + + let plain = session.add("Plain", None); + session.library().delete("Plain").unwrap(); + session.ask(Request::Refresh { id: Some(plain.clone()), package: None }); + let gone = session.until(&plain, "the notice", |widget| widget["installed"] == false && widget["busy"] == false); + assert_eq!(gone["version"], 2, "the notice replaces the picture"); + assert_eq!(session.refused(Request::Menu { id: plain.clone(), word: "edit".into() }), "package.unknown"); + session.ask(Request::Menu { id: plain, word: "remove".into() }); + assert_eq!(session.widgets().len(), 1); + + // A socket that does not exist is the client's "not running". + assert!(matches!(client::request(Path::new("/nonexistent/scriptwidget/host.sock"), &Request::Status), Err(client::ClientError::NotRunning))); +} + +#[test] +fn the_clock_of_the_system_goes_forward() { + let clock = SystemClock::new(); + let (first, wall) = (clock.now(), clock.wall()); + std::thread::sleep(Duration::from_millis(20)); + assert!(clock.now() >= first + 15); + assert!(wall > 1_700_000_000_000); +} diff --git a/Linux/crates/sw-quickjs/Cargo.toml b/Linux/crates/sw-quickjs/Cargo.toml new file mode 100644 index 00000000..7e1138e6 --- /dev/null +++ b/Linux/crates/sw-quickjs/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "sw-quickjs" +description = "The QuickJS-NG engine ScriptWidget vendors, behind a safe wrapper" +version.workspace = true +edition.workspace = true +license.workspace = true +publish.workspace = true +links = "swquickjs" + +[build-dependencies] +cc.workspace = true diff --git a/Linux/crates/sw-quickjs/build.rs b/Linux/crates/sw-quickjs/build.rs new file mode 100644 index 00000000..280f2d4a --- /dev/null +++ b/Linux/crates/sw-quickjs/build.rs @@ -0,0 +1,51 @@ +//! Compiles the QuickJS-NG sources the Android and Windows apps build +//! (`Android/core/src/main/cpp/quickjs`) together with the C binding in +//! `native/swquickjs.c`. No system or third-party QuickJS is used, so every +//! platform runs the same engine. + +use std::path::PathBuf; + +fn main() { + let manifest = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR")); + let quickjs = manifest.join("../../../Android/core/src/main/cpp/quickjs"); + let binding = manifest.join("native/swquickjs.c"); + assert!( + quickjs.join("quickjs.c").is_file(), + "QuickJS-NG sources not found at {}; sw-quickjs builds inside the ScriptWidget repository", + quickjs.display() + ); + + let sources = ["quickjs.c", "libregexp.c", "libunicode.c", "dtoa.c"]; + let mut build = cc::Build::new(); + build + .std("c11") + .include(&quickjs) + .define("_GNU_SOURCE", None) + // The interpreter is unusable unoptimized (Babel alone takes many + // seconds), so it is optimized in debug builds as well, with the + // level the Android build uses. + .opt_level(2) + .warnings(false) + .flag_if_supported("-fvisibility=hidden") + .file(&binding); + for name in sources { + build.file(quickjs.join(name)); + } + // SW_QUICKJS_ASAN=1 builds the C side with AddressSanitizer and links its + // runtime. Rust itself is not instrumented (that needs a nightly + // toolchain), so the runtime is not first in the library list: run the + // tests with LD_PRELOAD set to the path `cc -print-file-name=libasan.so` + // prints. Linux only. + println!("cargo:rerun-if-env-changed=SW_QUICKJS_ASAN"); + if std::env::var_os("SW_QUICKJS_ASAN").is_some_and(|value| value == "1") { + build.flag("-fsanitize=address").flag("-fno-omit-frame-pointer"); + println!("cargo:rustc-link-lib=asan"); + } + build.compile("swquickjs"); + + println!("cargo:rerun-if-changed={}", binding.display()); + for entry in std::fs::read_dir(&quickjs).expect("QuickJS source directory") { + println!("cargo:rerun-if-changed={}", entry.expect("QuickJS source file").path().display()); + } + println!("cargo:rustc-link-lib=m"); +} diff --git a/Linux/crates/sw-quickjs/native/swquickjs.c b/Linux/crates/sw-quickjs/native/swquickjs.c new file mode 100644 index 00000000..ab581c91 --- /dev/null +++ b/Linux/crates/sw-quickjs/native/swquickjs.c @@ -0,0 +1,328 @@ +// C binding between the Rust host and QuickJS-NG, the Linux counterpart of +// Windows/native/swcore.c and Android/core/src/main/cpp/scriptwidget_jni.c. +// One Engine owns one runtime and one context. All entry points must be +// called from the thread that created the engine. +// +// Text enters as UTF-8, which Rust strings already are. Text leaves as +// UTF-16, the engine's own form, so the Rust side decides what happens to a +// lone surrogate instead of receiving bytes that are not UTF-8. +// +// Local time: unlike the Windows build, quickjs.c is compiled unmodified. +// Outside Windows its getTimezoneOffset asks the C library (localtime_r and +// tm_gmtoff) for the offset at the instant in question, so dates on the other +// side of a daylight-saving change are right. The zone is the process's: the +// TZ variable, or /etc/localtime. + +#include +#include +#include +#include +#include + +#include "quickjs.h" + +// Status of a call that produces a value. +#define SW_NULL 0 // undefined or null +#define SW_VALUE 1 // sw_result holds the string or the bytes +#define SW_EXCEPTION -1 // sw_result holds the message and stack + +typedef struct Engine Engine; + +// __swNative(op, payload). The host answers by calling sw_host_return or +// sw_host_throw before it returns; neither means null. A length of -1 is null. +typedef void (*sw_host_fn)(void *user, const uint16_t *operation, intptr_t operationLength, const uint16_t *payload, intptr_t payloadLength); + +struct Engine { + JSRuntime *rt; + JSContext *ctx; + sw_host_fn host; + void *user; + int64_t deadline; // monotonic nanoseconds, 0 for none + bool interrupted; + // Answer of the host call in progress. + JSValue hostResult; + bool hostAnswered; + bool hostThrew; + // Result of the last call: UTF-16 text or raw bytes, owned by the engine. + uint8_t *result; + size_t resultLength; // UTF-16 code units for text, bytes for bytecode + size_t resultCapacity; +}; + +static int64_t monotonicNanoseconds(void) { + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + return (int64_t)now.tv_sec * 1000000000 + now.tv_nsec; +} + +static int interruptHandler(JSContext *ctx, void *opaque) { + (void)ctx; + Engine *engine = (Engine *)opaque; + if (engine->interrupted) return 1; + if (engine->deadline > 0 && monotonicNanoseconds() > engine->deadline) { + engine->interrupted = true; + return 1; + } + return 0; +} + +static bool reserve(Engine *engine, size_t bytes) { + if (bytes <= engine->resultCapacity) return true; + size_t capacity = bytes + bytes / 2 + 64; + uint8_t *grown = (uint8_t *)realloc(engine->result, capacity); + if (grown == NULL) return false; + engine->result = grown; + engine->resultCapacity = capacity; + return true; +} + +static void setResultBytes(Engine *engine, const void *bytes, size_t length) { + if (!reserve(engine, length)) { engine->resultLength = 0; return; } + if (length > 0) memcpy(engine->result, bytes, length); + engine->resultLength = length; +} + +static void appendResultText(Engine *engine, const uint16_t *chars, size_t length) { + if (!reserve(engine, (engine->resultLength + length) * sizeof(uint16_t))) return; + if (length > 0) memcpy(engine->result + engine->resultLength * sizeof(uint16_t), chars, length * sizeof(uint16_t)); + engine->resultLength += length; +} + +static void appendResultAscii(Engine *engine, const char *text) { + for (const char *cursor = text; *cursor; cursor += 1) { uint16_t unit = (uint16_t)(unsigned char)*cursor; appendResultText(engine, &unit, 1); } +} + +static JSValue stringFromHost(Engine *engine, const char *utf8, intptr_t length) { + if (utf8 == NULL || length < 0) return JS_NULL; + return JS_NewStringLen(engine->ctx, utf8, (size_t)length); +} + +// Appends value converted to a string; returns false when conversion throws +// (the exception is cleared). +static bool appendValue(Engine *engine, JSValueConst value) { + size_t length = 0; + const uint16_t *chars = JS_ToCStringLenUTF16(engine->ctx, &length, value); + if (chars == NULL) { JS_FreeValue(engine->ctx, JS_GetException(engine->ctx)); return false; } + appendResultText(engine, chars, length); + JS_FreeCStringUTF16(engine->ctx, chars); + return true; +} + +// Moves the pending JS exception into the result as "message\nstack". +static int takeException(Engine *engine) { + JSContext *ctx = engine->ctx; + JSValue error = JS_GetException(ctx); + engine->resultLength = 0; + if (!appendValue(engine, error)) appendResultAscii(engine, "unknown error"); + if (JS_IsError(error)) { + JSValue stack = JS_GetPropertyStr(ctx, error, "stack"); + if (JS_IsException(stack)) JS_FreeValue(ctx, JS_GetException(ctx)); + else if (!JS_IsUndefined(stack) && !JS_IsNull(stack)) { + size_t before = engine->resultLength; + appendResultAscii(engine, "\n"); + size_t afterSeparator = engine->resultLength; + if (!appendValue(engine, stack) || engine->resultLength == afterSeparator) engine->resultLength = before; + } + JS_FreeValue(ctx, stack); + } + JS_FreeValue(ctx, error); + return SW_EXCEPTION; +} + +static int finishValue(Engine *engine, JSValue value) { + if (JS_IsException(value)) return takeException(engine); + if (JS_IsUndefined(value) || JS_IsNull(value)) { JS_FreeValue(engine->ctx, value); engine->resultLength = 0; return SW_NULL; } + size_t length = 0; + const uint16_t *chars = JS_ToCStringLenUTF16(engine->ctx, &length, value); + JS_FreeValue(engine->ctx, value); + if (chars == NULL) return takeException(engine); + engine->resultLength = 0; + appendResultText(engine, chars, length); + JS_FreeCStringUTF16(engine->ctx, chars); + return SW_VALUE; +} + +// __swNative(op, payload) -> string | null. Synchronous call into the host. +static JSValue hostCall(JSContext *ctx, JSValueConst thisValue, int argc, JSValueConst *argv) { + (void)thisValue; + Engine *engine = (Engine *)JS_GetRuntimeOpaque(JS_GetRuntime(ctx)); + if (argc < 1) return JS_ThrowTypeError(ctx, "__swNative requires an operation"); + size_t operationLength = 0; + const uint16_t *operation = JS_ToCStringLenUTF16(ctx, &operationLength, argv[0]); + if (operation == NULL) return JS_EXCEPTION; + const uint16_t *payload = NULL; + size_t payloadLength = 0; + if (argc > 1 && !JS_IsUndefined(argv[1]) && !JS_IsNull(argv[1])) { + payload = JS_ToCStringLenUTF16(ctx, &payloadLength, argv[1]); + if (payload == NULL) { JS_FreeCStringUTF16(ctx, operation); return JS_EXCEPTION; } + } + // A host call can run JavaScript that makes another host call. + JSValue outerResult = engine->hostResult; + bool outerAnswered = engine->hostAnswered, outerThrew = engine->hostThrew; + engine->hostResult = JS_NULL; + engine->hostAnswered = false; + engine->hostThrew = false; + engine->host(engine->user, operation, (intptr_t)operationLength, payload, payload != NULL ? (intptr_t)payloadLength : -1); + JS_FreeCStringUTF16(ctx, operation); + if (payload != NULL) JS_FreeCStringUTF16(ctx, payload); + JSValue result = engine->hostResult; + bool threw = engine->hostThrew; + engine->hostResult = outerResult; + engine->hostAnswered = outerAnswered; + engine->hostThrew = outerThrew; + if (JS_IsException(result)) return JS_EXCEPTION; // the answer could not be allocated + if (threw) { + JSValue error = JS_NewError(ctx); + JS_SetPropertyStr(ctx, error, "message", result); + return JS_Throw(ctx, error); + } + return result; +} + +// __swEvaluate(code, fileName): classic-script evaluation in the global +// lexical scope, like JSContext.evaluateScript. Indirect eval would scope +// top-level let/const/class to the call and hide them from later scripts. +static JSValue evaluateGlobal(JSContext *ctx, JSValueConst thisValue, int argc, JSValueConst *argv) { + (void)thisValue; + if (argc < 1) return JS_ThrowTypeError(ctx, "__swEvaluate requires source text"); + size_t length = 0; + const char *code = JS_ToCStringLen(ctx, &length, argv[0]); + if (code == NULL) return JS_EXCEPTION; + const char *name = argc > 1 ? JS_ToCString(ctx, argv[1]) : NULL; + JSValue result = JS_Eval(ctx, code, length, name != NULL ? name : "script.js", JS_EVAL_TYPE_GLOBAL); + if (name != NULL) JS_FreeCString(ctx, name); + JS_FreeCString(ctx, code); + return result; +} + +static void enter(Engine *engine) { JS_UpdateStackTop(engine->rt); } + +Engine *sw_create(int64_t memoryLimit, int64_t stackSize, sw_host_fn host, void *user) { + Engine *engine = (Engine *)calloc(1, sizeof(Engine)); + if (engine == NULL) return NULL; + engine->rt = JS_NewRuntime(); + if (engine->rt == NULL) { free(engine); return NULL; } + JS_SetRuntimeOpaque(engine->rt, engine); + if (memoryLimit > 0) JS_SetMemoryLimit(engine->rt, (size_t)memoryLimit); + if (stackSize > 0) JS_SetMaxStackSize(engine->rt, (size_t)stackSize); + JS_SetInterruptHandler(engine->rt, interruptHandler, engine); + engine->ctx = JS_NewContext(engine->rt); + if (engine->ctx == NULL) { JS_FreeRuntime(engine->rt); free(engine); return NULL; } + engine->host = host; + engine->user = user; + engine->hostResult = JS_NULL; + JSValue global = JS_GetGlobalObject(engine->ctx); + JS_SetPropertyStr(engine->ctx, global, "__swNative", JS_NewCFunction(engine->ctx, hostCall, "__swNative", 2)); + JS_SetPropertyStr(engine->ctx, global, "__swEvaluate", JS_NewCFunction(engine->ctx, evaluateGlobal, "__swEvaluate", 2)); + JS_FreeValue(engine->ctx, global); + return engine; +} + +void sw_destroy(Engine *engine) { + enter(engine); + JS_FreeContext(engine->ctx); + JS_FreeRuntime(engine->rt); + free(engine->result); + free(engine); +} + +void sw_set_deadline(Engine *engine, int64_t millisecondsFromNow) { + engine->interrupted = false; + engine->deadline = millisecondsFromNow > 0 ? monotonicNanoseconds() + millisecondsFromNow * 1000000 : 0; +} + +int sw_interrupted(Engine *engine) { return engine->interrupted ? 1 : 0; } + +// Text or bytes of the last call, valid until the next call on this engine. +// The length is in UTF-16 code units for text and in bytes for bytecode. +const void *sw_result(Engine *engine, intptr_t *length) { + *length = (intptr_t)engine->resultLength; + return engine->result; +} + +void sw_host_return(Engine *engine, const char *utf8, intptr_t length) { + if (engine->hostAnswered) JS_FreeValue(engine->ctx, engine->hostResult); + engine->hostResult = stringFromHost(engine, utf8, length); + engine->hostAnswered = true; + engine->hostThrew = false; +} + +void sw_host_throw(Engine *engine, const char *utf8, intptr_t length) { + if (engine->hostAnswered) JS_FreeValue(engine->ctx, engine->hostResult); + engine->hostResult = utf8 != NULL && length >= 0 ? JS_NewStringLen(engine->ctx, utf8, (size_t)length) : JS_NewString(engine->ctx, "native host error"); + engine->hostAnswered = true; + engine->hostThrew = true; +} + +// `code` is UTF-8 with a NUL at code[length], as JS_Eval requires. +int sw_evaluate(Engine *engine, const char *code, intptr_t length, const char *fileName) { + enter(engine); + return finishValue(engine, JS_Eval(engine->ctx, code, (size_t)length, fileName, JS_EVAL_TYPE_GLOBAL)); +} + +int sw_compile(Engine *engine, const char *code, intptr_t length, const char *fileName) { + enter(engine); + JSValue function = JS_Eval(engine->ctx, code, (size_t)length, fileName, JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAG_COMPILE_ONLY); + if (JS_IsException(function)) return takeException(engine); + size_t size = 0; + uint8_t *bytes = JS_WriteObject(engine->ctx, &size, function, JS_WRITE_OBJ_BYTECODE); + JS_FreeValue(engine->ctx, function); + if (bytes == NULL) return takeException(engine); + setResultBytes(engine, bytes, size); + js_free(engine->ctx, bytes); + return SW_VALUE; +} + +// Bytecode must come from sw_compile in this application build; it is never +// read from packages or other untrusted input. +int sw_evaluate_bytecode(Engine *engine, const uint8_t *bytes, intptr_t length) { + enter(engine); + JSValue function = JS_ReadObject(engine->ctx, bytes, (size_t)length, JS_READ_OBJ_BYTECODE); + if (JS_IsException(function)) return takeException(engine); + return finishValue(engine, JS_EvalFunction(engine->ctx, function)); +} + +// Calls a global function with string arguments; a length of -1 passes null. +int sw_call(Engine *engine, const char *functionName, const char *const *arguments, const intptr_t *lengths, int count) { + enter(engine); + JSContext *ctx = engine->ctx; + JSValue *values = count > 0 ? (JSValue *)calloc((size_t)count, sizeof(JSValue)) : NULL; + if (count > 0 && values == NULL) { JS_ThrowOutOfMemory(ctx); return takeException(engine); } + for (int index = 0; index < count; index += 1) { + values[index] = stringFromHost(engine, arguments[index], lengths[index]); + if (JS_IsException(values[index])) { + for (int freed = 0; freed < index; freed += 1) JS_FreeValue(ctx, values[freed]); + free(values); + return takeException(engine); + } + } + JSValue global = JS_GetGlobalObject(ctx); + JSValue function = JS_GetPropertyStr(ctx, global, functionName); + JSValue result = JS_Call(ctx, function, global, count, values); + for (int index = 0; index < count; index += 1) JS_FreeValue(ctx, values[index]); + free(values); + JS_FreeValue(ctx, function); + JS_FreeValue(ctx, global); + return finishValue(engine, result); +} + +// Runs queued promise jobs. Returns the number executed, or -1 when a job +// threw (sw_result holds the message). +int sw_run_jobs(Engine *engine, int maximum) { + enter(engine); + int executed = 0; + while (executed < maximum) { + JSContext *jobContext = NULL; + int status = JS_ExecutePendingJob(engine->rt, &jobContext); + if (status == 0) break; + if (status < 0) { takeException(engine); return -1; } + executed += 1; + } + return executed; +} + +int64_t sw_memory_used(Engine *engine) { + JSMemoryUsage usage; + JS_ComputeMemoryUsage(engine->rt, &usage); + return (int64_t)usage.malloc_size; +} diff --git a/Linux/crates/sw-quickjs/src/lib.rs b/Linux/crates/sw-quickjs/src/lib.rs new file mode 100644 index 00000000..cebcc08e --- /dev/null +++ b/Linux/crates/sw-quickjs/src/lib.rs @@ -0,0 +1,320 @@ +//! QuickJS-NG, as vendored in `Android/core/src/main/cpp/quickjs`, behind a +//! safe wrapper. This is the Linux counterpart of +//! `Windows/src/ScriptWidget.Core/Js/QuickJs.cs`. +//! +//! One [`Engine`] owns one runtime and one context and stays on the thread +//! that created it. JavaScript reaches the host only through the +//! `__swNative(operation, payload)` global, which calls the [`Host`] closure; +//! the engine exposes no file, process or network interface of its own. + +use std::cell::Cell; +use std::ffi::{CString, c_char, c_int, c_void}; +use std::fmt; +use std::marker::PhantomData; +use std::panic::{AssertUnwindSafe, catch_unwind}; +use std::ptr; +use std::sync::Mutex; + +/// Heap limit of an execution context, the same on every platform. +pub const DEFAULT_MEMORY_LIMIT: usize = 64 * 1024 * 1024; +/// JavaScript stack limit of an execution context. The thread that runs the +/// engine needs a larger native stack: see [`run_on_js_thread`]. +pub const DEFAULT_STACK_LIMIT: usize = 3 * 1024 * 1024; +/// Native stack of the thread [`run_on_js_thread`] starts. +pub const JS_THREAD_STACK: usize = 8 * 1024 * 1024; + +/// An uncaught JavaScript exception, or an interrupted execution. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct JsError { + /// The exception converted to text, followed by a line break and the stack when there is one. + pub message: String, + /// True when the deadline stopped the script. + pub interrupted: bool, +} + +impl fmt::Display for JsError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(&self.message) + } +} + +impl std::error::Error for JsError {} + +/// Answer of the host to `__swNative(operation, payload)`: text, null, or a +/// message that JavaScript receives as a thrown `Error`. +pub type HostResult = Result, String>; + +/// The synchronous bridge behind `__swNative`. It may be called again while a +/// call is in progress only if it runs JavaScript itself, which the wrapper +/// does not allow, so interior state can live in `Cell` and `RefCell`. +pub type Host<'host> = dyn Fn(&str, Option<&str>) -> HostResult + 'host; + +struct Shared<'host> { + host: Box>, + engine: Cell<*mut ffi::Engine>, +} + +/// One QuickJS-NG runtime and context. +/// +/// Not `Send`: QuickJS measures its stack from the thread it runs on, and the +/// host closure need not be thread safe. +pub struct Engine<'host> { + pointer: *mut ffi::Engine, + shared: *mut Shared<'host>, + _thread_bound: PhantomData<*mut ()>, +} + +impl<'host> Engine<'host> { + /// A context with the default limits of an execution. + pub fn new(host: impl Fn(&str, Option<&str>) -> HostResult + 'host) -> Result { + Self::with_limits(host, DEFAULT_MEMORY_LIMIT, DEFAULT_STACK_LIMIT) + } + + pub fn with_limits(host: impl Fn(&str, Option<&str>) -> HostResult + 'host, memory_limit: usize, stack_limit: usize) -> Result { + let shared = Box::into_raw(Box::new(Shared { host: Box::new(host), engine: Cell::new(ptr::null_mut()) })); + // SAFETY: `shared` outlives the engine: it is freed in `drop` after `sw_destroy`. + let pointer = unsafe { ffi::sw_create(memory_limit as i64, stack_limit as i64, host_call, shared.cast()) }; + if pointer.is_null() { + // SAFETY: created above and not shared with anything. + drop(unsafe { Box::from_raw(shared) }); + return Err(JsError { message: "QuickJS runtime could not be created".into(), interrupted: false }); + } + // SAFETY: `shared` is valid and only this thread touches it. + unsafe { (*shared).engine.set(pointer) }; + Ok(Self { pointer, shared, _thread_bound: PhantomData }) + } + + /// Stops running JavaScript after the given time; 0 clears the deadline. + pub fn set_deadline(&mut self, milliseconds: u64) { + // SAFETY: the engine is alive and used on its own thread. + unsafe { ffi::sw_set_deadline(self.pointer, milliseconds.min(i64::MAX as u64 / 1_000_000) as i64) } + } + + /// Evaluates a classic script in the global scope. Returns the completion + /// value as text, or `None` for undefined and null. + pub fn evaluate(&mut self, code: &str, file_name: &str) -> Result, JsError> { + let (code, length) = terminated(code); + let name = c_name(file_name); + // SAFETY: `code` holds `length` bytes followed by a NUL, as JS_Eval requires. + let status = unsafe { ffi::sw_evaluate(self.pointer, code.as_ptr().cast(), length as isize, name.as_ptr()) }; + self.finish(status) + } + + /// Compiles a classic script to bytecode for [`Engine::evaluate_bytecode`]. + pub fn compile(&mut self, code: &str, file_name: &str) -> Result, JsError> { + let (code, length) = terminated(code); + let name = c_name(file_name); + // SAFETY: as in `evaluate`. + let status = unsafe { ffi::sw_compile(self.pointer, code.as_ptr().cast(), length as isize, name.as_ptr()) }; + if status < 0 { + return Err(self.failure()); + } + let mut length = 0isize; + // SAFETY: after a successful compile the result holds `length` bytes until the next call. + let bytes = unsafe { + let data = ffi::sw_result(self.pointer, &mut length).cast::(); + if length <= 0 || data.is_null() { &[][..] } else { std::slice::from_raw_parts(data, length as usize) } + }; + Ok(bytes.to_vec()) + } + + /// Runs bytecode. + /// + /// # Safety + /// `bytecode` must come from [`Engine::compile`] of this build of the + /// program. QuickJS does not verify bytecode: anything else, and above + /// all anything read from a package or another untrusted source, can + /// corrupt memory. + pub unsafe fn evaluate_bytecode(&mut self, bytecode: &[u8]) -> Result, JsError> { + // SAFETY: the caller vouches for the bytecode; the pointer and length describe it. + let status = unsafe { ffi::sw_evaluate_bytecode(self.pointer, bytecode.as_ptr(), bytecode.len() as isize) }; + self.finish(status) + } + + /// Calls a global function with string arguments; `None` passes null. + pub fn call(&mut self, function: &str, arguments: &[Option<&str>]) -> Result, JsError> { + let name = c_name(function); + let pointers: Vec<*const c_char> = arguments.iter().map(|argument| argument.map_or(ptr::null(), |text| text.as_ptr().cast())).collect(); + let lengths: Vec = arguments.iter().map(|argument| argument.map_or(-1, |text| text.len() as isize)).collect(); + // SAFETY: both arrays have `arguments.len()` entries that stay alive for the call. + let status = unsafe { ffi::sw_call(self.pointer, name.as_ptr(), pointers.as_ptr(), lengths.as_ptr(), arguments.len() as c_int) }; + self.finish(status) + } + + /// Runs queued promise jobs, at most `maximum`; returns how many ran. + pub fn run_jobs(&mut self, maximum: usize) -> Result { + // SAFETY: the engine is alive and used on its own thread. + let executed = unsafe { ffi::sw_run_jobs(self.pointer, maximum.min(c_int::MAX as usize) as c_int) }; + if executed < 0 { Err(self.failure()) } else { Ok(executed as usize) } + } + + /// Bytes the engine has allocated. + pub fn memory_used(&self) -> usize { + // SAFETY: the engine is alive and used on its own thread. + unsafe { ffi::sw_memory_used(self.pointer) }.max(0) as usize + } + + fn finish(&mut self, status: c_int) -> Result, JsError> { + match status { + status if status < 0 => Err(self.failure()), + 0 => Ok(None), + _ => Ok(Some(self.result_text())), + } + } + + fn result_text(&self) -> String { + let mut length = 0isize; + // SAFETY: the result holds `length` UTF-16 code units until the next call; malloc aligns it. + unsafe { + let data = ffi::sw_result(self.pointer, &mut length).cast::(); + text_from_engine(data, length).unwrap_or_default() + } + } + + fn failure(&self) -> JsError { + // SAFETY: the engine is alive and used on its own thread. + let interrupted = unsafe { ffi::sw_interrupted(self.pointer) } != 0; + JsError { message: self.result_text(), interrupted } + } +} + +impl Drop for Engine<'_> { + fn drop(&mut self) { + // SAFETY: both pointers were created in `with_limits` and are released once, the engine first. + unsafe { + ffi::sw_destroy(self.pointer); + drop(Box::from_raw(self.shared)); + } + } +} + +/// UTF-8 bytes followed by the NUL that JS_Eval requires, and the length without it. +fn terminated(code: &str) -> (Vec, usize) { + let mut bytes = Vec::with_capacity(code.len() + 1); + bytes.extend_from_slice(code.as_bytes()); + bytes.push(0); + (bytes, code.len()) +} + +/// File and function names are chosen by the host; a NUL would be a programming error and is dropped. +fn c_name(name: &str) -> CString { + CString::new(name.replace('\0', "")).expect("NUL bytes were removed") +} + +/// Text the engine hands over as UTF-16. A lone surrogate, which a Rust +/// string cannot hold, becomes U+FFFD. +/// +/// # Safety +/// `data` must point to `length` code units, or `length` must be negative. +unsafe fn text_from_engine(data: *const u16, length: isize) -> Option { + if length < 0 { + return None; + } + if length == 0 || data.is_null() { + return Some(String::new()); + } + // SAFETY: guaranteed by the caller. + Some(String::from_utf16_lossy(unsafe { std::slice::from_raw_parts(data, length as usize) })) +} + +extern "C" fn host_call(user: *mut c_void, operation: *const u16, operation_length: isize, payload: *const u16, payload_length: isize) { + // SAFETY: `user` is the `Shared` of the engine that is making this call, + // alive until the engine is dropped; the text pointers are valid for the call. + let (shared, operation, payload) = unsafe { + let shared = &*user.cast::>(); + let payload = if payload.is_null() { None } else { text_from_engine(payload, payload_length) }; + (shared, text_from_engine(operation, operation_length).unwrap_or_default(), payload) + }; + let engine = shared.engine.get(); + // A panic must not unwind through the C frames of the interpreter: hand it to JavaScript as an error. + let answer = catch_unwind(AssertUnwindSafe(|| (shared.host)(&operation, payload.as_deref()))).unwrap_or_else(|_| Err("native host error".into())); + // SAFETY: `engine` is the engine in the middle of this host call; the text outlives the call. + unsafe { + match answer { + Ok(None) => {} + Ok(Some(text)) => ffi::sw_host_return(engine, text.as_ptr().cast(), text.len() as isize), + Err(message) => { + let message = if message.is_empty() { "native host error" } else { message.as_str() }; + ffi::sw_host_throw(engine, message.as_ptr().cast(), message.len() as isize); + } + } + } +} + +/// Runs `work` on a fresh thread whose stack is large enough for the +/// JavaScript stack limit, and returns its result. A panic in `work` resumes +/// on the calling thread. +pub fn run_on_js_thread(name: &str, stack_bytes: usize, work: impl FnOnce() -> T + Send) -> T { + std::thread::scope(|scope| { + let thread = std::thread::Builder::new() + .name(name.to_owned()) + .stack_size(stack_bytes) + .spawn_scoped(scope, work) + .expect("JavaScript thread could not be started"); + match thread.join() { + Ok(value) => value, + Err(panic) => std::panic::resume_unwind(panic), + } + }) +} + +static TIME_ZONE: Mutex>> = Mutex::new(None); + +/// Sets the time zone of JavaScript's local time (`Date`'s getters, +/// `toString`, `getTimezoneOffset`) for every engine in the process: an IANA +/// name such as "America/Los_Angeles", or `None` to go back to the zone the +/// process started with. +/// +/// QuickJS asks the C library for local time, so this sets the `TZ` variable +/// and calls `tzset`. It also changes local time for everything else in the +/// process. Tests use it to pin the zone; the service leaves the system zone +/// alone. +/// +/// # Safety +/// The environment is process-wide and not synchronized: no other thread may +/// read or write environment variables, or convert local time, while this +/// runs. Call it before the threads that run engines start. +pub unsafe fn set_process_time_zone(iana_name: Option<&str>) { + unsafe extern "C" { + fn tzset(); + } + let mut original = TIME_ZONE.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + let start = original.get_or_insert_with(|| std::env::var_os("TZ")).clone(); + // SAFETY: the caller guarantees that nothing else uses the environment or local time now. + unsafe { + match (iana_name, start) { + // A leading colon tells the C library that this is a zone file name, not a POSIX rule. + (Some(name), _) => std::env::set_var("TZ", format!(":{name}")), + (None, Some(value)) => std::env::set_var("TZ", value), + (None, None) => std::env::remove_var("TZ"), + } + tzset(); + } +} + +mod ffi { + use std::ffi::{c_char, c_int, c_void}; + + #[repr(C)] + pub struct Engine { + _opaque: [u8; 0], + } + + pub type HostFn = extern "C" fn(user: *mut c_void, operation: *const u16, operation_length: isize, payload: *const u16, payload_length: isize); + + unsafe extern "C" { + pub fn sw_create(memory_limit: i64, stack_size: i64, host: HostFn, user: *mut c_void) -> *mut Engine; + pub fn sw_destroy(engine: *mut Engine); + pub fn sw_set_deadline(engine: *mut Engine, milliseconds_from_now: i64); + pub fn sw_interrupted(engine: *mut Engine) -> c_int; + pub fn sw_result(engine: *mut Engine, length: *mut isize) -> *const c_void; + pub fn sw_host_return(engine: *mut Engine, utf8: *const c_char, length: isize); + pub fn sw_host_throw(engine: *mut Engine, utf8: *const c_char, length: isize); + pub fn sw_evaluate(engine: *mut Engine, code: *const c_char, length: isize, file_name: *const c_char) -> c_int; + pub fn sw_compile(engine: *mut Engine, code: *const c_char, length: isize, file_name: *const c_char) -> c_int; + pub fn sw_evaluate_bytecode(engine: *mut Engine, bytes: *const u8, length: isize) -> c_int; + pub fn sw_call(engine: *mut Engine, function_name: *const c_char, arguments: *const *const c_char, lengths: *const isize, count: c_int) -> c_int; + pub fn sw_run_jobs(engine: *mut Engine, maximum: c_int) -> c_int; + pub fn sw_memory_used(engine: *mut Engine) -> i64; + } +} diff --git a/Linux/crates/sw-quickjs/tests/engine.rs b/Linux/crates/sw-quickjs/tests/engine.rs new file mode 100644 index 00000000..b9e3177a --- /dev/null +++ b/Linux/crates/sw-quickjs/tests/engine.rs @@ -0,0 +1,151 @@ +//! The engine binding, ported from Windows/tests/ScriptWidget.Core.Tests/QuickJsTests.cs. + +use sw_quickjs::{DEFAULT_MEMORY_LIMIT, DEFAULT_STACK_LIMIT, Engine, HostResult, JS_THREAD_STACK, JsError, run_on_js_thread}; + +fn on_js_thread(block: impl FnOnce(&mut Engine) -> T + Send) -> T { + with_host(|_, _| Ok(None), DEFAULT_MEMORY_LIMIT, block) +} + +fn with_host(host: impl Fn(&str, Option<&str>) -> HostResult + Send, memory: usize, block: impl FnOnce(&mut Engine) -> T + Send) -> T { + run_on_js_thread("test-js", JS_THREAD_STACK, move || { + let mut engine = Engine::with_limits(host, memory, DEFAULT_STACK_LIMIT).expect("engine"); + block(&mut engine) + }) +} + +fn evaluate(code: &'static str) -> Result, JsError> { + on_js_thread(move |engine| engine.evaluate(code, "script.js")) +} + +#[test] +fn evaluates_and_returns_strings() { + assert_eq!(evaluate("[1,2,3].map(x => x * 2).join(',')").unwrap().as_deref(), Some("2,4,6")); + assert_eq!(evaluate("undefined").unwrap(), None); + assert_eq!(evaluate("null").unwrap(), None); + assert_eq!(evaluate("''").unwrap().as_deref(), Some("")); +} + +#[test] +fn keeps_astral_characters_and_replaces_lone_surrogates() { + assert_eq!(evaluate("'你好 🌤️'").unwrap().as_deref(), Some("你好 🌤️")); + // A Rust string cannot hold a lone surrogate; it arrives as one U+FFFD. + assert_eq!(evaluate("'a\\ud83db'").unwrap().as_deref(), Some("a\u{fffd}b")); + let echoed = on_js_thread(|engine| { + engine.evaluate("globalThis.echo = (text) => text + '|' + text.length;", "script.js").unwrap(); + engine.call("echo", &[Some("a🌤b")]) + }); + assert_eq!(echoed.unwrap().as_deref(), Some("a🌤b|4")); +} + +#[test] +fn source_text_may_contain_nul_characters() { + assert_eq!(evaluate("'a\0b'.length + ''").unwrap().as_deref(), Some("3")); +} + +#[test] +fn top_level_declarations_are_visible_to_later_scripts() { + let value = on_js_thread(|engine| { + engine.evaluate("const a = 1; let b = 2; class C {}", "first.js").unwrap(); + engine.evaluate("String(a + b)", "second.js") + }); + assert_eq!(value.unwrap().as_deref(), Some("3")); +} + +#[test] +fn calls_functions_with_null_arguments() { + let value = on_js_thread(|engine| { + engine.evaluate("globalThis.f = (a, b) => String(a) + '|' + b;", "script.js").unwrap(); + engine.call("f", &[None, Some("x")]) + }); + assert_eq!(value.unwrap().as_deref(), Some("null|x")); +} + +#[test] +fn runs_promise_jobs() { + let value = on_js_thread(|engine| { + engine.evaluate("globalThis.r = 'pending'; (async () => { r = await Promise.resolve('done'); })();", "script.js").unwrap(); + assert!(engine.run_jobs(10_000).unwrap() > 0); + engine.evaluate("r", "script.js") + }); + assert_eq!(value.unwrap().as_deref(), Some("done")); +} + +#[test] +fn host_calls_return_values_null_and_errors() { + fn host(operation: &str, payload: Option<&str>) -> HostResult { + match operation { + "echo" => Ok(payload.map(str::to_owned)), + "null" => Ok(None), + "panic" => panic!("host panicked"), + other => Err(format!("host refused {other}")), + } + } + let run = |code: &'static str| with_host(host, DEFAULT_MEMORY_LIMIT, move |engine| engine.evaluate(code, "script.js")).unwrap(); + assert_eq!(run("__swNative('echo', 'payload ✓')").as_deref(), Some("payload ✓")); + assert_eq!(run("String(__swNative('echo') === null)").as_deref(), Some("true")); + assert_eq!(run("String(__swNative('null') === null)").as_deref(), Some("true")); + assert_eq!(run("try { __swNative('other'); 'no error' } catch (error) { error.message }").as_deref(), Some("host refused other")); + // A panic in the host becomes a JavaScript error instead of unwinding through the interpreter. + assert_eq!(run("try { __swNative('panic'); 'no error' } catch (error) { error.message }").as_deref(), Some("native host error")); +} + +#[test] +fn uncaught_errors_carry_message_and_stack() { + let error = on_js_thread(|engine| engine.evaluate("function boom() { throw new TypeError('bad thing'); } boom();", "widget.js")).unwrap_err(); + assert!(!error.interrupted); + assert!(error.message.contains("TypeError: bad thing"), "{}", error.message); + assert!(error.message.contains("widget.js"), "{}", error.message); +} + +#[test] +fn deadline_interrupts_an_endless_loop() { + let error = on_js_thread(|engine| { + engine.set_deadline(200); + engine.evaluate("for (;;) {}", "script.js") + }) + .unwrap_err(); + assert!(error.interrupted); +} + +#[test] +fn clearing_the_deadline_lets_later_code_run() { + let value = on_js_thread(|engine| { + engine.set_deadline(100); + assert!(engine.evaluate("for (;;) {}", "script.js").is_err()); + engine.set_deadline(0); + engine.evaluate("'ok'", "script.js") + }); + assert_eq!(value.unwrap().as_deref(), Some("ok")); +} + +#[test] +fn memory_limit_stops_runaway_allocation() { + let error = with_host(|_, _| Ok(None), 16 * 1024 * 1024, |engine| engine.evaluate("const a = []; for (;;) a.push(new Array(100000).fill(1));", "script.js")).unwrap_err(); + assert!(error.message.to_lowercase().contains("out of memory"), "{}", error.message); +} + +#[test] +fn deep_recursion_raises_a_range_error() { + let error = evaluate("function f() { return f() + 1 } f()").unwrap_err(); + assert!(error.message.to_lowercase().contains("stack"), "{}", error.message); +} + +#[test] +fn bytecode_round_trips() { + let bytes = on_js_thread(|engine| engine.compile("globalThis.answer = 6 * 7; String(answer)", "script.js")).unwrap(); + assert!(!bytes.is_empty()); + // SAFETY: the bytecode was produced by `compile` of this build a line above. + let value = on_js_thread(move |engine| unsafe { engine.evaluate_bytecode(&bytes) }); + assert_eq!(value.unwrap().as_deref(), Some("42")); +} + +#[test] +fn reports_memory_in_use() { + assert!(on_js_thread(|engine| engine.memory_used()) > 0); +} + +#[test] +fn the_engine_has_no_system_interfaces() { + let value = evaluate("['std', 'os', 'bjson', 'print', 'require', 'process', 'console', 'fetch', 'setTimeout'].filter((name) => name in globalThis).join(',')"); + assert_eq!(value.unwrap().as_deref(), Some("")); +} diff --git a/Linux/crates/sw-quickjs/tests/local_time.rs b/Linux/crates/sw-quickjs/tests/local_time.rs new file mode 100644 index 00000000..8ab9534e --- /dev/null +++ b/Linux/crates/sw-quickjs/tests/local_time.rs @@ -0,0 +1,23 @@ +//! Local time. This file holds a single test because it changes the time +//! zone of the whole process: Cargo runs each test file as its own program, +//! and with one test nothing else reads the environment at the same time. + +use sw_quickjs::{Engine, JS_THREAD_STACK, run_on_js_thread, set_process_time_zone}; + +fn evaluate(code: &'static str) -> String { + run_on_js_thread("test-js", JS_THREAD_STACK, move || Engine::new(|_, _| Ok(None)).expect("engine").evaluate(code, "script.js").expect("evaluation").unwrap_or_default()) +} + +#[test] +fn local_time_uses_the_offset_of_the_date_not_of_today() { + // SAFETY: the only test of this program; no other thread uses the environment or local time. + unsafe { set_process_time_zone(Some("America/Los_Angeles")) }; + // Winter and summer of the same year differ by the daylight-saving hour. + assert_eq!(evaluate("new Date(Date.UTC(2026, 0, 15, 12)).getTimezoneOffset() + ',' + new Date(Date.UTC(2026, 6, 15, 12)).getTimezoneOffset()"), "480,420"); + assert_eq!(evaluate("String(new Date(Date.UTC(2026, 6, 15, 11, 30)).getHours())"), "4"); + // SAFETY: as above. + unsafe { set_process_time_zone(Some("Asia/Shanghai")) }; + assert_eq!(evaluate("String(new Date(Date.UTC(2026, 6, 15, 12)).getTimezoneOffset())"), "-480"); + // SAFETY: as above. + unsafe { set_process_time_zone(None) }; +} diff --git a/Linux/crates/sw-render/Cargo.toml b/Linux/crates/sw-render/Cargo.toml new file mode 100644 index 00000000..02f886c9 --- /dev/null +++ b/Linux/crates/sw-render/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "sw-render" +description = "The ScriptWidget painter for Linux: element tree to bitmap and tap regions, on Skia" +version.workspace = true +edition.workspace = true +license.workspace = true +publish.workspace = true + +[dependencies] +sw-core.workspace = true +sw-quickjs.workspace = true +# The recorded element tree nests deeper than serde_json reads by default. +serde_json = { workspace = true, features = ["unbounded_depth"] } +serde = "1" +skia-safe.workspace = true diff --git a/Linux/crates/sw-render/examples/render_tree.rs b/Linux/crates/sw-render/examples/render_tree.rs new file mode 100644 index 00000000..b916d43c --- /dev/null +++ b/Linux/crates/sw-render/examples/render_tree.rs @@ -0,0 +1,52 @@ +//! Draws a canonical element tree to a PNG, without a graphical session. +//! +//! cargo run -p sw-render --example render_tree -- [assets directory] +//! +//! `family` is small, medium, large or extraLarge. The assets directory is +//! the one `node Linux/scripts/build-shared-js.mjs` writes (default: +//! `Linux/assets/scriptwidget`). A tree is what a package execution returns; +//! this example does not run packages. + +use std::path::PathBuf; +use std::process::ExitCode; +use std::sync::Arc; + +use sw_core::SharedScripts; +use sw_render::{RenderRequest, WidgetRenderer}; + +fn run(arguments: &[String]) -> Result { + let [tree_file, family, scale, output, rest @ ..] = arguments else { + return Err("usage: render_tree [assets directory]".into()); + }; + let assets = rest.first().map_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets/scriptwidget"), PathBuf::from); + let tree = std::fs::read_to_string(tree_file).map_err(|error| format!("{tree_file}: {error}"))?; + let scale: f32 = scale.parse().map_err(|_| format!("{scale} is not a scale"))?; + let request = RenderRequest::for_family(tree, family, scale).ok_or_else(|| format!("{family} is not a widget family"))?; + let scripts = Arc::new(SharedScripts::new(assets).map_err(|error| error.to_string())?); + let renderer = WidgetRenderer::new(scripts).map_err(|error| error.to_string())?; + let rendered = renderer.render(request).map_err(|error| error.to_string())?; + std::fs::write(output, rendered.encode_png().map_err(|error| error.to_string())?).map_err(|error| format!("{output}: {error}"))?; + Ok(format!( + "{output}: {}x{} pixels, layout {:.2} ms, paint {:.2} ms, {} tap regions, {} animated GIFs", + rendered.pixel_width, + rendered.pixel_height, + rendered.layout_time.as_secs_f64() * 1000.0, + rendered.paint_time.as_secs_f64() * 1000.0, + rendered.hit_regions.len(), + rendered.gif_slots.len() + )) +} + +fn main() -> ExitCode { + let arguments: Vec = std::env::args().skip(1).collect(); + match run(&arguments) { + Ok(summary) => { + println!("{summary}"); + ExitCode::SUCCESS + } + Err(message) => { + eprintln!("{message}"); + ExitCode::FAILURE + } + } +} diff --git a/Linux/crates/sw-render/src/css.rs b/Linux/crates/sw-render/src/css.rs new file mode 100644 index 00000000..1b6d792d --- /dev/null +++ b/Linux/crates/sw-render/src/css.rs @@ -0,0 +1,355 @@ +//! Parsers for the small CSS subset the shared renderer writes. Ported from +//! `Windows/src/ScriptWidget.Core/Render/Css.cs`; gradients become Skia +//! shaders as in the Android original. + +use skia_safe::gradient::interpolation::InPremul; +use skia_safe::gradient::{Colors, Gradient, Interpolation, shaders}; +use skia_safe::{Color, Color4f, Matrix, Point, Shader, TileMode}; + +pub const GRAY: Color = Color::from_argb(255, 136, 136, 136); + +/// A number as Kotlin's `toFloatOrNull` reads it: `None` when the text is not one. +pub fn number(text: Option<&str>) -> Option { + let text = text?.trim(); + // `parse` also accepts "inf" and "nan", which are not CSS numbers. + if text.is_empty() || !text.bytes().all(|byte| byte.is_ascii_digit() || matches!(byte, b'+' | b'-' | b'.' | b'e' | b'E')) { + return None; + } + text.parse::().ok().filter(|value| value.is_finite()) +} + +fn channel(value: f32) -> u8 { + (value as i32).clamp(0, 255) as u8 +} + +/// Parses rgba()/rgb()/#hex/named colors. Returns `None` for `none`, empty or unknown values. +pub fn color(value: Option<&str>) -> Option { + let text = value?.trim().to_ascii_lowercase(); + match text.as_str() { + "" | "none" => return None, + "white" => return Some(Color::WHITE), + "black" => return Some(Color::BLACK), + "transparent" => return Some(Color::TRANSPARENT), + "red" => return Some(Color::from_argb(255, 255, 0, 0)), + "green" => return Some(Color::from_argb(255, 0, 128, 0)), + "blue" => return Some(Color::from_argb(255, 0, 0, 255)), + "gray" | "grey" => return Some(GRAY), + _ => {} + } + if text.starts_with("rgb") { + let inner = text.split_once('(').map_or(text.as_str(), |(_, rest)| rest); + let inner = inner.split_once(')').map_or(inner, |(before, _)| before); + let parts: Vec<&str> = inner.split([',', '/']).map(str::trim).collect(); + if parts.len() < 3 { + return None; + } + let (red, green, blue) = (number(Some(parts[0]))?, number(Some(parts[1]))?, number(Some(parts[2]))?); + let alpha = parts.get(3).and_then(|part| number(Some(part))).unwrap_or(1.0); + return Some(Color::from_argb((alpha.clamp(0.0, 1.0) * 255.0 + 0.5) as u8, channel(red), channel(green), channel(blue))); + } + if let Some(hex) = text.strip_prefix('#') { + if !hex.bytes().all(|byte| byte.is_ascii_hexdigit()) { + return None; + } + let value = u32::from_str_radix(hex, 16).ok()?; + return match hex.len() { + 3 => Some(Color::from_argb(255, ((value >> 8) & 0xF) as u8 * 17, ((value >> 4) & 0xF) as u8 * 17, (value & 0xF) as u8 * 17)), + 6 => Some(Color::from_argb(255, (value >> 16) as u8, (value >> 8) as u8, value as u8)), + 8 => Some(Color::from_argb(value as u8, (value >> 24) as u8, (value >> 16) as u8, (value >> 8) as u8)), + _ => None, + }; + } + None +} + +/// `12px`, `12` or a bare number. Returns `None` for auto, percentages and keywords. +pub fn pixels(value: Option<&str>) -> Option { + let text = value?.trim(); + if text.is_empty() || text.ends_with('%') { + return None; + } + number(Some(text.strip_suffix("px").unwrap_or(text))) +} + +/// A length that may be a percentage of `basis`. +pub fn length(value: Option<&str>, basis: f32) -> Option { + let text = value?.trim(); + match text.strip_suffix('%') { + Some(percent) => number(Some(percent)).map(|percent| percent / 100.0 * basis), + None => pixels(Some(text)), + } +} + +/// Splits on commas that are not inside parentheses. +pub fn split_top_level(value: &str) -> Vec<&str> { + let mut parts = Vec::new(); + let (mut depth, mut start) = (0i32, 0usize); + for (index, character) in value.char_indices() { + match character { + '(' => depth += 1, + ')' => depth -= 1, + ',' if depth == 0 => { + parts.push(value[start..index].trim()); + start = index + 1; + } + _ => {} + } + } + parts.push(value[start..].trim()); + parts.retain(|part| !part.is_empty()); + parts +} + +struct Stop { + color: Color, + position: f32, +} + +/// ` ` where position is in px or deg. +fn parse_stop(text: &str) -> Option { + let text = text.trim(); + let split = text.rfind(' ').filter(|&at| at > 0)?; + let color = color(Some(&text[..split]))?; + let position = &text[split + 1..]; + let position = position.strip_suffix("px").unwrap_or(position); + let position = number(Some(position.strip_suffix("deg").unwrap_or(position)))?; + Some(Stop { color, position }) +} + +fn colors_of(stops: &[Stop]) -> Vec { + stops.iter().map(|stop| Color4f::from(stop.color)).collect() +} + +/// CSS interpolates gradient colors premultiplied: a stop that fades to transparent keeps its hue. +fn premultiplied() -> Interpolation { + Interpolation { in_premul: InPremul::Yes, ..Default::default() } +} + +/// Skia, like Android, wants non-decreasing positions. +fn monotonic(positions: impl Iterator) -> Vec { + let mut previous = 0f32; + positions + .map(|position| { + previous = if position.is_nan() { previous } else { previous.max(position) }; + previous + }) + .collect() +} + +/// The number that follows `keyword` and whitespace, and the text after it. +fn number_after<'a>(text: &'a str, keyword: &str) -> Option<(f32, &'a str)> { + let rest = text[text.find(keyword)? + keyword.len()..].trim_start(); + let end = rest.find(|character: char| !(character.is_ascii_digit() || matches!(character, '-' | '+' | '.' | 'e' | 'E'))).unwrap_or(rest.len()); + Some((number(Some(&rest[..end]))?, &rest[end..])) +} + +/// `at px px`. +fn center_of(argument: &str) -> Option { + let (x, rest) = number_after(argument, "at ")?; + let (y, rest) = number_after(rest.strip_prefix("px")?, "")?; + rest.starts_with("px").then_some(Point::new(x, y)) +} + +/// Shader for a CSS gradient painted in a `width` x `height` box moved by +/// `offset`, or `None` when `value` is not a gradient this renderer emits. +/// Colors are interpolated premultiplied, as CSS does. +pub fn gradient(value: Option<&str>, width: f32, height: f32, offset: (f32, f32)) -> Option { + let text = value?.trim(); + let open = text.find('(')?; + if !text.ends_with(')') { + return None; + } + let arguments = split_top_level(&text[open + 1..text.len() - 1]); + let first = *arguments.first()?; + let translation = Matrix::translate(offset); + match &text[..open] { + "linear-gradient" => { + let angled = first.ends_with("deg"); + let degrees = if angled { number(first.strip_suffix("deg")).unwrap_or(180.0) } else { 180.0 }; + let stop_texts = if angled { &arguments[1..] } else { &arguments[..] }; + let radians = f64::from(degrees).to_radians(); + let (direction_x, direction_y) = (radians.sin() as f32, -(radians.cos() as f32)); + let line_length = (width * direction_x).abs() + (height * direction_y).abs(); + let parsed: Vec> = stop_texts.iter().map(|text| parse_stop(text)).collect(); + let stops: Vec = if parsed.iter().all(Option::is_some) { + parsed.into_iter().flatten().collect() + } else { + // Stops without positions (a solid two-stop fallback) span the gradient line. + let last = stop_texts.len().saturating_sub(1).max(1) as f32; + stop_texts.iter().enumerate().filter_map(|(index, text)| Some(Stop { color: color(Some(text))?, position: line_length * index as f32 / last })).collect() + }; + if stops.len() < 2 { + return None; + } + let (start, end) = (stops[0].position, stops[stops.len() - 1].position); + if !(end - start > 0.0) { + return None; + } + let point = |distance: f32| Point::new(width / 2.0 + (distance - line_length / 2.0) * direction_x, height / 2.0 + (distance - line_length / 2.0) * direction_y); + let positions = monotonic(stops.iter().map(|stop| (stop.position - start) / (end - start))); + shaders::linear_gradient((point(start), point(end)), &Gradient::new(Colors::new(&colors_of(&stops), Some(&positions), TileMode::Clamp, None), premultiplied()), &translation) + } + "radial-gradient" => { + let center = center_of(first)?; + let stops: Vec = arguments[1..].iter().filter_map(|text| parse_stop(text)).collect(); + if stops.len() < 2 { + return None; + } + let radius = stops[stops.len() - 1].position; + if !(radius > 0.0) { + return None; + } + let positions = monotonic(stops.iter().map(|stop| (stop.position / radius).clamp(0.0, 1.0))); + shaders::radial_gradient((center, radius), &Gradient::new(Colors::new(&colors_of(&stops), Some(&positions), TileMode::Clamp, None), premultiplied()), &translation) + } + "conic-gradient" => { + let center = center_of(first)?; + let from = number_after(first, "from ").filter(|(_, rest)| rest.starts_with("deg")).map_or(0.0, |(degrees, _)| degrees); + let stops: Vec = arguments[1..].iter().filter_map(|text| parse_stop(text)).collect(); + if stops.len() < 2 { + return None; + } + let positions = monotonic(stops.iter().map(|stop| (stop.position / 360.0).clamp(0.0, 1.0))); + // CSS measures conic angles clockwise from 12 o'clock; a sweep gradient starts at 3 o'clock. + let mut matrix = translation; + matrix.pre_concat(&Matrix::rotate_deg_pivot(from - 90.0, center)); + shaders::sweep_gradient(center, (0.0, 360.0), &Gradient::new(Colors::new(&colors_of(&stops), Some(&positions), TileMode::Clamp, None), premultiplied()), &matrix) + } + _ => None, + } +} + +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Shadow { + pub dx: f32, + pub dy: f32, + pub blur: f32, + pub color: Color, +} + +/// `drop-shadow(Xpx Ypx Rpx color)` from a filter list; the color is a functional one. +pub fn drop_shadow(filter: Option<&str>) -> Option { + let filter = filter?; + let inner = &filter[filter.find("drop-shadow(")? + "drop-shadow(".len()..]; + let mut rest = inner.trim_start(); + let mut values = [0f32; 3]; + for value in &mut values { + let end = rest.find("px")?; + *value = number(Some(&rest[..end]))?; + let after = &rest[end + 2..]; + // The lengths are separated by whitespace. + if !after.starts_with(char::is_whitespace) { + return None; + } + rest = after.trim_start(); + } + let close = rest.find(')')?; + if !rest[close + 1..].trim_start().starts_with(')') { + return None; + } + Some(Shadow { dx: values[0], dy: values[1], blur: values[2], color: color(Some(&rest[..=close]))? }) +} + +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct BoxShadow { + pub inset: bool, + pub dx: f32, + pub dy: f32, + pub blur: f32, + pub spread: f32, + pub color: Color, +} + +/// `[inset] X Y blur [spread] color`. +pub fn box_shadow(value: Option<&str>) -> Option { + let mut text = value?.trim(); + let inset = text.starts_with("inset"); + if inset { + text = text["inset".len()..].trim(); + } + let color_start = text.find(|character: char| character.is_alphabetic() && character != 'p' && character != 'x')?; + let numbers: Vec = text[..color_start].split_whitespace().filter_map(|part| pixels(Some(part))).collect(); + let color = color(Some(&text[color_start..]))?; + if numbers.len() < 2 { + return None; + } + Some(BoxShadow { inset, dx: numbers[0], dy: numbers[1], blur: numbers.get(2).copied().unwrap_or(0.0), spread: numbers.get(3).copied().unwrap_or(0.0), color }) +} + +/// Every `name(arguments)` of a transform list, in order. +pub fn functions(value: &str) -> Vec<(&str, &str)> { + let mut result = Vec::new(); + let mut rest = value; + while let Some(open) = rest.find('(') { + let Some(close) = rest[open..].find(')') else { break }; + let name_start = rest[..open].rfind(|character: char| !character.is_ascii_alphanumeric()).map_or(0, |at| at + 1); + if name_start < open { + result.push((&rest[name_start..open], &rest[open + 1..open + close])); + } + rest = &rest[open + close + 1..]; + } + result +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_colors() { + assert_eq!(color(Some("#f00")), Some(Color::from_argb(255, 255, 0, 0))); + assert_eq!(color(Some("#11223380")), Some(Color::from_argb(0x80, 0x11, 0x22, 0x33))); + assert_eq!(color(Some("rgba(10, 20, 30, 0.5)")), Some(Color::from_argb(128, 10, 20, 30))); + assert_eq!(color(Some("rgb(10 , 20,30)")), Some(Color::from_argb(255, 10, 20, 30))); + assert_eq!(color(Some(" White ")), Some(Color::WHITE)); + for unknown in ["", "none", "#12", "#gggggg", "rgb(1,2)", "papayawhip", "rgb(a,b,c)"] { + assert_eq!(color(Some(unknown)), None, "{unknown}"); + } + assert_eq!(color(None), None); + } + + #[test] + fn reads_lengths() { + assert_eq!(pixels(Some("12px")), Some(12.0)); + assert_eq!(pixels(Some(" -1.5 ")), Some(-1.5)); + assert_eq!(pixels(Some("50%")), None); + assert_eq!(pixels(Some("auto")), None); + assert_eq!(pixels(Some("inf")), None); + assert_eq!(pixels(Some("nan")), None); + assert_eq!(length(Some("50%"), 80.0), Some(40.0)); + assert_eq!(length(Some("7px"), 80.0), Some(7.0)); + assert_eq!(number(Some("1e2")), Some(100.0)); + } + + #[test] + fn reads_shadows() { + assert_eq!(drop_shadow(Some("drop-shadow(1px -2px 3.5px rgba(0, 0, 0, 0.5))")), Some(Shadow { dx: 1.0, dy: -2.0, blur: 3.5, color: Color::from_argb(128, 0, 0, 0) })); + assert_eq!(drop_shadow(Some("blur(2px)")), None); + assert_eq!(drop_shadow(Some("drop-shadow(1px 2px red)")), None); + let outer = box_shadow(Some("0px 4px 12px rgba(0,0,0,0.25)")).unwrap(); + assert_eq!((outer.inset, outer.dx, outer.dy, outer.blur, outer.spread), (false, 0.0, 4.0, 12.0, 0.0)); + let inner = box_shadow(Some("inset 0 0 0 1px rgba(255,255,255,0.2)")).unwrap(); + assert_eq!((inner.inset, inner.spread, inner.color), (true, 1.0, Color::from_argb(51, 255, 255, 255))); + assert_eq!(box_shadow(Some("1px")), None); + } + + #[test] + fn splits_arguments_and_functions() { + assert_eq!(split_top_level("90deg, rgba(1, 2, 3, 1) 0px, #fff 10px"), ["90deg", "rgba(1, 2, 3, 1) 0px", "#fff 10px"]); + assert_eq!(functions("perspective(400px) rotate3d(0, 1, 0, 30deg) scale(1.5)"), [("perspective", "400px"), ("rotate3d", "0, 1, 0, 30deg"), ("scale", "1.5")]); + assert_eq!(center_of("from 90deg at 10px 20.5px"), Some(Point::new(10.0, 20.5))); + assert_eq!(center_of("circle at 1e1px -3px"), Some(Point::new(10.0, -3.0))); + assert_eq!(center_of("circle"), None); + } + + #[test] + fn builds_the_gradients_the_renderer_writes() { + assert!(gradient(Some("linear-gradient(90deg, rgba(255, 0, 0, 1) 0px, rgba(0, 0, 255, 1) 100px)"), 100.0, 50.0, (0.0, 0.0)).is_some()); + assert!(gradient(Some("linear-gradient(#ff0000, #0000ff)"), 100.0, 50.0, (0.0, 0.0)).is_some()); + assert!(gradient(Some("radial-gradient(circle at 50px 25px, rgba(255, 0, 0, 1) 0px, rgba(0, 0, 255, 1) 40px)"), 100.0, 50.0, (0.0, 0.0)).is_some()); + assert!(gradient(Some("conic-gradient(from 90deg at 50px 25px, rgba(255, 0, 0, 1) 0deg, rgba(0, 0, 255, 1) 360deg)"), 100.0, 50.0, (3.0, 4.0)).is_some()); + for other in ["none", "url(a.png)", "linear-gradient(90deg, red 0px)", "linear-gradient(90deg, red 5px, blue 5px)", "radial-gradient(circle, red 0px, blue 4px)"] { + assert!(gradient(Some(other), 100.0, 50.0, (0.0, 0.0)).is_none(), "{other}"); + } + } +} diff --git a/Linux/crates/sw-render/src/dom.rs b/Linux/crates/sw-render/src/dom.rs new file mode 100644 index 00000000..af360a67 --- /dev/null +++ b/Linux/crates/sw-render/src/dom.rs @@ -0,0 +1,70 @@ +//! The element tree the shared renderer records (see `serialize` in +//! `Android/core/src/main/js/render/entry.mjs`). + +use std::collections::HashMap; + +use serde_json::Value; +use sw_core::json::{Object, as_text}; + +/// One recorded element. +#[derive(Debug, Default)] +pub struct DomNode { + pub tag: String, + pub svg: bool, + pub style: HashMap, + pub attributes: HashMap, + pub dataset: HashMap, + pub text: Option, + pub children: Vec, +} + +impl DomNode { + pub fn from_json(json: &Object) -> Result { + Self::read(json, 0) + } + + fn read(json: &Object, depth: usize) -> Result { + // The layout nests a few dozen levels at most; a deeper tree is not one it wrote. + if depth > MAXIMUM_DEPTH { + return Err("The recorded element tree is nested too deeply".into()); + } + let mut children = Vec::new(); + if let Some(Value::Array(items)) = json.get("c") { + children.reserve(items.len()); + for item in items { + let Value::Object(child) = item else { return Err("A recorded element is not an object".into()) }; + children.push(Self::read(child, depth + 1)?); + } + } + Ok(Self { + tag: json.get("t").and_then(as_text).unwrap_or_default(), + svg: json.get("g").and_then(Value::as_i64) == Some(1), + style: string_map(json.get("s")), + attributes: string_map(json.get("a")), + dataset: string_map(json.get("d")), + text: json.get("x").map(|value| as_text(value).unwrap_or_default()), + children, + }) + } + + pub fn style(&self, name: &str) -> Option<&str> { + self.style.get(name).map(String::as_str) + } + + pub fn attribute(&self, name: &str) -> Option<&str> { + self.attributes.get(name).map(String::as_str) + } + + pub fn data(&self, name: &str) -> Option<&str> { + self.dataset.get(name).map(String::as_str) + } +} + +fn string_map(value: Option<&Value>) -> HashMap { + match value { + Some(Value::Object(object)) => object.iter().map(|(key, value)| (key.clone(), as_text(value).unwrap_or_default())).collect(), + _ => HashMap::new(), + } +} + +const MAXIMUM_DEPTH: usize = 512; diff --git a/Linux/crates/sw-render/src/fonts.rs b/Linux/crates/sw-render/src/fonts.rs new file mode 100644 index 00000000..695060fe --- /dev/null +++ b/Linux/crates/sw-render/src/fonts.rs @@ -0,0 +1,418 @@ +//! Resolves the CSS font-family strings the shared renderer emits to Skia +//! font families. Ported from `Windows/src/ScriptWidget.Core/Render/FontRegistry.cs`. +//! +//! The default face is the bundled Inter, so layout is the same on every +//! machine. Chinese, Japanese and Korean text and emoji come from the system +//! fonts through fontconfig. Which CJK face is tried first is decided here, +//! from the locale: left to fontconfig, a machine without a configured +//! preference shows Chinese text with Korean glyph shapes. + +use std::collections::HashMap; +use std::rc::Rc; + +use skia_safe::font_arguments::variation_position::Coordinate; +use skia_safe::font_arguments::VariationPosition; +use skia_safe::font_style::{Slant, Width}; +use skia_safe::textlayout::{FontCollection, TypefaceFontProvider}; +use skia_safe::{Font, FontArguments, FontMgr, FontStyle, FourByteTag, Typeface}; + +/// The font that ships inside the program: Inter, the file the Android app +/// ships, embedded at build time from its asset folder. It is under the SIL +/// Open Font License (OFL-Inter.txt in that folder). +const INTER: &[u8] = include_bytes!("../../../../Android/core/src/main/assets/fonts/Inter.ttf"); + +/// Display fonts selectable with `font="custom,,"`: the names a +/// script may use, the alias the face is registered under, and the file name. +/// +/// Unlike on Android and Windows these six are NOT inside the program: the +/// repository records no licence for them, so a Linux release does not +/// redistribute them. A file with one of these names in the optional font +/// folder (see [`display_font_folder`]) is used; without it the text is drawn +/// with Inter. +const DISPLAY_FONTS: &[(&[&str], &str, &str)] = &[ + (&["apple ][", "apple"], "sw-display-apple", "Apple ][.ttf"), + (&["larabiefont free", "larabiefontfree"], "sw-display-larabiefont", "Larabiefont Free.ttf"), + (&["monofonto"], "sw-display-monofonto", "Monofonto.ttf"), + (&["white rabbit", "whiterabbit", "new"], "sw-display-whiterabbit", "New.ttf"), + (&["oneslot"], "sw-display-oneslot", "OneSlot.ttf"), + (&["unispace"], "sw-display-unispace", "Unispace.ttf"), +]; + +/// A display font file larger than this is ignored. +const MAXIMUM_DISPLAY_FONT_BYTES: u64 = 16 * 1024 * 1024; + +/// Where optional display fonts are looked for: the folder `SCRIPTWIDGET_FONTS` +/// names, else `fonts` next to the `runtime` folder of the installed +/// resources (`share/scriptwidget/fonts`). `None` when neither exists. Unit +/// tests of this crate read the Android asset folder of the repository. +pub fn display_font_folder() -> Option { + static FOLDER: std::sync::OnceLock> = std::sync::OnceLock::new(); + FOLDER + .get_or_init(|| { + if let Some(value) = std::env::var_os("SCRIPTWIDGET_FONTS").filter(|value| !value.is_empty()) { + let folder = std::path::PathBuf::from(value); + return folder.is_absolute().then_some(folder); + } + if cfg!(test) { + return Some(std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../Android/core/src/main/assets/fonts")); + } + let resources = sw_core::Resources::locate().ok()?; + let folder = resources.runtime.parent()?.join("fonts"); + folder.is_dir().then_some(folder) + }) + .clone() +} + +/// The bytes of an optional display font, or `None` when it is not installed. +fn display_font_bytes(file: &str) -> Option> { + let path = display_font_folder()?.join(file); + let metadata = std::fs::symlink_metadata(&path).ok()?; + if !metadata.is_file() || metadata.len() > MAXIMUM_DISPLAY_FONT_BYTES { + return None; + } + std::fs::read(path).ok() +} + +/// System families tried for the generic names: the one the user configured +/// for the generic name in fontconfig first, then common ones by name. +const MONOSPACE: &[&str] = &["monospace", "DejaVu Sans Mono", "Liberation Mono", "Noto Sans Mono", "Menlo", "Courier New"]; +const SERIF: &[&str] = &["serif", "Noto Serif", "DejaVu Serif", "Liberation Serif", "Georgia", "Times New Roman"]; +const SANS_SERIF: &[&str] = &["sans-serif", "Noto Sans", "DejaVu Sans", "Liberation Sans", "Helvetica Neue", "Arial"]; +/// Color emoji families, the usual Linux one first. +const EMOJI: &[&str] = &["Noto Color Emoji", "Twemoji", "JoyPixels", "Apple Color Emoji", "Segoe UI Emoji"]; +/// Names that fontconfig maps to whatever the user configured; other font managers do not know them. +const GENERIC_FAMILIES: &[&str] = &["monospace", "serif", "sans-serif"]; + +/// The script a locale reads Han characters in. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CjkRegion { + SimplifiedChinese, + TraditionalChinese, + HongKong, + Japanese, + Korean, +} + +impl CjkRegion { + /// Families that carry this region's glyph shapes, the usual Linux ones first. + fn families(self) -> &'static [&'static str] { + match self { + Self::SimplifiedChinese => &["Noto Sans CJK SC", "Noto Sans SC", "Source Han Sans SC", "Source Han Sans CN", "WenQuanYi Micro Hei", "WenQuanYi Zen Hei", "PingFang SC", "Microsoft YaHei"], + Self::TraditionalChinese => &["Noto Sans CJK TC", "Noto Sans TC", "Source Han Sans TC", "Source Han Sans TW", "PingFang TC", "Microsoft JhengHei"], + Self::HongKong => &["Noto Sans CJK HK", "Noto Sans HK", "Source Han Sans HC", "Source Han Sans HK", "PingFang HK"], + Self::Japanese => &["Noto Sans CJK JP", "Noto Sans JP", "Source Han Sans JP", "Source Han Sans", "IPAGothic", "Hiragino Sans"], + Self::Korean => &["Noto Sans CJK KR", "Noto Sans KR", "Source Han Sans KR", "Source Han Sans K", "NanumGothic", "Apple SD Gothic Neo"], + } + } +} + +/// The order in which CJK faces are tried for a BCP 47 locale tag. A Han +/// character exists in every one of them, so the first decides its shape: +/// the reader's own region comes first, and Simplified Chinese is the +/// default for every locale that is not Chinese, Japanese or Korean, as on +/// Android, where the bundled fallback is Noto Sans SC. +pub fn cjk_region_order(locale: &str) -> [CjkRegion; 5] { + use CjkRegion::{HongKong, Japanese, Korean, SimplifiedChinese, TraditionalChinese}; + let lower = locale.to_ascii_lowercase().replace('_', "-"); + let mut parts = lower.split('-'); + let language = parts.next().unwrap_or(""); + let rest: Vec<&str> = parts.collect(); + let has = |subtag: &str| rest.contains(&subtag); + match language { + "ja" => [Japanese, SimplifiedChinese, TraditionalChinese, HongKong, Korean], + "ko" => [Korean, SimplifiedChinese, TraditionalChinese, HongKong, Japanese], + "zh" | "yue" | "cmn" => { + // The script subtag decides; without one, the region does. + let traditional = has("hant") || (!has("hans") && (has("tw") || has("hk") || has("mo") || language == "yue")); + if traditional && (has("hk") || has("mo") || language == "yue") { + [HongKong, TraditionalChinese, SimplifiedChinese, Japanese, Korean] + } else if traditional { + [TraditionalChinese, HongKong, SimplifiedChinese, Japanese, Korean] + } else { + [SimplifiedChinese, TraditionalChinese, HongKong, Japanese, Korean] + } + } + _ => [SimplifiedChinese, TraditionalChinese, HongKong, Japanese, Korean], + } +} + +/// One resolved face: the families a paragraph asks for, in order, the +/// weight, and the ascent and descent of the first family as fractions of +/// the font size. +#[derive(Debug)] +pub struct Face { + /// Names registered with the paragraph's own font provider, the face of the style first, then the CJK faces. + pub families: Vec, + /// The real family name of the first face; empty when the style has no face of its own on this machine. + pub primary: String, + pub weight: i32, + pub ascent: f32, + pub descent: f32, +} + +/// The name and the face of a system family at `weight`, or `None` when it +/// is not installed. Asking for a family that is missing does not fail: +/// fontconfig answers with its nearest substitute, so the answer only counts +/// when it is the family that was asked for. A generic name is answered with +/// the family the user configured for it. +fn installed(system: &FontMgr, family: &str, weight: i32) -> Option<(String, Typeface)> { + let generic = GENERIC_FAMILIES.contains(&family); + if generic && !cfg!(all(unix, not(target_os = "macos"))) { + return None; + } + let typeface = system.match_family_style(family, FontStyle::new(weight.into(), Width::NORMAL, Slant::Upright))?; + let name = typeface.family_name(); + (generic || name.eq_ignore_ascii_case(family)).then_some((name, typeface)) +} + +/// Every face a paragraph may name is registered here under a name of its +/// own, the system's faces included. A paragraph that named a system family +/// directly would have Skia look it up in fontconfig's list for that name, +/// which on a machine with substitution rules also holds other families, and +/// the face it picks from that list is not always the one that was asked for +/// (on the test machine it picked none, and text fell to the last-resort +/// fallback). The faces registered here are the ones fontconfig's own match +/// returns. +pub struct FontRegistry { + collection: FontCollection, + provider: TypefaceFontProvider, + system: FontMgr, + /// Makes typefaces from the bundled files. + loader: FontMgr, + inter: Option, + /// Names already registered with `provider`, with their faces; `None` for a face that could not be made. + registered: HashMap>, + cache: HashMap>, + use_system_font: bool, + locale: String, + cjk: Vec, +} + +impl FontRegistry { + /// `locale` is a BCP 47 tag; it orders the CJK faces (see [`cjk_region_order`]). + pub fn new(locale: &str) -> Self { + let system = FontMgr::new(); + // FreeType reads the bundled files on every platform that has it, so their metrics do not depend on the system. + let loader = FontMgr::custom_empty().unwrap_or_else(|| system.clone()); + let provider = TypefaceFontProvider::new(); + let mut collection = FontCollection::new(); + collection.set_asset_font_manager(Some(provider.clone().into())); + // Only for what the registered faces lack: emoji and other scripts. + collection.set_default_font_manager(system.clone(), None); + let inter = loader.new_from_bytes(INTER, None); + let mut registry = Self { collection, provider, system, loader, inter, registered: HashMap::new(), cache: HashMap::new(), use_system_font: false, locale: String::new(), cjk: Vec::new() }; + registry.set_locale(locale); + registry + } + + pub fn collection(&self) -> &FontCollection { + &self.collection + } + + pub fn locale(&self) -> &str { + &self.locale + } + + /// The installed CJK families, in the order they are tried. + pub fn cjk_families(&self) -> &[String] { + &self.cjk + } + + /// True when the bundled Inter could be loaded. + pub fn has_bundled_font(&self) -> bool { + self.inter.is_some() + } + + pub fn set_locale(&mut self, locale: &str) { + if locale == self.locale && !self.locale.is_empty() { + return; + } + self.locale = if locale.is_empty() { "en-US".into() } else { locale.to_owned() }; + let mut cjk = Vec::new(); + for region in cjk_region_order(&self.locale) { + for family in region.families() { + if let Some((name, _)) = installed(&self.system, family, 400) { + cjk.push(name); + } + } + } + self.cjk = cjk; + self.cache.clear(); + } + + /// When true, default-design text uses the machine's own sans-serif face. + pub fn set_use_system_font(&mut self, value: bool) { + if value != self.use_system_font { + self.use_system_font = value; + self.cache.clear(); + } + } + + pub fn face(&mut self, family: &str, weight: i32) -> Rc { + let clamped = weight.clamp(100, 900); + let key = format!("{family}|{clamped}"); + if let Some(face) = self.cache.get(&key) { + return face.clone(); + } + if self.cache.len() > MAXIMUM_FACES { + self.cache.clear(); + } + let face = Rc::new(self.resolve(family, clamped)); + self.cache.insert(key, face.clone()); + face + } + + fn resolve(&mut self, family: &str, weight: i32) -> Face { + let first = family.split(',').next().unwrap_or("").trim().trim_matches('"'); + let lower = first.to_lowercase(); + if let Some((_, alias, file)) = DISPLAY_FONTS.iter().find(|(names, _, _)| names.contains(&lower.as_str())) { + // A display font has one face; its weight is not synthesized, as on Android. + // It is optional on Linux: without the file the text is drawn with Inter. + let loader = self.loader.clone(); + if let Some(typeface) = self.register(alias, || display_font_bytes(file).and_then(|data| loader.new_from_bytes(&data, None))) { + return self.finish(Some(((*alias).to_owned(), typeface)), 400); + } + return self.bundled(weight); + } + if lower == "ui-monospace" || lower.contains("mono") || lower == "consolas" { + return self.system_face(MONOSPACE, weight); + } + if lower == "new york" || lower == "ui-serif" { + return self.system_face(SERIF, weight); + } + if self.use_system_font { + return self.system_face(SANS_SERIF, weight); + } + self.bundled(weight) + } + + /// Inter is a variable font: each weight is its own instance, registered under its own name. + fn bundled(&mut self, weight: i32) -> Face { + let alias = format!("sw-inter-{weight}"); + let inter = self.inter.clone(); + let instance = self.register(&alias, || { + let inter = inter?; + let coordinates = [Coordinate { axis: FourByteTag::from_chars('w', 'g', 'h', 't'), value: weight as f32 }]; + let arguments = FontArguments::new().set_variation_design_position(VariationPosition { coordinates: &coordinates }); + inter.clone_with_arguments(&arguments).or(Some(inter)) + }); + match instance { + Some(typeface) => self.finish(Some((alias, typeface)), weight), + None => self.system_face(SANS_SERIF, weight), + } + } + + fn register(&mut self, alias: &str, make: impl FnOnce() -> Option) -> Option { + if let Some(existing) = self.registered.get(alias) { + return existing.clone(); + } + let typeface = make(); + if let Some(typeface) = &typeface { + self.provider.register_typeface(typeface.clone(), Some(alias)); + } + self.registered.insert(alias.to_owned(), typeface.clone()); + typeface + } + + /// The system family at `weight`, registered under a name of its own. + fn system_family(&mut self, family: &str, weight: i32) -> Option<(String, Typeface)> { + let alias = format!("sw-system-{weight}-{}", family.to_lowercase()); + let system = self.system.clone(); + let typeface = self.register(&alias, || installed(&system, family, weight).map(|(_, typeface)| typeface))?; + Some((alias, typeface)) + } + + /// The registered name of the color emoji face, or `None` when the machine has none of the known ones. + pub fn emoji_family(&mut self) -> Option { + EMOJI.iter().find_map(|family| self.system_family(family, 400)).map(|(alias, _)| alias) + } + + fn system_face(&mut self, candidates: &[&str], weight: i32) -> Face { + let first = candidates.iter().find_map(|family| self.system_family(family, weight)); + // With no candidate installed the text falls to the CJK faces and the system's fallback. + self.finish(first, weight) + } + + fn finish(&mut self, first: Option<(String, Typeface)>, weight: i32) -> Face { + let (primary, ascent, descent) = match &first { + Some((_, typeface)) => { + let mut font = Font::from_typeface(typeface, METRICS_SIZE); + font.set_linear_metrics(true).set_hinting(skia_safe::FontHinting::None).set_subpixel(true); + let (_, metrics) = font.metrics(); + (typeface.family_name(), -metrics.ascent / METRICS_SIZE, metrics.descent / METRICS_SIZE) + } + // Typical metrics stand in for the unknown face. + None => (String::new(), 1.079, 0.251), + }; + let mut families: Vec = first.into_iter().map(|(alias, _)| alias).collect(); + for family in self.cjk.clone() { + if let Some((alias, _)) = self.system_family(&family, weight) { + families.push(alias); + } + } + Face { families, primary, weight, ascent, descent } + } +} + +const MAXIMUM_FACES: usize = 512; +/// Font metrics scale with the size; they are read once at this size. +const METRICS_SIZE: f32 = 100.0; + +#[cfg(test)] +mod tests { + use super::CjkRegion::{HongKong, Japanese, Korean, SimplifiedChinese, TraditionalChinese}; + use super::*; + + #[test] + fn the_readers_region_comes_first() { + for locale in ["zh-Hans", "zh-CN", "zh", "zh-Hans-HK", "zh_CN", "zh-SG", "cmn-Hans"] { + assert_eq!(cjk_region_order(locale)[0], SimplifiedChinese, "{locale}"); + } + for locale in ["zh-Hant", "zh-TW", "zh-Hant-TW", "zh_TW"] { + assert_eq!(cjk_region_order(locale)[..2], [TraditionalChinese, HongKong], "{locale}"); + } + for locale in ["zh-HK", "zh-Hant-HK", "zh-MO", "yue", "yue-Hant-HK"] { + assert_eq!(cjk_region_order(locale)[..2], [HongKong, TraditionalChinese], "{locale}"); + } + assert_eq!(cjk_region_order("ja-JP")[0], Japanese); + assert_eq!(cjk_region_order("ko")[0], Korean); + } + + #[test] + fn other_locales_read_han_characters_as_simplified_chinese() { + for locale in ["en-US", "de-DE", "", "C", "fr"] { + assert_eq!(cjk_region_order(locale), [SimplifiedChinese, TraditionalChinese, HongKong, Japanese, Korean], "{locale}"); + } + // Korean is never ahead of a Chinese face unless the locale is Korean. + for locale in ["en-US", "zh-CN", "zh-TW", "zh-HK", "ja-JP"] { + assert_eq!(cjk_region_order(locale)[4], Korean, "{locale}"); + } + } + + #[test] + fn every_region_is_tried() { + for locale in ["en", "zh-TW", "zh-HK", "ja", "ko"] { + let order = cjk_region_order(locale); + for region in [SimplifiedChinese, TraditionalChinese, HongKong, Japanese, Korean] { + assert!(order.contains(®ion), "{locale}: {region:?}"); + } + } + } + + #[test] + fn bundled_fonts_load_and_weights_are_separate_instances() { + let mut fonts = FontRegistry::new("en-US"); + assert!(fonts.has_bundled_font()); + let regular = fonts.face("-apple-system, sans-serif", 400); + let bold = fonts.face("-apple-system, sans-serif", 700); + assert_eq!(regular.families[0], "sw-inter-400"); + assert_eq!(bold.families[0], "sw-inter-700"); + assert!(regular.ascent > 0.7 && regular.ascent < 1.3 && regular.descent > 0.1 && regular.descent < 0.5, "{regular:?}"); + // Out-of-range weights are clamped, and a face is resolved once. + assert!(Rc::ptr_eq(&fonts.face("x", 1200), &fonts.face("x", 900))); + for (name, alias) in [("Monofonto, sans-serif", "sw-display-monofonto"), ("\"Apple ][\"", "sw-display-apple"), ("New", "sw-display-whiterabbit"), ("OneSlot", "sw-display-oneslot"), ("unispace", "sw-display-unispace"), ("Larabiefont Free", "sw-display-larabiefont")] { + let face = fonts.face(name, 700); + assert_eq!((face.families[0].as_str(), face.weight), (alias, 400), "{name}"); + } + } +} diff --git a/Linux/crates/sw-render/src/gif.rs b/Linux/crates/sw-render/src/gif.rs new file mode 100644 index 00000000..912d224f --- /dev/null +++ b/Linux/crates/sw-render/src/gif.rs @@ -0,0 +1,286 @@ +//! Decodes a GIF into whole frames with their delays, for a display layer +//! that cycles through them. The data comes from a package and is not +//! trusted: its size, the canvas, the number of frames and the memory all +//! frames take together are bounded here, as +//! `Windows/src/ScriptWidget.Core/Render/GifFrames.cs` bounds them. Unlike +//! that sampler, every frame is kept with its own delay. + +use skia_safe::codec::codec_animation::DisposalMethod; +use skia_safe::codec::{self, Codec, Options, ZeroInitialized, gif_decoder}; +use skia_safe::{AlphaType, ColorType, Data, ImageInfo}; + +/// A larger file is not opened. +pub const MAXIMUM_INPUT_BYTES: usize = 8 * 1024 * 1024; +/// Frames after this many are dropped. +pub const MAXIMUM_FRAMES: usize = 60; +/// The canvas of the GIF: a larger one is refused. +pub const MAXIMUM_CANVAS_PIXELS: i64 = 16 * 1024 * 1024; +/// All kept frames together; a GIF that needs more shows its first frame only. +pub const MAXIMUM_TOTAL_PIXELS: i64 = 16 * 1024 * 1024; +/// Browsers show a frame with no delay, or one too short to see, for this long. +const DEFAULT_DELAY_MILLISECONDS: u32 = 100; +const MINIMUM_DELAY_MILLISECONDS: u32 = 20; + +/// Size and timing of a GIF, read without decoding pixels. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct GifTimeline { + pub width: u32, + pub height: u32, + /// How long each kept frame is shown, in milliseconds. One entry for a still image. + pub delays: Vec, +} + +impl GifTimeline { + pub fn is_animated(&self) -> bool { + self.delays.len() > 1 + } + + /// The length of one pass through the frames. + pub fn duration(&self) -> u64 { + self.delays.iter().map(|delay| u64::from(*delay)).sum() + } + + /// The frame on screen `milliseconds` after the start; the animation repeats. + pub fn frame_at(&self, milliseconds: u64) -> usize { + let duration = self.duration(); + if duration == 0 || self.delays.len() < 2 { + return 0; + } + let mut time = milliseconds % duration; + for (index, delay) in self.delays.iter().enumerate() { + if time < u64::from(*delay) { + return index; + } + time -= u64::from(*delay); + } + self.delays.len() - 1 + } +} + +/// One whole frame: `width * height * 4` bytes of premultiplied RGBA, rows from the top. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct GifFrame { + pub pixels: Vec, + pub delay: u32, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct GifAnimation { + pub width: u32, + pub height: u32, + pub frames: Vec, +} + +impl GifAnimation { + pub fn timeline(&self) -> GifTimeline { + GifTimeline { width: self.width, height: self.height, delays: self.frames.iter().map(|frame| frame.delay).collect() } + } +} + +pub fn is_gif(data: &[u8]) -> bool { + data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") +} + +fn open(data: &[u8]) -> Option<(Codec<'static>, GifTimeline)> { + if data.len() > MAXIMUM_INPUT_BYTES || !is_gif(data) { + return None; + } + let mut codec = Codec::from_data_with_decoders(Data::new_copy(data), &[gif_decoder::decoder()])?; + let size = codec.dimensions(); + if size.width <= 0 || size.height <= 0 || i64::from(size.width) * i64::from(size.height) > MAXIMUM_CANVAS_PIXELS { + return None; + } + let frame_pixels = i64::from(size.width) * i64::from(size.height); + let mut count = codec.get_frame_count().clamp(1, MAXIMUM_FRAMES); + if frame_pixels * count as i64 > MAXIMUM_TOTAL_PIXELS { + count = 1; + } + let mut delays = Vec::with_capacity(count); + for index in 0..count { + let delay = codec.get_frame_info(index).map_or(0, |info| info.duration.max(0) as u32); + delays.push(if delay < MINIMUM_DELAY_MILLISECONDS { DEFAULT_DELAY_MILLISECONDS } else { delay }); + } + Some((codec, GifTimeline { width: size.width as u32, height: size.height as u32, delays })) +} + +/// Size and timing, or `None` when the data is not a GIF this module accepts. +pub fn inspect(data: &[u8]) -> Option { + open(data).map(|(_, timeline)| timeline) +} + +/// Every kept frame, composed onto the frames before it, or `None` when the +/// data is not a GIF this module accepts or cannot be decoded. +pub fn decode(data: &[u8]) -> Option { + decode_frames(data, usize::MAX, true) +} + +/// Frame `index` alone (the last frame when there are fewer), with the timing of the whole GIF. +/// The frames before it are decoded too, because it is drawn over them, but not kept. +pub fn decode_frame(data: &[u8], index: usize) -> Option<(GifTimeline, Vec)> { + let mut animation = decode_frames(data, index.saturating_add(1), false)?; + let frame = animation.frames.pop()?; + Some((inspect(data)?, frame.pixels)) +} + +/// The first `limit` frames; with `keep_all` false only the last of them is returned. +fn decode_frames(data: &[u8], limit: usize, keep_all: bool) -> Option { + let (mut codec, timeline) = open(data)?; + let info = ImageInfo::new((timeline.width as i32, timeline.height as i32), ColorType::RGBA8888, AlphaType::Premul, None); + let row_bytes = info.min_row_bytes(); + let byte_count = info.compute_byte_size(row_bytes); + let mut frames: Vec = Vec::new(); + // What the previous frame left on the canvas, when the next frame may be drawn over it. + let mut prior: Option = None; + let mut canvas = vec![0u8; byte_count]; + for (index, delay) in timeline.delays.iter().enumerate().take(limit) { + let frame_info = codec.get_frame_info(index); + // The canvas can be reused when the frame that is in it is one the new frame may be blended with. + let reusable = match (prior, frame_info.as_ref()) { + (Some(prior), Some(frame)) => frame.required_frame != codec::NO_FRAME && frame.required_frame as usize <= prior, + _ => false, + }; + if !reusable { + canvas.fill(0); + prior = None; + } + let options = Options { zero_initialized: if reusable { ZeroInitialized::No } else { ZeroInitialized::Yes }, subset: None, frame_index: index, prior_frame: prior, max_decode_memory: Some(MAXIMUM_DECODE_BYTES) }; + match codec.get_pixels_with_options(&info, &mut canvas, row_bytes, Some(&options)) { + codec::Result::Success => {} + // A damaged later frame ends the animation; what was decoded before is kept. + _ if index > 0 => break, + // The first frame of a cut-off file is shown as far as it goes. + codec::Result::IncompleteInput => {} + _ => return None, + } + if !keep_all { + frames.clear(); + } + frames.push(GifFrame { pixels: canvas.clone(), delay: *delay }); + let keeps = frame_info.is_some_and(|frame| frame.disposal_method != DisposalMethod::RestorePrevious); + prior = if keeps { Some(index) } else { None }; + } + if frames.is_empty() { + return None; + } + Some(GifAnimation { width: timeline.width, height: timeline.height, frames }) +} + +/// What the decoder may allocate besides the frames it writes into. +const MAXIMUM_DECODE_BYTES: usize = 64 * 1024 * 1024; + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + + /// A GIF of `frames` frames, each one solid color from a four-entry + /// palette (black, red, green, blue), `width` x `height`, uncompressed LZW. + pub(crate) fn gif(width: u16, height: u16, frames: &[(u8, u16)]) -> Vec { + let mut data = Vec::new(); + data.extend_from_slice(b"GIF89a"); + data.extend_from_slice(&width.to_le_bytes()); + data.extend_from_slice(&height.to_le_bytes()); + // A global color table of four entries, color resolution 2 bits. + data.extend_from_slice(&[0x91, 0, 0]); + data.extend_from_slice(&[0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255]); + // Loop forever. + data.extend_from_slice(&[0x21, 0xFF, 0x0B]); + data.extend_from_slice(b"NETSCAPE2.0"); + data.extend_from_slice(&[3, 1, 0, 0, 0]); + for (color, delay) in frames { + data.extend_from_slice(&[0x21, 0xF9, 4, 0]); + data.extend_from_slice(&delay.to_le_bytes()); + data.extend_from_slice(&[0, 0]); + data.push(0x2C); + data.extend_from_slice(&[0, 0, 0, 0]); + data.extend_from_slice(&width.to_le_bytes()); + data.extend_from_slice(&height.to_le_bytes()); + data.push(0); + // Codes of 3 bits (minimum code size 2): a clear code before every pixel keeps them 3 bits wide. + data.push(2); + let mut bits: Vec = Vec::new(); + let mut push = |code: u8| (0..3).for_each(|bit| bits.push(code >> bit & 1 == 1)); + for _ in 0..usize::from(width) * usize::from(height) { + push(4); + push(*color); + } + push(5); + let bytes: Vec = bits.chunks(8).map(|chunk| chunk.iter().enumerate().fold(0u8, |byte, (bit, set)| byte | (u8::from(*set) << bit))).collect(); + for block in bytes.chunks(255) { + data.push(block.len() as u8); + data.extend_from_slice(block); + } + data.push(0); + } + data.push(0x3B); + data + } + + #[test] + fn decodes_every_frame_with_its_delay() { + let data = gif(4, 3, &[(1, 10), (2, 25), (3, 0)]); + let timeline = inspect(&data).expect("timeline"); + assert_eq!((timeline.width, timeline.height), (4, 3)); + // Hundredths of a second become milliseconds; no delay becomes the browsers' 100 ms. + assert_eq!(timeline.delays, [100, 250, 100]); + assert!(timeline.is_animated()); + let animation = decode(&data).expect("frames"); + assert_eq!(animation.timeline(), timeline); + for (frame, color) in animation.frames.iter().zip([[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255]]) { + assert_eq!(frame.pixels.len(), 4 * 3 * 4); + assert!(frame.pixels.chunks(4).all(|pixel| pixel == color), "{:?}", &frame.pixels[..8]); + } + let (_, second) = decode_frame(&data, 1).expect("second frame"); + assert_eq!(second, animation.frames[1].pixels); + } + + #[test] + fn the_frame_on_screen_follows_the_delays_and_repeats() { + let timeline = GifTimeline { width: 1, height: 1, delays: vec![100, 250, 100] }; + assert_eq!(timeline.duration(), 450); + for (time, frame) in [(0, 0), (99, 0), (100, 1), (349, 1), (350, 2), (449, 2), (450, 0), (450 * 7 + 120, 1)] { + assert_eq!(timeline.frame_at(time), frame, "{time}"); + } + assert_eq!(GifTimeline { width: 1, height: 1, delays: vec![40] }.frame_at(1000), 0); + } + + #[test] + fn keeps_no_more_frames_than_the_limit() { + let frames: Vec<(u8, u16)> = (0..75).map(|index| (1 + index % 3, 5)).collect(); + let data = gif(2, 2, &frames); + assert_eq!(inspect(&data).expect("timeline").delays.len(), MAXIMUM_FRAMES); + assert_eq!(decode(&data).expect("frames").frames.len(), MAXIMUM_FRAMES); + } + + #[test] + fn refuses_what_is_not_a_bounded_gif() { + assert_eq!(inspect(b"not a gif"), None); + assert_eq!(decode(&[]), None); + // A PNG signature. + assert_eq!(decode(&[0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A]), None); + // A header that claims a 60000 x 60000 canvas is refused before any pixel is allocated. + let mut huge = gif(2, 2, &[(1, 10), (2, 10)]); + huge[6..8].copy_from_slice(&60000u16.to_le_bytes()); + huge[8..10].copy_from_slice(&60000u16.to_le_bytes()); + assert_eq!(inspect(&huge), None); + assert_eq!(decode(&huge), None); + // A file over the size limit is not opened. + let mut long = gif(2, 2, &[(1, 10)]); + long.resize(MAXIMUM_INPUT_BYTES + 1, 0); + assert_eq!(inspect(&long), None); + // Cut off in the middle of the first frame's header: nothing to show, and no panic. + let whole = gif(2, 2, &[(1, 10), (2, 10)]); + let _ = decode(&whole[..40]); + let _ = decode(&whole[..whole.len() - 12]); + } + + #[test] + fn a_gif_whose_frames_exceed_the_memory_budget_shows_its_first_frame() { + // 3000 x 3000 is 9 million pixels a frame: two frames are over the budget of 16 million. + let mut data = gif(2, 2, &[(1, 10), (2, 10)]); + data[6..8].copy_from_slice(&3000u16.to_le_bytes()); + data[8..10].copy_from_slice(&3000u16.to_le_bytes()); + let timeline = inspect(&data).expect("timeline"); + assert_eq!((timeline.width, timeline.height, timeline.delays.len()), (3000, 3000, 1)); + assert!(!timeline.is_animated()); + } +} diff --git a/Linux/crates/sw-render/src/gif_cache.rs b/Linux/crates/sw-render/src/gif_cache.rs new file mode 100644 index 00000000..9ff7c881 --- /dev/null +++ b/Linux/crates/sw-render/src/gif_cache.rs @@ -0,0 +1,128 @@ +//! Decoded GIF frames kept between renders, for a caller that draws every +//! frame of an animation one after the other (the background service). +//! +//! Showing frame N of a GIF means decoding frames 0 to N, because a frame is +//! drawn over the ones before it. Drawing all N frames of a widget, one +//! render each, would decode N²/2 frames. While a [`FrameCacheScope`] is +//! alive, the first render of a GIF decodes it once and the following renders +//! take their frame from here. Outside a scope nothing is kept and every +//! render decodes for itself, as before. +//! +//! The cache is bounded by [`gif::MAXIMUM_TOTAL_PIXELS`] over all the GIFs in +//! it, the same bound one GIF has, and is emptied when the last scope ends. +//! A GIF is recognised by a keyed hash of its bytes; the key is random per +//! process, so package data cannot be made to collide with another GIF. + +use std::collections::VecDeque; +use std::hash::{BuildHasher, RandomState}; +use std::sync::{Arc, Mutex, OnceLock}; + +use crate::gif::{self, GifAnimation, GifTimeline}; + +struct Entry { + length: usize, + hash: u64, + animation: Arc, +} + +#[derive(Default)] +struct Cache { + scopes: usize, + entries: VecDeque, +} + +fn cache() -> &'static Mutex { + static CACHE: OnceLock> = OnceLock::new(); + CACHE.get_or_init(Mutex::default) +} + +fn hasher() -> &'static RandomState { + static HASHER: OnceLock = OnceLock::new(); + HASHER.get_or_init(RandomState::new) +} + +fn pixels(animation: &GifAnimation) -> i64 { + i64::from(animation.width) * i64::from(animation.height) * animation.frames.len() as i64 +} + +/// Keeps decoded GIF frames while it is alive. Scopes nest; the frames are dropped when the last one ends. +pub struct FrameCacheScope(()); + +impl FrameCacheScope { + pub fn new() -> Self { + cache().lock().unwrap_or_else(|poisoned| poisoned.into_inner()).scopes += 1; + Self(()) + } +} + +impl Default for FrameCacheScope { + fn default() -> Self { + Self::new() + } +} + +impl Drop for FrameCacheScope { + fn drop(&mut self) { + let mut cache = cache().lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + cache.scopes = cache.scopes.saturating_sub(1); + if cache.scopes == 0 { + cache.entries.clear(); + } + } +} + +/// [`gif::decode_frame`], answered from the cache while a [`FrameCacheScope`] is alive. +pub fn frame(data: &[u8], index: usize) -> Option<(GifTimeline, Vec)> { + if cache().lock().unwrap_or_else(|poisoned| poisoned.into_inner()).scopes == 0 { + return gif::decode_frame(data, index); + } + let hash = hasher().hash_one(data); + let cached = { + let cache = cache().lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + cache.entries.iter().find(|entry| entry.length == data.len() && entry.hash == hash).map(|entry| entry.animation.clone()) + }; + let animation = match cached { + Some(animation) => animation, + None => { + // Decoded outside the lock: another thread's render is not held up by this one's GIF. + let animation = Arc::new(gif::decode(data)?); + let mut cache = cache().lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + if cache.scopes > 0 { + cache.entries.push_back(Entry { length: data.len(), hash, animation: animation.clone() }); + while cache.entries.len() > 1 && cache.entries.iter().map(|entry| pixels(&entry.animation)).sum::() > gif::MAXIMUM_TOTAL_PIXELS { + cache.entries.pop_front(); + } + } + animation + } + }; + let frame = animation.frames.get(index.min(animation.frames.len().checked_sub(1)?))?; + // The timing of the whole GIF, as `decode_frame` reports it, also when a damaged later frame cut the decoding short. + Some((gif::inspect(data)?, frame.pixels.clone())) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_scope_answers_like_the_decoder_and_forgets_when_it_ends() { + let data = gif::tests::gif(4, 4, &[(1, 10), (2, 20), (3, 30)]); + for index in [0, 1, 2, 9] { + assert_eq!(frame(&data, index), gif::decode_frame(&data, index), "no scope, frame {index}"); + } + { + let _scope = FrameCacheScope::new(); + for index in [2, 0, 1, 9] { + assert_eq!(frame(&data, index), gif::decode_frame(&data, index), "in a scope, frame {index}"); + } + let held = cache().lock().unwrap().entries.iter().filter(|entry| entry.length == data.len()).count(); + assert_eq!(held, 1, "decoded once, kept once"); + assert_eq!(frame(b"not a gif", 0), None); + } + // Other tests may hold scopes of their own; this GIF's scope has ended. + if cache().lock().unwrap().scopes == 0 { + assert!(cache().lock().unwrap().entries.is_empty()); + } + } +} diff --git a/Linux/crates/sw-render/src/images.rs b/Linux/crates/sw-render/src/images.rs new file mode 100644 index 00000000..81fcbffd --- /dev/null +++ b/Linux/crates/sw-render/src/images.rs @@ -0,0 +1,178 @@ +//! Decodes package, inline and fetched images for one render. Ported from +//! `PackageImageSource` in `Windows/src/ScriptWidget.Core/Render/WidgetRenderer.cs`. +//! Image data comes from packages and the network: sizes are checked before +//! anything is decoded. + +use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::Arc; + +use skia_safe::codec::Codec; +use skia_safe::{AlphaType, ColorType, Data, FilterMode, Image, ImageInfo, MipmapMode, Paint, Rect, SamplingOptions, images, surfaces}; +use sw_core::{PackageFiles, normalize_package_path}; + +use crate::gif::{self, GifTimeline}; + +/// A larger file is not decoded. +pub const MAXIMUM_IMAGE_BYTES: usize = 16 * 1024 * 1024; +/// A picture with more pixels is not decoded: 40 million pixels (larger than an 8K frame) are 160 MB. +pub const MAXIMUM_IMAGE_PIXELS: i64 = 40 * 1000 * 1000; + +pub struct PackageImageSource { + files: Option>, + remote: HashMap, + /// The moment of every GIF's animation to show; `None` shows the first frame. + gif_time: Option, + decoded: HashMap>, + reduced: HashMap<(String, i64), Option>, + timelines: HashMap>, +} + +impl PackageImageSource { + pub fn new(files: Option>, remote: HashMap, gif_time: Option) -> Self { + Self { files, remote, gif_time, decoded: HashMap::new(), reduced: HashMap::new(), timelines: HashMap::new() } + } + + fn bytes(&self, source: &str) -> Option> { + let data = if let Some(path) = source.strip_prefix("res:") { + // The layout only writes paths of the package's own files; a path that could leave the package is not read. + self.files.as_ref()?.read_bytes(&normalize_package_path(Some(path))?)? + } else if source.starts_with("data:") { + let encoded = source.split_once("base64,").map_or(source, |(_, encoded)| encoded); + if encoded.len() / 4 * 3 > MAXIMUM_IMAGE_BYTES + 3 { + return None; + } + base64(encoded)? + } else { + let path = self.remote.get(source)?; + // The size is checked before the file is read. + let length = std::fs::metadata(path).ok().filter(|metadata| metadata.is_file())?.len(); + if length > MAXIMUM_IMAGE_BYTES as u64 { + return None; + } + std::fs::read(path).ok()? + }; + (!data.is_empty() && data.len() <= MAXIMUM_IMAGE_BYTES).then_some(data) + } + + /// The whole image, decoded once per render; `None` when the source cannot be decoded. + fn decode(&mut self, source: &str) -> Option { + if let Some(existing) = self.decoded.get(source) { + return existing.clone(); + } + let image = self.bytes(source).and_then(|data| { + if gif::is_gif(&data) { + let timeline = gif::inspect(&data); + self.timelines.insert(source.to_owned(), timeline.clone()); + let timeline = timeline?; + let frame = self.gif_time.map_or(0, |time| timeline.frame_at(time)); + let (_, pixels) = crate::gif_cache::frame(&data, frame)?; + let info = ImageInfo::new((timeline.width as i32, timeline.height as i32), ColorType::RGBA8888, AlphaType::Premul, None); + return images::raster_from_data(&info, Data::new_copy(&pixels), info.min_row_bytes()); + } + decode_still(&data) + }); + self.decoded.insert(source.to_owned(), image.clone()); + image + } + + /// Natural size in pixels, or `None` when the source cannot be decoded. + pub fn size(&mut self, source: &str) -> Option<(i32, i32)> { + self.decode(source).map(|image| (image.width(), image.height())) + } + + /// The timing of `source` when it is an animated GIF that was decoded in this render. + pub fn animation(&mut self, source: &str) -> Option { + self.decode(source)?; + self.timelines.get(source).cloned().flatten().filter(GifTimeline::is_animated) + } + + /// The image, with no more than about four times `maximum_pixels` pixels. + pub fn image(&mut self, source: &str, maximum_pixels: i64) -> Option { + let key = (source.to_owned(), maximum_pixels); + if let Some(existing) = self.reduced.get(&key) { + return existing.clone(); + } + let result = self.decode(source).map(|image| reduce(&image, maximum_pixels).unwrap_or(image)); + self.reduced.insert(key, result.clone()); + result + } +} + +/// Decodes PNG, JPEG, WebP, BMP and the other formats Skia reads into pixels. +fn decode_still(data: &[u8]) -> Option { + let mut codec = Codec::from_data(Data::new_copy(data))?; + // The header says how large the picture is, before any of it is decoded. + let size = codec.dimensions(); + if size.width <= 0 || size.height <= 0 || i64::from(size.width) * i64::from(size.height) > MAXIMUM_IMAGE_PIXELS { + return None; + } + // Decoded now, once: a damaged file is found here, not while painting. A photo is turned as its orientation says. + codec.get_image(None, None).ok() +} + +pub fn sampling() -> SamplingOptions { + SamplingOptions::new(FilterMode::Linear, MipmapMode::Linear) +} + +/// Halves the image until it has fewer than `maximum_pixels` pixels after one more halving. +fn reduce(image: &Image, maximum_pixels: i64) -> Option { + let (width, height) = (i64::from(image.width()), i64::from(image.height())); + let mut sample = 1i64; + while (width / (sample * 2)) * (height / (sample * 2)) >= maximum_pixels.max(1) { + sample *= 2; + } + if sample == 1 { + return None; + } + let (target_width, target_height) = ((width / sample).max(1) as i32, (height / sample).max(1) as i32); + let mut surface = surfaces::raster(&ImageInfo::new((target_width, target_height), ColorType::RGBA8888, AlphaType::Premul, None), None, None)?; + surface.canvas().draw_image_rect_with_sampling_options(image, None, Rect::from_wh(target_width as f32, target_height as f32), sampling(), &Paint::default()); + Some(surface.image_snapshot()) +} + +/// Standard base64 with optional padding; whitespace is skipped. `None` for anything else. +fn base64(text: &str) -> Option> { + let mut output = Vec::with_capacity(text.len() / 4 * 3); + let (mut buffer, mut bits) = (0u32, 0u32); + let mut padding = false; + for byte in text.bytes() { + let value = match byte { + b'A'..=b'Z' => byte - b'A', + b'a'..=b'z' => byte - b'a' + 26, + b'0'..=b'9' => byte - b'0' + 52, + b'+' => 62, + b'/' => 63, + b'=' => { + padding = true; + continue; + } + b' ' | b'\n' | b'\r' | b'\t' => continue, + _ => return None, + }; + if padding { + return None; + } + buffer = buffer << 6 | u32::from(value); + bits += 6; + if bits >= 8 { + bits -= 8; + output.push((buffer >> bits) as u8); + } + } + Some(output) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn decodes_base64() { + assert_eq!(base64("aGVsbG8=").as_deref(), Some(&b"hello"[..])); + assert_eq!(base64("aGVs\nbG8").as_deref(), Some(&b"hello"[..])); + assert_eq!(base64("").as_deref(), Some(&b""[..])); + assert_eq!(base64("a*b"), None); + assert_eq!(base64("aGVsbG8=x"), None); + } +} diff --git a/Linux/crates/sw-render/src/lib.rs b/Linux/crates/sw-render/src/lib.rs new file mode 100644 index 00000000..976cfa55 --- /dev/null +++ b/Linux/crates/sw-render/src/lib.rs @@ -0,0 +1,94 @@ +//! The ScriptWidget painter for Linux: turns the element tree a package +//! produces into a bitmap with per-pixel transparency plus the list of tap +//! regions, on a CPU surface, with no graphical session. +//! +//! Ported from `Windows/src/ScriptWidget.Core/Render` (itself a port of +//! `Android/core/.../render`). The shared web renderer (`render.js`) lays the +//! tree out in a QuickJS context of its own and asks this crate to measure +//! text; the laid-out result is painted with Skia. Text is measured and +//! drawn by the same Skia paragraph code, so wrapping and truncation agree. +//! +//! What a tree may reference is untrusted: images and GIFs come from +//! packages and the network, and their sizes are bounded before decoding. + +mod css; +mod dom; +mod fonts; +pub mod gif; +pub mod gif_cache; +mod images; +mod painter; +#[cfg(test)] +mod painter_tests; +mod renderer; +pub mod rounded_corners; +mod svg; +mod text; +pub mod widget; + +use serde_json::{Map, Value}; + +pub use fonts::{CjkRegion, cjk_region_order}; +pub use images::{MAXIMUM_IMAGE_BYTES, MAXIMUM_IMAGE_PIXELS}; +pub use renderer::{FontReport, RenderError, RenderOutput, RenderRequest, WidgetRenderer, encode_png}; +pub use widget::{Presentation, RenderedRun, render_run, render_tree}; + +/// The widget sizes in points; a display multiplies them by its scale. +/// The same as on Mac and Windows; there are no lock screen sizes. +pub const FAMILY_SIZES: [(&str, f32, f32); 4] = [("small", 170.0, 170.0), ("medium", 329.0, 170.0), ("large", 329.0, 345.0), ("extraLarge", 639.0, 345.0)]; + +/// Width and height in points of a widget family, or `None` for an unknown name. +pub fn family_size(family: &str) -> Option<(f32, f32)> { + FAMILY_SIZES.iter().find(|(name, _, _)| *name == family).map(|(_, width, height)| (*width, *height)) +} + +/// A rectangle in points from the widget's top-left corner. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Bounds { + pub left: f32, + pub top: f32, + pub right: f32, + pub bottom: f32, +} + +impl Bounds { + pub fn width(&self) -> f32 { + self.right - self.left + } + + pub fn height(&self) -> f32 { + self.bottom - self.top + } + + /// The part shared with `other`, or `None` when the two do not overlap. + pub fn intersect(&self, other: &Bounds) -> Option { + if !(self.left < other.right && other.left < self.right && self.top < other.bottom && other.top < self.bottom) { + return None; + } + Some(Bounds { left: self.left.max(other.left), top: self.top.max(other.top), right: self.right.min(other.right), bottom: self.bottom.min(other.bottom) }) + } +} + +/// A tappable area of the widget: a button, a toggle or a link. +/// `interaction` is what the shared renderer recorded for it: `type` is +/// "button" (with `action`, `actionID`, `handler`), "toggle" (with `on`, +/// `actionID`, `stateKey`, `handler`) or "link" (with `url`). The host decides +/// what a tap does, and validates the values: they come from the package. +#[derive(Debug, Clone, PartialEq)] +pub struct HitRegion { + pub bounds: Bounds, + pub interaction: Map, +} + +/// An animated GIF in the bitmap. The bitmap shows the frame that +/// [`RenderRequest::gif_time_milliseconds`] selects; a display layer renders +/// the widget again at the times the delays give, or decodes the frames +/// itself with [`gif::decode`]. +#[derive(Debug, Clone, PartialEq)] +pub struct GifSlot { + pub bounds: Bounds, + /// The `src` of the image: `res:` for a package file. + pub source: String, + /// How long each frame is shown, in milliseconds. + pub delays: Vec, +} diff --git a/Linux/crates/sw-render/src/painter.rs b/Linux/crates/sw-render/src/painter.rs new file mode 100644 index 00000000..800a00d2 --- /dev/null +++ b/Linux/crates/sw-render/src/painter.rs @@ -0,0 +1,488 @@ +//! Paints a recorded element tree on a canvas. It implements exactly the +//! HTML/CSS and SVG subset that `Web/preview-engine/src/render/renderer.js` +//! writes: absolutely positioned boxes, text, images and inline SVG. Ported +//! from `Windows/src/ScriptWidget.Core/Render/DomPainter.cs`, with the +//! drawing calls of the Android original, which already painted on Skia. + +use skia_safe::canvas::SaveLayerRec; +use skia_safe::paint::Style; +use skia_safe::{BlurStyle, Canvas, Color, Color4f, MaskFilter, Matrix, Paint, Path, PathEffect, RRect, Rect, image_filters}; + +use sw_core::json::parse_object; + +use crate::css::{self, BoxShadow, Shadow}; +use crate::dom::DomNode; +use crate::gif::GifTimeline; +use crate::images::{PackageImageSource, sampling}; +use crate::svg::SvgPainter; +use crate::text::{TextEngine, TextStyle}; +use crate::{Bounds, GifSlot, HitRegion}; + +#[derive(Clone)] +struct TextContext { + family: String, + size: f32, + weight: i32, + line_height: Option, + align: String, + wrap: bool, + color: Color, +} + +impl TextContext { + fn style(&self) -> TextStyle { + TextStyle { family: self.family.clone(), size: self.size, weight: self.weight, leading: self.line_height.unwrap_or(self.size * 1.2) } + } +} + +fn opacity(node: &DomNode) -> f32 { + css::number(node.style("opacity")).map_or(1.0, |value| value.clamp(0.0, 1.0)) +} + +fn bounds_of(rect: Rect) -> Bounds { + Bounds { left: rect.left, top: rect.top, right: rect.right, bottom: rect.bottom } +} + +/// The standard deviation of Android's BlurMaskFilter for a radius, so +/// shadows are as soft as in the Android bitmap. +fn blur_sigma(radius: f32) -> f32 { + if radius > 0.0 { 0.57735 * radius + 0.5 } else { 0.0 } +} + +/// Pixels per unit of the canvas transform, at least 1. +fn device_scale(canvas: &Canvas) -> f32 { + let matrix = canvas.local_to_device_as_3x3(); + matrix.scale_x().hypot(matrix.skew_y()).max(matrix.skew_x().hypot(matrix.scale_y())).max(1.0) +} + +pub struct DomPainter<'a> { + text: &'a mut TextEngine, + images: &'a mut PackageImageSource, + pub hit_regions: Vec, + pub gif_slots: Vec, +} + +impl<'a> DomPainter<'a> { + pub fn new(text: &'a mut TextEngine, images: &'a mut PackageImageSource) -> Self { + Self { text, images, hit_regions: Vec::new(), gif_slots: Vec::new() } + } + + /// Paints `root` (the widget element). `scale` converts points to pixels. + pub fn paint(&mut self, canvas: &Canvas, root: &DomNode, scale: f32) { + self.hit_regions.clear(); + self.gif_slots.clear(); + canvas.save(); + canvas.scale((scale, scale)); + let width = css::pixels(root.style("width")).unwrap_or(0.0); + let height = css::pixels(root.style("height")).unwrap_or(0.0); + let context = TextContext { family: "sans-serif".into(), size: 17.0, weight: 400, line_height: None, align: "left".into(), wrap: true, color: Color::BLACK }; + self.element(canvas, root, width, height, &context, &Matrix::new_identity()); + canvas.restore(); + } + + fn element(&mut self, canvas: &Canvas, node: &DomNode, parent_width: f32, parent_height: f32, inherited: &TextContext, to_widget: &Matrix) { + if node.tag == "style" || node.tag.is_empty() { + return; + } + if node.svg { + let left = css::pixels(node.style("left")).unwrap_or(0.0); + let top = css::pixels(node.style("top")).unwrap_or(0.0); + canvas.save(); + canvas.translate((left, top)); + SvgPainter::paint(self.text, canvas, node, node.style("color").and_then(|color| css::color(Some(color))).unwrap_or(inherited.color)); + canvas.restore(); + return; + } + + let absolute = node.style("position") == Some("absolute"); + let x = if absolute { css::length(node.style("left"), parent_width).unwrap_or(0.0) } else { 0.0 }; + let y = if absolute { css::length(node.style("top"), parent_height).unwrap_or(0.0) } else { 0.0 } + css::pixels(node.style("marginTop")).unwrap_or(0.0); + let width = css::length(node.style("width"), parent_width).unwrap_or(parent_width); + let height = css::length(node.style("height"), parent_height).unwrap_or(parent_height); + let bounds = Rect::from_wh(width, height); + + let mut local = *to_widget; + local.pre_translate((x, y)); + canvas.save(); + canvas.translate((x, y)); + if let Some(matrix) = transform(node, width, height) { + // Skia draws through a matrix with perspective directly; no offscreen pass is needed. + canvas.concat(&matrix); + local.pre_concat(&matrix); + } + + let shadow = css::drop_shadow(node.style("filter")); + let opacity = opacity(node); + if shadow.is_some() || opacity < 1.0 { + // What is drawn until the layer is restored is blended as one group; a + // drop shadow is cast by the silhouette of the whole group. + let mut paint = Paint::default(); + paint.set_alpha_f(opacity); + if let Some(Shadow { dx, dy, blur, color }) = shadow { + let scale = device_scale(canvas); + // The filter takes its lengths in the element's own units. + let sigma = if blur > 0.0 { blur_sigma(blur * scale).clamp(0.1, 250.0) / scale } else { 0.0 }; + paint.set_image_filter(image_filters::drop_shadow((dx, dy), (sigma, sigma), Color4f::from(color), None, None, None)); + } + canvas.save_layer(&SaveLayerRec::default().paint(&paint)); + self.content(canvas, node, bounds, inherited, &local); + canvas.restore(); + } else { + self.content(canvas, node, bounds, inherited, &local); + } + canvas.restore(); + } + + fn content(&mut self, canvas: &Canvas, node: &DomNode, bounds: Rect, inherited: &TextContext, to_widget: &Matrix) { + let (width, height) = (bounds.width(), bounds.height()); + let radius = radius(node.style("borderRadius"), width, height); + let outline = outline(node, bounds, radius); + + // An outer shadow lies under the box; an inset one is drawn over its background. + let box_shadow = css::box_shadow(node.style("boxShadow")); + if let Some(shadow) = box_shadow.filter(|shadow| !shadow.inset) { + paint_box_shadow(canvas, shadow, bounds, radius); + } + background(canvas, node, bounds, &outline); + if let Some(shadow) = box_shadow.filter(|shadow| shadow.inset) { + paint_box_shadow(canvas, shadow, bounds, radius); + } + if let Some((_, color)) = node.style("border").and_then(|border| border.split_once("dashed")) { + let mut stroke = Paint::default(); + stroke.set_anti_alias(true).set_style(Style::Stroke).set_stroke_width(1.0).set_color(css::color(Some(color)).unwrap_or(css::GRAY)); + if let Some(effect) = PathEffect::dash(&[3.0, 3.0], 0.0) { + stroke.set_path_effect(effect); + } + canvas.draw_rect(Rect::from_ltrb(0.5, 0.5, (width - 0.5).max(0.5), (height - 0.5).max(0.5)), &stroke); + } + + if let Some(interaction) = node.data("swInteraction").and_then(|text| parse_object(text).ok()) { + self.hit_regions.push(HitRegion { bounds: bounds_of(to_widget.map_rect(bounds).0), interaction }); + } + + let clips = node.style("overflow") == Some("hidden") || node.style("clipPath").is_some(); + if clips { + canvas.save(); + canvas.clip_path(&outline, None, true); + } + + let context = text_context(node, inherited); + if node.tag == "img" { + self.image(canvas, node, bounds, to_widget); + } else if let Some(text) = node.text.as_deref().filter(|_| node.children.is_empty()) { + self.draw_text(canvas, text, node, bounds, &context); + } + if node.data("swList").is_none() { + for child in &node.children { + self.element(canvas, child, width, height, &context, to_widget); + } + } else { + // A list is drawn in place: the rows that fit are visible, the rest are cut off, and so are their tap targets. + let first_hit = self.hit_regions.len(); + let visible = to_widget.map_rect(bounds).0; + canvas.save(); + canvas.clip_rect(bounds, None, true); + for child in &node.children { + self.element(canvas, child, width, height, &context, to_widget); + } + canvas.restore(); + let inside: Vec = self.hit_regions.drain(first_hit..).filter_map(|hit| Some(HitRegion { bounds: hit.bounds.intersect(&bounds_of(visible))?, interaction: hit.interaction })).collect(); + self.hit_regions.extend(inside); + } + if clips { + canvas.restore(); + } + } + + fn draw_text(&mut self, canvas: &Canvas, content: &str, node: &DomNode, bounds: Rect, context: &TextContext) { + if content.is_empty() { + return; + } + let style = context.style(); + let clipped_to_text = node.style("backgroundClip") == Some("text") || node.style("webkitBackgroundClip") == Some("text"); + let gradient = if clipped_to_text { css::gradient(node.style("backgroundImage"), bounds.width(), bounds.height(), (0.0, 0.0)) } else { None }; + if gradient.is_none() && context.color.a() == 0 { + return; + } + let mut paint = Paint::default(); + paint.set_anti_alias(true); + match gradient { + Some(shader) => paint.set_color(Color::BLACK).set_shader(shader), + None => paint.set_color(context.color), + }; + let lines = self.text.lines(content, &style, context.wrap.then_some(bounds.width())); + let baseline = self.text.baseline(&style); + for (index, line) in lines.iter().enumerate() { + let x = match context.align.as_str() { + "center" => (bounds.width() - line.width) / 2.0, + "right" => bounds.width() - line.width, + _ => 0.0, + }; + self.text.draw(canvas, &line.text, &style, x, baseline + index as f32 * style.leading, &paint); + } + } + + fn image(&mut self, canvas: &Canvas, node: &DomNode, bounds: Rect, to_widget: &Matrix) { + let Some(source) = node.attribute("src") else { return }; + let scale = Some(canvas.local_to_device_as_3x3().scale_x()).filter(|scale| *scale > 0.0).unwrap_or(1.0); + let pixels = (f64::from(bounds.width()) * f64::from(scale) * f64::from(bounds.height()) * f64::from(scale)).clamp(1.0, i32::MAX as f64) as i64; + let Some(image) = self.images.image(source, pixels) else { return }; + if let Some(GifTimeline { delays, .. }) = self.images.animation(source) { + // The display layer cycles the frames; this bitmap shows the one the request asked for. + self.gif_slots.push(GifSlot { bounds: bounds_of(to_widget.map_rect(bounds).0), source: source.to_owned(), delays }); + } + let mut destination = bounds; + let fit = node.style("objectFit").unwrap_or("fill"); + if fit == "contain" || fit == "cover" { + let ratio = image.width() as f32 / image.height().max(1) as f32; + let box_ratio = bounds.width() / bounds.height().max(0.0001); + let fit_width = if (fit == "contain") == (ratio > box_ratio) { bounds.width() } else { bounds.height() * ratio }; + let fit_height = fit_width / ratio; + destination = Rect::from_ltrb((bounds.width() - fit_width) / 2.0, (bounds.height() - fit_height) / 2.0, (bounds.width() + fit_width) / 2.0, (bounds.height() + fit_height) / 2.0); + } + let mut paint = Paint::default(); + paint.set_anti_alias(true); + canvas.save(); + canvas.clip_rect(bounds, None, true); + canvas.draw_image_rect_with_sampling_options(&image, None, destination, sampling(), &paint); + canvas.restore(); + } +} + +fn text_context(node: &DomNode, inherited: &TextContext) -> TextContext { + if !["fontFamily", "fontSize", "fontWeight", "lineHeight", "textAlign", "whiteSpace", "color"].iter().any(|name| node.style.contains_key(*name)) { + return inherited.clone(); + } + TextContext { + family: node.style("fontFamily").map_or_else(|| inherited.family.clone(), str::to_owned), + size: css::pixels(node.style("fontSize")).unwrap_or(inherited.size), + weight: node.style("fontWeight").and_then(|weight| weight.trim().parse().ok()).unwrap_or(inherited.weight), + line_height: css::pixels(node.style("lineHeight")).or(inherited.line_height), + align: node.style("textAlign").map_or_else(|| inherited.align.clone(), str::to_owned), + wrap: node.style("whiteSpace").map_or(inherited.wrap, |white_space| white_space != "pre"), + color: node.style("color").and_then(|color| css::color(Some(color))).unwrap_or(inherited.color), + } +} + +fn background(canvas: &Canvas, node: &DomNode, bounds: Rect, outline: &Path) { + let mut fill = Paint::default(); + fill.set_anti_alias(true); + if let Some(color) = css::color(node.style("backgroundColor")).filter(|color| color.a() != 0) { + fill.set_color(color); + canvas.draw_path(outline, &fill); + } + let Some(image) = node.style("backgroundImage").filter(|image| *image != "none") else { return }; + if node.style("backgroundClip") == Some("text") || node.style("webkitBackgroundClip") == Some("text") { + return; + } + // Shapes paint a gradient sized for the whole view behind a smaller outline. + let lengths = |name: &str| node.style(name).map(|value| value.split(' ').filter_map(|part| css::pixels(Some(part))).collect::>()).unwrap_or_default(); + let size = lengths("backgroundSize"); + let position = lengths("backgroundPosition"); + let width = size.first().copied().unwrap_or(bounds.width()); + let height = size.get(1).copied().unwrap_or(bounds.height()); + let offset = if position.len() == 2 { (position[0], position[1]) } else { (0.0, 0.0) }; + if let Some(shader) = css::gradient(Some(image), width, height, offset) { + fill.set_color(Color::BLACK).set_shader(shader); + canvas.draw_path(outline, &fill); + } else if let Some(solid) = css::color(Some(image)) { + fill.set_color(solid); + canvas.draw_path(outline, &fill); + } +} + +fn paint_box_shadow(canvas: &Canvas, shadow: BoxShadow, bounds: Rect, radius: f32) { + let mut paint = Paint::default(); + paint.set_anti_alias(true).set_color(shadow.color); + if shadow.inset { + // `inset 0 0 0 color` is the renderer's one-point inner border. + let width = if shadow.spread > 0.0 { shadow.spread } else { 1.0 }; + let inset = width / 2.0; + let corner = (radius - inset).max(0.0); + paint.set_style(Style::Stroke).set_stroke_width(width); + let rect = Rect::from_ltrb(inset, inset, (bounds.width() - inset).max(inset), (bounds.height() - inset).max(inset)); + canvas.draw_rrect(RRect::new_rect_xy(rect, corner, corner), &paint); + return; + } + // Outer shadow under an opaque shape: the blurred shape is drawn first. + if shadow.blur > 0.0 { + paint.set_mask_filter(MaskFilter::blur(BlurStyle::Normal, blur_sigma(shadow.blur / 2.0 + 0.1), true)); + } + canvas.save(); + canvas.translate((shadow.dx, shadow.dy)); + canvas.draw_rrect(RRect::new_rect_xy(bounds, radius, radius), &paint); + canvas.restore(); +} + +fn radius(value: Option<&str>, width: f32, height: f32) -> f32 { + let Some(text) = value.map(str::trim) else { return 0.0 }; + match text.strip_suffix('%') { + Some(percent) => width.min(height) * css::number(Some(percent)).unwrap_or(0.0) / 100.0, + None => css::pixels(Some(text)).unwrap_or(0.0).min(width.min(height) / 2.0).max(0.0), + } +} + +fn outline(node: &DomNode, bounds: Rect, radius: f32) -> Path { + let rect = Rect::from_ltrb(bounds.left, bounds.top, bounds.left + bounds.width().max(0.0), bounds.top + bounds.height().max(0.0)); + let clip = node.style("clipPath").unwrap_or(""); + if clip.starts_with("circle") { + Path::circle((rect.center_x(), rect.center_y()), rect.width().min(rect.height()) / 2.0, None) + } else if clip.starts_with("ellipse") { + Path::oval(rect, None) + } else if clip.starts_with("inset") { + let round = rect.width().min(rect.height()) / 2.0; + Path::rrect(RRect::new_rect_xy(rect, round, round), None) + } else if node.style("borderRadius").is_some_and(|value| value.trim().ends_with('%')) { + Path::oval(rect, None) + } else if radius > 0.0 { + Path::rrect(RRect::new_rect_xy(rect, radius, radius), None) + } else { + Path::rect(rect, None) + } +} + +/// CSS transform list with its transform-origin, as a matrix in element coordinates. +fn transform(node: &DomNode, width: f32, height: f32) -> Option { + let value = node.style("transform")?; + let mut origin = node.style("transformOrigin").unwrap_or("").split(' '); + let origin_x = css::length(origin.next(), width).unwrap_or(width / 2.0); + let origin_y = css::length(origin.next(), height).unwrap_or(height / 2.0); + let degrees = |text: &str| css::number(Some(text.strip_suffix("deg").unwrap_or(text))); + let mut matrix = Matrix::new_identity(); + let mut perspective = 0f32; + for (name, arguments) in css::functions(value) { + let arguments: Vec<&str> = arguments.split(',').map(str::trim).collect(); + let argument = |index: usize| arguments.get(index).copied(); + let step = match name { + "rotate" => Matrix::rotate_deg(argument(0).and_then(degrees).unwrap_or(0.0)), + "translateX" => Matrix::translate((css::pixels(argument(0)).unwrap_or(0.0), 0.0)), + "translateY" => Matrix::translate((0.0, css::pixels(argument(0)).unwrap_or(0.0))), + "translate" => Matrix::translate((css::pixels(argument(0)).unwrap_or(0.0), css::pixels(argument(1)).unwrap_or(0.0))), + "scale" => { + let sx = css::number(argument(0)).unwrap_or(1.0); + Matrix::scale((sx, css::number(argument(1)).unwrap_or(sx))) + } + "perspective" => { + perspective = css::pixels(argument(0)).unwrap_or(0.0); + continue; + } + "rotate3d" => { + if arguments.len() < 4 { + continue; + } + let axis_x = css::number(argument(0)).unwrap_or(0.0); + let axis_y = css::number(argument(1)).unwrap_or(0.0); + let axis_z = css::number(argument(2)).unwrap_or(0.0); + let angle = argument(3).and_then(degrees).unwrap_or(0.0); + let length = (axis_x * axis_x + axis_y * axis_y + axis_z * axis_z).sqrt(); + if length == 0.0 { + continue; + } + // The CSS rotate3d() matrix itself, applied to the element's plane (z = 0) and + // followed by the perspective division w = 1 - z / distance, as on Windows. + let (ux, uy, uz) = (axis_x / length, axis_y / length, axis_z / length); + let half = angle.to_radians() / 2.0; + let sc = half.sin() * half.cos(); + let sq = half.sin() * half.sin(); + let r00 = 1.0 - 2.0 * (uy * uy + uz * uz) * sq; + let r01 = 2.0 * (ux * uy * sq - uz * sc); + let r10 = 2.0 * (ux * uy * sq + uz * sc); + let r11 = 1.0 - 2.0 * (ux * ux + uz * uz) * sq; + let r20 = 2.0 * (ux * uz * sq - uy * sc); + let r21 = 2.0 * (uy * uz * sq + ux * sc); + let depth = if perspective > 0.0 { 1.0 / perspective } else { 0.0 }; + Matrix::new_all(r00, r01, 0.0, r10, r11, 0.0, -r20 * depth, -r21 * depth, 1.0) + } + _ => continue, + }; + matrix.pre_concat(&step); + } + matrix.pre_translate((-origin_x, -origin_y)); + matrix.post_translate((origin_x, origin_y)); + Some(matrix) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn styled(style: &[(&str, &str)]) -> DomNode { + DomNode { tag: "div".into(), style: style.iter().map(|(name, value)| ((*name).to_owned(), (*value).to_owned())).collect(), ..Default::default() } + } + + fn close(point: skia_safe::Point, x: f32, y: f32) -> bool { + (point.x - x).abs() < 0.01 && (point.y - y).abs() < 0.01 + } + + #[test] + fn transforms_turn_about_their_origin() { + // A quarter turn about the centre of a 100 x 40 box. + let quarter = transform(&styled(&[("transform", "rotate(90deg)")]), 100.0, 40.0).expect("matrix"); + assert!(close(quarter.map_point((50.0, 20.0)), 50.0, 20.0)); + assert!(close(quarter.map_point((100.0, 20.0)), 50.0, 70.0), "{:?}", quarter.map_point((100.0, 20.0))); + // About the top-left corner, then moved: the list applies right to left. + let moved = transform(&styled(&[("transform", "translate(10px, 5px) scale(2)"), ("transformOrigin", "0px 0px")]), 100.0, 40.0).expect("matrix"); + assert!(close(moved.map_point((3.0, 4.0)), 16.0, 13.0)); + let percent = transform(&styled(&[("transform", "scale(2, 3)"), ("transformOrigin", "100% 50%")]), 100.0, 40.0).expect("matrix"); + assert!(close(percent.map_point((100.0, 20.0)), 100.0, 20.0)); + assert!(close(percent.map_point((90.0, 30.0)), 80.0, 50.0)); + assert!(transform(&styled(&[]), 1.0, 1.0).is_none()); + } + + #[test] + fn a_turn_in_depth_narrows_the_far_side() { + let node = styled(&[("transform", "perspective(200px) rotate3d(0, 1, 0, 60deg)")]); + let matrix = transform(&node, 100.0, 100.0).expect("matrix"); + assert!(matrix.has_perspective()); + // The axis through the centre stays where it is. + assert!(close(matrix.map_point((50.0, 50.0)), 50.0, 50.0)); + let (near, far) = (matrix.map_point((0.0, 0.0)), matrix.map_point((100.0, 0.0))); + let (near_bottom, far_bottom) = (matrix.map_point((0.0, 100.0)), matrix.map_point((100.0, 100.0))); + // Turned about the vertical axis, the box is narrower, and its two sides have different heights. + assert!(far.x - near.x < 70.0, "{near:?} {far:?}"); + let (left_height, right_height) = (near_bottom.y - near.y, far_bottom.y - far.y); + assert!((left_height - right_height).abs() > 10.0, "{left_height} {right_height}"); + // Without a perspective the same turn only squeezes the box. + let flat = transform(&styled(&[("transform", "rotate3d(0, 1, 0, 60deg)")]), 100.0, 100.0).expect("matrix"); + assert!(!flat.has_perspective()); + assert!(close(flat.map_point((100.0, 0.0)), 75.0, 0.0), "{:?}", flat.map_point((100.0, 0.0))); + } + + #[test] + fn corner_radii_stay_inside_the_box() { + assert_eq!(radius(Some("8px"), 100.0, 40.0), 8.0); + assert_eq!(radius(Some("999px"), 100.0, 40.0), 20.0); + assert_eq!(radius(Some("50%"), 100.0, 40.0), 20.0); + assert_eq!(radius(Some("-4px"), 100.0, 40.0), 0.0); + assert_eq!(radius(None, 100.0, 40.0), 0.0); + } + + #[test] + fn outlines_follow_the_clip_and_the_radius() { + let bounds = Rect::from_wh(100.0, 40.0); + let inside = |node: &DomNode, x: f32, y: f32| outline(node, bounds, radius(node.style("borderRadius"), 100.0, 40.0)).contains((x, y)); + let plain = styled(&[]); + assert!(inside(&plain, 1.0, 1.0)); + let round = styled(&[("borderRadius", "20px")]); + assert!(!inside(&round, 1.0, 1.0) && inside(&round, 50.0, 1.0) && inside(&round, 8.0, 8.0)); + let ellipse = styled(&[("borderRadius", "50%")]); + assert!(!inside(&ellipse, 4.0, 4.0) && inside(&ellipse, 50.0, 20.0)); + let circle = styled(&[("clipPath", "circle(50%)")]); + assert!(!inside(&circle, 10.0, 20.0) && inside(&circle, 50.0, 20.0) && inside(&circle, 32.0, 20.0)); + let capsule = styled(&[("clipPath", "inset(0 round 999px)")]); + assert!(!inside(&capsule, 2.0, 2.0) && inside(&capsule, 2.0, 20.0)); + } + + #[test] + fn text_properties_are_inherited() { + let root = TextContext { family: "a".into(), size: 17.0, weight: 400, line_height: None, align: "left".into(), wrap: true, color: Color::BLACK }; + let same = text_context(&styled(&[("left", "0px")]), &root); + assert_eq!((same.family.as_str(), same.size, same.weight), ("a", 17.0, 400)); + let child = text_context(&styled(&[("fontSize", "12px"), ("fontWeight", "700"), ("color", "rgba(255, 0, 0, 1)"), ("whiteSpace", "pre"), ("lineHeight", "16px")]), &root); + assert_eq!((child.size, child.weight, child.wrap, child.color), (12.0, 700, false, Color::from_argb(255, 255, 0, 0))); + assert_eq!(child.style().leading, 16.0); + assert_eq!(root.style().leading, 17.0 * 1.2); + let clear = text_context(&styled(&[("color", "transparent")]), &child); + assert_eq!((clear.color.a(), clear.size, clear.wrap), (0, 12.0, false)); + } +} diff --git a/Linux/crates/sw-render/src/painter_tests.rs b/Linux/crates/sw-render/src/painter_tests.rs new file mode 100644 index 00000000..54c5a23b --- /dev/null +++ b/Linux/crates/sw-render/src/painter_tests.rs @@ -0,0 +1,693 @@ +//! The painter, the text engine and the image source on their own, without +//! the layout engine. Ported from +//! `Windows/tests/ScriptWidget.Core.Tests/Render/PainterTests.cs`; pixels are +//! premultiplied RGBA here. + +use std::collections::HashMap; +use std::sync::Arc; + +use skia_safe::{AlphaType, Color, ColorType, ImageInfo, surfaces}; +use sw_core::MemoryPackageFiles; +use sw_core::json::{ObjectExt, parse_object, quote}; + +use crate::dom::DomNode; +use crate::fonts::FontRegistry; +use crate::images::PackageImageSource; +use crate::painter::DomPainter; +use crate::text::{TextEngine, TextStyle}; +use crate::{GifSlot, HitRegion}; + +const INTER: &str = "\"ScriptWidget Inter\", Arial, sans-serif"; + +struct Painted { + pixels: Vec, + width: usize, + hit_regions: Vec, + gif_slots: Vec, +} + +impl Painted { + /// Red, green, blue, alpha of one pixel. + fn pixel(&self, x: usize, y: usize) -> (u8, u8, u8, u8) { + let at = (y * self.width + x) * 4; + (self.pixels[at], self.pixels[at + 1], self.pixels[at + 2], self.pixels[at + 3]) + } + + fn inked(&self) -> usize { + self.pixels.chunks_exact(4).filter(|pixel| pixel[3] != 0).count() + } + + /// Left, top, right, bottom of the pixels that are not transparent. + fn ink_bounds(&self) -> (i32, i32, i32, i32) { + let (mut left, mut top, mut right, mut bottom) = (i32::MAX, i32::MAX, -1, -1); + for (index, pixel) in self.pixels.chunks_exact(4).enumerate() { + if pixel[3] == 0 { + continue; + } + let (x, y) = ((index % self.width) as i32, (index / self.width) as i32); + (left, top, right, bottom) = (left.min(x), top.min(y), right.max(x), bottom.max(y)); + } + (left, top, right, bottom) + } +} + +fn engine() -> TextEngine { + TextEngine::new(FontRegistry::new("en-US")) +} + +fn no_images() -> PackageImageSource { + PackageImageSource::new(None, HashMap::new(), None) +} + +/// Paints a recorded element tree (JSON as render/entry.mjs writes it). +fn paint_with(json: &str, width: i32, height: i32, scale: f32, text: &mut TextEngine, images: &mut PackageImageSource) -> Painted { + let root = DomNode::from_json(&parse_object(json).expect("element JSON")).expect("element tree"); + let mut surface = surfaces::raster(&ImageInfo::new((width, height), ColorType::RGBA8888, AlphaType::Premul, None), None, None).expect("surface"); + surface.canvas().clear(Color::TRANSPARENT); + let mut painter = DomPainter::new(text, images); + painter.paint(surface.canvas(), &root, scale); + let (hit_regions, gif_slots) = (std::mem::take(&mut painter.hit_regions), std::mem::take(&mut painter.gif_slots)); + let pixels = surface.peek_pixels().and_then(|pixmap| pixmap.bytes().map(<[u8]>::to_vec)).expect("pixels"); + Painted { pixels, width: width as usize, hit_regions, gif_slots } +} + +fn paint(json: &str, width: i32, height: i32, scale: f32) -> Painted { + paint_with(json, width, height, scale, &mut engine(), &mut no_images()) +} + +fn root(width: i32, height: i32, children: &str) -> String { + format!("{{\"t\":\"div\",\"s\":{{\"width\":\"{width}px\",\"height\":\"{height}px\"}},\"c\":[{children}]}}") +} + +fn text_box(text: &str, left: f32, top: f32, width: f32, height: f32, extra_style: &str) -> String { + format!( + "{{\"t\":\"div\",\"s\":{{\"position\":\"absolute\",\"left\":\"{left}px\",\"top\":\"{top}px\",\"width\":\"{width}px\",\"height\":\"{height}px\",\"fontFamily\":{},\"fontSize\":\"17px\",\"fontWeight\":\"400\",\"lineHeight\":\"22px\",\"color\":\"rgb(0, 0, 0)\"{extra_style}}},\"x\":{}}}", + quote(INTER), + quote(text) + ) +} + +fn boxed(style: &str, extra: &str) -> String { + format!("{{\"t\":\"div\",\"s\":{{\"position\":\"absolute\",{style}}}{extra}}}") +} + +fn svg(attributes: &str, children: &str, style: &str) -> String { + format!("{{\"t\":\"svg\",\"g\":1,\"s\":{{\"position\":\"absolute\",\"left\":\"0px\",\"top\":\"0px\"{style}}},\"a\":{{{attributes}}},\"c\":[{children}]}}") +} + +fn shape(tag: &str, attributes: &str) -> String { + format!("{{\"t\":\"{tag}\",\"g\":1,\"a\":{{{attributes}}}}}") +} + +fn style(family: &str, size: f32, weight: i32) -> TextStyle { + TextStyle { family: family.into(), size, weight, leading: size * 1.2 } +} + +fn texts(lines: &[crate::text::TextLine]) -> Vec<&str> { + lines.iter().map(|line| line.text.as_str()).collect() +} + +const RED: (u8, u8, u8, u8) = (255, 0, 0, 255); +const GREEN: (u8, u8, u8, u8) = (0, 255, 0, 255); +const BLUE: (u8, u8, u8, u8) = (0, 0, 255, 255); + +// MARK: - Fonts and text + +#[test] +fn the_bundled_inter_is_loaded_and_its_weights_and_the_display_fonts_differ() { + let mut engine = engine(); + let regular = TextStyle { family: INTER.into(), size: 20.0, weight: 400, leading: 24.0 }; + let sample = "Hello world, Widget 123"; + let inter = engine.measure(sample, ®ular); + assert!(inter > 100.0, "{inter}"); + // Inter's metrics: ascent 0.96875 and descent 0.2412 of the size, placed with CSS half-leading. + let expected = (24.0 - (0.96875 + 0.241_210_94) * 20.0) / 2.0 + 0.96875 * 20.0; + assert!((engine.baseline(®ular) - expected).abs() < 0.01, "{} vs {expected}", engine.baseline(®ular)); + // The weight changes the advance: each weight is a real instance of the variable font, not a smeared regular. + let bold = engine.measure(sample, &TextStyle { weight: 700, ..regular.clone() }); + assert!(bold > inter + 2.0, "regular {inter}, bold {bold}"); + let light = engine.measure(sample, &TextStyle { weight: 200, ..regular.clone() }); + assert!(light < inter - 1.0, "regular {inter}, light {light}"); + // The display fonts are their own families. + for family in ["Apple ][", "Larabiefont Free", "Monofonto", "White Rabbit", "OneSlot", "Unispace"] { + let custom = engine.measure(sample, &TextStyle { family: format!("\"{family}\", -apple-system, \"ScriptWidget Inter\", system-ui, sans-serif"), ..regular.clone() }); + assert!((custom - inter).abs() > 1.0, "{family} measures {custom}, the same as Inter"); + } + // The system's own sans-serif is another face again, when it has one. + let mut system = engine; + system.fonts_mut().set_use_system_font(true); + let face = system.fonts_mut().face(INTER, 400); + if face.primary.is_empty() || face.primary == "Inter" { + println!("no system sans-serif face was found here ({:?}); skipped", face.primary); + } else { + // Which face it is, not how wide: on Arch Linux the system's Noto Sans sets this sample within half a pixel of Inter. + assert!(system.measure(sample, ®ular) > 100.0); + assert_eq!(system.families_used(sample, ®ular), [face.primary.clone()]); + } +} + +#[test] +fn a_monospace_face_has_one_advance() { + let mut engine = engine(); + let mono = TextStyle { family: "ui-monospace, \"SF Mono\", SFMono-Regular, Menlo, Consolas, monospace".into(), size: 20.0, weight: 400, leading: 24.0 }; + let face = engine.fonts_mut().face(&mono.family, 400); + if face.primary.is_empty() { + println!("no monospace font is installed here; skipped"); + return; + } + // The text is drawn with the face that was resolved, not with a substitute. + assert_eq!(engine.families_used("Hello world, Widget 123", &mono), [face.primary.clone()]); + let (one, sample) = (engine.measure("0", &mono), engine.measure("Hello world, Widget 123", &mono)); + assert!((sample - 23.0 * one).abs() < 0.5, "{sample} is not 23 x {one} in {}", face.primary); +} + +#[test] +fn width_includes_trailing_spaces() { + let mut engine = engine(); + let style = style(INTER, 17.0, 400); + let (word, spaced) = (engine.measure("alpha", &style), engine.measure("alpha ", &style)); + assert!(spaced > word + 4.0, "{word} {spaced}"); + assert_eq!(engine.measure("", &style), 0.0); + // A size the layout should never send still measures instead of failing. + assert!(engine.measure("x", &TextStyle { size: f32::NAN, ..style.clone() }).is_finite()); + assert!(engine.measure("x", &TextStyle { size: -3.0, ..style }).is_finite()); +} + +#[test] +fn lines_break_like_pre_wrap_with_break_word() { + let mut engine = engine(); + let style = TextStyle { family: INTER.into(), size: 17.0, weight: 400, leading: 22.0 }; + // Explicit newlines always break, and an empty line stays. + assert_eq!(texts(&engine.lines("one\n\ntwo", &style, None)), ["one", "", "two"]); + // Without a width nothing wraps. + assert_eq!(engine.lines("a long line of text that would never fit a narrow box", &style, None).len(), 1); + // With a width, words wrap and trailing spaces are trimmed. + let limit = engine.measure("alpha beta gam", &style); + let wrapped = engine.lines("alpha beta gamma delta epsilon", &style, Some(limit)); + assert_eq!(texts(&wrapped), ["alpha beta", "gamma delta", "epsilon"]); + for line in &wrapped { + assert!((engine.measure(&line.text, &style) - line.width).abs() < 0.001); + } + // A word breaks inside only when it does not fit on a line of its own. + let narrow = engine.measure("abcdef", &style); + let broken = engine.lines("hi abcdefghijkl", &style, Some(narrow + 0.5)); + assert_eq!(broken[0].text, "hi"); + assert!(broken.len() >= 3 && broken[1..].iter().map(|line| line.text.as_str()).collect::() == "abcdefghijkl", "{:?}", texts(&broken)); + for line in &broken { + assert!(line.width <= narrow + 0.51, "{line:?}"); + } + // Chinese breaks between characters, and never inside one. + let sentence = "这是一个很长的中文句子用来测试换行"; + let limit = engine.measure("这是一个很长", &style) + 1.0; + let chinese = engine.lines(sentence, &style, Some(limit)); + // Six characters fit a line: seventeen take three lines, each as full as it can be. + assert_eq!(chinese.iter().map(|line| line.text.chars().count()).collect::>(), [6, 6, 5], "{:?}", texts(&chinese)); + // Mixed text: line ends are found by character, not by byte or by UTF-16 unit. + let limit = engine.measure("今天 weather 多", &style) + 0.5; + let mixed = engine.lines("今天 weather 多云 😀😀 fine", &style, Some(limit)); + assert_eq!(texts(&mixed), ["今天 weather 多", "云 😀😀 fine"]); + assert_eq!(chinese.iter().map(|line| line.text.as_str()).collect::(), sentence); + // Emoji made of several code points stay whole. + let family = "👨‍👩‍👧‍👦"; + let limit = engine.measure(family, &style) * 1.5; + let emoji = engine.lines(&format!("{family}{family}{family}{family}"), &style, Some(limit)); + assert!(emoji.len() >= 3 && emoji.iter().all(|line| line.text.chars().count() % family.chars().count() == 0), "{:?}", texts(&emoji)); +} + +/// A line measured without a width is one line again when laid out in a box of exactly that width. +#[test] +fn a_measured_line_is_not_wrapped_in_a_box_of_its_measured_width() { + let mut engine = engine(); + let samples = ["Hello widget world", "Tuesday, November 17", "72°F · Partly Cloudy", "1,234,567.89 kWh", "你好,世界 hello", "Steps 🚶 8,432 today", "AV To. WAVE fi ffl", "a b c d e f g h i j k l m n o p"]; + let families = [INTER, "ui-monospace, Consolas, monospace", "\"New York\", ui-serif, Georgia, serif", "\"Unispace\", -apple-system, sans-serif"]; + for family in families { + for size in [9.0f32, 12.0, 13.5, 17.0, 28.0, 51.3] { + for weight in [300, 400, 600, 700, 900] { + for sample in samples { + let style = style(family, size, weight); + let measured = engine.lines(sample, &style, None); + assert_eq!(measured.len(), 1); + let laid_out = engine.lines(sample, &style, Some(measured[0].width)); + assert!(laid_out.len() == 1 && laid_out[0].text == sample, "{family} {size} {weight} '{sample}' wrapped into {} lines at its own width {}", laid_out.len(), measured[0].width); + assert_eq!(measured[0].width, laid_out[0].width); + } + } + } + } +} + +/// The layout sizes a text box to the widest line it was told, then asks again at that width: +/// the lines must be the same ones, or text that fits would be truncated. +#[test] +fn wrapped_text_keeps_its_lines_in_a_box_of_its_widest_line() { + let mut engine = engine(); + let samples = ["Build with primitives", "Good morning, Friend and welcome back", "Partly cloudy with a chance of rain later today", "今天多云,气温十八度,适合出门散步", "Steps 🚶 8,432 today and counting", "Tuesday, November 17 2026"]; + for family in [INTER, "ui-monospace, Consolas, monospace", "\"Monofonto\", sans-serif"] { + for size in [11.0f32, 13.0, 17.0, 22.0, 34.0] { + for weight in [400, 700] { + for sample in samples { + let style = style(family, size, weight); + let whole = engine.measure(sample, &style); + for fraction in [0.31f32, 0.45, 0.5, 0.62, 0.84] { + let first = engine.lines(sample, &style, Some(whole * fraction)); + let widest = first.iter().map(|line| line.width).fold(0.0, f32::max); + let again = engine.lines(sample, &style, Some(widest)); + assert!(texts(&again) == texts(&first), "{family} {size} {weight} '{sample}': {:?} at {} became {:?} at its widest line {widest}", texts(&first), whole * fraction, texts(&again)); + // No line is wider than the box, unless it is a single character that cannot be divided. + for line in &first { + assert!(line.width <= whole * fraction + 0.01 || line.text.chars().count() <= 2, "{family} {size} '{}' is {} wide in {}", line.text, line.width, whole * fraction); + } + } + } + } + } + } + // The case that showed it: "primitives" is 107.2 wide and was broken in a box of 107.5. + let bold = TextStyle { family: INTER.into(), size: 22.0, weight: 700, leading: 28.0 }; + let word = engine.measure("primitives", &bold); + assert_eq!(texts(&engine.lines("Build with primitives", &bold, Some(word + 0.3))), ["Build with", "primitives"]); +} + +/// What is drawn in a box of the measured width is, pixel for pixel, what is drawn with room to spare. +#[test] +fn text_drawn_at_its_measured_width_is_not_wrapped_or_cut() { + let mut engine = engine(); + let style = TextStyle { family: INTER.into(), size: 17.0, weight: 400, leading: 22.0 }; + for sample in ["Hello widget world", "Weather in 北京 ☀ 23°", "Wavy AVATAR Type"] { + let width = engine.measure(sample, &style); + let exact = paint(&root(400, 60, &text_box(sample, 10.0, 10.0, width, 22.0, "")), 800, 120, 2.0); + let roomy = paint(&root(400, 60, &text_box(sample, 10.0, 10.0, 380.0, 22.0, ",\"whiteSpace\":\"pre\"")), 800, 120, 2.0); + assert!(exact.inked() > 200, "{sample}"); + assert!(exact.pixels == roomy.pixels, "'{sample}' is drawn differently in a box of its measured width"); + // One line: the ink stays inside the 22-point line box, and inside the measured width. + let (left, top, right, bottom) = exact.ink_bounds(); + assert!(top >= 20 - 4 && bottom <= (10 + 22) * 2 + 4, "'{sample}' ink rows {top}..{bottom}"); + assert!(left >= 20 - 4 && right as f32 <= (10.0 + width) * 2.0 + 4.0, "'{sample}' ink columns {left}..{right} for width {width}"); + } +} + +#[test] +fn text_alignment_places_the_line_in_its_box() { + let left = paint(&root(200, 30, &text_box("Hi", 0.0, 0.0, 200.0, 22.0, "")), 200, 30, 1.0).ink_bounds(); + let center = paint(&root(200, 30, &text_box("Hi", 0.0, 0.0, 200.0, 22.0, ",\"textAlign\":\"center\"")), 200, 30, 1.0).ink_bounds(); + let right = paint(&root(200, 30, &text_box("Hi", 0.0, 0.0, 200.0, 22.0, ",\"textAlign\":\"right\"")), 200, 30, 1.0).ink_bounds(); + assert!(left.0 < 4 && left.2 < 30, "{left:?}"); + assert!(((center.0 + center.2) as f32 / 2.0 - 100.0).abs() < 2.0, "{center:?}"); + assert!(right.2 >= 196 && right.0 > 170, "{right:?}"); + // Wrapped text takes one line box per line. + let wrapped = paint(&root(60, 100, &text_box("alpha beta gamma", 0.0, 0.0, 60.0, 66.0, "")), 60, 100, 1.0).ink_bounds(); + assert!(wrapped.3 > 44 && wrapped.3 < 66, "{wrapped:?}"); + // The baseline is where the style says, whatever the glyphs: a capital H stands on it. + let mut engine = engine(); + let baseline = engine.baseline(&TextStyle { family: INTER.into(), size: 17.0, weight: 400, leading: 22.0 }); + let capital = paint(&root(200, 30, &text_box("H", 0.0, 0.0, 200.0, 22.0, "")), 400, 60, 2.0).ink_bounds(); + assert!((capital.3 as f32 + 1.0 - baseline * 2.0).abs() <= 1.5, "ink bottom {} for baseline {baseline}", capital.3); +} + +#[test] +fn text_is_drawn_in_its_color_or_its_gradient() { + let latin = paint(&root(200, 40, &text_box("Widget", 4.0, 4.0, 190.0, 22.0, "")), 400, 80, 2.0); + assert!(latin.pixels.chunks_exact(4).any(|pixel| pixel == [0, 0, 0, 255]), "default-color text is black"); + let clear = paint(&root(200, 40, &text_box("Widget", 4.0, 4.0, 190.0, 22.0, ",\"color\":\"transparent\"")), 400, 80, 2.0); + assert_eq!(clear.inked(), 0); + // A gradient clipped to the text colors the glyphs from left to right; the box itself stays empty. + let gradient = paint(&root(200, 40, &text_box("WWWWWWWWWWWWWWWW", 0.0, 4.0, 200.0, 22.0, ",\"color\":\"transparent\",\"backgroundClip\":\"text\",\"backgroundImage\":\"linear-gradient(90deg, rgb(255, 0, 0) 0px, rgb(0, 0, 255) 200px)\"")), 400, 80, 2.0); + let solid: Vec<(usize, &[u8])> = gradient.pixels.chunks_exact(4).enumerate().filter(|(_, pixel)| pixel[3] == 255).collect(); + assert!(solid.len() > 200, "{}", solid.len()); + assert!(solid.iter().any(|(index, pixel)| index % 400 < 60 && pixel[0] > 200 && pixel[2] < 60), "red at the left"); + assert!(solid.iter().any(|(index, pixel)| index % 400 > 300 && pixel[2] > 180 && pixel[0] < 80), "blue at the right"); + assert_eq!(gradient.pixel(399, 79).3, 0); +} + +#[test] +fn chinese_and_emoji_text_is_drawn() { + let mut engine = engine(); + if engine.fonts().cjk_families().is_empty() { + println!("no CJK font is installed here; Chinese text is not checked"); + } else { + let chinese = paint(&root(200, 40, &text_box("你好世界", 4.0, 4.0, 190.0, 22.0, "")), 400, 80, 2.0); + assert!(chinese.inked() > 400, "Chinese ink {}", chinese.inked()); + // Every character has a glyph of its own: no row of identical missing-glyph boxes. + let style = style(INTER, 17.0, 400); + assert!((engine.measure("你好世界", &style) - 4.0 * 17.0).abs() < 4.0, "{}", engine.measure("你好世界", &style)); + } + let emoji = paint(&root(200, 40, &text_box("😀🌈🔥", 4.0, 4.0, 190.0, 22.0, "")), 400, 80, 2.0); + let colored = emoji.pixels.chunks_exact(4).filter(|pixel| pixel[3] > 200 && pixel[..3].iter().max().unwrap() - pixel[..3].iter().min().unwrap() > 60).count(); + let families = engine.families_used("😀🌈🔥", &style(INTER, 17.0, 400)); + if families.iter().any(|family| family.to_lowercase().contains("emoji")) { + assert!(emoji.inked() > 400, "emoji ink {}", emoji.inked()); + // Emoji are drawn in color, not as black outlines. + assert!(colored > 100, "colored emoji pixels {colored}"); + // A heart and a sun with the emoji selector are emoji too, though Inter has plain glyphs for both; + // without the selector they stay text, in the text color. + let is_colored = |painted: &Painted| painted.pixels.chunks_exact(4).filter(|pixel| pixel[3] > 200 && pixel[..3].iter().max().unwrap() - pixel[..3].iter().min().unwrap() > 60).count(); + let selected = paint(&root(200, 40, &text_box("\u{2764}\u{fe0f} \u{2600}\u{fe0f}", 4.0, 4.0, 190.0, 22.0, "")), 400, 80, 2.0); + assert!(is_colored(&selected) > 100, "colored pixels {}", is_colored(&selected)); + let bare = paint(&root(200, 40, &text_box("\u{2764} \u{2600}", 4.0, 4.0, 190.0, 22.0, "")), 400, 80, 2.0); + assert_eq!(is_colored(&bare), 0); + assert_eq!(engine.families_used("I \u{2764}\u{fe0f} it", &style(INTER, 17.0, 400)), ["Inter".to_owned(), families.iter().find(|family| family.to_lowercase().contains("emoji")).cloned().unwrap_or_default()]); + // Measured as drawn: the line with the emoji is wider than the one with the narrow text glyph, or as wide. + let style = style(INTER, 17.0, 400); + assert!(engine.measure("\u{2764}\u{fe0f}", &style) > 10.0); + assert_eq!(texts(&engine.lines("a \u{2764}\u{fe0f} b", &style, None)), ["a \u{2764}\u{fe0f} b"]); + } else { + println!("no emoji font is installed here ({families:?}); emoji are not checked"); + } +} + +// MARK: - Boxes + +#[test] +fn boxes_are_filled_clipped_and_rounded() { + let child = boxed("\"left\":\"-10px\",\"top\":\"-10px\",\"width\":\"50px\",\"height\":\"50px\",\"backgroundColor\":\"#ff0000\"", ""); + let painted = paint(&root(100, 100, &boxed("\"left\":\"10px\",\"top\":\"10px\",\"width\":\"80px\",\"height\":\"80px\",\"backgroundColor\":\"rgb(0, 0, 255)\",\"borderRadius\":\"20px\",\"overflow\":\"hidden\"", &format!(",\"c\":[{child}]"))), 200, 200, 2.0); + // Outside the box, and in the cut-off corner of the rounded outline. + assert_eq!(painted.pixel(10, 10).3, 0); + assert_eq!(painted.pixel(24, 24).3, 0); + // The child is clipped by the rounded corner but shows inside it. + assert_eq!(painted.pixel(60, 60), RED); + // The rest of the box is blue. + assert_eq!(painted.pixel(140, 140), BLUE); + // A percentage radius is an ellipse. + let circle = paint(&root(100, 100, &boxed("\"left\":\"0px\",\"top\":\"0px\",\"width\":\"100px\",\"height\":\"100px\",\"backgroundColor\":\"#00ff00\",\"borderRadius\":\"50%\"", "")), 100, 100, 1.0); + assert_eq!(circle.pixel(5, 5).3, 0); + assert_eq!(circle.pixel(50, 50), GREEN); + // Percentages place and size a box against its parent; a dashed border is drawn on its edge. + let nested = boxed("\"left\":\"50%\",\"top\":\"25%\",\"width\":\"50%\",\"height\":\"50%\",\"backgroundColor\":\"#ff0000\"", ""); + let percent = paint(&root(100, 100, &boxed("\"left\":\"0px\",\"top\":\"0px\",\"width\":\"80px\",\"height\":\"40px\",\"border\":\"1px dashed rgb(0, 0, 255)\"", &format!(",\"c\":[{nested}]"))), 100, 100, 1.0); + assert_eq!(percent.pixel(60, 20), RED); + assert_eq!(percent.pixel(30, 20).3, 0); + assert!((0..80).filter(|x| percent.pixel(*x, 0).2 > 100).count() > 20, "dashes along the top edge"); + assert!((0..80).filter(|x| percent.pixel(*x, 0).3 == 0).count() > 20, "gaps along the top edge"); + // An element the layout did not write is skipped, and so is a style element. + let skipped = paint(&root(10, 10, "{\"t\":\"style\",\"x\":\"div{}\"},{\"t\":\"\"}"), 10, 10, 1.0); + assert_eq!(skipped.inked(), 0); +} + +#[test] +fn opacity_transforms_and_hit_regions_follow_the_element() { + let turned = "{\"t\":\"div\",\"s\":{\"position\":\"absolute\",\"left\":\"50px\",\"top\":\"50px\",\"width\":\"40px\",\"height\":\"20px\",\"backgroundColor\":\"#0000ff\",\"transform\":\"rotate(90deg)\"},\"d\":{\"swInteraction\":\"{\\\"type\\\":\\\"link\\\"}\"}}"; + let painted = paint(&root(100, 100, &format!("{},{turned}", boxed("\"left\":\"0px\",\"top\":\"0px\",\"width\":\"40px\",\"height\":\"40px\",\"backgroundColor\":\"#ff0000\",\"opacity\":\"0.5\"", ""))), 100, 100, 1.0); + let faded = painted.pixel(20, 20); + assert!((126..=129).contains(&faded.3) && (126..=129).contains(&faded.0), "{faded:?}"); + // The 40x20 box turned a quarter about its centre (70, 60) covers x 60..80, y 40..80. + assert_eq!(painted.pixel(70, 45), BLUE); + assert_eq!(painted.pixel(54, 60).3, 0); + assert_eq!(painted.hit_regions.len(), 1); + let hit = &painted.hit_regions[0]; + assert_eq!(hit.interaction.opt_string("type"), "link"); + for (actual, expected) in [(hit.bounds.left, 60.0), (hit.bounds.top, 40.0), (hit.bounds.right, 80.0), (hit.bounds.bottom, 80.0)] { + assert!((actual - expected).abs() < 0.01, "{:?}", hit.bounds); + } + // Tap regions are in points whatever the scale, and a malformed interaction is not a tap region. + let button = "{\"t\":\"div\",\"s\":{\"position\":\"absolute\",\"left\":\"10px\",\"top\":\"20px\",\"width\":\"30px\",\"height\":\"40px\"},\"d\":{\"swInteraction\":\"{\\\"type\\\":\\\"button\\\",\\\"action\\\":\\\"go\\\"}\"}}"; + let broken = "{\"t\":\"div\",\"s\":{\"position\":\"absolute\",\"left\":\"0px\",\"top\":\"0px\",\"width\":\"5px\",\"height\":\"5px\"},\"d\":{\"swInteraction\":\"{not json\"}}"; + let scaled = paint(&root(100, 100, &format!("{button},{broken}")), 300, 300, 3.0); + assert_eq!(scaled.hit_regions.len(), 1); + assert_eq!(scaled.hit_regions[0].bounds, crate::Bounds { left: 10.0, top: 20.0, right: 40.0, bottom: 60.0 }); + assert_eq!(scaled.hit_regions[0].interaction.opt_string("action"), "go"); +} + +#[test] +fn gradients_and_shadows_are_painted() { + let full = "\"left\":\"0px\",\"top\":\"0px\",\"width\":\"100px\",\"height\":\"100px\""; + let linear = paint(&root(100, 100, &boxed(&format!("{full},\"backgroundImage\":\"linear-gradient(90deg, rgb(255, 0, 0) 0px, rgb(0, 0, 255) 100px)\""), "")), 100, 100, 1.0); + let (start, end) = (linear.pixel(2, 50), linear.pixel(97, 50)); + assert!(start.0 > 240 && start.2 < 15 && end.2 > 240 && end.0 < 15, "{start:?} {end:?}"); + let radial = paint(&root(100, 100, &boxed(&format!("{full},\"backgroundImage\":\"radial-gradient(circle at 50px 50px, rgb(255, 255, 255) 0px, rgb(0, 0, 0) 50px)\""), "")), 100, 100, 1.0); + assert!(radial.pixel(50, 50).0 > 240 && radial.pixel(2, 50).0 < 15); + // A conic gradient starts at 12 o'clock and runs clockwise. + let conic = paint(&root(100, 100, &boxed(&format!("{full},\"backgroundImage\":\"conic-gradient(from 0deg at 50px 50px, rgb(255, 0, 0) 0deg, rgb(0, 0, 255) 360deg)\""), "")), 100, 100, 1.0); + let (just_after_twelve, just_before_twelve, six) = (conic.pixel(53, 10), conic.pixel(46, 10), conic.pixel(50, 90)); + assert!(just_after_twelve.0 > 235 && just_before_twelve.2 > 235, "{just_after_twelve:?} {just_before_twelve:?}"); + assert!((i32::from(six.0) - i32::from(six.2)).abs() < 12, "{six:?}"); + // `from` turns the start: at 90 degrees it is at 3 o'clock. + let turned = paint(&root(100, 100, &boxed(&format!("{full},\"backgroundImage\":\"conic-gradient(from 90deg at 50px 50px, rgb(255, 0, 0) 0deg, rgb(0, 0, 255) 360deg)\""), "")), 100, 100, 1.0); + assert!(turned.pixel(90, 53).0 > 235 && turned.pixel(90, 46).2 > 235, "{:?} {:?}", turned.pixel(90, 53), turned.pixel(90, 46)); + // A gradient sized for a larger view is moved behind a smaller outline. + let moved = paint(&root(100, 100, &boxed("\"left\":\"0px\",\"top\":\"0px\",\"width\":\"50px\",\"height\":\"50px\",\"backgroundImage\":\"linear-gradient(90deg, rgb(255, 0, 0) 0px, rgb(0, 0, 255) 200px)\",\"backgroundSize\":\"200px 50px\",\"backgroundPosition\":\"-150px 0px\"", "")), 100, 100, 1.0); + assert!(moved.pixel(45, 25).2 > 230 && moved.pixel(45, 25).0 < 25, "{:?}", moved.pixel(45, 25)); + // A stop that fades out keeps its color: no gray fringe half way. + let fading = paint(&root(100, 100, &boxed(&format!("{full},\"backgroundImage\":\"linear-gradient(90deg, rgba(255, 0, 0, 1) 0px, rgba(0, 0, 255, 0) 100px)\""), "")), 100, 100, 1.0); + let middle = fading.pixel(50, 50); + assert!((120..=135).contains(&middle.3) && middle.0 > 115 && middle.2 < 12, "{middle:?}"); + + // A drop shadow lies under and beside the element, softly. + let shadow = paint(&root(100, 100, &boxed("\"left\":\"30px\",\"top\":\"30px\",\"width\":\"30px\",\"height\":\"30px\",\"backgroundColor\":\"#ffffff\",\"filter\":\"drop-shadow(8px 8px 3px rgba(0, 0, 0, 1))\"", "")), 100, 100, 1.0); + assert_eq!(shadow.pixel(45, 45), (255, 255, 255, 255)); + let cast = shadow.pixel(64, 64); + assert!(cast.3 > 200 && cast.0 < 30, "{cast:?}"); + assert_eq!(shadow.pixel(10, 10).3, 0); + assert!((1..=254).contains(&shadow.pixel(70, 50).3), "{:?}", shadow.pixel(70, 50)); + // At twice the scale the shadow is as far away and as soft, in points. + let doubled = paint(&root(100, 100, &boxed("\"left\":\"30px\",\"top\":\"30px\",\"width\":\"30px\",\"height\":\"30px\",\"backgroundColor\":\"#ffffff\",\"filter\":\"drop-shadow(8px 8px 3px rgba(0, 0, 0, 1))\"", "")), 200, 200, 2.0); + assert!(doubled.pixel(128, 128).3 > 200 && doubled.pixel(20, 20).3 == 0); + assert!((i32::from(doubled.pixel(140, 100).3) - i32::from(shadow.pixel(70, 50).3)).abs() < 40, "{:?} {:?}", doubled.pixel(140, 100), shadow.pixel(70, 50)); + // A box shadow is drawn under the box, an inset one as an inner border. + let outer = paint(&root(100, 100, &boxed("\"left\":\"30px\",\"top\":\"30px\",\"width\":\"30px\",\"height\":\"30px\",\"backgroundColor\":\"#ffffff\",\"boxShadow\":\"0px 10px 6px rgba(0, 0, 0, 0.5)\"", "")), 100, 100, 1.0); + assert!((60..=140).contains(&outer.pixel(45, 66).3), "{:?}", outer.pixel(45, 66)); + let inset = paint(&root(100, 100, &boxed("\"left\":\"30px\",\"top\":\"30px\",\"width\":\"30px\",\"height\":\"30px\",\"boxShadow\":\"inset 0 0 0 2px rgb(255, 0, 0)\"", "")), 100, 100, 1.0); + assert_eq!(inset.pixel(31, 45), RED); + assert_eq!(inset.pixel(45, 45).3, 0); +} + +/// A view turned in 3D with perspective is still drawn, narrower than flat. +#[test] +fn perspective_rotation_is_drawn() { + let flat = paint(&root(100, 100, &boxed("\"left\":\"20px\",\"top\":\"20px\",\"width\":\"60px\",\"height\":\"60px\",\"backgroundColor\":\"#ff0000\"", "")), 200, 200, 2.0); + let turned = paint(&root(100, 100, &boxed("\"left\":\"20px\",\"top\":\"20px\",\"width\":\"60px\",\"height\":\"60px\",\"backgroundColor\":\"#ff0000\",\"transform\":\"perspective(300px) rotate3d(0, 1, 0, 50deg)\"", "")), 200, 200, 2.0); + let (flat_bounds, turned_bounds) = (flat.ink_bounds(), turned.ink_bounds()); + assert!(turned.inked() > 2000, "{}", turned.inked()); + assert!(turned_bounds.2 - turned_bounds.0 < flat_bounds.2 - flat_bounds.0 - 20, "{flat_bounds:?} {turned_bounds:?}"); + // One side is nearer than the other: the two vertical edges have different heights. + let column_height = |x: usize| (0..200).filter(|y| turned.pixel(x, *y).3 > 128).count(); + let (left, right) = (column_height(turned_bounds.0 as usize + 3), column_height(turned_bounds.2 as usize - 3)); + assert!(left.abs_diff(right) > 6, "{left} {right}"); + assert_eq!(turned.pixel(100, 100), RED); +} + +// MARK: - SVG + +#[test] +fn svg_shapes_strokes_and_dashes_are_drawn() { + let children = [ + shape("rect", "\"x\":\"0\",\"y\":\"0\",\"width\":\"10\",\"height\":\"10\",\"fill\":\"#ff0000\""), + shape("path", "\"d\":\"M30 0h10v10h-10z M32 2v6h6v-6z\",\"fill\":\"currentColor\",\"fill-rule\":\"evenodd\""), + shape("circle", "\"cx\":\"25\",\"cy\":\"30\",\"r\":\"10\",\"fill\":\"none\",\"stroke\":\"#0000ff\",\"stroke-width\":\"4\",\"pathLength\":\"100\",\"stroke-dasharray\":\"25 75\""), + shape("line", "\"x1\":\"0\",\"y1\":\"48\",\"x2\":\"50\",\"y2\":\"48\",\"stroke\":\"#00ff00\",\"stroke-width\":\"2\""), + ] + .join(","); + let painted = paint(&root(100, 100, &svg("\"width\":\"100\",\"height\":\"100\",\"viewBox\":\"0 0 50 50\"", &children, ",\"color\":\"rgb(0, 128, 0)\"")), 100, 100, 1.0); + // The view box doubles everything. + assert_eq!(painted.pixel(10, 10), RED); + assert_eq!(painted.pixel(30, 10).3, 0); + // currentColor is the CSS color; the even-odd hole stays empty. + assert_eq!(painted.pixel(62, 10), (0, 128, 0, 255)); + assert_eq!(painted.pixel(70, 10).3, 0); + // A quarter of the circle is stroked: from 3 o'clock clockwise to 6 o'clock. + assert_eq!(painted.pixel(64, 74), BLUE); + assert_eq!(painted.pixel(36, 46).3, 0); + assert_eq!(painted.pixel(64, 46).3, 0); + assert_eq!(painted.pixel(36, 74).3, 0); + assert_eq!(painted.pixel(50, 96), GREEN); +} + +/// A ring drawn as one dash as long as its outline closes, and a dash offset moves the start. +#[test] +fn a_full_ring_closes_and_a_dash_offset_turns_it() { + let ring = |dashes: &str, extra: &str| { + let circle = shape("circle", &format!("\"cx\":\"50\",\"cy\":\"50\",\"r\":\"40\",\"fill\":\"none\",\"stroke\":\"#ff0000\",\"stroke-width\":\"8\",\"pathLength\":\"100\",\"stroke-dasharray\":\"{dashes}\"{extra}")); + paint(&root(100, 100, &svg("\"width\":\"100\",\"height\":\"100\"", &circle, "")), 300, 300, 3.0) + }; + let full = ring("100 100", ""); + // Every point of the ring's centre line is red, also just before the start at 3 o'clock. + for degrees in (0..360).step_by(3) { + let angle = (degrees as f32).to_radians(); + let (x, y) = ((150.0 + 120.0 * angle.cos()) as usize, (150.0 + 120.0 * angle.sin()) as usize); + assert_eq!(full.pixel(x, y), RED, "{degrees} degrees"); + } + // The root turned back a quarter starts at 12 o'clock, as the progress rings do. + let circle = shape("circle", "\"cx\":\"50\",\"cy\":\"50\",\"r\":\"40\",\"fill\":\"none\",\"stroke\":\"#ff0000\",\"stroke-width\":\"8\",\"pathLength\":\"100\",\"stroke-dasharray\":\"25 100\",\"stroke-linecap\":\"butt\""); + let quarter = paint(&root(100, 100, &svg("\"width\":\"100\",\"height\":\"100\"", &circle, ",\"transform\":\"rotate(-90deg)\"")), 100, 100, 1.0); + // From 12 to 3 o'clock: the upper right is stroked, the other three quarters are not. + assert_eq!(quarter.pixel(78, 22), RED); + for (x, y) in [(78, 78), (22, 78), (22, 22)] { + assert_eq!(quarter.pixel(x, y).3, 0, "{x},{y}"); + } + // A negative offset of a quarter moves the dash on by a quarter of the outline: from 6 to 9 o'clock. + let offset = ring("25 100", ",\"stroke-dashoffset\":\"-25\""); + assert_eq!(offset.pixel(66, 234), RED, "the lower left quarter"); + assert_eq!(offset.pixel(234, 234).3, 0); + assert_eq!(offset.pixel(234, 66).3, 0); + // A positive one moves it back, off the start of the outline; what follows is the gap. + assert_eq!(ring("25 200", ",\"stroke-dashoffset\":\"25\"").inked(), 0); + // The pattern repeats: past the gap the next dash shows at the end of the outline, before 3 o'clock. + let wrapped = ring("25 100", ",\"stroke-dashoffset\":\"40\""); + assert_eq!(wrapped.pixel(264, 105), RED); + assert_eq!(wrapped.pixel(264, 195).3, 0); + // A dash of no length draws nothing, not even a round dot. + assert_eq!(ring("0 100", ",\"stroke-linecap\":\"round\"").inked(), 0); +} + +#[test] +fn svg_arcs_masks_and_transforms_are_drawn() { + // A half disc on the right of x = 50, drawn with an arc. + let arc = paint(&root(100, 100, &svg("\"width\":\"100\",\"height\":\"100\"", &shape("path", "\"d\":\"M50 10 A40 40 0 0 1 50 90 Z\",\"fill\":\"#ff0000\""), "")), 100, 100, 1.0); + assert_eq!(arc.pixel(70, 50), RED); + assert_eq!(arc.pixel(30, 50).3, 0); + // A mask keeps the drawing where the mask is white. + let mask = format!("{{\"t\":\"mask\",\"g\":1,\"a\":{{\"id\":\"m\"}},\"c\":[{}]}}", shape("rect", "\"x\":\"0\",\"y\":\"0\",\"width\":\"50\",\"height\":\"100\",\"fill\":\"white\"")); + let masked = paint(&root(100, 100, &svg("\"width\":\"100\",\"height\":\"100\"", &format!("{mask},{}", shape("rect", "\"x\":\"10\",\"y\":\"10\",\"width\":\"80\",\"height\":\"80\",\"fill\":\"#0000ff\",\"mask\":\"url(#m)\"")), "")), 100, 100, 1.0); + assert_eq!(masked.pixel(30, 50), BLUE); + assert_eq!(masked.pixel(70, 50).3, 0); + // A black part of the mask hides the drawing, and a gray part lets half of it through. + let shaded = format!( + "{{\"t\":\"mask\",\"g\":1,\"a\":{{\"id\":\"m\"}},\"c\":[{},{}]}}", + shape("rect", "\"x\":\"0\",\"y\":\"0\",\"width\":\"100\",\"height\":\"100\",\"fill\":\"white\""), + shape("rect", "\"x\":\"0\",\"y\":\"0\",\"width\":\"50\",\"height\":\"50\",\"fill\":\"black\"") + "," + &shape("rect", "\"x\":\"50\",\"y\":\"0\",\"width\":\"50\",\"height\":\"50\",\"fill\":\"#808080\"") + ); + let partly = paint(&root(100, 100, &svg("\"width\":\"100\",\"height\":\"100\"", &format!("{shaded},{}", shape("rect", "\"x\":\"0\",\"y\":\"0\",\"width\":\"100\",\"height\":\"100\",\"fill\":\"#0000ff\",\"mask\":\"url(#m)\"")), "")), 100, 100, 1.0); + assert_eq!(partly.pixel(25, 25).3, 0); + assert!((120..=136).contains(&partly.pixel(75, 25).3), "{:?}", partly.pixel(75, 25)); + assert_eq!(partly.pixel(50, 75), BLUE); + // What is drawn before the masked element is not touched by its mask. + let behind = shape("rect", "\"x\":\"60\",\"y\":\"40\",\"width\":\"30\",\"height\":\"20\",\"fill\":\"#00ff00\""); + let kept = paint(&root(100, 100, &svg("\"width\":\"100\",\"height\":\"100\"", &format!("{mask},{behind},{}", shape("rect", "\"x\":\"10\",\"y\":\"10\",\"width\":\"80\",\"height\":\"80\",\"fill\":\"#0000ff\",\"mask\":\"url(#m)\"")), "")), 100, 100, 1.0); + assert_eq!(kept.pixel(70, 50), GREEN); + assert_eq!(kept.pixel(30, 50), BLUE); + // A group transform moves and turns its children. + let group = format!("{{\"t\":\"g\",\"g\":1,\"a\":{{\"transform\":\"translate(50 50) rotate(90)\",\"opacity\":\"0.5\"}},\"c\":[{}]}}", shape("rect", "\"x\":\"0\",\"y\":\"0\",\"width\":\"40\",\"height\":\"10\",\"fill\":\"#ff0000\"")); + let moved = paint(&root(100, 100, &svg("\"width\":\"100\",\"height\":\"100\"", &group, "")), 100, 100, 1.0); + assert!((126..=129).contains(&moved.pixel(45, 70).3), "{:?}", moved.pixel(45, 70)); + assert_eq!(moved.pixel(70, 55).3, 0); + // Text in an SVG is anchored at its x. + let label = "{\"t\":\"text\",\"g\":1,\"a\":{\"x\":\"50\",\"y\":\"30\",\"text-anchor\":\"middle\",\"font-size\":\"16\",\"fill\":\"#ff0000\"},\"x\":\"HHHH\"}"; + let anchored = paint(&root(100, 100, &svg("\"width\":\"100\",\"height\":\"100\"", label, "")), 100, 100, 1.0).ink_bounds(); + assert!(((anchored.0 + anchored.2) as f32 / 2.0 - 50.0).abs() < 2.0 && anchored.3 <= 31 && anchored.3 >= 28, "{anchored:?}"); +} + +// MARK: - Images + +/// A `width` x `height` PNG: red on the left half, blue on the right. +fn two_color_png(width: i32, height: i32) -> Vec { + let mut pixels = Vec::with_capacity((width * height * 4) as usize); + for _ in 0..height { + for x in 0..width { + pixels.extend_from_slice(if x < width / 2 { &[255, 0, 0, 255] } else { &[0, 0, 255, 255] }); + } + } + crate::encode_png(&pixels, width as u32, height as u32).expect("PNG") +} + +fn image_source(files: &[(&str, Vec)], gif_time: Option) -> PackageImageSource { + let files = MemoryPackageFiles::new([], files.iter().map(|(path, data)| ((*path).to_owned(), data.clone()))); + PackageImageSource::new(Some(Arc::new(files)), HashMap::new(), gif_time) +} + +fn image_element(source: &str, fit: &str) -> String { + format!("{{\"t\":\"img\",\"s\":{{\"position\":\"absolute\",\"left\":\"10px\",\"top\":\"10px\",\"width\":\"80px\",\"height\":\"40px\",\"objectFit\":\"{fit}\"}},\"a\":{{\"src\":{}}}}}", quote(source)) +} + +#[test] +fn images_are_fitted_into_their_box() { + let mut text = engine(); + let mut images = image_source(&[("art/square.png", two_color_png(40, 40))], None); + assert_eq!(images.size("res:art/square.png"), Some((40, 40))); + // contain: the square is 40 points wide in the middle of the 80-point box. + let contain = paint_with(&root(100, 60, &image_element("res:art/square.png", "contain")), 100, 60, 1.0, &mut text, &mut images); + assert_eq!(contain.pixel(15, 30).3, 0); + assert_eq!(contain.pixel(40, 30), RED); + assert_eq!(contain.pixel(60, 30), BLUE); + assert_eq!(contain.pixel(85, 30).3, 0); + // cover: the square fills the width and is cut above and below; nothing leaves the box. + let cover = paint_with(&root(100, 60, &image_element("res:art/square.png", "cover")), 100, 60, 1.0, &mut text, &mut images); + assert_eq!(cover.pixel(15, 30), RED); + assert_eq!(cover.pixel(85, 30), BLUE); + assert_eq!(cover.pixel(50, 5).3, 0); + assert_eq!(cover.pixel(50, 55).3, 0); + // fill stretches. + let fill = paint_with(&root(100, 60, &image_element("res:art/square.png", "fill")), 100, 60, 1.0, &mut text, &mut images); + assert_eq!(fill.pixel(12, 12), RED); + assert_eq!(fill.pixel(88, 48), BLUE); + // An inline image is decoded from its data URL. + let inline = format!("data:image/png;base64,{}", base64(&two_color_png(8, 8))); + assert_eq!(images.size(&inline), Some((8, 8))); + assert_eq!(paint_with(&root(100, 60, &image_element(&inline, "fill")), 100, 60, 1.0, &mut text, &mut images).pixel(12, 12), RED); +} + +#[test] +fn images_that_cannot_be_shown_are_left_out() { + let mut text = engine(); + let mut images = image_source(&[("broken.png", b"\x89PNG\r\n\x1a\nnot really".to_vec()), ("empty.png", Vec::new()), ("text.png", b"hello".to_vec())], None); + for source in ["res:broken.png", "res:empty.png", "res:text.png", "res:missing.png", "res:../outside.png", "https://example.com/not-fetched.png", "data:image/png;base64,@@@", "file:///etc/passwd", ""] { + assert_eq!(images.size(source), None, "{source}"); + let painted = paint_with(&root(100, 60, &image_element(source, "contain")), 100, 60, 1.0, &mut text, &mut images); + assert_eq!(painted.inked(), 0, "{source}"); + } + // A header that announces more pixels than the limit is refused before anything is allocated. + let mut huge = two_color_png(4, 4); + huge[16..20].copy_from_slice(&60_000u32.to_be_bytes()); + huge[20..24].copy_from_slice(&60_000u32.to_be_bytes()); + let mut images = image_source(&[("huge.png", huge)], None); + assert_eq!(images.size("res:huge.png"), None); +} + +#[test] +fn a_large_image_is_reduced_for_a_small_box() { + let mut images = image_source(&[("photo.png", two_color_png(512, 512))], None); + let small = images.image("res:photo.png", 32 * 32).expect("image"); + assert!(small.width() <= 64 && small.width() >= 32, "{}", small.width()); + let large = images.image("res:photo.png", 512 * 512).expect("image"); + assert_eq!(large.width(), 512); + // The natural size is still the file's. + assert_eq!(images.size("res:photo.png"), Some((512, 512))); +} + +#[test] +fn a_gif_shows_the_frame_of_the_requested_moment_and_reports_its_timing() { + let data = crate::gif::tests::gif(8, 8, &[(1, 20), (2, 30), (3, 50)]); + let element = root(100, 60, &image_element("res:cat.gif", "fill")); + let mut text = engine(); + for (time, color) in [(None, RED), (Some(0), RED), (Some(250), GREEN), (Some(600), BLUE), (Some(1000), RED), (Some(1000 + 499), GREEN)] { + let mut images = image_source(&[("cat.gif", data.clone())], time); + let painted = paint_with(&element, 100, 60, 1.0, &mut text, &mut images); + assert_eq!(painted.pixel(50, 30), color, "{time:?}"); + assert_eq!(painted.gif_slots.len(), 1); + let slot = &painted.gif_slots[0]; + assert_eq!((slot.source.as_str(), slot.delays.as_slice()), ("res:cat.gif", &[200, 300, 500][..])); + assert_eq!(slot.bounds, crate::Bounds { left: 10.0, top: 10.0, right: 90.0, bottom: 50.0 }); + } + // A GIF of one frame is a picture, not an animation. + let mut images = image_source(&[("still.gif", crate::gif::tests::gif(8, 8, &[(2, 0)]))], Some(500)); + let still = paint_with(&root(100, 60, &image_element("res:still.gif", "fill")), 100, 60, 1.0, &mut text, &mut images); + assert_eq!(still.pixel(50, 30), GREEN); + assert!(still.gif_slots.is_empty()); +} + +fn base64(data: &[u8]) -> String { + const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let mut text = String::new(); + for chunk in data.chunks(3) { + let value = u32::from(chunk[0]) << 16 | u32::from(*chunk.get(1).unwrap_or(&0)) << 8 | u32::from(*chunk.get(2).unwrap_or(&0)); + for index in 0..4 { + text.push(if index <= chunk.len() { ALPHABET[(value >> (18 - 6 * index) & 63) as usize] as char } else { '=' }); + } + } + text +} + +// MARK: - Lists + +#[test] +fn a_list_drawn_in_place_keeps_only_the_visible_tap_targets() { + let row = |index: usize| format!("{{\"t\":\"div\",\"s\":{{\"position\":\"absolute\",\"left\":\"0px\",\"top\":\"{}px\",\"width\":\"100px\",\"height\":\"20px\",\"backgroundColor\":\"#ff0000\"}},\"d\":{{\"swListRow\":\"1\",\"swInteraction\":\"{{\\\"type\\\":\\\"toggle\\\",\\\"stateKey\\\":\\\"row.{index}\\\"}}\"}}}}", index * 25); + let rows: Vec = (0..10).map(row).collect(); + let list = format!("{{\"t\":\"div\",\"s\":{{\"position\":\"absolute\",\"left\":\"10px\",\"top\":\"30px\",\"width\":\"100px\",\"height\":\"60px\"}},\"d\":{{\"swList\":\"{{\\\"contentHeight\\\":245}}\"}},\"c\":[{}]}}", rows.join(",")); + let painted = paint(&root(120, 120, &list), 120, 120, 1.0); + // Rows 0 to 2 reach into the 60 points that show; the third is cut off at the list's lower edge. + assert_eq!(painted.hit_regions.iter().map(|hit| hit.interaction.opt_string("stateKey")).collect::>(), ["row.0", "row.1", "row.2"]); + assert_eq!(painted.hit_regions[2].bounds, crate::Bounds { left: 10.0, top: 80.0, right: 110.0, bottom: 90.0 }); + assert_eq!(painted.pixel(50, 85), RED); + assert_eq!(painted.pixel(50, 95).3, 0); + assert_eq!(painted.pixel(50, 110).3, 0); +} diff --git a/Linux/crates/sw-render/src/renderer.rs b/Linux/crates/sw-render/src/renderer.rs new file mode 100644 index 00000000..824a484d --- /dev/null +++ b/Linux/crates/sw-render/src/renderer.rs @@ -0,0 +1,469 @@ +//! Turns a canonical element tree into a bitmap. Layout runs in the shared +//! web renderer inside a long-lived QuickJS context of its own, which only +//! ever sees the tree as data, never package code; the recorded elements are +//! painted with [`DomPainter`] on a CPU surface. Layout, text measurement and +//! painting all happen on one thread with a large stack. Ported from +//! `Windows/src/ScriptWidget.Core/Render/WidgetRenderer.cs`. + +use std::cell::RefCell; +use std::collections::HashMap; +use std::fmt; +use std::path::PathBuf; +use std::rc::Rc; +use std::sync::Arc; +use std::sync::mpsc::{self, Sender}; +use std::thread::JoinHandle; +use std::time::{Duration, Instant}; + +use serde_json::{Value, json}; +use skia_safe::{AlphaType, Color, ColorType, EncodedImageFormat, ImageInfo, images, surfaces}; +use sw_core::json::{Object, ObjectExt, stringify}; +use sw_core::{PackageFiles, SharedScripts, intl}; +use sw_quickjs::Engine; + +use crate::dom::DomNode; +use crate::fonts::FontRegistry; +use crate::images::PackageImageSource; +use crate::painter::DomPainter; +use crate::text::{TextEngine, TextStyle}; +use crate::{GifSlot, HitRegion}; + +/// What to draw and at what size. Lengths are in points. +#[derive(Clone)] +pub struct RenderRequest { + /// The canonical element tree, as JSON text. + pub tree: String, + pub width: f32, + pub height: f32, + /// Pixels per point. + pub scale: f32, + /// "small", "medium", "large" or "extraLarge". + pub family: String, + pub dark_mode: bool, + pub files: Option>, + /// Remote image URLs that were fetched under the package's network policy, with the paths of their files. + pub remote_images: HashMap, + /// Fixed clock for deterministic output, in milliseconds since 1970; `None` uses the real clock. + pub now_milliseconds: Option, + /// Bitmap budget in bytes for this render; the output is scaled down to fit. + pub maximum_bitmap_bytes: u64, + /// The moment of every GIF's animation to show, in milliseconds from its start; `None` shows the first frame. + pub gif_time_milliseconds: Option, + /// System theme colors behind the `material.` names; `None` leaves them unknown. Android only; Linux hosts leave it off. + pub colors: Option, +} + +impl RenderRequest { + /// A request for `tree` at the size of a widget family (see [`crate::family_size`]), or `None` for an unknown family. + pub fn for_family(tree: impl Into, family: &str, scale: f32) -> Option { + let (width, height) = crate::family_size(family)?; + Some(Self { family: family.to_owned(), ..Self::new(tree, width, height, scale) }) + } + + pub fn new(tree: impl Into, width: f32, height: f32, scale: f32) -> Self { + Self { + tree: tree.into(), + width, + height, + scale, + family: "medium".into(), + dark_mode: false, + files: None, + remote_images: HashMap::new(), + now_milliseconds: None, + maximum_bitmap_bytes: u64::MAX, + gif_time_milliseconds: None, + colors: None, + } + } +} + +#[derive(Debug, Clone)] +pub struct RenderOutput { + /// Premultiplied RGBA pixels, `pixel_width` x `pixel_height`, rows from the top, no padding. + pub pixels: Vec, + pub pixel_width: u32, + pub pixel_height: u32, + /// Pixels per point actually used (lower than requested when the budget forced a smaller bitmap). + pub scale: f32, + /// Tappable areas, in the order they were painted: a later one lies on top of an earlier one. + pub hit_regions: Vec, + /// The animated GIFs in the bitmap, for a display layer that cycles their frames. + pub gif_slots: Vec, + /// What the layout could not show as on iPhone, as the shared renderer reports it. + pub diagnostics: Vec, + pub layout_time: Duration, + pub paint_time: Duration, +} + +impl RenderOutput { + /// The bitmap as PNG data, with straight alpha as PNG stores it. + pub fn encode_png(&self) -> Result, RenderError> { + encode_png(&self.pixels, self.pixel_width, self.pixel_height) + } + + /// The pixels with straight (not premultiplied) alpha. + pub fn straight_pixels(&self) -> Vec { + let mut pixels = self.pixels.clone(); + for pixel in pixels.chunks_exact_mut(4) { + let alpha = u32::from(pixel[3]); + if alpha != 0 && alpha != 255 { + for channel in &mut pixel[..3] { + *channel = ((u32::from(*channel) * 255 + alpha / 2) / alpha).min(255) as u8; + } + } + } + pixels + } +} + +/// Writes premultiplied RGBA pixels as a PNG. +pub fn encode_png(pixels: &[u8], width: u32, height: u32) -> Result, RenderError> { + let mismatch = || RenderError::new("Pixel data does not match the size"); + if width == 0 || height == 0 || width > i32::MAX as u32 || height > i32::MAX as u32 { + return Err(mismatch()); + } + let info = ImageInfo::new((width as i32, height as i32), ColorType::RGBA8888, AlphaType::Premul, None); + if pixels.len() != info.compute_byte_size(info.min_row_bytes()) { + return Err(mismatch()); + } + let image = images::raster_from_data(&info, skia_safe::Data::new_copy(pixels), info.min_row_bytes()).ok_or_else(mismatch)?; + let data = image.encode(None, EncodedImageFormat::PNG, None).ok_or_else(|| RenderError::new("The bitmap could not be encoded as PNG"))?; + Ok(data.as_bytes().to_vec()) +} + +/// A layout or paint failure. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct RenderError { + pub message: String, +} + +impl RenderError { + fn new(message: impl Into) -> Self { + Self { message: message.into() } + } +} + +impl fmt::Display for RenderError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(&self.message) + } +} + +impl std::error::Error for RenderError {} + +/// Which fonts the renderer found; for diagnosis and tests. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct FontReport { + pub locale: String, + /// True when the bundled Inter could be loaded. + pub bundled: bool, + /// The installed CJK families, in the order they are tried. + pub cjk_families: Vec, + /// The system families behind the monospaced and the serif design; empty when the machine has none. + pub monospace: String, + pub serif: String, +} + +enum Job { + Render(Box, Sender>), + Layout(Box, Sender>), + SetLocale(String), + SetUseSystemFont(bool), + Fonts(Sender), + FamiliesUsed(String, TextStyle, Sender>), + Release, +} + +/// The renderer. One instance owns one thread, on which the layout context, +/// the fonts and every canvas live; calls from any thread are queued to it. +pub struct WidgetRenderer { + jobs: Option>, + worker: Option>, +} + +impl WidgetRenderer { + /// A renderer whose CJK font order follows the locale of the process + /// (see [`sw_core::intl::default_locale_tag`] and [`crate::cjk_region_order`]). + pub fn new(scripts: Arc) -> std::io::Result { + Self::with_locale(scripts, &intl::default_locale_tag()) + } + + pub fn with_locale(scripts: Arc, locale: &str) -> std::io::Result { + let (jobs, queue) = mpsc::channel::(); + let locale = locale.to_owned(); + let worker = std::thread::Builder::new().name("scriptwidget-render".into()).stack_size(RENDER_THREAD_STACK).spawn(move || { + let mut worker = Worker { scripts, shared: Rc::new(Shared { text: RefCell::new(TextEngine::new(FontRegistry::new(&locale))), images: RefCell::new(None) }), engine: None }; + for job in queue { + match job { + Job::Render(request, reply) => { + let _ = reply.send(worker.render(&request)); + } + Job::Layout(request, reply) => { + let _ = reply.send(worker.with_images(&request, |worker| worker.layout(&request)).map(|layout| stringify(&Value::Object(layout)))); + } + Job::SetLocale(locale) => worker.shared.text.borrow_mut().fonts_mut().set_locale(&locale), + Job::SetUseSystemFont(value) => worker.shared.text.borrow_mut().fonts_mut().set_use_system_font(value), + Job::Fonts(reply) => { + let mut text = worker.shared.text.borrow_mut(); + let (monospace, serif) = (text.primary_family("ui-monospace", 400), text.primary_family("ui-serif", 400)); + let fonts = text.fonts(); + let _ = reply.send(FontReport { locale: fonts.locale().to_owned(), bundled: fonts.has_bundled_font(), cjk_families: fonts.cjk_families().to_vec(), monospace, serif }); + } + Job::FamiliesUsed(text, style, reply) => { + let _ = reply.send(worker.shared.text.borrow_mut().families_used(&text, &style)); + } + Job::Release => worker.engine = None, + } + } + })?; + Ok(Self { jobs: Some(jobs), worker: Some(worker) }) + } + + fn send(&self, job: Job) -> Result<(), RenderError> { + self.jobs.as_ref().and_then(|jobs| jobs.send(job).ok()).ok_or_else(stopped) + } + + /// Lays out and paints one widget. + pub fn render(&self, request: RenderRequest) -> Result { + let (reply, answer) = mpsc::channel(); + self.send(Job::Render(Box::new(request), reply))?; + answer.recv().map_err(|_| stopped())? + } + + /// The element tree the layout records for a request, as JSON text; for tests and diagnosis. + pub fn layout(&self, request: RenderRequest) -> Result { + let (reply, answer) = mpsc::channel(); + self.send(Job::Layout(Box::new(request), reply))?; + answer.recv().map_err(|_| stopped())? + } + + /// Changes the locale that orders the CJK fonts, for the renders that follow. + pub fn set_locale(&self, locale: &str) { + let _ = self.send(Job::SetLocale(locale.to_owned())); + } + + /// When true, default-design text uses the machine's own sans-serif face instead of the bundled Inter. + pub fn set_use_system_font(&self, value: bool) { + let _ = self.send(Job::SetUseSystemFont(value)); + } + + pub fn fonts(&self) -> Result { + let (reply, answer) = mpsc::channel(); + self.send(Job::Fonts(reply))?; + answer.recv().map_err(|_| stopped()) + } + + /// The font families `text` is drawn with in the default face at `weight`, in order of first use. + pub fn families_used(&self, text: &str, weight: i32) -> Result, RenderError> { + let (reply, answer) = mpsc::channel(); + self.send(Job::FamiliesUsed(text.to_owned(), TextStyle { family: "sans-serif".into(), size: 17.0, weight, leading: 22.0 }, reply))?; + answer.recv().map_err(|_| stopped()) + } + + /// Frees the layout context; the next render recreates it. + pub fn release(&self) { + let _ = self.send(Job::Release); + } +} + +impl Drop for WidgetRenderer { + fn drop(&mut self) { + // Closing the queue ends the worker, which releases the engine and the fonts on its own thread. + self.jobs = None; + if let Some(worker) = self.worker.take() { + let _ = worker.join(); + } + } +} + +fn stopped() -> RenderError { + RenderError::new("The render thread has stopped") +} + +/// What the layout context's host calls reach. +struct Shared { + text: RefCell, + /// The images of the render in progress. + images: RefCell>, +} + +struct Worker { + scripts: Arc, + shared: Rc, + engine: Option>, +} + +fn number(value: f32) -> String { + if value.is_finite() { value.to_string() } else { "0".into() } +} + +fn style_of(json: &Object) -> TextStyle { + let or = |name: &str, fallback: f64| Some(json.opt_f64(name)).filter(|value| !value.is_nan()).unwrap_or(fallback); + TextStyle { family: json.opt_string("family"), size: or("size", 17.0) as f32, weight: json.opt_i32("weight", 400), leading: or("leading", 20.0) as f32 } +} + +/// The synchronous bridge of the layout context: text measurement by the engine that draws, image sizes, and Intl. +fn host(shared: &Shared, operation: &str, payload: Option<&str>) -> Result, String> { + match operation { + "text.measure" => { + let json = sw_core::json::parse_object(payload.unwrap_or(""))?; + let width = if json.is_null("width") { None } else { Some(json.opt_f64("width") as f32).filter(|width| !width.is_nan()) }; + let lines = shared.text.borrow_mut().lines(&json.opt_string("text"), &style_of(&json), width); + let widest = lines.iter().map(|line| line.width).fold(0f32, f32::max); + Ok(Some(format!("{{\"width\":{},\"lines\":{}}}", number(widest), lines.len().max(1)))) + } + "text.baseline" => Ok(Some(number(shared.text.borrow_mut().baseline(&style_of(&sw_core::json::parse_object(payload.unwrap_or(""))?))))), + "image.size" => Ok(Some(match shared.images.borrow_mut().as_mut().and_then(|images| images.size(payload.unwrap_or(""))) { + Some((width, height)) => format!("[{width},{height}]"), + None => "null".into(), + })), + _ => intl::call(operation, payload), + } +} + +impl Worker { + fn engine(&mut self) -> Result<&mut Engine<'static>, RenderError> { + if self.engine.is_none() { + let failed = |error: sw_quickjs::JsError| RenderError::new(format!("The layout context could not be created: {}", error.message)); + let shared = self.shared.clone(); + let mut engine = Engine::with_limits(move |operation, payload| host(&shared, operation, payload), LAYOUT_MEMORY, LAYOUT_JS_STACK).map_err(failed)?; + // Keep the native entry point for the trusted render bundle before the host file removes it. + engine.evaluate("globalThis.__swRenderNative = globalThis.__swNative;", "render-prelude.js").map_err(failed)?; + for name in ["host.js", "render.js"] { + let text = self.scripts.text(name).map_err(|error| RenderError::new(format!("ScriptWidget runtime asset could not be read: {error}")))?; + engine.evaluate(&text, name).map_err(failed)?; + } + engine.evaluate(RENDER_DRIVER, "render-driver.js").map_err(failed)?; + self.engine = Some(engine); + } + Ok(self.engine.as_mut().expect("created above")) + } + + /// Runs `work` with the images of `request` available to the layout and the painter. + fn with_images(&mut self, request: &RenderRequest, work: impl FnOnce(&mut Self) -> Result) -> Result { + *self.shared.images.borrow_mut() = Some(PackageImageSource::new(request.files.clone(), request.remote_images.clone(), request.gif_time_milliseconds)); + let result = work(self); + *self.shared.images.borrow_mut() = None; + result + } + + /// Runs the shared layout; the images must be set. + fn layout(&mut self, request: &RenderRequest) -> Result { + let mut resources = Object::new(); + if let Some(files) = &request.files { + for path in files.paths() { + if is_image_file(&path) { + resources.insert(path.clone(), Value::String(format!("res:{path}"))); + } + } + } + let mut job = json!({ + "tree": request.tree, "width": f64::from(request.width), "height": f64::from(request.height), + "family": request.family, "dark": request.dark_mode, "resources": resources, + "images": request.remote_images.keys().collect::>(), + }); + if let Some(now) = request.now_milliseconds { + job["now"] = json!(now); + } + if let Some(colors) = &request.colors { + job["colors"] = colors.clone(); + } + let job = stringify(&job); + + let result = self.engine().and_then(|engine| { + engine.set_deadline(LAYOUT_TIMEOUT_MILLISECONDS); + let outcome = run_layout(engine, &job); + engine.set_deadline(0); + outcome + }); + if result.is_err() { + // A failed or interrupted context may hold partial state; start fresh next time. + self.engine = None; + } + result + } + + fn render(&mut self, request: &RenderRequest) -> Result { + if !(request.width > 0.0 && request.height > 0.0 && request.scale > 0.0) || !request.width.is_finite() || !request.height.is_finite() || !request.scale.is_finite() { + return Err(RenderError::new("The size and the scale of a render must be positive numbers")); + } + self.with_images(request, |worker| { + let layout_start = Instant::now(); + let rendered = worker.layout(request)?; + let layout_time = layout_start.elapsed(); + + let paint_start = Instant::now(); + let mut scale = request.scale; + let bytes_at_requested_scale = (f64::from(request.width * scale).ceil() * f64::from(request.height * scale).ceil() * 4.0).max(1.0); + if bytes_at_requested_scale > request.maximum_bitmap_bytes as f64 { + scale *= (request.maximum_bitmap_bytes as f64 / bytes_at_requested_scale).sqrt() as f32; + } + let pixel_width = ((request.width * scale).ceil() as i32).clamp(1, MAXIMUM_BITMAP_SIDE); + let pixel_height = ((request.height * scale).ceil() as i32).clamp(1, MAXIMUM_BITMAP_SIDE); + let root = DomNode::from_json(rendered.opt_object("root").ok_or_else(|| RenderError::new("Layout returned no element tree"))?).map_err(RenderError::new)?; + + let info = ImageInfo::new((pixel_width, pixel_height), ColorType::RGBA8888, AlphaType::Premul, None); + let mut surface = surfaces::raster(&info, None, None).ok_or_else(|| RenderError::new("The bitmap could not be allocated"))?; + surface.canvas().clear(Color::TRANSPARENT); + let (hit_regions, gif_slots) = { + let mut text = worker.shared.text.borrow_mut(); + let mut images = worker.shared.images.borrow_mut(); + let mut painter = DomPainter::new(&mut text, images.as_mut().expect("set by with_images")); + painter.paint(surface.canvas(), &root, scale); + (painter.hit_regions, painter.gif_slots) + }; + let pixels = surface.peek_pixels().and_then(|pixmap| pixmap.bytes().map(<[u8]>::to_vec)).ok_or_else(|| RenderError::new("The bitmap could not be read"))?; + Ok(RenderOutput { + pixels, + pixel_width: pixel_width as u32, + pixel_height: pixel_height as u32, + scale, + hit_regions, + gif_slots, + diagnostics: rendered.opt_array("diagnostics").cloned().unwrap_or_default(), + layout_time, + paint_time: paint_start.elapsed(), + }) + }) + } +} + +fn run_layout(engine: &mut Engine<'static>, job: &str) -> Result { + let failed = |error: sw_quickjs::JsError| RenderError::new(if error.interrupted { "Layout took too long".to_owned() } else { format!("Layout failed: {}", error.message) }); + engine.call("__swRenderStart", &[Some(job)]).map_err(failed)?; + while engine.run_jobs(JOB_BATCH).map_err(failed)? > 0 {} + if let Some(failure) = engine.call("__swRenderTake", &[Some("__swRenderError")]).map_err(failed)? { + return Err(RenderError::new(format!("Layout failed: {failure}"))); + } + let text = engine.call("__swRenderTake", &[Some("__swRenderResult")]).map_err(failed)?.ok_or_else(|| RenderError::new("Layout did not finish"))?; + // The recorded tree nests an object and an array for every element: deeper than serde_json reads by default. + let mut deserializer = serde_json::Deserializer::from_str(&text); + deserializer.disable_recursion_limit(); + match serde::de::Deserialize::deserialize(&mut deserializer) { + Ok(Value::Object(object)) => Ok(object), + Ok(_) => Err(RenderError::new("Layout returned something that is not an object")), + Err(error) => Err(RenderError::new(format!("Layout returned invalid JSON: {error}"))), + } +} + +/// The package files handed to the layout as images. +fn is_image_file(path: &str) -> bool { + let lower = path.to_ascii_lowercase(); + [".png", ".jpg", ".jpeg", ".webp", ".gif", ".heic", ".bmp"].iter().any(|extension| lower.ends_with(extension)) +} + +const RENDER_DRIVER: &str = "globalThis.__swRenderStart = function (request) { globalThis.__swRenderResult = null; globalThis.__swRenderError = null;\ + __swRenderBundle.render(globalThis.__swRenderNative, request).then(function (value) { globalThis.__swRenderResult = value; },\ + function (error) { globalThis.__swRenderError = (error && error.stack ? String(error.message || error) + '\\n' + error.stack : String(error)); }); };\ + globalThis.__swRenderTake = function (name) { var value = globalThis[name]; globalThis[name] = null; return value; };"; + +/// The layout context is larger than an execution context: it holds the whole web renderer. +const LAYOUT_MEMORY: usize = 96 * 1024 * 1024; +const LAYOUT_JS_STACK: usize = 6 * 1024 * 1024; +/// Room for the JavaScript stack and, on top of it, text shaping called from the deepest layout frame. +const RENDER_THREAD_STACK: usize = 16 * 1024 * 1024; +const LAYOUT_TIMEOUT_MILLISECONDS: u64 = 8000; +const JOB_BATCH: usize = 100_000; +/// A side longer than this is not allocated, whatever the request says. +const MAXIMUM_BITMAP_SIDE: i32 = 16384; diff --git a/Linux/crates/sw-render/src/rounded_corners.rs b/Linux/crates/sw-render/src/rounded_corners.rs new file mode 100644 index 00000000..a87b5b63 --- /dev/null +++ b/Linux/crates/sw-render/src/rounded_corners.rs @@ -0,0 +1,94 @@ +//! Rounds the corners of a rendered widget. The renderer draws the widget as +//! a full rectangle, as on Android, where the launcher clips it; the Linux +//! host owns the surface, so it cuts the corners itself. Ported from +//! `Windows/src/ScriptWidget.Core/Render/RoundedCorners.cs`. + +/// Corner radius of a desktop widget, in points. +pub const DEFAULT_RADIUS: f32 = 20.0; + +/// Makes the corners of a premultiplied bitmap of four bytes a pixel +/// transparent, with an antialiased edge. The bitmap is changed in place. +/// `radius` is in pixels. +pub fn apply(pixels: &mut [u8], width: usize, height: usize, radius: f32) { + if !(radius > 0.0) || width == 0 || height == 0 || width.checked_mul(height).and_then(|count| count.checked_mul(4)).is_none_or(|bytes| pixels.len() < bytes) { + return; + } + let radius = radius.min(width.min(height) as f32 / 2.0); + let reach = (radius.ceil() as usize).min(width).min(height); + for row in 0..reach { + for column in 0..reach { + // Distance of the pixel centre from the corner circle's centre. + let dx = radius - (column as f32 + 0.5); + let dy = radius - (row as f32 + 0.5); + if dx <= 0.0 || dy <= 0.0 { + continue; + } + let coverage = (radius - dx.hypot(dy) + 0.5).clamp(0.0, 1.0); + if coverage >= 1.0 { + continue; + } + let right = width - 1 - column; + let bottom = height - 1 - row; + scale(pixels, (row * width + column) * 4, coverage); + if right != column { + scale(pixels, (row * width + right) * 4, coverage); + } + if bottom == row { + continue; + } + scale(pixels, (bottom * width + column) * 4, coverage); + if right != column { + scale(pixels, (bottom * width + right) * 4, coverage); + } + } + } +} + +fn scale(pixels: &mut [u8], index: usize, coverage: f32) { + // Premultiplied color: every channel scales with the coverage. + for channel in &mut pixels[index..index + 4] { + *channel = (f32::from(*channel) * coverage + 0.5) as u8; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn corners_become_transparent_and_the_middle_stays() { + let (width, height) = (40usize, 30usize); + let mut pixels = vec![255u8; width * height * 4]; + apply(&mut pixels, width, height, 10.0); + let alpha = |x: usize, y: usize| pixels[(y * width + x) * 4 + 3]; + for (x, y) in [(0, 0), (width - 1, 0), (0, height - 1), (width - 1, height - 1)] { + assert_eq!(alpha(x, y), 0, "{x},{y}"); + } + for (x, y) in [(20, 15), (20, 0), (0, 15), (width - 1, 15), (20, height - 1), (10, 10), (4, 4)] { + assert_eq!(alpha(x, y), 255, "{x},{y}"); + } + // The edge is antialiased, and the color stays premultiplied. + let edge: Vec<&[u8]> = pixels.chunks(4).filter(|pixel| pixel[3] != 0 && pixel[3] != 255).collect(); + assert!(!edge.is_empty() && edge.iter().all(|pixel| pixel[0] == pixel[3])); + // Symmetric in both directions. + for (x, y) in [(1, 3), (2, 2), (5, 0)] { + assert_eq!(alpha(x, y), alpha(width - 1 - x, y)); + assert_eq!(alpha(x, y), alpha(x, height - 1 - y)); + } + } + + #[test] + fn a_radius_larger_than_the_bitmap_and_bad_input_are_safe() { + let mut small = vec![255u8; 3 * 3 * 4]; + apply(&mut small, 3, 3, 100.0); + assert_eq!(small[4 * 4 + 3], 255); + let mut short = vec![255u8; 8]; + apply(&mut short, 3, 3, 2.0); + assert_eq!(short, vec![255u8; 8]); + let mut untouched = vec![255u8; 16]; + apply(&mut untouched, 2, 2, 0.0); + apply(&mut untouched, 2, 2, f32::NAN); + apply(&mut untouched, usize::MAX, 2, 4.0); + assert_eq!(untouched, vec![255u8; 16]); + } +} diff --git a/Linux/crates/sw-render/src/svg.rs b/Linux/crates/sw-render/src/svg.rs new file mode 100644 index 00000000..4aa7c2ca --- /dev/null +++ b/Linux/crates/sw-render/src/svg.rs @@ -0,0 +1,407 @@ +//! Paints the inline SVG the shared renderer emits: icons, rings, gauges, +//! charts and shape strokes. Presentation attributes inherit from ancestors +//! as in SVG; `currentColor` resolves to the element's CSS color. Ported +//! from `Windows/src/ScriptWidget.Core/Render/SvgPainter.cs`; masks are +//! composited as in the Android original. + +use std::collections::HashMap; + +use skia_safe::canvas::SaveLayerRec; +use skia_safe::paint::{Cap, Join, Style}; +use skia_safe::{BlendMode, Canvas, Color, ContourMeasureIter, Matrix, Paint, Path, PathBuilder, PathDirection, PathEffect, PathFillType, Point, Rect, luma_color_filter}; + +use crate::css; +use crate::dom::DomNode; +use crate::text::{TextEngine, TextStyle}; + +#[derive(Clone)] +struct Attributes { + fill: String, + stroke: String, + stroke_width: f32, + line_cap: String, + line_join: String, + fill_rule: String, + font_size: f32, + font_family: String, + font_weight: i32, + text_anchor: String, +} + +impl Default for Attributes { + fn default() -> Self { + Self { + fill: "black".into(), + stroke: "none".into(), + stroke_width: 1.0, + line_cap: "butt".into(), + line_join: "miter".into(), + fill_rule: "nonzero".into(), + font_size: 16.0, + font_family: "sans-serif".into(), + font_weight: 400, + text_anchor: "start".into(), + } + } +} + +impl Attributes { + fn merged(&self, node: &DomNode) -> Self { + if node.attributes.is_empty() { + return self.clone(); + } + let text = |name: &str, inherited: &String| node.attribute(name).map_or_else(|| inherited.clone(), str::to_owned); + Self { + fill: text("fill", &self.fill), + stroke: text("stroke", &self.stroke), + stroke_width: css::number(node.attribute("stroke-width")).unwrap_or(self.stroke_width), + line_cap: text("stroke-linecap", &self.line_cap), + line_join: text("stroke-linejoin", &self.line_join), + fill_rule: text("fill-rule", &self.fill_rule), + font_size: css::pixels(node.attribute("font-size")).unwrap_or(self.font_size), + font_family: text("font-family", &self.font_family), + font_weight: node.attribute("font-weight").and_then(|value| value.trim().parse().ok()).unwrap_or(self.font_weight), + text_anchor: text("text-anchor", &self.text_anchor), + } + } +} + +fn numbers(value: Option<&str>) -> Vec { + value.map_or_else(Vec::new, |value| value.split(|character: char| character.is_whitespace() || character == ',').filter_map(|part| css::number(Some(part))).collect()) +} + +fn unit(value: Option) -> f32 { + value.map_or(1.0, |value| value.clamp(0.0, 1.0)) +} + +fn with_opacity(color: Color, opacity: f32) -> Color { + if opacity >= 1.0 { color } else { color.with_a((f32::from(color.a()) * opacity + 0.5) as u8) } +} + +pub struct SvgPainter<'a> { + text: &'a mut TextEngine, + current_color: Color, + masks: HashMap, +} + +impl<'a> SvgPainter<'a> { + /// Paints an `` element with its top-left corner at the canvas origin. + pub fn paint(text: &'a mut TextEngine, canvas: &Canvas, root: &'a DomNode, css_color: Color) { + let mut painter = Self { text, current_color: css_color, masks: HashMap::new() }; + collect_masks(root, &mut painter.masks); + let (Some(width), Some(height)) = (css::number(root.attribute("width")), css::number(root.attribute("height"))) else { return }; + canvas.save(); + if let Some(degrees) = root.style("transform").and_then(root_rotation) { + canvas.rotate(degrees, Some(Point::new(width / 2.0, height / 2.0))); + } + let view_box = numbers(root.attribute("viewBox")); + if view_box.len() == 4 && view_box[2] > 0.0 && view_box[3] > 0.0 { + let scale = (width / view_box[2]).min(height / view_box[3]); + canvas.translate(((width - view_box[2] * scale) / 2.0, (height - view_box[3] * scale) / 2.0)); + canvas.scale((scale, scale)); + canvas.translate((-view_box[0], -view_box[1])); + } + let attributes = Attributes::default().merged(root); + for child in &root.children { + painter.draw(canvas, child, &attributes); + } + canvas.restore(); + } + + fn draw(&mut self, canvas: &Canvas, node: &DomNode, inherited: &Attributes) { + if node.tag == "defs" || node.tag == "mask" || node.tag.is_empty() { + return; + } + let attributes = inherited.merged(node); + let mask = node.attribute("mask").and_then(mask_reference).and_then(|id| self.masks.get(id).copied()); + let opacity = unit(css::number(node.attribute("opacity"))); + + canvas.save(); + if let Some(transform) = node.attribute("transform") { + canvas.concat(&transform_list(transform)); + } + let layered = mask.is_some() || opacity < 1.0; + if layered { + canvas.save_layer_alpha_f(None, opacity); + } + match node.tag.as_str() { + "g" => { + for child in &node.children { + self.draw(canvas, child, &attributes); + } + } + "text" => self.draw_text(canvas, node, &attributes), + _ => self.draw_path(canvas, node, &attributes), + } + if let Some(mask) = mask { + // An SVG mask keeps the drawing where the mask is light: the mask's + // luminance, drawn as a layer of its own, becomes the alpha of the drawing. + let mut paint = Paint::default(); + paint.set_blend_mode(BlendMode::DstIn).set_color_filter(luma_color_filter::new()); + canvas.save_layer(&SaveLayerRec::default().paint(&paint)); + let mask_attributes = Attributes::default().merged(mask); + for child in &mask.children { + self.draw(canvas, child, &mask_attributes); + } + canvas.restore(); + } + if layered { + canvas.restore(); + } + canvas.restore(); + } + + fn resolve_color(&self, value: &str) -> Option { + if value == "currentColor" { Some(self.current_color) } else { css::color(Some(value)) } + } + + fn draw_path(&mut self, canvas: &Canvas, node: &DomNode, attributes: &Attributes) { + let Some(path) = shape(node, attributes) else { return }; + let fill_opacity = unit(css::number(node.attribute("fill-opacity"))); + let stroke_opacity = unit(css::number(node.attribute("stroke-opacity"))); + if node.tag != "line" + && let Some(fill) = self.resolve_color(&attributes.fill).filter(|color| color.a() != 0) + { + let mut paint = Paint::default(); + paint.set_anti_alias(true).set_color(with_opacity(fill, fill_opacity)); + canvas.draw_path(&path, &paint); + } + let Some(stroke) = self.resolve_color(&attributes.stroke).filter(|color| color.a() != 0) else { return }; + if !(attributes.stroke_width > 0.0) { + return; + } + let mut paint = Paint::default(); + paint + .set_anti_alias(true) + .set_style(Style::Stroke) + .set_stroke_width(attributes.stroke_width) + .set_stroke_miter(4.0) + .set_stroke_cap(match attributes.line_cap.as_str() { + "round" => Cap::Round, + "square" => Cap::Square, + _ => Cap::Butt, + }) + .set_stroke_join(match attributes.line_join.as_str() { + "round" => Join::Round, + "bevel" => Join::Bevel, + _ => Join::Miter, + }) + .set_color(with_opacity(stroke, stroke_opacity)); + let dashes = numbers(node.attribute("stroke-dasharray")); + if !dashes.is_empty() && dashes.iter().any(|value| *value > 0.0) { + // pathLength rescales dash lengths to the real outline length. + let unit = match css::number(node.attribute("pathLength")) { + Some(path_length) if path_length > 0.0 => path_length_of(&path) / path_length, + _ => 1.0, + }; + let even = if dashes.len().is_multiple_of(2) { dashes.clone() } else { [dashes.as_slice(), dashes.as_slice()].concat() }; + // A zero-length dash with round caps would still draw a dot. + if even[0] <= 0.0 { + return; + } + let intervals: Vec = even.iter().enumerate().map(|(index, value)| (value * unit).max(if index % 2 == 0 { 0.0001 } else { 0.0 })).collect(); + let total: f32 = intervals.iter().sum(); + let offset = css::number(node.attribute("stroke-dashoffset")).unwrap_or(0.0) * unit; + let mut phase = if total > 0.0 { (offset % total + total) % total } else { 0.0 }; + // An offset that ends exactly on a dash would start the outline with a dash of no + // length, which is stroked as a blot of the stroke's width: start in the gap after it. + let mut dash_end = 0.0; + for pair in intervals.chunks_exact(2) { + dash_end += pair[0]; + if phase <= dash_end && dash_end - phase < total * 1.0e-4 { + phase = dash_end + total * 1.0e-5; + break; + } + dash_end += pair[1]; + } + if let Some(effect) = PathEffect::dash(&intervals, phase) { + paint.set_path_effect(effect); + } + } + canvas.draw_path(&path, &paint); + } + + fn draw_text(&mut self, canvas: &Canvas, node: &DomNode, attributes: &Attributes) { + let Some(content) = node.text.as_deref() else { return }; + let Some(fill) = self.resolve_color(&attributes.fill) else { return }; + let style = TextStyle { family: attributes.font_family.clone(), size: attributes.font_size, weight: attributes.font_weight, leading: attributes.font_size * 1.2 }; + let mut x = css::number(node.attribute("x")).unwrap_or(0.0); + match attributes.text_anchor.as_str() { + "middle" => x -= self.text.measure(content, &style) / 2.0, + "end" => x -= self.text.measure(content, &style), + _ => {} + } + let mut paint = Paint::default(); + paint.set_anti_alias(true).set_color(fill); + self.text.draw(canvas, content, &style, x, css::number(node.attribute("y")).unwrap_or(0.0), &paint); + } +} + +fn collect_masks<'a>(node: &'a DomNode, into: &mut HashMap) { + if node.tag == "mask" + && let Some(id) = node.attribute("id") + { + into.insert(id.to_owned(), node); + } + for child in &node.children { + collect_masks(child, into); + } +} + +/// `rotate(deg)` on the root element. +fn root_rotation(value: &str) -> Option { + css::functions(value).into_iter().find(|(name, _)| *name == "rotate").and_then(|(_, argument)| css::number(argument.trim().strip_suffix("deg"))) +} + +/// The id in `url(#id)`. +fn mask_reference(value: &str) -> Option<&str> { + let rest = &value[value.find("url(#")? + "url(#".len()..]; + rest.find(')').filter(|end| *end > 0).map(|end| &rest[..end]) +} + +/// SVG transform list: translate, scale, rotate. +fn transform_list(value: &str) -> Matrix { + let mut matrix = Matrix::new_identity(); + for (name, arguments) in css::functions(value) { + let arguments = numbers(Some(arguments)); + let argument = |index: usize, fallback: f32| arguments.get(index).copied().unwrap_or(fallback); + let step = match name { + "translate" => Matrix::translate((argument(0, 0.0), argument(1, 0.0))), + "scale" => Matrix::scale((argument(0, 1.0), argument(1, argument(0, 1.0)))), + "rotate" if arguments.len() >= 3 => Matrix::rotate_deg_pivot(arguments[0], (arguments[1], arguments[2])), + "rotate" => Matrix::rotate_deg(argument(0, 0.0)), + _ => continue, + }; + matrix.pre_concat(&step); + } + matrix +} + +/// The length of the outline, measured finely: the default resolution cuts +/// curves into so few lines that a small circle comes out 2% short, and a +/// full ring drawn as one dash of its `pathLength` would not close. +fn path_length_of(path: &Path) -> f32 { + ContourMeasureIter::new(path, false, MEASURE_RESOLUTION).map(|contour| contour.length()).sum() +} + +const MEASURE_RESOLUTION: f32 = 64.0; + +fn shape(node: &DomNode, attributes: &Attributes) -> Option { + let number = |name: &str| css::number(node.attribute(name)).unwrap_or(0.0); + let fill_type = if attributes.fill_rule == "evenodd" { PathFillType::EvenOdd } else { PathFillType::Winding }; + match node.tag.as_str() { + "path" => Path::from_svg(node.attribute("d")?).map(|path| path.with_fill_type(fill_type)), + "rect" => { + let (x, y, width, height) = (number("x"), number("y"), number("width"), number("height")); + if width <= 0.0 || height <= 0.0 { + return None; + } + let rx = css::number(node.attribute("rx")).or_else(|| css::number(node.attribute("ry"))).unwrap_or(0.0).min(width / 2.0); + let ry = css::number(node.attribute("ry")).unwrap_or(rx).min(height / 2.0); + Some(rounded_rect(x, y, width, height, rx, ry)) + } + "circle" => Some(oval(number("cx"), number("cy"), number("r"), number("r"))), + "ellipse" => Some(oval(number("cx"), number("cy"), number("rx"), number("ry"))), + "line" => Some(Path::line((number("x1"), number("y1")), (number("x2"), number("y2")))), + "polyline" | "polygon" => { + let values = numbers(node.attribute("points")); + if values.len() < 4 { + return None; + } + let points: Vec = values.chunks_exact(2).map(|pair| Point::new(pair[0], pair[1])).collect(); + let mut builder = PathBuilder::new_with_fill_type(fill_type); + builder.add_polygon(&points, node.tag == "polygon"); + Some(builder.detach()) + } + _ => None, + } +} + +/// An oval that starts at 3 o'clock and runs clockwise, as SVG circles do; dash offsets depend on it. +fn oval(cx: f32, cy: f32, rx: f32, ry: f32) -> Path { + let mut builder = PathBuilder::new(); + if rx > 0.0 && ry > 0.0 { + builder.add_oval(Rect::from_ltrb(cx - rx, cy - ry, cx + rx, cy + ry), PathDirection::CW, 1); + } else { + builder.move_to((cx + rx, cy)).close(); + } + builder.detach() +} + +/// SVG rectangles start at (x + rx, y) and run clockwise; dash offsets depend on it. +fn rounded_rect(x: f32, y: f32, width: f32, height: f32, rx: f32, ry: f32) -> Path { + let mut builder = PathBuilder::new(); + let (right, bottom) = (x + width, y + height); + if rx <= 0.0 || ry <= 0.0 { + builder.move_to((x, y)).line_to((right, y)).line_to((right, bottom)).line_to((x, bottom)).close(); + return builder.detach(); + } + // A conic with this weight through the corner point is an exact quarter of an ellipse. + let quarter = std::f32::consts::FRAC_1_SQRT_2; + builder + .move_to((x + rx, y)) + .line_to((right - rx, y)) + .conic_to((right, y), (right, y + ry), quarter) + .line_to((right, bottom - ry)) + .conic_to((right, bottom), (right - rx, bottom), quarter) + .line_to((x + rx, bottom)) + .conic_to((x, bottom), (x, bottom - ry), quarter) + .line_to((x, y + ry)) + .conic_to((x, y), (x + rx, y), quarter) + .close(); + builder.detach() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_references_and_transforms() { + assert_eq!(mask_reference("url(#m1)"), Some("m1")); + assert_eq!(mask_reference("url(#)"), None); + assert_eq!(mask_reference("none"), None); + assert_eq!(root_rotation("rotate(-90deg)"), Some(-90.0)); + assert_eq!(root_rotation("scale(2)"), None); + let matrix = transform_list("translate(10, 20) scale(2)"); + assert_eq!(matrix.map_point((1.0, 1.0)), Point::new(12.0, 22.0)); + let turned = transform_list("rotate(90 10 10)").map_point((20.0, 10.0)); + assert!((turned.x - 10.0).abs() < 0.001 && (turned.y - 20.0).abs() < 0.001, "{turned:?}"); + assert_eq!(numbers(Some("1,2 3\n4.5")), [1.0, 2.0, 3.0, 4.5]); + } + + #[test] + fn outlines_start_where_svg_says() { + // Dash offsets are measured from the start of the outline. + let circle = oval(10.0, 10.0, 5.0, 5.0); + let mut contours = ContourMeasureIter::new(&circle, false, None); + let start = contours.next().and_then(|contour| contour.pos_tan(0.0)).expect("start").0; + assert!((start.x - 15.0).abs() < 0.01 && (start.y - 10.0).abs() < 0.01, "{start:?}"); + let length = path_length_of(&circle); + assert!((length - 2.0 * std::f32::consts::PI * 5.0).abs() < 0.02, "{length}"); + + let rect = rounded_rect(0.0, 0.0, 20.0, 10.0, 2.0, 2.0); + let mut contours = ContourMeasureIter::new(&rect, false, MEASURE_RESOLUTION); + let contour = contours.next().expect("contour"); + let (start, direction) = contour.pos_tan(0.0).expect("start"); + assert!((start.x - 2.0).abs() < 0.01 && start.y.abs() < 0.01 && direction.x > 0.9, "{start:?} {direction:?}"); + let expected = 2.0 * (16.0 + 6.0) + 2.0 * std::f32::consts::PI * 2.0; + assert!((contour.length() - expected).abs() < 0.1, "{}", contour.length()); + } + + #[test] + fn path_data_is_parsed_by_skia() { + let node = |tag: &str, attributes: &[(&str, &str)]| DomNode { tag: tag.into(), svg: true, attributes: attributes.iter().map(|(name, value)| ((*name).to_owned(), (*value).to_owned())).collect(), ..Default::default() }; + let attributes = Attributes::default(); + let path = shape(&node("path", &[("d", "M0 0h10v10H0z")]), &attributes).expect("path"); + assert_eq!(*path.bounds(), Rect::from_ltrb(0.0, 0.0, 10.0, 10.0)); + // Arc flags written without separators, as icon sets do. + assert!(shape(&node("path", &[("d", "M2 12a10 10 0 1020 0")]), &attributes).is_some()); + assert!(shape(&node("path", &[("d", "M0 0 L")]), &attributes).is_none()); + assert!(shape(&node("path", &[]), &attributes).is_none()); + assert!(shape(&node("rect", &[("width", "0"), ("height", "4")]), &attributes).is_none()); + assert!(shape(&node("polyline", &[("points", "0,0 4,4")]), &attributes).is_some()); + assert!(shape(&node("polyline", &[("points", "0,0")]), &attributes).is_none()); + assert!(shape(&node("unknown", &[]), &attributes).is_none()); + } +} diff --git a/Linux/crates/sw-render/src/text.rs b/Linux/crates/sw-render/src/text.rs new file mode 100644 index 00000000..2508ad6b --- /dev/null +++ b/Linux/crates/sw-render/src/text.rs @@ -0,0 +1,357 @@ +//! Text measurement and line breaking. The shared layout asks for sizes +//! through this module and the painter draws with it, so what was measured +//! is what gets drawn: both build the same Skia paragraph, with unhinted +//! glyphs at subpixel positions. All values are in points; the painter +//! scales the canvas. Ported from +//! `Windows/src/ScriptWidget.Core/Render/TextEngine.cs`. + +use std::collections::HashMap; + +use skia_safe::font::Edging; +use skia_safe::font_style::{Slant, Width}; +use skia_safe::textlayout::{Paragraph, ParagraphBuilder, ParagraphStyle, TextDirection, TextStyle as ParagraphTextStyle}; +use skia_safe::{Canvas, FontHinting, FontStyle, Paint}; + +use crate::fonts::FontRegistry; + +/// Font description shared by measurement and drawing. Sizes are in points. +#[derive(Debug, Clone, PartialEq)] +pub struct TextStyle { + pub family: String, + pub size: f32, + pub weight: i32, + pub leading: f32, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct TextLine { + pub text: String, + pub width: f32, +} + +pub struct TextEngine { + fonts: FontRegistry, + /// Widths of single lines; the layout asks for the same texts again and again. + widths: HashMap<(String, i32, u32, String), f32>, +} + +impl TextEngine { + pub fn new(fonts: FontRegistry) -> Self { + Self { fonts, widths: HashMap::new() } + } + + pub fn fonts(&self) -> &FontRegistry { + &self.fonts + } + + /// For changing the locale or the default face; measured widths are forgotten. + pub fn fonts_mut(&mut self) -> &mut FontRegistry { + self.widths.clear(); + &mut self.fonts + } + + /// The real family name of the face a CSS font-family resolves to; empty when the machine has none for it. + pub fn primary_family(&mut self, family: &str, weight: i32) -> String { + self.fonts.face(family, weight).primary.clone() + } + + /// One paragraph of `text` in `style`, not laid out yet. `paint` colors the glyphs. + fn paragraph(&mut self, text: &str, style: &TextStyle, paint: Option<&Paint>) -> Paragraph { + let face = self.fonts.face(&style.family, style.weight); + let mut text_style = ParagraphTextStyle::new(); + text_style + .set_font_size(if style.size > 0.0 && style.size.is_finite() { style.size } else { 0.01 }) + .set_font_style(FontStyle::new(face.weight.into(), Width::NORMAL, Slant::Upright)) + .set_font_families(&face.families) + // Fallback for what the listed families lack takes the locale into account too. + .set_locale(self.fonts.locale()) + // Glyphs keep their measured shapes and positions at every scale: no fitting to the pixel grid. + .set_subpixel(true) + .set_font_hinting(FontHinting::None) + // Grayscale: subpixel color fringes need an opaque background, and a widget bitmap is transparent. + .set_font_edging(Edging::AntiAlias); + if let Some(paint) = paint { + text_style.set_foreground_paint(paint); + } + let mut paragraph_style = ParagraphStyle::new(); + paragraph_style + .set_text_direction(TextDirection::LTR) + .turn_hinting_off() + // Skia otherwise lays out in the width rounded down to a whole number: a line measured + // at 107.2 would not fit a box of 107.5, and the layout sizes boxes to measured widths. + .set_apply_rounding_hack(false) + .set_text_style(&text_style); + // A character followed by the emoji variation selector is an emoji even when the text + // face has a plain glyph for it, as Inter has for the heart and the sun. + let emoji = if text.contains(EMOJI_SELECTOR) { self.fonts.emoji_family() } else { None }; + let mut builder = ParagraphBuilder::new(¶graph_style, self.fonts.collection()); + builder.push_style(&text_style); + match emoji { + Some(emoji) => { + let mut emoji_style = text_style.clone(); + emoji_style.set_font_families(&std::iter::once(&emoji).chain(&face.families).collect::>()); + let mut plain = 0usize; + for (start, end) in emoji_presentation_runs(text) { + builder.add_text(&text[plain..start]); + builder.push_style(&emoji_style).add_text(&text[start..end]).pop(); + plain = end; + } + builder.add_text(&text[plain..]); + } + None => { + builder.add_text(text); + } + } + builder.build() + } + + /// Width of `text` on one line, trailing spaces included. + pub fn measure(&mut self, text: &str, style: &TextStyle) -> f32 { + if text.is_empty() { + return 0.0; + } + let key = (style.family.clone(), style.weight, style.size.to_bits(), text.to_owned()); + if let Some(width) = self.widths.get(&key) { + return *width; + } + let mut paragraph = self.paragraph(text, style, None); + paragraph.layout(UNBOUNDED); + let width = paragraph.max_intrinsic_width(); + if self.widths.len() >= MAXIMUM_WIDTHS { + self.widths.clear(); + } + self.widths.insert(key, width); + width + } + + /// Lines of `text` in a box `width` points wide. Explicit newlines always + /// break. With a width, text wraps like CSS `white-space: pre-wrap` with + /// `overflow-wrap: break-word`; without one it does not wrap. + pub fn lines(&mut self, text: &str, style: &TextStyle, width: Option) -> Vec { + let mut result = Vec::new(); + for paragraph_text in text.split('\n') { + if paragraph_text.is_empty() { + result.push(TextLine { text: String::new(), width: 0.0 }); + continue; + } + let whole = self.measure(paragraph_text, style); + let limit = match width { + Some(limit) if whole > limit + 0.01 => limit, + _ => { + result.push(TextLine { text: paragraph_text.to_owned(), width: whole }); + continue; + } + }; + let mut paragraph = self.paragraph(paragraph_text, style, None); + paragraph.layout(limit.max(0.01)); + let ends: Vec = paragraph.get_line_metrics().iter().map(|metrics| byte_offset(paragraph_text, metrics.end_index)).collect(); + let mut start = 0usize; + for end in ends { + // The end takes the spaces the line ends with. + if end <= start || end >= paragraph_text.len() { + continue; + } + if !self.is_between_words(¶graph_text[start..], end - start, style) { + // A word wider than the box: Skia fills the line before it with its first letters. + break; + } + let line = paragraph_text[start..end].trim_end_matches(' '); + result.push(TextLine { text: line.to_owned(), width: self.measure(line, style) }); + start = end; + } + self.break_long_words(¶graph_text[start..], style, limit, &mut result); + } + result + } + + /// The rest of a paragraph line by line, as CSS `overflow-wrap: + /// break-word` breaks it: a line ends inside a word only when it has no + /// other place to break, so a word wider than the box starts on a line of + /// its own. Skia alone would start it on the line before. + fn break_long_words(&mut self, text: &str, style: &TextStyle, limit: f32, result: &mut Vec) { + let mut rest = text; + for _ in 0..MAXIMUM_BROKEN_LINES { + let trimmed = rest.trim_end_matches(' '); + if self.measure(trimmed, style) <= limit + 0.01 { + break; + } + let mut paragraph = self.paragraph(rest, style, None); + paragraph.layout(limit.max(0.01)); + // At least one character a line. + let mut end = paragraph.get_line_metrics().first().map_or(0, |metrics| byte_offset(rest, metrics.end_index)).max(rest.chars().next().map_or(0, char::len_utf8)); + if end >= rest.len() { + break; + } + if !self.is_between_words(rest, end, style) { + end = self.last_break(&rest[..end], style).unwrap_or(end); + } + let line = rest[..end].trim_end_matches(' '); + result.push(TextLine { text: line.to_owned(), width: self.measure(line, style) }); + rest = &rest[end..]; + } + if !rest.is_empty() { + let line = rest.trim_end_matches(' '); + result.push(TextLine { text: line.to_owned(), width: self.measure(line, style) }); + } + } + + /// Whether a line of `text` may end at `end`, a place the layout chose: true between two + /// words, and inside a word when the line holds nothing but a part of that word. + fn is_between_words(&mut self, text: &str, end: usize, style: &TextStyle) -> bool { + match text[end..].chars().next() { + None | Some(' ') => true, + Some(next) => self.last_break(&text[..end + next.len_utf8()], style) == Some(end), + } + } + + /// The last place in `text` where a line may end. Found by laying the + /// text out in a box a little too narrow for it: its last word then moves + /// to a second line, or, when it is one word, its last character does. + fn last_break(&mut self, text: &str, style: &TextStyle) -> Option { + let trimmed = text.trim_end_matches(' '); + let whole = self.measure(trimmed, style); + if trimmed.is_empty() || whole <= PROBE_MARGIN { + return None; + } + let mut paragraph = self.paragraph(trimmed, style, None); + paragraph.layout(whole - PROBE_MARGIN); + let end = byte_offset(trimmed, paragraph.get_line_metrics().first()?.end_index); + if end == 0 || end >= trimmed.len() { + return None; + } + // Where a line may end the parts are as wide as the whole. Inside an emoji made of + // several characters, or a ligature, they are not: the parts are other glyphs. + let parts = self.measure(&trimmed[..end], style) + self.measure(&trimmed[end..], style); + ((parts - whole).abs() <= style.size.abs() * 0.05 + 0.01).then_some(end) + } + + /// First baseline below the top of a line box of the style's leading (CSS half-leading). + pub fn baseline(&mut self, style: &TextStyle) -> f32 { + let face = self.fonts.face(&style.family, style.weight); + let ascent = face.ascent * style.size; + let descent = face.descent * style.size; + (style.leading - (ascent + descent)) / 2.0 + ascent + } + + /// Draws one line with its left edge at `x` and its baseline at + /// `baseline`, with the paragraph that [`TextEngine::measure`] measures. + pub fn draw(&mut self, canvas: &Canvas, text: &str, style: &TextStyle, x: f32, baseline: f32, paint: &Paint) { + if text.is_empty() { + return; + } + let mut paragraph = self.paragraph(text, style, Some(paint)); + paragraph.layout(UNBOUNDED); + // A fallback font may be taller than the style's font; the line's own baseline says where its top is. + paragraph.paint(canvas, (x, baseline - paragraph.alphabetic_baseline())); + } + + /// The font families that `text` would be drawn with, in order of first use; for diagnosis and tests. + pub fn families_used(&mut self, text: &str, style: &TextStyle) -> Vec { + let mut paragraph = self.paragraph(text, style, None); + paragraph.layout(UNBOUNDED); + let mut families: Vec = Vec::new(); + for info in paragraph.get_fonts() { + let name = info.font.typeface().family_name(); + if !families.contains(&name) { + families.push(name); + } + } + families + } +} + +/// U+FE0F asks for the emoji form of the character before it. +const EMOJI_SELECTOR: char = '\u{fe0f}'; +const ZERO_WIDTH_JOINER: char = '\u{200d}'; +const KEYCAP: char = '\u{20e3}'; + +/// The byte ranges of `text` that ask for emoji presentation: a character +/// with the emoji variation selector, and what is joined to it (a keycap, or +/// more emoji through zero-width joiners). A sequence that starts with an +/// emoji that needs no selector is left to the font fallback, which keeps it +/// in one piece. +fn emoji_presentation_runs(text: &str) -> Vec<(usize, usize)> { + let characters: Vec<(usize, char)> = text.char_indices().collect(); + let is = |index: usize, character: char| characters.get(index).is_some_and(|(_, found)| *found == character); + let mut runs = Vec::new(); + let mut index = 0usize; + while index < characters.len() { + let joined_to_previous = index > 0 && is(index - 1, ZERO_WIDTH_JOINER); + if !is(index + 1, EMOJI_SELECTOR) || joined_to_previous || characters[index].1 == ZERO_WIDTH_JOINER { + index += 1; + continue; + } + let mut end = index + 2; + loop { + if is(end, KEYCAP) { + end += 1; + } else if is(end, ZERO_WIDTH_JOINER) && end + 1 < characters.len() { + end += if is(end + 2, EMOJI_SELECTOR) { 3 } else { 2 }; + } else { + break; + } + } + runs.push((characters[index].0, characters.get(end).map_or(text.len(), |(offset, _)| *offset))); + index = end; + } + runs +} + +/// The byte offset in `text` of an offset in UTF-16 code units, which is how +/// Skia's line metrics count; an offset past the end is the end. +fn byte_offset(text: &str, utf16_offset: usize) -> usize { + let mut units = 0usize; + for (index, character) in text.char_indices() { + if units >= utf16_offset { + return index; + } + units += character.len_utf16(); + } + text.len() +} + +/// A width no line reaches: the paragraph does not wrap. +const UNBOUNDED: f32 = 1.0e9; +const MAXIMUM_WIDTHS: usize = 8192; +/// How much narrower than a text the box is that moves its last word to a second line. +const PROBE_MARGIN: f32 = 0.5; +/// A paragraph with an overlong word is broken into no more lines than this; the rest stays on the last one. +const MAXIMUM_BROKEN_LINES: usize = 200; + +#[cfg(test)] +mod tests { + use super::{byte_offset, emoji_presentation_runs}; + + #[test] + fn characters_with_the_emoji_selector_are_emoji_runs() { + fn run(text: &str) -> Vec<&str> { + emoji_presentation_runs(text).into_iter().map(|(start, end)| &text[start..end]).collect() + } + assert_eq!(run("I \u{2764}\u{fe0f} widgets"), ["\u{2764}\u{fe0f}"]); + assert_eq!(run("\u{2600}\u{fe0f}\u{2764}\u{fe0f}"), ["\u{2600}\u{fe0f}", "\u{2764}\u{fe0f}"]); + // A keycap and a joined sequence stay in one run. + assert_eq!(run("1\u{fe0f}\u{20e3} first"), ["1\u{fe0f}\u{20e3}"]); + assert_eq!(run("\u{1f3f3}\u{fe0f}\u{200d}\u{1f308}!"), ["\u{1f3f3}\u{fe0f}\u{200d}\u{1f308}"]); + assert_eq!(run("\u{1f441}\u{fe0f}\u{200d}\u{1f5e8}\u{fe0f}"), ["\u{1f441}\u{fe0f}\u{200d}\u{1f5e8}\u{fe0f}"]); + // A sequence that starts with an emoji of its own is not cut where a later part has the selector. + assert!(run("\u{1f9d1}\u{200d}\u{2695}\u{fe0f}").is_empty()); + // Plain text, a bare heart, and a selector with nothing before it. + assert!(run("hello \u{2764} \u{1f600}").is_empty()); + assert!(run("\u{fe0f}").is_empty()); + assert!(run("").is_empty()); + assert_eq!(run("\u{2764}\u{fe0f}\u{200d}"), ["\u{2764}\u{fe0f}"]); + } + + #[test] + fn line_ends_counted_in_utf16_units_become_byte_offsets() { + assert_eq!(byte_offset("hello", 2), 2); + // A Han character is one unit and three bytes; an emoji is two units and four bytes. + assert_eq!(byte_offset("今天多云", 2), 6); + assert_eq!(byte_offset("a😀b", 3), 5); + // An offset inside a surrogate pair moves to the end of the character. + assert_eq!(byte_offset("a😀b", 2), 5); + assert_eq!(byte_offset("abc", 0), 0); + assert_eq!(byte_offset("abc", 99), 3); + assert_eq!(byte_offset("", 4), 0); + } +} diff --git a/Linux/crates/sw-render/src/widget.rs b/Linux/crates/sw-render/src/widget.rs new file mode 100644 index 00000000..ed3c499f --- /dev/null +++ b/Linux/crates/sw-render/src/widget.rs @@ -0,0 +1,94 @@ +//! The last step of showing a package: the result of +//! [`sw_core::WidgetRuntime::run`] as a bitmap. Together with that type this +//! is the port of `Windows/src/ScriptWidget.Core/WidgetRuntime.cs`; the +//! painting half lives here because this crate depends on `sw-core`, which +//! stays free of Skia. + +use std::collections::HashMap; + +use sw_core::executor::ExecutionError; +use sw_core::{RunRequest, RunResult, WidgetRuntime}; + +use crate::renderer::{RenderError, RenderOutput, RenderRequest, WidgetRenderer}; +use crate::rounded_corners; + +/// How to draw one run. Lengths are in points. +#[derive(Debug, Clone, PartialEq)] +pub struct Presentation { + pub width: f32, + pub height: f32, + /// Pixels per point of the display the widget is on. + pub scale: f32, + /// The theme to draw in; `None` uses the one the package ran under. + pub dark_mode: Option, + /// Fixed clock for dates, in milliseconds since 1970; `None` uses the real clock. + pub now_milliseconds: Option, + pub maximum_bitmap_bytes: u64, + /// Corner radius in points; 0 leaves the widget square. + pub corner_radius: f32, +} + +impl Presentation { + /// A widget family at its size in points (see [`crate::family_size`]), or `None` for an unknown family. + pub fn for_family(family: &str, scale: f32) -> Option { + let (width, height) = crate::family_size(family)?; + Some(Self { width, height, scale, dark_mode: None, now_milliseconds: None, maximum_bitmap_bytes: u64::MAX, corner_radius: rounded_corners::DEFAULT_RADIUS }) + } +} + +/// The picture of one run. +#[derive(Debug, Clone)] +pub struct RenderedRun { + pub output: RenderOutput, + /// Why the package's own tree could not be drawn; the picture then shows a card with this message. + pub failure: Option, +} + +/// Renders the result of a run. An execution error becomes a visible card, +/// and so does a failure to draw the package's own tree, so a widget is never +/// blank; only a failure to draw that card is returned as an error. Remote +/// images are fetched first, under the same network policy as `fetch`. +pub fn render_run(renderer: &WidgetRenderer, runtime: &WidgetRuntime, request: &RunRequest, result: &RunResult, presentation: &Presentation) -> Result { + let dark_mode = presentation.dark_mode.unwrap_or(request.dark_mode); + let base = |tree: String| RenderRequest { + family: request.family.clone(), + dark_mode, + now_milliseconds: presentation.now_milliseconds, + maximum_bitmap_bytes: presentation.maximum_bitmap_bytes, + ..RenderRequest::new(tree, presentation.width, presentation.height, presentation.scale) + }; + let drawn = match &result.execution.tree { + Some(tree) => { + let remote_images = result.remote_images.as_ref().map(|scope| runtime.fetch_remote_images(tree, scope)).unwrap_or_default(); + renderer.render(RenderRequest { files: Some(request.files.clone()), remote_images, ..base(tree.clone()) }) + } + None => renderer.render(RenderRequest { remote_images: HashMap::new(), ..base(runtime.error_tree(result.execution.error.as_ref())) }), + }; + let (mut output, failure) = match drawn { + Ok(output) => (output, None), + Err(error) if result.execution.tree.is_some() => { + let message = if error.message.is_empty() { "Rendering failed".to_owned() } else { error.message.clone() }; + (renderer.render(base(runtime.error_tree(Some(&ExecutionError { kind: "internalError".into(), message }))))?, Some(error)) + } + Err(error) => return Err(error), + }; + round(&mut output, presentation.corner_radius); + Ok(RenderedRun { output, failure }) +} + +/// Renders a canonical tree that is not the output of a package, such as a message card. +pub fn render_tree(renderer: &WidgetRenderer, tree: &str, family: &str, presentation: &Presentation) -> Result { + let mut output = renderer.render(RenderRequest { + family: family.to_owned(), + dark_mode: presentation.dark_mode.unwrap_or(false), + now_milliseconds: presentation.now_milliseconds, + maximum_bitmap_bytes: presentation.maximum_bitmap_bytes, + ..RenderRequest::new(tree, presentation.width, presentation.height, presentation.scale) + })?; + round(&mut output, presentation.corner_radius); + Ok(output) +} + +fn round(output: &mut RenderOutput, corner_radius: f32) { + rounded_corners::apply(&mut output.pixels, output.pixel_width as usize, output.pixel_height as usize, corner_radius * output.scale); +} diff --git a/Linux/crates/sw-render/tests/corpus.rs b/Linux/crates/sw-render/tests/corpus.rs new file mode 100644 index 00000000..c7b66e3d --- /dev/null +++ b/Linux/crates/sw-render/tests/corpus.rs @@ -0,0 +1,249 @@ +//! Executes every package of the parity corpus and draws its tree at the +//! three desktop widget sizes, scale 2. Fails on a layout or paint error and +//! on an empty bitmap; a handful of the pictures are saved for review. The +//! Linux counterpart of `EveryCorpusPackageRendersAtEverySize` in +//! `Windows/tests/ScriptWidget.Core.Tests/Render/RenderTests.cs`. +//! +//! node Linux/scripts/build-shared-js.mjs +//! cargo test -p sw-render --test corpus -- --nocapture +//! SCRIPTWIDGET_RENDER_ALL=1 cargo test -p sw-render --test corpus # keep every picture +//! +//! This file holds a single test because it sets the time zone of the whole +//! process (see `sw-core/tests/parity.rs`). + +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use std::time::Duration; + +use serde_json::Value; +use sw_core::api::StorageModule; +use sw_core::compiler::PrepareOptions; +use sw_core::{ExecutionRequest, MemoryPackageFiles, NativeModule, PackageCompiler, PackageFiles, ScriptExecutor, SharedScripts, intl}; +use sw_render::{RenderRequest, WidgetRenderer}; + +/// 2026-01-01 12:00 UTC. +const FIXED_NOW: i64 = 1_767_268_800_000; + +/// Packages whose pictures are written for review: clocks, charts, Chinese text, emoji, images, gradients and whole templates. +const REVIEWED: &[&str] = &[ + "api/gradient", + "component/button", + "component/chart", + "component/gauge", + "component/gif", + "component/icon", + "component/image", + "component/ring", + "component/text", + "component/toggle", + "template/Activity Rings", + "template/Animation Aquarium", + "template/Animation Clock", + "template/Daily Agenda", + "template/Image Basic Usage", + "template/Lunar Date", + "template/Material Study", + "template/Month Calendar", + "template/Mood Check-in", + "template/Nyan Cat", + "template/Personal Dashboard", + "template/Shape", + "template/System Status Panel", + "template/Text Year Days Left", + "template/Weather Glance", + "template/World Clock Faces", +]; + +fn linux_directory() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..") +} + +fn file_name(name: &str) -> String { + name.replace(['/', ' '], "_") +} + +/// The bytes of a package image, from the repository's bundled packages. The +/// corpus lists binary files by name only; without the file the package is +/// drawn with an empty one, which the layout shows as a missing image. +fn binary(package: &str, path: &str) -> Vec { + let repository = linux_directory().join(".."); + let bundled = repository.join("Shared/ScriptWidgetRuntime/Resource/Script.bundle").join(package).join(path); + let gallery = repository.join("Gallery/widgets").join(package.trim_start_matches("gallery/")).join(path); + std::fs::read(bundled).or_else(|_| std::fs::read(gallery)).unwrap_or_default() +} + +fn clip(text: &str, length: usize) -> String { + text.chars().take(length).collect() +} + +fn percentile(sorted: &[Duration], fraction: f64) -> Duration { + sorted.get(((sorted.len() as f64 - 1.0) * fraction).round() as usize).copied().unwrap_or_default() +} + +fn milliseconds(duration: Duration) -> String { + format!("{:.2}", duration.as_secs_f64() * 1000.0) +} + +#[test] +fn every_corpus_package_renders_at_every_size() { + let corpus_file = linux_directory().join("tests/assets/parity-corpus.json"); + let corpus_text = std::fs::read_to_string(&corpus_file).unwrap_or_else(|error| panic!("{}: {error}. Run node Linux/scripts/build-shared-js.mjs.", corpus_file.display())); + let corpus: Value = serde_json::from_str(&corpus_text).expect("parity corpus is JSON"); + let time_zone = corpus["timeZone"].as_str().expect("corpus time zone"); + let locale = corpus["locale"].as_str().unwrap_or("en-US"); + let prelude = corpus["prelude"].as_str().unwrap_or(""); + let packages = corpus["corpus"].as_array().expect("corpus packages"); + + // SAFETY: the only test of this program, and no other thread exists yet + // that reads the environment or converts local time. + unsafe { intl::set_time_zone_override(Some(time_zone)) }; + intl::set_locale_override(Some(locale)); + + let write_all = std::env::var("SCRIPTWIDGET_RENDER_ALL").as_deref() == Ok("1"); + let output = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/out"); + let sizes = [("small", 170.0f32, 170.0f32), ("medium", 329.0, 170.0), ("large", 329.0, 345.0)]; + for (family, _, _) in sizes { + let directory = output.join(family); + let _ = std::fs::remove_dir_all(&directory); + std::fs::create_dir_all(&directory).expect("output directory"); + } + + let scripts = Arc::new(SharedScripts::new(linux_directory().join("assets/scriptwidget")).expect("shared scripts")); + let compiler = PackageCompiler::new(scripts.clone()).expect("compiler thread"); + let executor = ScriptExecutor::new(scripts.clone()); + let start = std::time::Instant::now(); + let renderer = WidgetRenderer::with_locale(scripts, locale).expect("render thread"); + let fonts = renderer.fonts().expect("fonts"); + // Starting the thread, loading the bundled fonts and asking fontconfig for the system ones. + let font_time = start.elapsed(); + let storage_directory = std::env::temp_dir().join(format!("scriptwidget-render-storage-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&storage_directory); + let storage: Arc = Arc::new(StorageModule::new(&storage_directory)); + + let mut failures: Vec = Vec::new(); + let (mut layout, mut paint, mut encode) = (Duration::ZERO, Duration::ZERO, Duration::ZERO); + let mut totals: Vec = Vec::new(); + let mut slowest = (Duration::ZERO, String::new()); + let mut first = None; + let (mut binaries_found, mut binaries_missing, mut hit_regions, mut gif_slots, mut written) = (0usize, 0usize, 0usize, 0usize, 0usize); + for (index, item) in packages.iter().enumerate() { + let name = item["name"].as_str().unwrap_or("?"); + let text = item["files"].as_object().expect("package files").iter().map(|(path, content)| (path.clone(), content.as_str().unwrap_or("").to_owned())); + let binaries: Vec<(String, Vec)> = item["binaries"].as_array().into_iter().flatten().filter_map(|path| Some((path.as_str()?.to_owned(), binary(name, path.as_str()?)))).collect(); + binaries_found += binaries.iter().filter(|(_, data)| !data.is_empty()).count(); + binaries_missing += binaries.iter().filter(|(_, data)| data.is_empty()).count(); + let files = Arc::new(MemoryPackageFiles::new(text, binaries)); + + let prepared = match compiler.prepare(files.as_ref(), &PrepareOptions { prelude, ..Default::default() }) { + Ok(prepared) => prepared, + Err(error) => { + failures.push(format!("{name}: prepare failed [{}] {}", error.kind, clip(&error.message, 300))); + continue; + } + }; + let mut request = ExecutionRequest::new(&prepared, files.as_ref()); + request.modules = vec![storage.clone()]; + request.package_id = format!("render-{index}"); + let result = executor.execute(&request); + let Some(tree) = result.tree else { + let error = result.error.as_ref(); + failures.push(format!("{name}: no tree: [{}] {}", error.map_or("", |error| error.kind.as_str()), clip(error.map_or("", |error| error.message.as_str()), 300))); + continue; + }; + + for (family, width, height) in sizes { + let mut request = RenderRequest::new(tree.clone(), width, height, 2.0); + request.family = family.to_owned(); + request.files = Some(files.clone() as Arc); + request.now_milliseconds = Some(FIXED_NOW); + let rendered = match renderer.render(request.clone()) { + Ok(rendered) => rendered, + Err(error) => { + failures.push(format!("{name} ({family}): {}", clip(&error.message, 500))); + continue; + } + }; + let total = rendered.layout_time + rendered.paint_time; + first.get_or_insert(total); + layout += rendered.layout_time; + paint += rendered.paint_time; + if total > slowest.0 { + slowest = (total, format!("{name} ({family})")); + } + totals.push(total); + hit_regions += rendered.hit_regions.len(); + gif_slots += rendered.gif_slots.len(); + if (rendered.pixel_width, rendered.pixel_height) != ((width * 2.0) as u32, (height * 2.0) as u32) { + failures.push(format!("{name} ({family}): bitmap is {}x{}", rendered.pixel_width, rendered.pixel_height)); + } + if rendered.pixels.len() != rendered.pixel_width as usize * rendered.pixel_height as usize * 4 { + failures.push(format!("{name} ({family}): {} bytes for {}x{}", rendered.pixels.len(), rendered.pixel_width, rendered.pixel_height)); + } + if rendered.pixels.chunks_exact(4).all(|pixel| pixel[3] == 0) { + failures.push(format!("{name} ({family}): bitmap is empty")); + } + // Premultiplied: no color channel exceeds the alpha. + if rendered.pixels.chunks_exact(4).any(|pixel| pixel[0] > pixel[3] || pixel[1] > pixel[3] || pixel[2] > pixel[3]) { + failures.push(format!("{name} ({family}): a pixel is not premultiplied")); + } + for hit in &rendered.hit_regions { + let bounds = hit.bounds; + if !(bounds.width() > 0.0 && bounds.height() > 0.0) || hit.interaction.is_empty() { + failures.push(format!("{name} ({family}): tap region {bounds:?} {:?}", hit.interaction)); + } + } + if write_all || REVIEWED.contains(&name) { + let encode_start = std::time::Instant::now(); + match rendered.encode_png() { + Ok(png) => { + encode += encode_start.elapsed(); + written += 1; + std::fs::write(output.join(family).join(file_name(name) + ".png"), png).expect("PNG file"); + } + Err(error) => failures.push(format!("{name} ({family}): {error}")), + } + // The recorded elements, to compare a picture with the CSS it was drawn from. + if write_all { + std::fs::write(output.join(family).join(file_name(name) + ".json"), renderer.layout(request).unwrap_or_default()).expect("layout file"); + } + } + } + } + let _ = std::fs::remove_dir_all(&storage_directory); + drop(renderer); + drop(compiler); + intl::set_locale_override(None); + // SAFETY: as above; the compiler and render threads have ended. + unsafe { intl::set_time_zone_override(None) }; + + let renders = totals.len(); + totals.sort_unstable(); + let mean = |total: Duration| milliseconds(total / renders.max(1) as u32); + let summary = format!( + "rendered {renders} bitmaps of {} packages on {} {}; layout mean {} ms, paint mean {} ms; whole render median {} ms, 95th percentile {} ms, slowest {} ms ({}), first {} ms (it creates the layout context), finding the fonts before it {} ms; PNG encoding mean {} ms over {written} files; {hit_regions} tap regions, {gif_slots} animated GIFs; package images found {binaries_found}, missing {binaries_missing}; bundled Inter {}; CJK families {:?}; monospace {:?}; serif {:?}; {}", + packages.len(), + std::env::consts::OS, + std::env::consts::ARCH, + mean(layout), + mean(paint), + milliseconds(percentile(&totals, 0.5)), + milliseconds(percentile(&totals, 0.95)), + milliseconds(slowest.0), + slowest.1, + milliseconds(first.unwrap_or_default()), + milliseconds(font_time), + milliseconds(encode / written.max(1) as u32), + fonts.bundled, + fonts.cjk_families, + fonts.monospace, + fonts.serif, + output.display(), + ); + println!("{summary}"); + for failure in &failures { + println!(" {failure}"); + } + assert!(!packages.is_empty(), "corpus is empty"); + assert!(fonts.bundled, "the bundled Inter was not loaded"); + assert!(failures.is_empty(), "{summary}\n{}", failures.join("\n")); +} diff --git a/Linux/crates/sw-render/tests/render.rs b/Linux/crates/sw-render/tests/render.rs new file mode 100644 index 00000000..72071f35 --- /dev/null +++ b/Linux/crates/sw-render/tests/render.rs @@ -0,0 +1,451 @@ +//! The renderer as a host uses it: layout in the shared web renderer, then +//! paint. Ported from `Windows/tests/ScriptWidget.Core.Tests/Render/RenderTests.cs`; +//! pixels are premultiplied RGBA here. The corpus run is in `corpus.rs`. +//! +//! node Linux/scripts/build-shared-js.mjs +//! cargo test -p sw-render --test render + +use std::path::PathBuf; +use std::sync::{Arc, OnceLock}; + +use serde_json::{Value, json}; +use sw_core::{MemoryPackageFiles, PackageFiles, SharedScripts}; +use sw_render::{RenderOutput, RenderRequest, WidgetRenderer, cjk_region_order, family_size}; + +const FIXED_NOW: i64 = 1_767_268_800_000; + +fn linux_directory() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..") +} + +fn scripts() -> Arc { + static SCRIPTS: OnceLock> = OnceLock::new(); + SCRIPTS.get_or_init(|| Arc::new(SharedScripts::new(linux_directory().join("assets/scriptwidget")).expect("shared scripts: run node Linux/scripts/build-shared-js.mjs"))).clone() +} + +/// One renderer (and its layout context) for the whole file; calls from the test threads are queued to its thread. +fn renderer() -> &'static WidgetRenderer { + static RENDERER: OnceLock = OnceLock::new(); + RENDERER.get_or_init(|| WidgetRenderer::with_locale(scripts(), "en-US").expect("render thread")) +} + +fn render(tree: &str, width: f32, height: f32, scale: f32) -> RenderOutput { + renderer().render(RenderRequest::new(tree, width, height, scale)).expect("render") +} + +/// Red, green, blue, alpha of one pixel. +fn pixel(output: &RenderOutput, x: u32, y: u32) -> (u8, u8, u8, u8) { + let at = ((y * output.pixel_width + x) * 4) as usize; + (output.pixels[at], output.pixels[at + 1], output.pixels[at + 2], output.pixels[at + 3]) +} + +fn count(pixels: &[u8], matches: impl Fn(&[u8]) -> bool) -> usize { + pixels.chunks_exact(4).filter(|pixel| matches(pixel)).count() +} + +fn is_red(pixel: &[u8]) -> bool { + pixel == [255, 0, 0, 255] +} + +fn corpus_tree(name: &str) -> String { + static CORPUS: OnceLock = OnceLock::new(); + let corpus = CORPUS.get_or_init(|| serde_json::from_str(&std::fs::read_to_string(linux_directory().join("tests/assets/parity-corpus.json")).expect("parity corpus")).expect("corpus JSON")); + corpus["corpus"].as_array().into_iter().flatten().find(|item| item["name"] == name).and_then(|item| item["expected"]["tree"].as_str()).unwrap_or_else(|| panic!("{name} is not in the corpus")).to_owned() +} + +const RED_CARD: &str = r##"{"$element":"vstack","props":{"frame":"max","background":"#ff0000"},"children":[{"$element":"text","props":{"color":"white"},"children":["Hello"]}]}"##; + +// MARK: - Output + +#[test] +fn the_widget_families_have_the_desktop_sizes() { + assert_eq!(family_size("small"), Some((170.0, 170.0))); + assert_eq!(family_size("medium"), Some((329.0, 170.0))); + assert_eq!(family_size("large"), Some((329.0, 345.0))); + assert_eq!(family_size("extraLarge"), Some((639.0, 345.0))); + assert_eq!(family_size("accessoryCircular"), None); + // The bitmap is the size in points times the scale, for every family. + for (family, width, height) in sw_render::FAMILY_SIZES { + for scale in [1.0f32, 1.5, 2.0] { + let rendered = renderer().render(RenderRequest::for_family(RED_CARD, family, scale).expect("family")).expect("render"); + assert_eq!((rendered.pixel_width, rendered.pixel_height), ((width * scale).ceil() as u32, (height * scale).ceil() as u32), "{family} at {scale}"); + assert_eq!(rendered.scale, scale); + assert_eq!(pixel(&rendered, 0, 0), (255, 0, 0, 255)); + // The last column and row are partly covered when the size is not a whole number of pixels. + assert_eq!(pixel(&rendered, rendered.pixel_width - 2, rendered.pixel_height - 2), (255, 0, 0, 255)); + assert_eq!(pixel(&rendered, rendered.pixel_width - 1, rendered.pixel_height - 1).3 == 255, (width * scale).fract() == 0.0 && (height * scale).fract() == 0.0); + } + } + assert!(RenderRequest::for_family(RED_CARD, "huge", 1.0).is_none()); +} + +/// The bitmap budget lowers the scale instead of failing, and the PNG keeps the pixels. +#[test] +fn the_bitmap_budget_scales_the_output_and_png_keeps_the_pixels() { + let full = render(RED_CARD, 200.0, 100.0, 2.0); + assert_eq!((full.pixel_width, full.pixel_height, full.scale), (400, 200, 2.0)); + let mut request = RenderRequest::new(RED_CARD, 200.0, 100.0, 2.0); + request.maximum_bitmap_bytes = 80_000; + let limited = renderer().render(request).expect("render"); + assert!(limited.scale < 2.0 && limited.scale > 0.9, "scale {}", limited.scale); + assert!(u64::from(limited.pixel_width) * u64::from(limited.pixel_height) * 4 <= 82_000, "{}x{}", limited.pixel_width, limited.pixel_height); + assert!(count(&limited.pixels, is_red) > (limited.pixel_width * limited.pixel_height / 2) as usize); + + let png = full.encode_png().expect("PNG"); + assert_eq!(&png[..8], b"\x89PNG\r\n\x1a\n"); + let image = skia_safe::Image::from_encoded(skia_safe::Data::new_copy(&png)).expect("decoded PNG"); + assert_eq!((image.width() as u32, image.height() as u32), (full.pixel_width, full.pixel_height)); + let info = skia_safe::ImageInfo::new((image.width(), image.height()), skia_safe::ColorType::RGBA8888, skia_safe::AlphaType::Premul, None); + let mut decoded = vec![0u8; full.pixels.len()]; + assert!(image.read_pixels(&info, decoded.as_mut_slice(), info.min_row_bytes(), (0, 0), skia_safe::image::CachingHint::Disallow)); + // Straight alpha and back may move a translucent channel by a step or two. + for (at, (left, right)) in decoded.iter().zip(&full.pixels).enumerate() { + assert!(left.abs_diff(*right) <= 2, "byte {at}: {left} vs {right}"); + } + // The straight pixels are what the PNG stores. + let straight = full.straight_pixels(); + assert_eq!(straight.len(), full.pixels.len()); + for (straight, premultiplied) in straight.chunks_exact(4).zip(full.pixels.chunks_exact(4)) { + assert_eq!(straight[3], premultiplied[3]); + assert!(straight[0] >= premultiplied[0]); + } + assert!(sw_render::encode_png(&[0; 7], 2, 1).is_err()); + assert!(sw_render::encode_png(&[], 0, 0).is_err()); +} + +/// Outside the rounded widget background nothing is drawn: those pixels stay fully transparent. +#[test] +fn pixels_outside_the_rounded_background_are_transparent() { + // A 120-point red square with 30-point corners, centred in a 170-point widget with a clear background. + let tree = r##"{"$element":"zstack","props":{"frame":"max","background":"clear"},"children":[{"$element":"roundedrect","props":{"color":"#ff0000","frame":"120,120","radius":"30"},"children":[]}]}"##; + let rendered = renderer().render(RenderRequest::for_family(tree, "small", 2.0).expect("family")).expect("render"); + assert_eq!((rendered.pixel_width, rendered.pixel_height), (340, 340)); + // The widget's own corners and edges. + for (x, y) in [(0, 0), (339, 0), (0, 339), (339, 339), (170, 20), (20, 170), (170, 320), (320, 170)] { + assert_eq!(pixel(&rendered, x, y), (0, 0, 0, 0), "{x},{y}"); + } + // Inside the square's bounding box (50..290) but outside its rounded corners. + for (x, y) in [(54, 54), (285, 54), (54, 285), (285, 285), (62, 58)] { + assert_eq!(pixel(&rendered, x, y).3, 0, "{x},{y}"); + } + // The square itself, up to the middle of each edge and well inside each corner arc. + for (x, y) in [(170, 170), (170, 53), (53, 170), (286, 170), (170, 286), (75, 75), (265, 265)] { + assert_eq!(pixel(&rendered, x, y), (255, 0, 0, 255), "{x},{y}"); + } + // Only the anti-aliased edge is translucent, and premultiplied: no channel exceeds alpha. + let red = count(&rendered.pixels, is_red); + let clear = count(&rendered.pixels, |pixel| pixel[3] == 0); + assert!((240 * 240 - 4 * 900 - 1500..=240 * 240).contains(&red), "{red} red pixels"); + assert!(340 * 340 - red - clear < 1500, "{} translucent pixels", 340 * 340 - red - clear); + assert_eq!(count(&rendered.pixels, |pixel| pixel[0] > pixel[3] || pixel[1] > pixel[3] || pixel[2] > pixel[3]), 0); + + // A widget with its own background fills the bitmap; the host rounds its corners. + let mut card = renderer().render(RenderRequest::for_family(RED_CARD, "small", 2.0).expect("family")).expect("render"); + assert_eq!(pixel(&card, 0, 0), (255, 0, 0, 255)); + // Its text is drawn in white on the red card. + assert!(count(&card.pixels, |pixel| pixel == [255, 255, 255, 255]) > 50); + sw_render::rounded_corners::apply(&mut card.pixels, 340, 340, sw_render::rounded_corners::DEFAULT_RADIUS * card.scale); + assert_eq!(pixel(&card, 0, 0).3, 0); + assert_eq!(pixel(&card, 170, 0), (255, 0, 0, 255)); +} + +/// A tree that is not one fails, and the renderer works again afterwards. +#[test] +fn a_malformed_tree_fails_and_the_next_render_works() { + assert!(renderer().render(RenderRequest::new("{not json", 100.0, 100.0, 1.0)).is_err()); + assert!(count(&render(RED_CARD, 100.0, 100.0, 1.0).pixels, is_red) > 1000); + // Sizes that cannot be a bitmap are refused rather than allocated. + for (width, height, scale) in [(0.0, 100.0, 1.0), (100.0, -1.0, 1.0), (100.0, 100.0, 0.0), (f32::NAN, 100.0, 1.0), (100.0, f32::INFINITY, 1.0)] { + assert!(renderer().render(RenderRequest::new(RED_CARD, width, height, scale)).is_err(), "{width} x {height} at {scale}"); + } + // A request for an enormous bitmap is cut down to the largest side instead of exhausting memory. + let mut request = RenderRequest::new(RED_CARD, 100.0, 10.0, 1000.0); + request.maximum_bitmap_bytes = 4 * 1024 * 1024; + let limited = renderer().render(request).expect("render"); + assert!(u64::from(limited.pixel_width) * u64::from(limited.pixel_height) * 4 <= 4 * 1024 * 1024 + 65536, "{}x{}", limited.pixel_width, limited.pixel_height); + // A tree nested far deeper than any widget either renders or fails; it does not take the renderer down. + for depth in [60usize, 400, 5000] { + let tree = format!("{}{}{}", r##"{"$element":"zstack","props":{"background":"#ff0000"},"children":["##.repeat(depth), r##"{"$element":"text","props":null,"children":["deep"]}"##, "]}".repeat(depth)); + match renderer().render(RenderRequest::new(tree, 100.0, 100.0, 1.0)) { + Ok(rendered) => assert_eq!(rendered.pixels.len(), 100 * 100 * 4, "{depth} levels"), + Err(error) => assert!(depth > 60 && !error.message.is_empty(), "{depth} levels: {error}"), + } + assert!(count(&render(RED_CARD, 100.0, 100.0, 1.0).pixels, is_red) > 1000, "after {depth} levels"); + } + // Releasing the layout context only costs the time to make it again. + renderer().release(); + assert!(count(&render(RED_CARD, 100.0, 100.0, 1.0).pixels, is_red) > 1000); +} + +/// The layout records what the painter draws; a host can read it for diagnosis. +#[test] +fn the_layout_is_a_tree_of_recorded_elements() { + let layout: Value = serde_json::from_str(&renderer().layout(RenderRequest::new(RED_CARD, 100.0, 100.0, 1.0)).expect("layout")).expect("layout JSON"); + assert_eq!(layout["width"], json!(100)); + assert_eq!(layout["root"]["s"]["width"], json!("100px")); + assert!(layout["root"]["c"].as_array().is_some_and(|children| !children.is_empty())); + assert!(layout["diagnostics"].is_array()); +} + +// MARK: - Tap regions + +fn types(output: &RenderOutput) -> Vec<&str> { + output.hit_regions.iter().map(|hit| hit.interaction.get("type").and_then(Value::as_str).unwrap_or("?")).collect() +} + +fn text<'a>(output: &'a RenderOutput, index: usize, name: &str) -> Option<&'a str> { + output.hit_regions[index].interaction.get(name).and_then(Value::as_str) +} + +#[test] +fn buttons_toggles_and_links_are_tap_regions() { + let mut request = RenderRequest::for_family(corpus_tree("component/button"), "medium", 2.0).expect("family"); + request.now_milliseconds = Some(FIXED_NOW); + let buttons = renderer().render(request).expect("render"); + assert_eq!(buttons.hit_regions.len(), 2, "{:?}", buttons.hit_regions); + for hit in &buttons.hit_regions { + // In points, inside the widget, whatever the scale of the bitmap. + assert!(hit.bounds.left >= 0.0 && hit.bounds.top >= 0.0 && hit.bounds.right <= 329.5 && hit.bounds.bottom <= 170.5, "{:?}", hit.bounds); + assert!(hit.bounds.width() > 20.0 && hit.bounds.height() > 10.0, "{:?}", hit.bounds); + } + // The first button calls a function of the script, the second reloads the widget. + assert_eq!(types(&buttons), ["button", "button"]); + assert_eq!((text(&buttons, 0, "handler"), text(&buttons, 0, "action")), (Some("onButtonClick"), None)); + assert_eq!((text(&buttons, 1, "handler"), text(&buttons, 1, "action")), (None, Some("reload"))); + assert!(buttons.hit_regions[0].bounds.bottom <= buttons.hit_regions[1].bounds.top + 0.5, "the buttons are stacked"); + // The same regions at another scale. + let mut request = RenderRequest::for_family(corpus_tree("component/button"), "medium", 1.0).expect("family"); + request.now_milliseconds = Some(FIXED_NOW); + assert_eq!(renderer().render(request).expect("render").hit_regions, buttons.hit_regions); + + let toggle = renderer().render(RenderRequest::for_family(corpus_tree("component/toggle"), "medium", 2.0).expect("family")).expect("render"); + assert_eq!(types(&toggle), ["toggle"]); + assert_eq!(text(&toggle, 0, "handler"), Some("onToggleClick")); + assert_eq!(toggle.hit_regions[0].interaction.get("on"), Some(&json!(true))); + + // Three links, and the whole widget is a link too: it is painted first, so it lies under them. + let links = renderer().render(RenderRequest::for_family(corpus_tree("component/link"), "medium", 2.0).expect("family")).expect("render"); + assert_eq!(types(&links), ["link", "link", "link", "link"]); + assert_eq!((0..4).map(|index| text(&links, index, "url").unwrap_or("")).collect::>(), ["https://atomsprout.com/scriptwidget", "https://www.baidu.com", "https://www.google.com", "https://www.bing.com"]); + assert!(links.hit_regions[0].bounds.width() > 300.0 && links.hit_regions[0].bounds.height() > 150.0, "{:?}", links.hit_regions[0].bounds); + for hit in &links.hit_regions[1..] { + assert!(hit.bounds.intersect(&links.hit_regions[0].bounds) == Some(hit.bounds), "{:?}", hit.bounds); + } + + // A widget without controls has no tap regions. + assert!(render(RED_CARD, 100.0, 100.0, 1.0).hit_regions.is_empty()); +} + +// MARK: - Lists and theme colors (Android inputs that the shared layout still understands) + +fn list_tree(rows: usize) -> String { + let items: Vec = (1..=rows).map(|row| format!(r##"{{"$element":"toggle","props":{{"stateKey":"check.{row}","on":false}},"children":[{{"$element":"text","props":{{"color":"material.primary"}},"children":["Row {row}"]}}]}}"##)).collect(); + format!( + r##"{{"$element":"vstack","props":{{"frame":"max","alignment":"leading","background":"material.surface"}},"children":[{{"$element":"text","props":null,"children":["Header"]}},{{"$element":"list","props":{{"spacing":"2"}},"children":[{}]}}]}}"##, + items.join(",") + ) +} + +fn test_colors() -> Value { + json!({ "light": { "primary": [255, 0, 0, 1], "surface": [0, 0, 255, 1] }, "dark": { "primary": [0, 255, 0, 1], "surface": [10, 20, 30, 1] } }) +} + +/// A desktop does not scroll: the list is drawn in place and only the visible rows can be tapped. +#[test] +fn a_list_is_drawn_in_place_with_its_visible_tap_targets() { + let mut request = RenderRequest::new(list_tree(40), 338.0, 300.0, 2.0); + request.family = "large".into(); + request.colors = Some(test_colors()); + let rendered = renderer().render(request).expect("render"); + assert!((3..=15).contains(&rendered.hit_regions.len()), "{} tap regions", rendered.hit_regions.len()); + for hit in &rendered.hit_regions { + assert!(hit.bounds.top >= 0.0 && hit.bounds.bottom <= 300.5, "{:?}", hit.bounds); + } + let keys: Vec<&str> = rendered.hit_regions.iter().filter_map(|hit| hit.interaction.get("stateKey").and_then(Value::as_str)).collect(); + assert_eq!(keys[..3], ["check.1", "check.2", "check.3"]); + // The rows are part of the bitmap, in the theme color, on the theme surface. + assert!(count(&rendered.pixels, is_red) > 500); + assert_eq!(pixel(&rendered, 4, rendered.pixel_height - 4), (0, 0, 255, 255)); +} + +/// The dark bitmap takes the dark palette, and without a palette the names are unknown, as on iPhone. +#[test] +fn theme_colors_follow_the_appearance_and_are_unknown_without_a_palette() { + let mut request = RenderRequest::new(list_tree(3), 200.0, 200.0, 1.0); + request.dark_mode = true; + request.colors = Some(test_colors()); + assert_eq!(pixel(&renderer().render(request).expect("render"), 2, 198), (10, 20, 30, 255)); + // The widget keeps the system background: white in light mode, and not white in dark mode. + assert_eq!(pixel(&render(&list_tree(3), 200.0, 200.0, 1.0), 2, 198), (255, 255, 255, 255)); + let mut dark = RenderRequest::new(list_tree(3), 200.0, 200.0, 1.0); + dark.dark_mode = true; + let dark = pixel(&renderer().render(dark).expect("render"), 2, 198); + assert!(dark.3 == 255 && dark.0 < 80, "{dark:?}"); + // A malformed palette is ignored rather than failing the render. + let mut request = RenderRequest::new(list_tree(3), 200.0, 200.0, 1.0); + request.colors = Some(json!({ "light": { "surface": "red", "primary": [1, 2] } })); + assert_eq!(pixel(&renderer().render(request).expect("render"), 2, 198), (255, 255, 255, 255)); +} + +// MARK: - Dates and animation + +/// A date is drawn into the bitmap; the host renders again when it changes. +#[test] +fn a_timer_is_drawn_as_text_and_follows_the_clock_of_the_request() { + let tree = r##"{"$element":"vstack","props":{"frame":"max"},"children":[{"$element":"date","props":{"date":"now","style":"timer","color":"#ff0000"},"children":[]}]}"##; + let at = |now: i64| { + let mut request = RenderRequest::new(tree, 200.0, 100.0, 2.0); + request.now_milliseconds = Some(now); + renderer().render(request).expect("render") + }; + let first = at(FIXED_NOW); + assert!(count(&first.pixels, is_red) > 20, "the date text is drawn in red"); + // The same moment gives the same bitmap, byte for byte. + assert!(first.pixels == at(FIXED_NOW).pixels); +} + +/// Clock hands are drawn where the time of the request puts them. +#[test] +fn an_animated_clock_shows_the_time_of_the_request() { + let tree = corpus_tree("template/Animation Clock"); + let at = |now: i64| { + let mut request = RenderRequest::for_family(tree.clone(), "small", 2.0).expect("family"); + request.now_milliseconds = Some(now); + renderer().render(request).expect("render") + }; + let noon = at(FIXED_NOW); + assert!(count(&noon.pixels, |pixel| pixel[3] != 0) > 1000); + assert!(noon.pixels == at(FIXED_NOW).pixels, "the same moment draws the same clock"); + // A quarter of an hour later the minute hand has moved. + assert!(noon.pixels != at(FIXED_NOW + 15 * 60 * 1000).pixels); +} + +// MARK: - GIFs + +/// A GIF of `frames` solid frames (1 red, 2 green, 3 blue) with delays in hundredths of a second. +fn gif(width: u16, height: u16, frames: &[(u8, u16)]) -> Vec { + let mut data = Vec::new(); + data.extend_from_slice(b"GIF89a"); + data.extend_from_slice(&width.to_le_bytes()); + data.extend_from_slice(&height.to_le_bytes()); + data.extend_from_slice(&[0x91, 0, 0]); + data.extend_from_slice(&[0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255]); + for (color, delay) in frames { + data.extend_from_slice(&[0x21, 0xF9, 4, 0]); + data.extend_from_slice(&delay.to_le_bytes()); + data.extend_from_slice(&[0, 0, 0x2C, 0, 0, 0, 0]); + data.extend_from_slice(&width.to_le_bytes()); + data.extend_from_slice(&height.to_le_bytes()); + data.extend_from_slice(&[0, 2]); + // Three-bit codes; a clear code before every pixel keeps them three bits wide. + let mut bits: Vec = Vec::new(); + let mut push = |code: u8| (0..3).for_each(|bit| bits.push(code >> bit & 1 == 1)); + for _ in 0..usize::from(width) * usize::from(height) { + push(4); + push(*color); + } + push(5); + let bytes: Vec = bits.chunks(8).map(|chunk| chunk.iter().enumerate().fold(0u8, |byte, (bit, set)| byte | (u8::from(*set) << bit))).collect(); + for block in bytes.chunks(255) { + data.push(block.len() as u8); + data.extend_from_slice(block); + } + data.push(0); + } + data.push(0x3B); + data +} + +#[test] +fn a_gif_is_reported_with_its_delays_and_drawn_at_the_requested_moment() { + let tree = r##"{"$element":"vstack","props":{"frame":"max","background":"clear"},"children":[{"$element":"gif","props":{"file":"spin","frame":"80,80"},"children":[]}]}"##; + let files: Arc = Arc::new(MemoryPackageFiles::new([], [("image/spin.gif".to_owned(), gif(16, 16, &[(1, 20), (2, 30), (3, 50)]))])); + let at = |time: Option| { + let mut request = RenderRequest::for_family(tree, "small", 2.0).expect("family"); + request.files = Some(files.clone()); + request.gif_time_milliseconds = time; + renderer().render(request).expect("render") + }; + let first = at(None); + assert_eq!(first.gif_slots.len(), 1, "{:?}", first.diagnostics); + let slot = &first.gif_slots[0]; + assert_eq!((slot.source.as_str(), slot.delays.as_slice()), ("res:image/spin.gif", &[200, 300, 500][..])); + assert!(slot.bounds.width() > 40.0 && slot.bounds.height() > 40.0 && slot.bounds.left >= 0.0 && slot.bounds.right <= 170.5, "{:?}", slot.bounds); + // A single render shows the first frame; a display layer asks for the later ones by time. + let middle = |output: &RenderOutput| pixel(output, ((slot.bounds.left + slot.bounds.right) * output.scale / 2.0) as u32, ((slot.bounds.top + slot.bounds.bottom) * output.scale / 2.0) as u32); + assert_eq!(middle(&first), (255, 0, 0, 255)); + assert_eq!(middle(&at(Some(0))), (255, 0, 0, 255)); + assert_eq!(middle(&at(Some(200))), (0, 255, 0, 255)); + assert_eq!(middle(&at(Some(999))), (0, 0, 255, 255)); + assert_eq!(middle(&at(Some(1000))), (255, 0, 0, 255)); + // The frames are also available on their own, bounded, for a host that composes them itself. + let animation = sw_render::gif::decode(&gif(16, 16, &[(1, 20), (2, 30), (3, 50)])).expect("frames"); + assert_eq!((animation.width, animation.height, animation.frames.len()), (16, 16, 3)); + assert_eq!(animation.frames[2].pixels[..4], [0, 0, 255, 255]); + + // A file that is not an image leaves the widget drawn without it. + let broken: Arc = Arc::new(MemoryPackageFiles::new([], [("image/spin.gif".to_owned(), b"GIF89a broken".to_vec())])); + let mut request = RenderRequest::for_family(tree, "small", 2.0).expect("family"); + request.files = Some(broken); + let without = renderer().render(request).expect("render"); + assert!(without.gif_slots.is_empty()); +} + +// MARK: - Fonts + +/// Which CJK face Chinese text is drawn with is decided by the locale, not left to the system's fallback order. +#[test] +fn chinese_text_uses_the_face_of_the_locale() { + let own = WidgetRenderer::with_locale(scripts(), "zh-Hans").expect("render thread"); + let report = own.fonts().expect("fonts"); + assert!(report.bundled, "the bundled Inter was not loaded"); + assert_eq!(report.locale, "zh-Hans"); + // Latin text is the bundled Inter everywhere. + assert_eq!(own.families_used("Widget 123", 400).expect("families"), ["Inter"]); + println!("CJK families installed, in the order of zh-Hans: {:?}", report.cjk_families); + if report.cjk_families.is_empty() { + println!("no CJK font is installed on this machine; the face of Chinese text was not checked"); + return; + } + let chinese = "今天多云,适合出门散步"; + let simplified = own.families_used(chinese, 400).expect("families"); + assert_eq!(simplified, [report.cjk_families[0].clone()], "Chinese text is drawn with the first installed face of the locale"); + // The pan-CJK families carry the region in their name; when they are installed the choice is checked by name. + let noto = |region: &str| format!("Noto Sans CJK {region}"); + if report.cjk_families.contains(¬o("SC")) && report.cjk_families.contains(¬o("KR")) && report.cjk_families.contains(¬o("JP")) && report.cjk_families.contains(¬o("TC")) { + assert_eq!(simplified, [noto("SC")]); + // Latin, Chinese and Korean in one line: Inter, then the Chinese face, which also has the Korean letters. + assert_eq!(own.families_used("Hi 今天 한국어", 400).expect("families"), ["Inter".to_owned(), noto("SC")]); + for (locale, region) in [("en-US", "SC"), ("zh-CN", "SC"), ("zh-TW", "TC"), ("zh-Hant-HK", "HK"), ("ja-JP", "JP"), ("ko-KR", "KR"), ("de-DE", "SC")] { + own.set_locale(locale); + let report = own.fonts().expect("fonts"); + assert_eq!(report.locale, locale); + if !report.cjk_families.contains(¬o(region)) { + println!("{} is not installed; {locale} was not checked", noto(region)); + continue; + } + assert_eq!(own.families_used(chinese, 400).expect("families"), [noto(region)], "{locale}"); + assert_eq!(own.families_used(chinese, 700).expect("families"), [noto(region)], "{locale}, bold"); + } + } else { + println!("the Noto Sans CJK families are not all installed; the per-locale choice was not checked by name"); + } + // The order itself does not depend on what is installed. + assert_eq!(cjk_region_order("zh-Hans")[0], sw_render::CjkRegion::SimplifiedChinese); + assert_eq!(cjk_region_order("ko-KR")[0], sw_render::CjkRegion::Korean); +} + +/// The renderer is used from several threads at once; every call is queued to its one thread. +#[test] +fn renders_from_several_threads_are_queued() { + let expected = render(RED_CARD, 120.0, 60.0, 1.0).pixels; + std::thread::scope(|scope| { + for _ in 0..4 { + scope.spawn(|| { + for _ in 0..5 { + assert!(render(RED_CARD, 120.0, 60.0, 1.0).pixels == expected); + } + }); + } + }); +} diff --git a/Linux/crates/sw-studio/Cargo.toml b/Linux/crates/sw-studio/Cargo.toml new file mode 100644 index 00000000..0955edc9 --- /dev/null +++ b/Linux/crates/sw-studio/Cargo.toml @@ -0,0 +1,45 @@ +[package] +name = "sw-studio" +description = "The ScriptWidget management window for Linux: your widgets, the templates, the editor with a live preview, and the settings" +version.workspace = true +edition.workspace = true +license.workspace = true +publish.workspace = true + +# The window itself is behind the `ui` feature, which is off by default: +# +# cargo build --locked --release -p sw-studio --features ui +# +# GPUI needs a Linux graphical stack to build and brings several hundred +# crates. Without the feature the crate is the window's logic only (library +# listing, template copies, saving, previews, the single-instance handshake, +# the strings), so `cargo test --workspace` stays quick and works on a Mac +# and in CI. +[features] +ui = ["dep:gpui-kit", "dep:lsp-types", "dep:futures", "dep:image", "dep:smallvec"] + +[[bin]] +name = "scriptwidget-studio" +path = "src/main.rs" +required-features = ["ui"] + +[dependencies] +sw-core.workspace = true +sw-render.workspace = true +serde_json.workspace = true +libc.workspace = true +getrandom.workspace = true + +# gpui-kit (github.com/longbridge/gpui-kit, Apache-2.0) is released weekly and +# its interfaces move, so the version is exact. 0.10.0 re-exports +# gpui-component 0.10.0 and pins GPUI to the crates.io snapshot gpui-pre 0.3.12. +gpui-kit = { version = "=0.10.0", features = ["tree-sitter-javascript"], optional = true } +# The types of the editor's completion interface; the version gpui-base 0.10.0 uses. +lsp-types = { version = "0.97.0", features = ["proposed"], optional = true } +# All three are already in GPUI's tree. futures: the channel that carries +# results from worker threads to the window. image and smallvec: the types +# GPUI takes rendered pixels in, so a preview is shown without encoding it. +futures = { version = "0.3", optional = true } +image = { version = "0.25", default-features = false, optional = true } +smallvec = { version = "1", optional = true } + diff --git a/Linux/crates/sw-studio/build.rs b/Linux/crates/sw-studio/build.rs new file mode 100644 index 00000000..7fec6dc7 --- /dev/null +++ b/Linux/crates/sw-studio/build.rs @@ -0,0 +1,19 @@ +//! Builds the API reference of the code editor into the program. +//! +//! `node Linux/scripts/build-shared-js.mjs` writes it to +//! `Linux/assets/scriptwidget/api-reference.json` from the metadata the other +//! editors complete from. When that file is missing the program still +//! builds, with an editor that completes nothing, and the build says so. + +use std::path::PathBuf; + +fn main() { + let source = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR")).join("../../assets/scriptwidget/api-reference.json"); + let target = PathBuf::from(std::env::var_os("OUT_DIR").expect("OUT_DIR")).join("api-reference.json"); + println!("cargo:rerun-if-changed={}", source.display()); + let json = std::fs::read_to_string(&source).unwrap_or_else(|_| { + println!("cargo:warning={} is missing: the code editor will complete nothing. Run node Linux/scripts/build-shared-js.mjs.", source.display()); + "{}".to_owned() + }); + std::fs::write(target, json).expect("write the API reference"); +} diff --git a/Linux/crates/sw-studio/src/completion.rs b/Linux/crates/sw-studio/src/completion.rs new file mode 100644 index 00000000..cff5d758 --- /dev/null +++ b/Linux/crates/sw-studio/src/completion.rs @@ -0,0 +1,426 @@ +//! What the code editor offers while typing. The data is the metadata the +//! other editors complete from (`Editor/editorfe/src/scriptWidgetAPI.js`), +//! written as JSON by `Linux/scripts/build-shared-js.mjs` and built into the +//! program; the rules follow `Editor/editorfe/src/scriptWidgetCompletions.js`. +//! +//! Differences from the web editor: a component is found whatever the case +//! of its tag (the templates write ``, the metadata says `VStack`); +//! `$` offers the script globals; globals Linux does not implement are not +//! offered; and what is inserted is plain text, because this editor has no +//! snippet placeholders. + +use serde_json::Value; + +/// The JSON of `Linux/assets/scriptwidget/api-reference.json`, or `{}` when it was not generated (see `build.rs`). +const EMBEDDED: &str = include_str!(concat!(env!("OUT_DIR"), "/api-reference.json")); + +/// How far back from the cursor the text is looked at, as in the web editor. +const WINDOW_BYTES: usize = 1200; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Kind { + Component, + Property, + Value, + Function, + Global, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Item { + pub label: String, + /// A short note beside the label: a type, or what a function returns. + pub detail: String, + pub documentation: String, + /// The text that replaces what was typed. + pub insert: String, + pub kind: Kind, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Completions { + /// Byte offset where the replaced text starts; it ends at the cursor. + pub from: usize, + pub items: Vec, +} + +#[derive(Debug, Clone, Default)] +struct Property { + name: String, + kind: String, + documentation: String, + values: Vec, + required: bool, +} + +#[derive(Debug, Clone, Default)] +struct Component { + name: String, + documentation: String, + properties: Vec, +} + +#[derive(Debug, Clone, Default)] +struct Function { + /// "$storage.getString", or "fetch" for a function without an object. + id: String, + returns: String, + documentation: String, + arguments: Vec, +} + +#[derive(Debug, Clone, Default)] +pub struct ApiReference { + components: Vec, + global_properties: Vec, + functions: Vec, +} + +impl ApiReference { + /// The reference built into the program. + pub fn embedded() -> Self { + Self::parse(EMBEDDED) + } + + /// Reads the JSON leniently: what is missing or of another shape is left out. + pub fn parse(json: &str) -> Self { + let root: Value = serde_json::from_str(json).unwrap_or(Value::Null); + let text = |value: &Value, key: &str| value.get(key).and_then(Value::as_str).unwrap_or_default().to_owned(); + let strings = |value: &Value, key: &str| value.get(key).and_then(Value::as_array).map(|items| items.iter().filter_map(|item| item.as_str().map(str::to_owned)).collect::>()).unwrap_or_default(); + let properties = |value: Option<&Value>| { + value + .and_then(Value::as_object) + .map(|entries| { + entries + .iter() + .map(|(name, property)| Property { + name: name.clone(), + kind: text(property, "type"), + documentation: match property.get("deprecated").and_then(Value::as_str) { + Some(deprecated) => format!("Deprecated: {deprecated}"), + None => text(property, "documentation"), + }, + values: strings(property, "values"), + required: property.get("required").and_then(Value::as_bool).unwrap_or(false), + }) + .collect::>() + }) + .unwrap_or_default() + }; + let components = root + .get("components") + .and_then(Value::as_object) + .map(|entries| entries.iter().map(|(name, component)| Component { name: name.clone(), documentation: text(component, "documentation"), properties: properties(component.get("properties")) }).collect()) + .unwrap_or_default(); + let functions = root + .get("functions") + .and_then(Value::as_object) + .map(|entries| entries.iter().map(|(id, function)| Function { id: id.clone(), returns: text(function, "returns"), documentation: text(function, "documentation"), arguments: strings(function, "arguments") }).collect()) + .unwrap_or_default(); + Self { components, global_properties: properties(root.get("globalProperties")), functions } + } + + pub fn is_empty(&self) -> bool { + self.components.is_empty() && self.functions.is_empty() + } + + /// True when typing `text` should ask for completions: a name is being typed, or a tag, a member or a value was just opened. + pub fn is_trigger(text: &str) -> bool { + !text.is_empty() && text.chars().all(|character| is_word(character) || matches!(character, '$' | '<' | '.' | '"' | '\'' | ' ')) + } + + /// What to offer at byte `offset` of `text`, or `None` for nothing. + pub fn complete(&self, text: &str, offset: usize) -> Option { + let offset = floor_boundary(text, offset.min(text.len())); + let start = floor_boundary(text, offset.saturating_sub(WINDOW_BYTES)); + let before = &text[start..offset]; + + if let Some(tag) = opening_tag(before) { + // A value of a property that takes one of a fixed list. + if let Some((property, typed)) = open_value(tag.attributes) + && let Some(definition) = self.property(tag.name, property) + && !definition.values.is_empty() + { + let items = definition.values.iter().filter(|value| starts_with(value, typed)).map(|value| Item { label: value.clone(), detail: definition.kind.clone(), documentation: String::new(), insert: value.clone(), kind: Kind::Value }).collect(); + return finish(offset - typed.len(), items); + } + // The name of the tag itself. + if tag.attributes.is_empty() { + return finish(offset - tag.name.len(), self.component_items(tag.name)); + } + // A property name: a word that follows white space inside the tag, outside any quoted value. + let word = trailing_word(tag.attributes); + let preceded_by_space = tag.attributes[..tag.attributes.len() - word.len()].chars().next_back().is_some_and(char::is_whitespace); + let quotes_closed = ['"', '\''].iter().all(|quote| tag.attributes.matches(*quote).count() % 2 == 0); + if preceded_by_space && quotes_closed { + let component = self.component(tag.name)?; + let used = used_properties(tag.attributes); + let items = component + .properties + .iter() + .chain(self.global_properties.iter().filter(|global| !component.properties.iter().any(|own| own.name == global.name))) + .filter(|property| !used.contains(&property.name.as_str()) && starts_with(&property.name, word)) + .map(|property| Item { + label: property.name.clone(), + detail: if property.required { format!("{} · required", property.kind) } else { property.kind.clone() }, + documentation: property.documentation.clone(), + insert: format!("{}=", property.name), + kind: Kind::Property, + }) + .collect(); + return finish(offset - word.len(), items); + } + // Otherwise the cursor is in an expression of the tag: the rules for code apply. + } + + // `$storage.` and what follows: the functions of one global. + let word = trailing_name(before); + if let Some((object, member)) = word.split_once('.') + && object.starts_with('$') + && !member.contains('.') + { + if unsupported(object) { + return None; + } + let prefix = &word[..object.len() + 1]; + let items = self.functions.iter().filter(|function| function.id.starts_with(prefix) && starts_with(&function.id[prefix.len()..], member)).map(|function| self.function_item(function, &function.id[prefix.len()..])).collect(); + return finish(offset - member.len(), items); + } + // `$`: the globals, and the functions that have no object. + if word.starts_with('$') && !word.contains('.') { + let mut items: Vec = Vec::new(); + for function in &self.functions { + match function.id.split_once('.') { + Some((object, _)) => { + if starts_with(object, word) && !unsupported(object) && !items.iter().any(|item| item.label == object) { + let count = self.functions.iter().filter(|other| other.id.split_once('.').is_some_and(|(owner, _)| owner == object)).count(); + items.push(Item { label: object.to_owned(), detail: format!("{count} functions"), documentation: String::new(), insert: object.to_owned(), kind: Kind::Global }); + } + } + None if starts_with(&function.id, word) => items.push(self.function_item(function, &function.id)), + None => {} + } + } + return finish(offset - word.len(), items); + } + None + } + + fn component(&self, name: &str) -> Option<&Component> { + self.components.iter().find(|component| component.name.eq_ignore_ascii_case(name)) + } + + fn property(&self, component: &str, property: &str) -> Option<&Property> { + self.component(component).and_then(|component| component.properties.iter().find(|own| own.name == property)).or_else(|| self.global_properties.iter().find(|global| global.name == property)) + } + + fn component_items(&self, typed: &str) -> Vec { + // The case the user started in is kept: ` Item { + Item { label: label.to_owned(), detail: format!("({}) → {}", function.arguments.join(", "), function.returns), documentation: function.documentation.clone(), insert: label.to_owned(), kind: Kind::Function } + } +} + +fn finish(from: usize, items: Vec) -> Option { + (!items.is_empty()).then_some(Completions { from, items }) +} + +fn unsupported(global: &str) -> bool { + sw_core::library::is_unsupported_api(global.trim_start_matches('$')) +} + +fn is_word(character: char) -> bool { + character.is_alphanumeric() || character == '_' +} + +/// Without regard to ASCII case, as the web editor filters. +fn starts_with(name: &str, typed: &str) -> bool { + name.len() >= typed.len() && name.is_char_boundary(typed.len()) && name[..typed.len()].eq_ignore_ascii_case(typed) +} + +fn floor_boundary(text: &str, mut offset: usize) -> usize { + while !text.is_char_boundary(offset) { + offset -= 1; + } + offset +} + +struct OpeningTag<'a> { + name: &'a str, + /// Everything after the name up to the cursor. + attributes: &'a str, +} + +/// The tag the cursor is inside: `` after the `<`. +fn opening_tag(before: &str) -> Option> { + let open = before.rfind('<')?; + let rest = &before[open + 1..]; + if rest.contains('>') { + return None; + } + let name_length = rest.find(|character: char| !is_word(character)).unwrap_or(rest.len()); + let (name, attributes) = rest.split_at(name_length); + // `a < b`, ` Option<(&str, &str)> { + let quote = attributes.rfind(['"', '\''])?; + let typed = &attributes[quote + 1..]; + let assignment = attributes[..quote].trim_end().strip_suffix('=')?.trim_end(); + let property = trailing_word(assignment); + (!property.is_empty() && !typed.contains(char::is_whitespace)).then_some((property, typed)) +} + +fn trailing_word(text: &str) -> &str { + let start = text.char_indices().rev().take_while(|(_, character)| is_word(*character)).last().map_or(text.len(), |(index, _)| index); + &text[start..] +} + +/// The name before the cursor with its `$` and dots: `$storage.get`. +fn trailing_name(text: &str) -> &str { + let start = text.char_indices().rev().take_while(|(_, character)| is_word(*character) || matches!(character, '$' | '.')).last().map_or(text.len(), |(index, _)| index); + &text[start..] +} + +/// The property names already written in a tag. +fn used_properties(attributes: &str) -> Vec<&str> { + attributes.match_indices('=').map(|(at, _)| trailing_word(attributes[..at].trim_end())).filter(|name| !name.is_empty()).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn reference() -> ApiReference { + let reference = ApiReference::embedded(); + assert!(!reference.is_empty(), "the API reference is not built in: run node Linux/scripts/build-shared-js.mjs and build again"); + reference + } + + /// Completes at the `|` in `text`. + fn at(text: &str) -> Option<(String, Vec)> { + let offset = text.find('|').expect("a | for the cursor"); + let source = text.replace('|', ""); + reference().complete(&source, offset).map(|completions| (source[completions.from..offset].to_owned(), completions.items)) + } + + fn labels(text: &str) -> Vec { + at(text).map(|(_, items)| items.into_iter().map(|item| item.label).collect()).unwrap_or_default() + } + + #[test] + fn the_metadata_of_the_other_editors_is_built_in() { + let reference = reference(); + assert!(reference.components.len() >= 29 && reference.functions.len() >= 100 && reference.global_properties.len() > 10); + assert!(reference.component("vstack").is_some() && reference.component("VStack").is_some()); + } + + #[test] + fn a_tag_offers_components_in_the_case_it_was_started_in() { + let (replaced, items) = at("$render(>(), ["vstack"]); + assert_eq!(items[0].kind, Kind::Component); + assert!(!items[0].documentation.is_empty()); + assert_eq!(labels("= 29, "every component right after <"); + assert_eq!(labels("::new()); + } + + #[test] + fn inside_a_tag_the_properties_not_yet_written_are_offered() { + let (replaced, items) = at("hello wor|"), Vec::::new()); + assert_eq!(labels("::new()); + assert_eq!(labels("::new()); + assert_eq!(labels("::new(), "inside a quoted value"); + assert!(labels(">(), ["thin", "thick"]); + assert!(items.iter().all(|item| item.kind == Kind::Value)); + assert!(labels(" { + ticket: u64, + value: T, + /// Run without waiting for the burst to end. + immediate: bool, +} + +/// `T` is a request, `R` its result. +pub struct Debouncer { + // A Mutex only so that the type is Sync; sending never blocks. + jobs: Mutex>>, + latest: Arc, +} + +impl Debouncer { + /// `run` does the work on the worker thread; `deliver` receives the + /// ticket of the request and its result, also on the worker thread. + /// A request that was replaced before its turn is never run. A result + /// may still arrive for a request that was replaced while it ran: compare + /// its ticket with [`Debouncer::latest`]. + pub fn start(delay: Duration, run: impl Fn(T) -> R + Send + 'static, deliver: impl Fn(u64, R) + Send + 'static) -> Self { + let (jobs, queue) = mpsc::channel::>(); + std::thread::Builder::new() + .name("studio-preview".into()) + .spawn(move || { + while let Ok(mut job) = queue.recv() { + // Wait until nothing newer arrives for `delay`. + loop { + if job.immediate { + // Still take what is already waiting: only the newest matters. + match queue.try_recv() { + Ok(newer) => job = Job { immediate: true, ..newer }, + Err(_) => break, + } + } else { + match queue.recv_timeout(delay) { + Ok(newer) => job = newer, + Err(RecvTimeoutError::Timeout) => break, + Err(RecvTimeoutError::Disconnected) => return, + } + } + } + deliver(job.ticket, run(job.value)); + } + }) + .expect("the preview thread"); + Self { jobs: Mutex::new(jobs), latest: Arc::new(AtomicU64::new(0)) } + } + + /// Asks for `value` once the burst is over. Returns the ticket its result will carry. + pub fn request(&self, value: T) -> u64 { + self.send(value, false) + } + + /// Asks for `value` now: for a change that is one deliberate step, such as another size. + pub fn request_now(&self, value: T) -> u64 { + self.send(value, true) + } + + /// The ticket of the newest request. + pub fn latest(&self) -> u64 { + self.latest.load(Ordering::SeqCst) + } + + fn send(&self, value: T, immediate: bool) -> u64 { + let jobs = self.jobs.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + // Tickets are given in the order the jobs are queued. + let ticket = self.latest.fetch_add(1, Ordering::SeqCst) + 1; + let _ = jobs.send(Job { ticket, value, immediate }); + ticket + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::mpsc::channel; + use std::time::Instant; + + fn collector() -> (Debouncer, mpsc::Receiver<(u64, u32)>, Arc) { + let (results, received) = channel(); + let runs = Arc::new(AtomicU64::new(0)); + let counted = runs.clone(); + let debouncer = Debouncer::start( + Duration::from_millis(120), + move |value: u32| { + counted.fetch_add(1, Ordering::SeqCst); + value * 2 + }, + move |ticket, result| { + let _ = results.send((ticket, result)); + }, + ); + (debouncer, received, runs) + } + + #[test] + fn a_burst_runs_once_with_the_last_request() { + let (debouncer, received, runs) = collector(); + let started = Instant::now(); + let mut last = 0; + for value in 1..=8 { + last = debouncer.request(value); + std::thread::sleep(Duration::from_millis(10)); + } + let (ticket, result) = received.recv_timeout(Duration::from_secs(5)).unwrap(); + assert!(started.elapsed() >= Duration::from_millis(120 + 70), "it waited for the burst to end"); + assert_eq!((ticket, result), (last, 16)); + assert_eq!(ticket, debouncer.latest()); + assert!(received.recv_timeout(Duration::from_millis(300)).is_err(), "nothing else was run"); + assert_eq!(runs.load(Ordering::SeqCst), 1); + } + + #[test] + fn separate_requests_each_run() { + let (debouncer, received, runs) = collector(); + let first = debouncer.request(1); + assert_eq!(received.recv_timeout(Duration::from_secs(5)).unwrap(), (first, 2)); + let second = debouncer.request(5); + assert_eq!(received.recv_timeout(Duration::from_secs(5)).unwrap(), (second, 10)); + assert!(second > first); + assert_eq!(runs.load(Ordering::SeqCst), 2); + } + + #[test] + fn an_immediate_request_does_not_wait() { + let (debouncer, received, _) = collector(); + let started = Instant::now(); + let ticket = debouncer.request_now(21); + assert_eq!(received.recv_timeout(Duration::from_secs(5)).unwrap(), (ticket, 42)); + assert!(started.elapsed() < Duration::from_millis(100), "{:?}", started.elapsed()); + } + + #[test] + fn a_result_for_a_replaced_request_can_be_told_apart() { + let (results, received) = channel(); + let (entered, running) = channel(); + let debouncer = Debouncer::start( + Duration::from_millis(20), + move |value: u32| { + let _ = entered.send(()); + std::thread::sleep(Duration::from_millis(150)); + value + }, + move |ticket, result| { + let _ = results.send((ticket, result)); + }, + ); + let stale = debouncer.request(1); + running.recv_timeout(Duration::from_secs(5)).unwrap(); + let fresh = debouncer.request(2); + let first = received.recv_timeout(Duration::from_secs(5)).unwrap(); + assert_eq!(first, (stale, 1)); + assert_ne!(first.0, debouncer.latest(), "the window drops this one"); + assert_eq!(received.recv_timeout(Duration::from_secs(5)).unwrap(), (fresh, 2)); + } + + #[test] + fn the_thread_ends_with_the_debouncer() { + let (debouncer, received, _) = collector(); + debouncer.request(1); + drop(debouncer); + // The pending request is dropped with the queue and the thread returns: the channel closes. + assert!(matches!(received.recv_timeout(Duration::from_secs(5)), Err(mpsc::RecvTimeoutError::Disconnected))); + } +} diff --git a/Linux/crates/sw-studio/src/instance.rs b/Linux/crates/sw-studio/src/instance.rs new file mode 100644 index 00000000..ea08c420 --- /dev/null +++ b/Linux/crates/sw-studio/src/instance.rs @@ -0,0 +1,313 @@ +//! One management window per user. The first start takes a lock under +//! `$XDG_RUNTIME_DIR/scriptwidget/` and listens on a socket there; a later +//! start (the service's "Edit…", a `.swt` file opened from the file manager) +//! hands its request to the first and exits. +//! +//! The folder and the socket are private to the user, the listener checks +//! the user of every peer, and a request is one short line that only names a +//! page, an installed widget or a file: nothing in it is ever executed. + +use std::fs::File; +use std::io::{BufRead, BufReader, Read, Write}; +use std::os::fd::AsRawFd; +use std::os::unix::fs::{DirBuilderExt, MetadataExt, OpenOptionsExt, PermissionsExt}; +use std::os::unix::net::{UnixListener, UnixStream}; +use std::path::{Path, PathBuf}; +use std::time::{Duration, Instant}; + +use serde_json::{Value, json}; + +const LOCK_FILE: &str = "studio.lock"; +const SOCKET_FILE: &str = "studio.sock"; +/// A request is a page name, a widget name or a path. +const MAXIMUM_REQUEST_BYTES: u64 = 8 * 1024; +const MAXIMUM_NAME_CHARACTERS: usize = 255; + +/// What a start of the program asks the window to do. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Request { + /// Come to the front. + Show, + /// Open an installed widget in the editor. + Edit(String), + /// Offer to import a `.swt` file. + Open(PathBuf), +} + +impl Request { + fn to_line(&self) -> String { + let value = match self { + Request::Show => json!({ "request": "show" }), + Request::Edit(name) => json!({ "request": "edit", "package": name }), + Request::Open(path) => json!({ "request": "open", "path": path.to_string_lossy() }), + }; + format!("{value}\n") + } + + /// Reads one request line. Anything that is not exactly one of the three requests is refused. + fn from_line(line: &str) -> Option { + let value: Value = serde_json::from_str(line.trim_end_matches(['\r', '\n'])).ok()?; + let object = value.as_object()?; + let text = |key: &str| object.get(key).and_then(Value::as_str).filter(|text| !text.is_empty() && !text.chars().any(char::is_control)); + match (object.get("request")?.as_str()?, object.len()) { + ("show", 1) => Some(Request::Show), + ("edit", 2) => text("package").filter(|name| name.chars().count() <= MAXIMUM_NAME_CHARACTERS && !name.contains('/')).map(|name| Request::Edit(name.to_owned())), + ("open", 2) => text("path").map(PathBuf::from).filter(|path| path.is_absolute()).map(Request::Open), + _ => None, + } + } +} + +/// The private folder for the lock and the socket: `scriptwidget` under +/// `runtime_directory` (the value of `XDG_RUNTIME_DIR`). It is created for +/// the user alone; a folder that belongs to someone else, is a link, or is +/// open to others is refused. +pub fn private_directory(runtime_directory: &Path) -> std::io::Result { + let refused = |reason: &str| std::io::Error::new(std::io::ErrorKind::PermissionDenied, reason.to_owned()); + if !runtime_directory.is_absolute() { + return Err(refused("XDG_RUNTIME_DIR is not an absolute path")); + } + let directory = runtime_directory.join("scriptwidget"); + match std::fs::DirBuilder::new().mode(0o700).create(&directory) { + Ok(()) => {} + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {} + Err(error) => return Err(error), + } + let metadata = std::fs::symlink_metadata(&directory)?; + if !metadata.file_type().is_dir() { + return Err(refused("the runtime folder is not a folder")); + } + if metadata.uid() != current_user() { + return Err(refused("the runtime folder belongs to another user")); + } + if metadata.permissions().mode() & 0o077 != 0 { + // The service may have created it; it is ours, so close it. + std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o700))?; + } + Ok(directory) +} + +pub enum Claim { + /// This process is the window. Keep the value for as long as it runs. + Primary(Primary), + /// A window was already running and has taken the request. + Forwarded, +} + +pub struct Primary { + listener: UnixListener, + socket: PathBuf, + /// Held for the life of the process; the kernel releases it when the process ends, however it ends. + _lock: File, +} + +/// Becomes the window, or hands `request` to the one that is running. +pub fn claim(directory: &Path, request: &Request) -> std::io::Result { + let lock = std::fs::OpenOptions::new().read(true).write(true).create(true).truncate(false).mode(0o600).custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC).open(directory.join(LOCK_FILE))?; + let socket = directory.join(SOCKET_FILE); + // SAFETY: flock on a descriptor this function owns. + if unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } == 0 { + // No window holds the lock, so a socket file here is left from one that ended. + match std::fs::remove_file(&socket) { + Ok(()) => {} + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + Err(error) => return Err(error), + } + let listener = UnixListener::bind(&socket)?; + std::fs::set_permissions(&socket, std::fs::Permissions::from_mode(0o600))?; + return Ok(Claim::Primary(Primary { listener, socket, _lock: lock })); + } + let error = std::io::Error::last_os_error(); + if error.kind() != std::io::ErrorKind::WouldBlock { + return Err(error); + } + forward(&socket, request)?; + Ok(Claim::Forwarded) +} + +/// Sends `request` to the running window and waits for it to say it took it. +fn forward(socket: &Path, request: &Request) -> std::io::Result<()> { + // The window that holds the lock may still be binding its socket. + let deadline = Instant::now() + Duration::from_secs(3); + let mut stream = loop { + match UnixStream::connect(socket) { + Ok(stream) => break stream, + Err(error) if Instant::now() >= deadline => return Err(error), + Err(_) => std::thread::sleep(Duration::from_millis(50)), + } + }; + if peer_user(&stream)? != current_user() { + return Err(std::io::Error::new(std::io::ErrorKind::PermissionDenied, "the window belongs to another user")); + } + stream.set_read_timeout(Some(Duration::from_secs(5)))?; + stream.set_write_timeout(Some(Duration::from_secs(5)))?; + stream.write_all(request.to_line().as_bytes())?; + let mut answer = String::new(); + BufReader::new(stream.take(16)).read_line(&mut answer)?; + if answer.trim_end() == "ok" { Ok(()) } else { Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "the running window refused the request")) } +} + +impl Primary { + /// Receives requests on a thread of its own for as long as the process + /// runs; `handle` is called on that thread. + pub fn listen(self, handle: impl Fn(Request) + Send + 'static) { + std::thread::Builder::new() + .name("studio-instance".into()) + .spawn(move || { + // The whole value moves here: the lock is held for as long as the thread, which is as long as the process. + let primary = self; + for stream in primary.listener.incoming() { + let Ok(stream) = stream else { continue }; + if let Some(request) = receive(stream) { + handle(request); + } + } + }) + .expect("the instance thread"); + } + + pub fn socket(&self) -> &Path { + &self.socket + } +} + +fn receive(stream: UnixStream) -> Option { + if peer_user(&stream).ok()? != current_user() { + return None; + } + stream.set_read_timeout(Some(Duration::from_secs(2))).ok()?; + stream.set_write_timeout(Some(Duration::from_secs(2))).ok()?; + let mut line = String::new(); + BufReader::new((&stream).take(MAXIMUM_REQUEST_BYTES)).read_line(&mut line).ok()?; + let request = line.ends_with('\n').then(|| Request::from_line(&line)).flatten(); + let _ = (&stream).write_all(if request.is_some() { b"ok\n" } else { b"refused\n" }); + request +} + +fn current_user() -> u32 { + // SAFETY: geteuid has no arguments and cannot fail. + unsafe { libc::geteuid() } +} + +/// The user at the other end of a connection, as the kernel recorded it. +#[cfg(target_os = "linux")] +fn peer_user(stream: &UnixStream) -> std::io::Result { + let mut credentials = libc::ucred { pid: 0, uid: u32::MAX, gid: u32::MAX }; + let mut length = std::mem::size_of::() as libc::socklen_t; + // SAFETY: the pointer and the length describe `credentials`, which outlives the call. + let result = unsafe { libc::getsockopt(stream.as_raw_fd(), libc::SOL_SOCKET, libc::SO_PEERCRED, (&raw mut credentials).cast(), &raw mut length) }; + if result == 0 && length as usize == std::mem::size_of::() { Ok(credentials.uid) } else { Err(std::io::Error::last_os_error()) } +} + +/// The same on the systems the tests also run on. +#[cfg(not(target_os = "linux"))] +fn peer_user(stream: &UnixStream) -> std::io::Result { + let (mut user, mut group) = (u32::MAX, u32::MAX); + // SAFETY: both pointers are to locals that outlive the call. + if unsafe { libc::getpeereid(stream.as_raw_fd(), &raw mut user, &raw mut group) } == 0 { Ok(user) } else { Err(std::io::Error::last_os_error()) } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::testing::Scratch; + use std::sync::mpsc::channel; + + #[test] + fn requests_survive_the_line_format_and_nothing_else_is_accepted() { + for request in [Request::Show, Request::Edit("My Widget 小组件".into()), Request::Edit("--help".into()), Request::Open("/home/u/Down loads/a \"b\".swt".into())] { + let line = request.to_line(); + assert!(line.ends_with('\n') && line.matches('\n').count() == 1, "{line:?}"); + assert_eq!(Request::from_line(&line), Some(request)); + } + for line in [ + "", + "show", + "[]", + r#"{"request":"quit"}"#, + r#"{"request":"show","extra":1}"#, + r#"{"request":"edit"}"#, + r#"{"request":"edit","package":""}"#, + r#"{"request":"edit","package":"a/b"}"#, + r#"{"request":"edit","package":"a\nb"}"#, + r#"{"request":"edit","package":7}"#, + r#"{"request":"open","path":"relative.swt"}"#, + r#"{"request":"open","path":"/a","run":"sh"}"#, + r#"{"request":"edit","path":"/a"}"#, + ] { + assert_eq!(Request::from_line(line), None, "{line}"); + } + assert_eq!(Request::from_line(&format!(r#"{{"request":"edit","package":"{}"}}"#, "a".repeat(256))), None); + } + + #[test] + fn the_private_folder_is_closed_to_others() { + let scratch = Scratch::short("folder"); + let directory = private_directory(scratch.root()).unwrap(); + assert_eq!(directory, scratch.path("scriptwidget")); + assert_eq!(std::fs::metadata(&directory).unwrap().permissions().mode() & 0o777, 0o700); + // An existing folder that is open to others is closed. + std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o755)).unwrap(); + private_directory(scratch.root()).unwrap(); + assert_eq!(std::fs::metadata(&directory).unwrap().permissions().mode() & 0o777, 0o700); + // A link in its place is refused. + std::fs::remove_dir(&directory).unwrap(); + std::fs::create_dir(scratch.path("elsewhere")).unwrap(); + std::os::unix::fs::symlink(scratch.path("elsewhere"), &directory).unwrap(); + assert!(private_directory(scratch.root()).is_err()); + assert!(private_directory(Path::new("relative")).is_err()); + } + + #[test] + fn a_second_start_hands_its_request_to_the_first() { + let scratch = Scratch::short("one"); + let directory = private_directory(scratch.root()).unwrap(); + let Claim::Primary(primary) = claim(&directory, &Request::Show).unwrap() else { panic!("the first start is the window") }; + assert_eq!(std::fs::metadata(primary.socket()).unwrap().permissions().mode() & 0o777, 0o600); + let (requests, received) = channel(); + primary.listen(move |request| { + let _ = requests.send(request); + }); + + for request in [Request::Edit("Clock".into()), Request::Open("/tmp/a.swt".into()), Request::Show] { + assert!(matches!(claim(&directory, &request).unwrap(), Claim::Forwarded)); + assert_eq!(received.recv_timeout(Duration::from_secs(5)).unwrap(), request); + } + + // What is not a request is answered with a refusal and never reaches the window. + let mut stream = UnixStream::connect(directory.join(SOCKET_FILE)).unwrap(); + stream.write_all(b"{\"request\":\"edit\",\"package\":\"../x\"}\n").unwrap(); + let mut answer = String::new(); + stream.read_to_string(&mut answer).unwrap(); + assert_eq!(answer, "refused\n"); + // A line without an end, and one that is too long, are refused too. + let mut stream = UnixStream::connect(directory.join(SOCKET_FILE)).unwrap(); + // The window stops reading at its limit, so the rest of the write may fail. + let _ = stream.write_all(&vec![b'a'; 20_000]); + let _ = stream.shutdown(std::net::Shutdown::Write); + let mut answer = String::new(); + let _ = stream.read_to_string(&mut answer); + assert_ne!(answer, "ok\n"); + assert!(received.recv_timeout(Duration::from_millis(200)).is_err()); + } + + #[test] + fn a_socket_left_by_a_window_that_ended_is_replaced() { + let scratch = Scratch::short("stale"); + let directory = private_directory(scratch.root()).unwrap(); + // A window that ended without cleaning up: the socket file is there, nobody holds the lock. + drop(UnixListener::bind(directory.join(SOCKET_FILE)).unwrap()); + std::fs::write(directory.join(LOCK_FILE), "").unwrap(); + assert!(matches!(claim(&directory, &Request::Show).unwrap(), Claim::Primary(_))); + } + + #[test] + fn the_lock_is_free_again_when_the_window_is_gone() { + let scratch = Scratch::short("again"); + let directory = private_directory(scratch.root()).unwrap(); + let first = claim(&directory, &Request::Show).unwrap(); + assert!(matches!(first, Claim::Primary(_))); + drop(first); + assert!(matches!(claim(&directory, &Request::Show).unwrap(), Claim::Primary(_))); + } +} diff --git a/Linux/crates/sw-studio/src/launch.rs b/Linux/crates/sw-studio/src/launch.rs new file mode 100644 index 00000000..1baeb357 --- /dev/null +++ b/Linux/crates/sw-studio/src/launch.rs @@ -0,0 +1,129 @@ +//! The command line of `scriptwidget-studio`. + +use std::path::PathBuf; + +use crate::instance::Request; + +pub const USAGE: &str = "\ +usage: scriptwidget-studio [options] [file.swt] + + file.swt offer to import this widget + --edit open one of your widgets in the editor + --data use this data folder instead of the user's + --version print the version + --help print this text + +For development: + --page start on library, templates, settings, template: or editor: + --no-single-instance always open a window of its own +"; + +/// The pages `--page` can name. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum StartPage { + Library, + Templates, + Settings, + Template(String), + Editor(String), +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Launch { + pub request: Request, + pub data: Option, + pub page: Option, + pub single_instance: bool, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Parsed { + Run(Launch), + Help, + Version, +} + +/// Reads the arguments after the program name. `current` is the folder a relative `.swt` path is taken from. +pub fn parse(arguments: &[String], current: Option<&std::path::Path>) -> Result { + let mut launch = Launch { request: Request::Show, data: None, page: None, single_instance: true }; + let mut chosen = false; + let mut choose = |launch: &mut Launch, request: Request| { + if std::mem::replace(&mut chosen, true) { + return Err("Give one widget to edit or one file to import, not several.".to_owned()); + } + launch.request = request; + Ok(()) + }; + let absolute = |text: &str| { + let path = PathBuf::from(text); + if path.is_absolute() { Ok(path) } else { current.map(|folder| folder.join(&path)).ok_or_else(|| format!("{text} is not an absolute path.")) } + }; + let mut rest = arguments.iter(); + let mut options = true; + while let Some(argument) = rest.next() { + let mut value = |name: &str| rest.next().filter(|value| !value.is_empty()).cloned().ok_or_else(|| format!("{name} needs a value.")); + match argument.as_str() { + "--help" | "-h" if options => return Ok(Parsed::Help), + "--version" if options => return Ok(Parsed::Version), + "--edit" if options => { + let name = value("--edit")?; + choose(&mut launch, Request::Edit(name))?; + } + "--data" if options => launch.data = Some(absolute(&value("--data")?)?), + "--page" if options => { + let page = value("--page")?; + launch.page = Some(match (page.as_str(), page.split_once(':')) { + ("library", _) => StartPage::Library, + ("templates", _) => StartPage::Templates, + ("settings", _) => StartPage::Settings, + (_, Some(("template", reference))) if !reference.is_empty() => StartPage::Template(reference.to_owned()), + (_, Some(("editor", name))) if !name.is_empty() => StartPage::Editor(name.to_owned()), + _ => return Err(format!("Unknown page {page}.")), + }); + } + "--no-single-instance" if options => launch.single_instance = false, + "--" if options => options = false, + option if options && option.starts_with('-') => return Err(format!("Unknown option {option}.")), + // A file manager may pass a file:// address for %U; this program asks for %f, a path. + file => choose(&mut launch, Request::Open(absolute(file)?))?, + } + } + Ok(Parsed::Run(launch)) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::path::Path; + + fn run(arguments: &[&str]) -> Result { + let arguments: Vec = arguments.iter().map(|argument| argument.to_string()).collect(); + match parse(&arguments, Some(Path::new("/home/u")))? { + Parsed::Run(launch) => Ok(launch), + other => Err(format!("{other:?}")), + } + } + + #[test] + fn the_ways_to_start() { + assert_eq!(run(&[]).unwrap(), Launch { request: Request::Show, data: None, page: None, single_instance: true }); + assert_eq!(run(&["--edit", "My Widget"]).unwrap().request, Request::Edit("My Widget".into())); + assert_eq!(run(&["/tmp/a b.swt"]).unwrap().request, Request::Open("/tmp/a b.swt".into())); + assert_eq!(run(&["Downloads/clock.swt"]).unwrap().request, Request::Open("/home/u/Downloads/clock.swt".into()), "a relative path is made absolute before it is handed on"); + assert_eq!(run(&["--", "-odd.swt"]).unwrap().request, Request::Open("/home/u/-odd.swt".into())); + let launch = run(&["--data", "/tmp/data", "--page", "template:template/Clock", "--no-single-instance"]).unwrap(); + assert_eq!((launch.data, launch.page, launch.single_instance), (Some("/tmp/data".into()), Some(StartPage::Template("template/Clock".into())), false)); + assert_eq!(run(&["--page", "editor:Clock"]).unwrap().page, Some(StartPage::Editor("Clock".into()))); + assert_eq!(run(&["--page", "settings"]).unwrap().page, Some(StartPage::Settings)); + } + + #[test] + fn mistakes_are_refused_with_a_reason() { + for arguments in [&["--edit"][..], &["--edit", ""], &["--edit", "A", "--edit", "B"], &["--edit", "A", "/tmp/b.swt"], &["--page", "nowhere"], &["--page", "editor:"], &["--frobnicate"], &["--data"]] { + assert!(run(arguments).is_err(), "{arguments:?}"); + } + assert_eq!(parse(&["--help".to_owned()], None), Ok(Parsed::Help)); + assert_eq!(parse(&["--version".to_owned()], None), Ok(Parsed::Version)); + assert!(parse(&["relative.swt".to_owned()], None).is_err()); + } +} diff --git a/Linux/crates/sw-studio/src/lib.rs b/Linux/crates/sw-studio/src/lib.rs new file mode 100644 index 00000000..e2c46549 --- /dev/null +++ b/Linux/crates/sw-studio/src/lib.rs @@ -0,0 +1,27 @@ +//! The ScriptWidget management window for Linux (`scriptwidget-studio`): +//! your widgets, the bundled templates, the code editor with a live preview, +//! and the settings. The Linux counterpart of `Windows/src/ScriptWidget.Studio`. +//! +//! The modules here are the window's logic and need no window; the window +//! itself ([`ui`], on GPUI and gpui-kit) is behind the `ui` feature. +//! +//! Package content is untrusted. The window shows it as text and as the +//! picture `sw-render` draws; it compiles and runs it only inside the engine +//! of `sw-core`, with the limits every run has. + +pub mod completion; +pub mod debounce; +pub mod instance; +pub mod launch; +pub mod model; +pub mod service; +pub mod settings; +pub mod strings; + +#[cfg(test)] +mod testing; + +#[cfg(feature = "ui")] +pub mod ui; + +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/Linux/crates/sw-studio/src/main.rs b/Linux/crates/sw-studio/src/main.rs new file mode 100644 index 00000000..ff96fa8d --- /dev/null +++ b/Linux/crates/sw-studio/src/main.rs @@ -0,0 +1,58 @@ +//! `scriptwidget-studio`: the ScriptWidget management window. + +use std::process::ExitCode; +use std::sync::Arc; + +use futures::channel::mpsc::unbounded; +use sw_studio::instance::{self, Claim, Request}; +use sw_studio::launch::{self, Parsed}; +use sw_studio::model::Studio; +use sw_studio::strings::{self, tf}; + +fn main() -> ExitCode { + strings::set_language(strings::detect(|name| std::env::var(name).ok())); + let arguments: Vec = std::env::args().skip(1).collect(); + let launch = match launch::parse(&arguments, std::env::current_dir().ok().as_deref()) { + Ok(Parsed::Run(launch)) => launch, + Ok(Parsed::Help) => { + print!("{}", launch::USAGE); + return ExitCode::SUCCESS; + } + Ok(Parsed::Version) => { + println!("scriptwidget-studio {}", sw_studio::VERSION); + return ExitCode::SUCCESS; + } + Err(problem) => { + eprintln!("error: {problem}\n\n{}", launch::USAGE); + return ExitCode::from(2); + } + }; + + // One window per user: a second start hands its request to the first and ends. + let (requests, received) = unbounded::(); + if launch.single_instance { + let claimed = std::env::var_os("XDG_RUNTIME_DIR") + .filter(|value| !value.is_empty()) + .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "XDG_RUNTIME_DIR is not set")) + .and_then(|runtime| instance::private_directory(std::path::Path::new(&runtime))) + .and_then(|directory| instance::claim(&directory, &launch.request)); + match claimed { + Ok(Claim::Forwarded) => return ExitCode::SUCCESS, + Ok(Claim::Primary(primary)) => primary.listen(move |request| { + let _ = requests.unbounded_send(request); + }), + // Without a runtime folder there is no handshake; the window still opens. + Err(error) => eprintln!("warning: single-instance check skipped: {error}"), + } + } + + let studio = match Studio::open(launch.data.as_deref()) { + Ok(studio) => Arc::new(studio), + Err(error) => { + eprintln!("{}", tf("startup.failed", &[&error.0])); + return ExitCode::from(2); + } + }; + sw_studio::ui::run(studio, launch.request, launch.page, received); + ExitCode::SUCCESS +} diff --git a/Linux/crates/sw-studio/src/model.rs b/Linux/crates/sw-studio/src/model.rs new file mode 100644 index 00000000..42b5069e --- /dev/null +++ b/Linux/crates/sw-studio/src/model.rs @@ -0,0 +1,717 @@ +//! What the management window knows and does, without the window: the +//! installed widgets, the bundled templates, copies of them, files of a +//! package, archives, and previews. Everything goes through the hardened +//! package store and the runtime of `sw-core`; nothing here interprets +//! package content itself. + +use std::collections::HashMap; +use std::fmt; +use std::io::{Read, Write}; +use std::os::unix::fs::OpenOptionsExt; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex}; + +use sw_core::compiler::is_text_file; +use sw_core::json::ObjectExt; +use sw_core::packages::limits::MAX_FILE_BYTES; +use sw_core::packages::{ArchiveLimits, DiskPackageFiles, PackageError, archive, manifest_present, manifests}; +use sw_core::{BundledPackage, DataPaths, InstalledPackage, Library, MemoryPackageFiles, PackageFiles, Resources, RunRequest, SharedScripts, WidgetRuntime}; +use sw_render::{Presentation, WidgetRenderer}; + +/// What a new, empty widget starts with. +pub const STARTER: &str = r##"// A ScriptWidget widget. The preview on the right follows what you type. + +const size = $getenv("widget-size"); + +$render( + + HELLO + ScriptWidget + {size} + +); +"##; + +/// A failure with a sentence for the user. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct StudioError(pub String); + +impl fmt::Display for StudioError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(&self.0) + } +} + +impl std::error::Error for StudioError {} + +impl From for StudioError { + fn from(error: PackageError) -> Self { + Self(error.message) + } +} + +impl From for StudioError { + fn from(error: std::io::Error) -> Self { + Self(error.to_string()) + } +} + +/// One installed widget, as a card shows it. +#[derive(Debug, Clone, PartialEq)] +pub struct WidgetCard { + /// The folder name: what every command takes, and what the card shows, because it is unique + /// (an imported copy keeps the name its manifest gives and gets a numbered folder). + pub name: String, + /// The name the manifest gives. + pub title: String, + pub description: String, + pub families: Vec<&'static str>, + /// False when `widget.json` is present but cannot be read: such a package is listed and can be removed, nothing else. + pub readable: bool, + pub installation: String, +} + +impl WidgetCard { + /// The size a card shows: medium when the widget has it, otherwise its first size. + pub fn card_family(&self) -> &'static str { + if self.families.contains(&"medium") { "medium" } else { self.families.first().copied().unwrap_or("medium") } + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TemplateCard { + /// "group/name", as [`Library::find_bundled`] takes it. + pub reference: String, + pub name: String, + pub network: bool, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TemplateGroup { + /// "template", "component" or "api". + pub group: String, + pub templates: Vec, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TemplateDetail { + pub reference: String, + pub name: String, + pub description: String, + /// The text of its `main.jsx`. + pub source: String, + /// The Internet hosts a copy would be allowed to ask. + pub hosts: Vec, +} + +/// What a `.swt` file holds, read without installing or running anything. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ArchiveSummary { + pub name: String, + pub description: String, + pub files: usize, + pub bytes: u64, + pub permissions: Vec, + pub hosts: Vec, +} + +/// What to draw. +#[derive(Debug, Clone, PartialEq)] +pub enum PreviewSource { + /// An installed widget by name; `edits` replace the text of files that are not saved yet. + Installed { name: String, edits: Vec<(String, String)> }, + /// A bundled template by "group/name". + Bundled(String), +} + +#[derive(Debug, Clone, PartialEq)] +pub struct PreviewRequest { + pub source: PreviewSource, + pub family: String, + pub dark: bool, + /// Pixels per point of the display the window is on. + pub scale: f32, +} + +/// A drawn widget: straight (not premultiplied) RGBA, rows from the top. +#[derive(Debug, Clone, PartialEq)] +pub struct Picture { + pub pixels: Vec, + pub pixel_width: u32, + pub pixel_height: u32, + /// The size in points. + pub width: f32, + pub height: f32, +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct PreviewResult { + /// The widget, or the card that explains its error. `None` only when nothing at all could be drawn. + pub picture: Option, + /// Category and message of a script, compile or drawing error. + pub error: Option<(String, String)>, + /// Level and message of what the script printed. + pub logs: Vec<(String, String)>, + pub elapsed_milliseconds: u64, +} + +pub struct Studio { + paths: DataPaths, + library: Library, + runtime: WidgetRuntime, + renderer: WidgetRenderer, + /// One run at a time: previews are asked for from several threads. + engine: Mutex<()>, +} + +impl Studio { + /// The installation's runtime files and templates, and the user's data folder (`data`, or the environment's). + pub fn open(data: Option<&Path>) -> Result { + let resources = Resources::locate()?; + Self::with(&DataPaths::new(data)?, &resources.runtime, resources.templates) + } + + pub fn with(paths: &DataPaths, runtime_files: &Path, templates: Option) -> Result { + let scripts = Arc::new(SharedScripts::new(runtime_files)?); + Ok(Self { + paths: paths.clone(), + library: Library::new(paths, templates), + runtime: WidgetRuntime::new(scripts.clone(), paths)?, + renderer: WidgetRenderer::new(scripts)?, + engine: Mutex::new(()), + }) + } + + pub fn paths(&self) -> &DataPaths { + &self.paths + } + + // My widgets + + pub fn widgets(&self) -> Result, StudioError> { + Ok(self.library.list()?.iter().map(card).collect()) + } + + pub fn widget(&self, name: &str) -> Option { + self.library.find(name).as_ref().map(card) + } + + /// Creates an empty widget; a taken name gets a numbered suffix. Returns the name it got. + pub fn new_widget(&self, name: &str) -> Result { + Ok(self.library.create(name.trim(), STARTER, None)?.name) + } + + pub fn remove(&self, name: &str) -> Result<(), StudioError> { + Ok(self.library.delete(name)?) + } + + /// Writes the widget as a `.swt` file. The archive is written next to + /// `target` under a private temporary name and renamed over it, so a + /// crash or a full disk never leaves half a file under the chosen name. + pub fn export(&self, name: &str, target: &Path) -> Result<(), StudioError> { + let mut archive = Vec::new(); + self.library.export_archive(name, &mut archive)?; + write_atomically(target, &archive, 0o644)?; + Ok(()) + } + + /// Reads a `.swt` file the way an import would, as far as the checks of + /// the archive and its files, and says what it holds. Nothing is + /// installed, compiled or run, and nothing stays on disk. + pub fn inspect(&self, file: &Path) -> Result { + let limits = ArchiveLimits::default(); + let mut input = open_archive(file)?; + let extracted = archive::extract(&mut input, &self.paths.package_staging(), &limits)?; + let directory = extracted.directory(); + let files = DiskPackageFiles::new(directory); + let manifest = if manifest_present(directory)? { + let text = files.read_text(manifests::FILE_NAME); + Some(manifests::parse(text.as_deref()).ok_or_else(|| StudioError("widget.json is invalid.".into()))?) + } else { + if !files.exists(manifests::LEGACY_ENTRY) { + return Err(StudioError("Entry file main.jsx does not exist.".into())); + } + None + }; + let strings = |key: &str| manifest.as_ref().and_then(|manifest| manifest.opt_array(key)).map(|items| items.iter().filter_map(|item| item.as_str().map(str::to_owned)).collect()).unwrap_or_default(); + let name = manifest + .as_ref() + .and_then(|manifest| manifests::string_value(manifest, "name")) + .or_else(|| extracted.root_name().map(str::to_owned)) + .or_else(|| file.file_stem().and_then(|stem| stem.to_str()).map(str::to_owned)) + .unwrap_or_else(|| "Imported Widget".into()); + let paths = files.list()?; + let bytes = paths.iter().filter_map(|path| files.read_bytes_up_to(path, MAX_FILE_BYTES)).map(|content| content.len() as u64).sum(); + Ok(ArchiveSummary { + name, + description: manifest.as_ref().map(|manifest| manifest.opt_string("description")).unwrap_or_default(), + files: paths.len(), + bytes, + permissions: strings("permissions"), + hosts: strings("networkDomains"), + }) + } + + /// Installs a `.swt` file. Its manifest and scripts are compiled first; a package that does not compile is refused. + pub fn import(&self, file: &Path) -> Result { + let mut input = open_archive(file)?; + let suggested = file.file_stem().and_then(|stem| stem.to_str()).map(str::to_owned); + let validate = |files: &dyn PackageFiles| self.runtime.validate_package(files).map_err(|error| PackageError::new(sw_core::packages::codes::INVALID_MANIFEST, error.message)); + Ok(self.library.import_archive(&mut input, suggested.as_deref(), Some(&validate))?.name) + } + + // Templates + + /// The bundled templates that run on Linux, in the groups the library reports. + pub fn template_groups(&self) -> Vec { + let mut groups: Vec = Vec::new(); + for bundled in self.library.bundled() { + let card = TemplateCard { reference: format!("{}/{}", bundled.group, bundled.name), name: bundled.name, network: bundled.network }; + match groups.last_mut().filter(|group| group.group == bundled.group) { + Some(group) => group.templates.push(card), + None => groups.push(TemplateGroup { group: bundled.group, templates: vec![card] }), + } + } + groups + } + + pub fn template(&self, reference: &str) -> Option { + let bundled = self.library.find_bundled(reference)?; + let files = self.library.bundled_files(&bundled); + let (source, manifest) = Library::bundled_sources(&bundled.name, &files).ok()?; + let manifest = manifests::parse(Some(&manifest))?; + let hosts = manifest.opt_array("networkDomains").map(|items| items.iter().filter_map(|item| item.as_str().map(str::to_owned)).collect()).unwrap_or_default(); + Some(TemplateDetail { reference: reference.to_owned(), name: bundled.name, description: manifest.opt_string("description"), source, hosts }) + } + + /// Copies a template into the user's widgets; a taken name gets a numbered suffix. Returns the name of the copy. + pub fn use_template(&self, reference: &str) -> Result { + let bundled = self.library.find_bundled(reference).ok_or_else(|| StudioError(format!("There is no template {reference}.")))?; + Ok(self.library.install_bundled(&bundled)?.name) + } + + // Files of a widget + + /// The script the widget starts from. + pub fn entry(&self, name: &str) -> String { + self.library.find(name).and_then(|package| package.manifest.as_ref().and_then(|manifest| manifests::string_value(manifest, "entry"))).unwrap_or_else(|| manifests::LEGACY_ENTRY.to_owned()) + } + + /// Every file of the widget, the entry first. + pub fn files(&self, name: &str) -> Result, StudioError> { + let package = self.installed(name)?; + let entry = self.entry(name); + let mut paths = package.files().list()?; + paths.sort_by_key(|path| (*path != entry, path.to_lowercase())); + Ok(paths) + } + + /// The text of a file, or `None` for a file that is not text. + pub fn read_text(&self, name: &str, path: &str) -> Result, StudioError> { + if !is_text_file(path) { + return Ok(None); + } + let bytes = self.library.store().read_file(name, path)?; + Ok(String::from_utf8(bytes).ok()) + } + + /// Saves a file through the package store: written beside the old file and renamed over it. + pub fn save_text(&self, name: &str, path: &str, text: &str) -> Result<(), StudioError> { + Ok(self.library.write_file(name, path, text.as_bytes())?) + } + + fn installed(&self, name: &str) -> Result { + self.library.find(name).ok_or_else(|| StudioError(format!("There is no widget named {name}."))) + } + + // Previews + + /// Compiles, runs and draws. The package runs inside the engine with the + /// limits every run has; an error becomes the picture of its error card. + /// Blocks for as long as the script runs: call it off the window's thread. + pub fn preview(&self, request: &PreviewRequest) -> PreviewResult { + let failed = |message: String| PreviewResult { error: Some(("internalError".into(), message)), ..PreviewResult::default() }; + let (files, package_id): (Arc, String) = match &request.source { + PreviewSource::Installed { name, edits } => { + let Some(package) = self.library.find(name) else { return failed(format!("There is no widget named {name}.")) }; + let files = Overlay { base: package.files(), edits: edits.iter().cloned().collect() }; + (Arc::new(files), package.installation) + } + PreviewSource::Bundled(reference) => { + let Some(bundled) = self.library.find_bundled(reference) else { return failed(format!("There is no template {reference}.")) }; + match self.bundled_view(&bundled) { + Ok(files) => (Arc::new(files), format!("studio:{reference}")), + Err(error) => return failed(error.0), + } + } + }; + let Some(presentation) = Presentation::for_family(&request.family, request.scale.clamp(0.5, 4.0)) else { return failed(format!("Unknown size {}", request.family)) }; + let mut run = RunRequest::new(files, package_id); + run.family = request.family.clone(); + run.dark_mode = request.dark; + + let _one_at_a_time = self.engine.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + let result = self.runtime.run(&run); + let mut error = result.execution.error.as_ref().map(|error| (error.kind.clone(), error.message.clone())); + let picture = match sw_render::render_run(&self.renderer, &self.runtime, &run, &result, &presentation) { + Ok(rendered) => { + if let (None, Some(failure)) = (&error, &rendered.failure) { + error = Some(("internalError".into(), failure.message.clone())); + } + let output = rendered.output; + Some(Picture { pixels: output.straight_pixels(), pixel_width: output.pixel_width, pixel_height: output.pixel_height, width: presentation.width, height: presentation.height }) + } + Err(failure) => { + error.get_or_insert(("internalError".into(), failure.message)); + None + } + }; + PreviewResult { + picture, + error, + logs: result.execution.logs.iter().map(|entry| (entry.level.clone(), entry.message.clone())).collect(), + elapsed_milliseconds: result.execution.elapsed.as_millis() as u64, + } + } + + /// A template as a copy of it would be: with the manifest the library gives a copy, so that its network requests are allowed in the preview. + fn bundled_view(&self, bundled: &BundledPackage) -> Result { + let mut files = self.library.bundled_files(bundled); + let (_, manifest) = Library::bundled_sources(&bundled.name, &files)?; + files.insert(manifests::FILE_NAME.to_owned(), manifest.into_bytes()); + let (mut text, mut binary) = (Vec::new(), Vec::new()); + for (path, bytes) in files { + match String::from_utf8(bytes) { + Ok(content) if is_text_file(&path) => text.push((path, content)), + Ok(content) => binary.push((path, content.into_bytes())), + Err(error) => binary.push((path, error.into_bytes())), + } + } + Ok(MemoryPackageFiles::new(text, binary)) + } +} + +fn card(package: &InstalledPackage) -> WidgetCard { + WidgetCard { + name: package.name.clone(), + title: package.display_name(), + description: package.description(), + families: package.families(), + readable: !package.manifest_present || package.manifest.is_some(), + installation: package.installation.clone(), + } +} + +/// The files of an installed package with the text of unsaved files laid over them. +struct Overlay { + base: DiskPackageFiles, + edits: HashMap, +} + +impl PackageFiles for Overlay { + fn paths(&self) -> Vec { + let mut paths = self.base.paths(); + paths.extend(self.edits.keys().filter(|path| !self.base.exists(path)).cloned()); + paths.sort_unstable(); + paths + } + + fn exists(&self, path: &str) -> bool { + self.edits.contains_key(path) || self.base.exists(path) + } + + fn read_text(&self, path: &str) -> Option { + self.edits.get(path).cloned().or_else(|| self.base.read_text(path)) + } + + fn read_bytes(&self, path: &str) -> Option> { + self.edits.get(path).map(|text| text.clone().into_bytes()).or_else(|| self.base.read_bytes(path)) + } +} + +/// A `.swt` file the user named: a regular file, never a device or a pipe, read up to the archive limit by the store. +fn open_archive(file: &Path) -> Result { + let opened = std::fs::OpenOptions::new().read(true).custom_flags(libc::O_CLOEXEC | libc::O_NONBLOCK).open(file).map_err(|error| StudioError(format!("{}: {error}", file.display())))?; + if !opened.metadata()?.is_file() { + return Err(StudioError(format!("{} is not a file.", file.display()))); + } + Ok(opened) +} + +/// Writes `bytes` under a private temporary name in the folder of `target` and renames it over `target`. +pub fn write_atomically(target: &Path, bytes: &[u8], mode: u32) -> std::io::Result<()> { + let folder = target.parent().filter(|parent| !parent.as_os_str().is_empty()).unwrap_or(Path::new(".")); + let name = target.file_name().ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "no file name"))?; + let mut random = [0u8; 8]; + getrandom::fill(&mut random).map_err(|error| std::io::Error::other(error.to_string()))?; + let suffix: String = random.iter().map(|byte| format!("{byte:02x}")).collect(); + let temporary = folder.join(format!(".{}.{suffix}.tmp", name.to_string_lossy())); + let written = (|| { + // Created new, never through a link, private until it is complete. + let mut file = std::fs::OpenOptions::new().write(true).create_new(true).mode(0o600).custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC).open(&temporary)?; + file.write_all(bytes)?; + file.sync_all()?; + std::fs::set_permissions(&temporary, std::os::unix::fs::PermissionsExt::from_mode(mode))?; + std::fs::rename(&temporary, target) + })(); + if written.is_err() { + let _ = std::fs::remove_file(&temporary); + } + written +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::testing::{Scratch, studio}; + + #[test] + fn the_engine_is_shared_between_threads() { + fn shared() {} + shared::(); + } + + #[test] + fn an_empty_library_lists_nothing_and_templates_come_in_groups() { + let scratch = Scratch::new("groups"); + let studio = studio(&scratch); + assert_eq!(studio.widgets().unwrap(), []); + let groups = studio.template_groups(); + assert_eq!(groups.iter().map(|group| group.group.as_str()).collect::>(), ["template", "api"], "the group with nothing Linux can run is not shown"); + assert_eq!(groups[0].templates.iter().map(|card| (card.reference.as_str(), card.network)).collect::>(), [("template/Clock", false), ("template/Quote", true)]); + assert!(groups.iter().all(|group| group.templates.iter().all(|card| card.name != "steps")), "a template that needs $health is not offered"); + } + + #[test] + fn a_template_shows_its_code_and_the_hosts_a_copy_may_ask() { + let scratch = Scratch::new("detail"); + let studio = studio(&scratch); + let quote = studio.template("template/Quote").unwrap(); + assert_eq!(quote.name, "Quote"); + assert!(quote.source.contains("zenquotes.io")); + assert_eq!(quote.hosts, ["zenquotes.io"]); + let clock = studio.template("template/Clock").unwrap(); + assert_eq!((clock.description.as_str(), clock.hosts.len()), ("A clock", 0)); + assert!(studio.template("component/steps").is_none() && studio.template("template/Missing").is_none()); + } + + #[test] + fn using_a_template_makes_an_editable_copy_and_leaves_the_template_alone() { + let scratch = Scratch::new("copy"); + let studio = studio(&scratch); + let before = std::fs::read_to_string(scratch.path("templates/template/Clock/main.jsx")).unwrap(); + let first = studio.use_template("template/Clock").unwrap(); + let second = studio.use_template("template/Clock").unwrap(); + assert_eq!((first.as_str(), second.as_str()), ("Clock", "Clock (1)")); + assert_eq!(studio.files(&first).unwrap(), ["main.jsx", "lib/label.js", "meta.json", "widget.json"], "the entry comes first"); + assert_eq!(studio.entry(&first), "main.jsx"); + + studio.save_text(&first, "main.jsx", "$render(mine);").unwrap(); + assert_eq!(studio.read_text(&first, "main.jsx").unwrap().as_deref(), Some("$render(mine);")); + assert_eq!(std::fs::read_to_string(scratch.path("templates/template/Clock/main.jsx")).unwrap(), before); + assert_eq!(studio.read_text(&second, "main.jsx").unwrap().as_deref(), Some(before.as_str()), "the second copy is its own"); + let cards = studio.widgets().unwrap(); + assert_eq!(cards.iter().map(|card| card.name.as_str()).collect::>(), ["Clock", "Clock (1)"]); + assert_ne!(cards[0].installation, cards[1].installation); + assert!(studio.use_template("component/steps").is_err()); + } + + #[test] + fn saving_is_a_round_trip_and_refuses_paths_outside_the_package() { + let scratch = Scratch::new("save"); + let studio = studio(&scratch); + let name = studio.new_widget(" My Widget ").unwrap(); + assert_eq!(name, "My Widget"); + assert_eq!(studio.read_text(&name, "main.jsx").unwrap().as_deref(), Some(STARTER)); + + let text = "// 你好\n$render(saved);\n"; + studio.save_text(&name, "main.jsx", text).unwrap(); + studio.save_text(&name, "lib/helper.js", "function helper() {}\n").unwrap(); + assert_eq!(studio.read_text(&name, "main.jsx").unwrap().as_deref(), Some(text)); + assert_eq!(studio.files(&name).unwrap(), ["main.jsx", "lib/helper.js", "widget.json"]); + // Nothing but the files themselves is left in the folder: no temporary file survives a save. + let mut left: Vec = std::fs::read_dir(scratch.path("data/Packages/My Widget")).unwrap().map(|entry| entry.unwrap().file_name().into_string().unwrap()).filter(|name| !name.starts_with(".scriptwidget")).collect(); + left.sort(); + assert_eq!(left, ["lib", "main.jsx", "widget.json"]); + + for path in ["../escape.js", "/tmp/escape.js", "lib/../../escape.js", "a\\b.js", ""] { + assert!(studio.save_text(&name, path, "x").is_err(), "{path:?} must be refused"); + } + assert!(!scratch.path("data/Packages/escape.js").exists() && !scratch.path("data/escape.js").exists()); + assert!(studio.save_text("No Such Widget", "main.jsx", "x").is_err()); + + // A link in the place of a file is not written through. + std::fs::write(scratch.path("outside.txt"), "untouched").unwrap(); + std::os::unix::fs::symlink(scratch.path("outside.txt"), scratch.path("data/Packages/My Widget/link.js")).unwrap(); + assert!(studio.save_text(&name, "link.js", "overwritten").is_err()); + assert_eq!(std::fs::read_to_string(scratch.path("outside.txt")).unwrap(), "untouched"); + } + + #[test] + fn a_file_that_is_not_text_is_not_opened_as_text() { + let scratch = Scratch::new("binary"); + let studio = studio(&scratch); + let name = studio.new_widget("Pictures").unwrap(); + std::fs::write(scratch.path("data/Packages/Pictures/photo.png"), [0x89, b'P', b'N', b'G', 0xff]).unwrap(); + std::fs::write(scratch.path("data/Packages/Pictures/broken.json"), [0xff, 0xfe, 0x00]).unwrap(); + assert_eq!(studio.read_text(&name, "photo.png").unwrap(), None); + assert_eq!(studio.read_text(&name, "broken.json").unwrap(), None, "not UTF-8"); + } + + #[test] + fn export_then_import_gives_the_same_widget_under_a_new_name() { + let scratch = Scratch::new("archive"); + let studio = studio(&scratch); + let name = studio.use_template("template/Clock").unwrap(); + let target = scratch.path("out/Clock.swt"); + std::fs::create_dir_all(scratch.path("out")).unwrap(); + std::fs::write(&target, "an older export").unwrap(); + studio.export(&name, &target).unwrap(); + assert_eq!(std::fs::read_dir(scratch.path("out")).unwrap().count(), 1, "no temporary file is left beside the export"); + + let summary = studio.inspect(&target).unwrap(); + assert_eq!((summary.name.as_str(), summary.files), ("Clock", 4)); + assert!(summary.bytes > 0 && summary.hosts.is_empty() && summary.permissions.is_empty()); + assert_eq!(studio.widgets().unwrap().len(), 1, "looking at an archive installs nothing"); + assert_eq!(std::fs::read_dir(scratch.path("data/Staging")).map(|entries| entries.count()).unwrap_or(0), 0, "and leaves nothing in staging"); + + let imported = studio.import(&target).unwrap(); + assert_eq!(imported, "Clock (1)"); + assert_eq!(studio.read_text(&imported, "lib/label.js").unwrap(), studio.read_text(&name, "lib/label.js").unwrap()); + let cards = studio.widgets().unwrap(); + assert_ne!(cards[0].installation, cards[1].installation, "an archive never carries an installation's identity"); + + studio.remove(&name).unwrap(); + assert_eq!(studio.widgets().unwrap().iter().map(|card| card.name.as_str()).collect::>(), ["Clock (1)"]); + assert!(studio.remove(&name).is_err()); + } + + #[test] + fn what_is_not_a_package_is_refused_before_anything_is_installed() { + let scratch = Scratch::new("refuse"); + let studio = studio(&scratch); + let not_an_archive = scratch.write("notes.swt", b"this is not a zip file"); + assert!(studio.inspect(¬_an_archive).is_err()); + assert!(studio.import(¬_an_archive).is_err()); + assert!(studio.inspect(&scratch.path("missing.swt")).is_err()); + assert!(studio.inspect(&scratch.path("data")).is_err(), "a folder is not an archive"); + // An archive whose script does not compile is shown, and refused on import. + let broken = scratch.write("broken.swt", &crate::testing::zip(&[("Broken/main.jsx", "$render(unclosed"), ("Broken/widget.json", &crate::testing::manifest("Broken"))])); + assert_eq!(studio.inspect(&broken).unwrap().name, "Broken"); + assert!(studio.import(&broken).is_err()); + // An entry that points outside the package fails the whole archive. + let escaping = scratch.write("escape.swt", &crate::testing::zip(&[("main.jsx", "$render(x);"), ("../evil.js", "x")])); + assert!(studio.inspect(&escaping).is_err() && studio.import(&escaping).is_err()); + assert_eq!(studio.widgets().unwrap(), []); + assert!(!scratch.path("data/evil.js").exists() && !scratch.path("evil.js").exists()); + } + + #[test] + fn a_widget_with_an_unreadable_manifest_is_listed_as_such() { + let scratch = Scratch::new("unreadable"); + let studio = studio(&scratch); + let name = studio.new_widget("Odd").unwrap(); + std::fs::write(scratch.path("data/Packages/Odd/widget.json"), "{ not json").unwrap(); + let card = studio.widget(&name).unwrap(); + assert!(!card.readable); + assert_eq!(card.title, "Odd"); + } + + #[test] + fn a_preview_draws_the_widget_at_the_size_asked_for() { + let scratch = Scratch::new("preview"); + let studio = studio(&scratch); + let name = studio.new_widget("Shown").unwrap(); + studio.save_text(&name, "main.jsx", "console.log('ran ' + $getenv('widget-size'));\n$render(hi);").unwrap(); + let request = |family: &str, scale: f32| PreviewRequest { source: PreviewSource::Installed { name: name.clone(), edits: vec![] }, family: family.into(), dark: false, scale }; + + let medium = studio.preview(&request("medium", 1.0)); + let picture = medium.picture.expect("a picture"); + assert_eq!((picture.pixel_width, picture.pixel_height, picture.width, picture.height), (329, 170, 329.0, 170.0)); + assert_eq!(picture.pixels.len(), 329 * 170 * 4); + let middle = (85 * 329 + 20) * 4; + assert_eq!(&picture.pixels[middle..middle + 4], [255, 0, 0, 255], "the script's background"); + assert_eq!(&picture.pixels[..4], [0, 0, 0, 0], "the corner is rounded away"); + assert_eq!(medium.error, None); + assert_eq!(medium.logs, [("log".to_owned(), "ran medium".to_owned())]); + + let small = studio.preview(&request("small", 2.0)).picture.unwrap(); + assert_eq!((small.pixel_width, small.pixel_height, small.width), (340, 340, 170.0)); + assert!(studio.preview(&request("huge", 1.0)).picture.is_none()); + } + + #[test] + fn unsaved_text_is_previewed_without_being_written() { + let scratch = Scratch::new("unsaved"); + let studio = studio(&scratch); + let name = studio.new_widget("Draft").unwrap(); + let edits = vec![("main.jsx".to_owned(), "console.log('draft');\n$render(draft);".to_owned())]; + let result = studio.preview(&PreviewRequest { source: PreviewSource::Installed { name: name.clone(), edits }, family: "small".into(), dark: true, scale: 1.0 }); + assert_eq!(result.logs, [("log".to_owned(), "draft".to_owned())]); + assert_eq!(studio.read_text(&name, "main.jsx").unwrap().as_deref(), Some(STARTER)); + assert!(!scratch.path("data/Packages/Draft/__Build").exists(), "a preview caches nothing in the package"); + } + + #[test] + fn a_script_error_is_reported_and_still_drawn_as_a_card() { + let scratch = Scratch::new("error"); + let studio = studio(&scratch); + let name = studio.new_widget("Broken").unwrap(); + let preview = |source: &str| studio.preview(&PreviewRequest { source: PreviewSource::Installed { name: name.clone(), edits: vec![("main.jsx".into(), source.into())] }, family: "medium".into(), dark: false, scale: 1.0 }); + + let thrown = preview("console.log('before');\nthrow new Error('boom');"); + let (_, message) = thrown.error.expect("an error"); + assert!(message.contains("boom"), "{message}"); + assert!(thrown.logs.iter().any(|(_, printed)| printed == "before"), "{:?}", thrown.logs); + assert!(thrown.picture.is_some(), "the error card"); + + let syntax = preview("$render(unclosed"); + assert!(syntax.error.is_some() && syntax.picture.is_some()); + // A script that never ends is stopped by the engine's deadline, not by the window. + let missing = studio.preview(&PreviewRequest { source: PreviewSource::Installed { name: "Nobody".into(), edits: vec![] }, family: "medium".into(), dark: false, scale: 1.0 }); + assert!(missing.error.is_some() && missing.picture.is_none()); + } + + #[test] + fn a_package_cannot_reach_files_outside_itself_through_a_preview() { + let scratch = Scratch::new("contained"); + let studio = studio(&scratch); + let name = studio.new_widget("Curious").unwrap(); + scratch.write("secret.txt", b"secret"); + let source = "let seen = 'nothing';\ntry { seen = String($file.read('../../../secret.txt')); } catch (error) { seen = 'refused'; }\nconsole.log(seen);\n$render(x);"; + let result = studio.preview(&PreviewRequest { source: PreviewSource::Installed { name, edits: vec![("main.jsx".into(), source.into())] }, family: "small".into(), dark: false, scale: 1.0 }); + let printed: Vec<&str> = result.logs.iter().map(|(_, message)| message.as_str()).collect(); + assert!(!printed.iter().any(|line| line.contains("secret")), "{printed:?}"); + } + + #[test] + fn a_template_is_previewed_without_being_installed() { + let scratch = Scratch::new("template-preview"); + let studio = studio(&scratch); + let result = studio.preview(&PreviewRequest { source: PreviewSource::Bundled("template/Clock".into()), family: "medium".into(), dark: false, scale: 1.0 }); + assert_eq!(result.error, None); + assert!(result.picture.is_some()); + assert_eq!(studio.widgets().unwrap(), []); + assert!(studio.preview(&PreviewRequest { source: PreviewSource::Bundled("component/steps".into()), family: "medium".into(), dark: false, scale: 1.0 }).picture.is_none()); + } + + #[test] + fn an_atomic_write_replaces_the_file_and_never_follows_a_link_for_its_temporary_file() { + let scratch = Scratch::new("atomic"); + let target = scratch.path("settings.json"); + write_atomically(&target, b"one", 0o600).unwrap(); + write_atomically(&target, b"two", 0o600).unwrap(); + assert_eq!(std::fs::read(&target).unwrap(), b"two"); + assert_eq!(std::fs::metadata(&target).unwrap().permissions().mode() & 0o777, 0o600); + assert_eq!(std::fs::read_dir(scratch.root()).unwrap().count(), 1); + assert!(write_atomically(&scratch.path("missing-folder/file"), b"x", 0o600).is_err()); + // A link at the target is replaced by the file, not written through. + std::fs::write(scratch.path("elsewhere"), "kept").unwrap(); + std::os::unix::fs::symlink(scratch.path("elsewhere"), scratch.path("linked")).unwrap(); + write_atomically(&scratch.path("linked"), b"new", 0o600).unwrap(); + assert_eq!(std::fs::read_to_string(scratch.path("elsewhere")).unwrap(), "kept"); + assert!(!std::fs::symlink_metadata(scratch.path("linked")).unwrap().file_type().is_symlink()); + } + + use std::os::unix::fs::PermissionsExt; +} diff --git a/Linux/crates/sw-studio/src/service.rs b/Linux/crates/sw-studio/src/service.rs new file mode 100644 index 00000000..35e7a64a --- /dev/null +++ b/Linux/crates/sw-studio/src/service.rs @@ -0,0 +1,155 @@ +//! Asking the background service to put a widget on the desktop. The window +//! does not talk to the service itself: it runs `scriptwidget add +//! [--family F]`, the same command a person would type, as a child process +//! with an argument list (never through a shell). + +use std::ffi::OsString; +use std::io::Read; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; +use std::time::{Duration, Instant}; + +/// The exit code of a command that needs the background service when it is not running. +pub const EXIT_SERVICE_NOT_RUNNING: i32 = 69; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum AddOutcome { + Added, + ServiceNotRunning, + /// The `scriptwidget` program was not found. + CommandMissing, + Failed(String), +} + +/// The `scriptwidget` program: `SCRIPTWIDGET_CLI` when set, the one installed +/// next to this program, otherwise whatever `PATH` finds. +pub fn command_line(variable: impl Fn(&str) -> Option, executable: Option<&Path>) -> PathBuf { + if let Some(named) = variable("SCRIPTWIDGET_CLI").filter(|value| !value.is_empty()) { + return PathBuf::from(named); + } + executable.and_then(Path::parent).map(|folder| folder.join("scriptwidget")).filter(|sibling| sibling.is_file()).unwrap_or_else(|| PathBuf::from("scriptwidget")) +} + +/// Runs ` add [--family ]` and waits for it, at most `limit`. +pub fn add_to_desktop(program: &Path, package: &str, family: Option<&str>, limit: Duration) -> AddOutcome { + // The command line would read a name that starts with a dash as an option. + if package.is_empty() || package.starts_with('-') { + return AddOutcome::Failed(format!("{package:?} cannot be passed to the command line.")); + } + let mut command = Command::new(program); + command.arg("add").arg(package); + if let Some(family) = family { + command.arg("--family").arg(family); + } + let mut child = match command.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::piped()).spawn() { + Ok(child) => child, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return AddOutcome::CommandMissing, + Err(error) => return AddOutcome::Failed(error.to_string()), + }; + let deadline = Instant::now() + limit; + let status = loop { + match child.try_wait() { + Ok(Some(status)) => break status, + Ok(None) if Instant::now() < deadline => std::thread::sleep(Duration::from_millis(20)), + Ok(None) => { + let _ = child.kill(); + let _ = child.wait(); + return AddOutcome::Failed("The scriptwidget command did not answer.".into()); + } + Err(error) => return AddOutcome::Failed(error.to_string()), + } + }; + match status.code() { + Some(0) => AddOutcome::Added, + Some(EXIT_SERVICE_NOT_RUNNING) => AddOutcome::ServiceNotRunning, + code => { + // What the command said, shortened: `error: [code] message` lines. + let mut said = String::new(); + if let Some(stderr) = child.stderr.take() { + let _ = stderr.take(4096).read_to_string(&mut said); + } + let line = said.lines().map(str::trim).find(|line| !line.is_empty()).unwrap_or_default().trim_start_matches("error:").trim().to_owned(); + AddOutcome::Failed(if line.is_empty() { format!("scriptwidget add ended with {}", code.map_or_else(|| "a signal".to_owned(), |code| format!("code {code}"))) } else { line }) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::testing::Scratch; + use std::os::unix::fs::PermissionsExt; + + /// A stand-in for the command line that records its arguments, one per line, and exits with `code`. + fn fake(scratch: &Scratch, code: i32, says: &str) -> PathBuf { + let program = scratch.path("scriptwidget"); + let record = scratch.path("arguments.txt"); + std::fs::write(&program, format!("#!/bin/sh\nfor argument in \"$@\"; do printf '%s\\n' \"$argument\" >> '{}'; done\n[ -n '{says}' ] && echo '{says}' >&2\nexit {code}\n", record.display())).unwrap(); + std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755)).unwrap(); + program + } + + fn recorded(scratch: &Scratch) -> Vec { + std::fs::read_to_string(scratch.path("arguments.txt")).unwrap_or_default().lines().map(str::to_owned).collect() + } + + const LIMIT: Duration = Duration::from_secs(10); + + #[test] + fn the_package_is_one_argument_whatever_it_contains() { + let scratch = Scratch::new("service-arguments"); + let program = fake(&scratch, 0, ""); + let name = "My Widget; rm -rf ~ $(id) `id` \"quoted\" 小组件"; + assert_eq!(add_to_desktop(&program, name, Some("large"), LIMIT), AddOutcome::Added); + assert_eq!(recorded(&scratch), ["add", name, "--family", "large"]); + std::fs::remove_file(scratch.path("arguments.txt")).unwrap(); + assert_eq!(add_to_desktop(&program, "Clock", None, LIMIT), AddOutcome::Added); + assert_eq!(recorded(&scratch), ["add", "Clock"]); + } + + #[test] + fn exit_code_69_means_the_service_is_not_running() { + let scratch = Scratch::new("service-69"); + assert_eq!(add_to_desktop(&fake(&scratch, 69, "error: [service.not_running] no service"), "Clock", None, LIMIT), AddOutcome::ServiceNotRunning); + } + + #[test] + fn other_failures_carry_what_the_command_said() { + let scratch = Scratch::new("service-failed"); + assert_eq!(add_to_desktop(&fake(&scratch, 1, "error: [package_not_found] No such package"), "Clock", None, LIMIT), AddOutcome::Failed("[package_not_found] No such package".into())); + let silent = Scratch::new("service-silent"); + assert_eq!(add_to_desktop(&fake(&silent, 3, ""), "Clock", None, LIMIT), AddOutcome::Failed("scriptwidget add ended with code 3".into())); + } + + #[test] + fn a_missing_program_and_a_name_that_reads_as_an_option_are_told_apart() { + let scratch = Scratch::new("service-missing"); + assert_eq!(add_to_desktop(&scratch.path("no-such-program"), "Clock", None, LIMIT), AddOutcome::CommandMissing); + let program = fake(&scratch, 0, ""); + assert!(matches!(add_to_desktop(&program, "--data", None, LIMIT), AddOutcome::Failed(_))); + assert!(matches!(add_to_desktop(&program, "", None, LIMIT), AddOutcome::Failed(_))); + assert_eq!(recorded(&scratch), Vec::::new(), "nothing was run"); + } + + #[test] + fn a_command_that_hangs_is_stopped() { + let scratch = Scratch::new("service-hang"); + let program = scratch.path("scriptwidget"); + std::fs::write(&program, "#!/bin/sh\nexec sleep 30\n").unwrap(); + std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755)).unwrap(); + let started = Instant::now(); + assert!(matches!(add_to_desktop(&program, "Clock", None, Duration::from_millis(300)), AddOutcome::Failed(_))); + assert!(started.elapsed() < Duration::from_secs(5)); + } + + #[test] + fn the_program_is_the_one_named_or_the_one_installed_beside_this_one() { + let scratch = Scratch::new("service-locate"); + let none = |_: &str| None; + assert_eq!(command_line(none, Some(&scratch.path("bin/scriptwidget-studio"))), PathBuf::from("scriptwidget"), "nothing beside it: PATH decides"); + scratch.write("bin/scriptwidget", b""); + assert_eq!(command_line(none, Some(&scratch.path("bin/scriptwidget-studio"))), scratch.path("bin/scriptwidget")); + assert_eq!(command_line(|name| (name == "SCRIPTWIDGET_CLI").then(|| "/opt/sw".into()), Some(&scratch.path("bin/scriptwidget-studio"))), PathBuf::from("/opt/sw")); + assert_eq!(command_line(none, None), PathBuf::from("scriptwidget")); + } +} diff --git a/Linux/crates/sw-studio/src/settings.rs b/Linux/crates/sw-studio/src/settings.rs new file mode 100644 index 00000000..4d4b6051 --- /dev/null +++ b/Linux/crates/sw-studio/src/settings.rs @@ -0,0 +1,100 @@ +//! The user's settings (`settings.json` in the data folder), shared with the +//! background service. The keys are the ones the Windows host uses. Keys this +//! window does not know are kept as they are. + +use std::path::{Path, PathBuf}; + +use serde_json::{Map, Value}; + +use crate::model::write_atomically; + +/// The refresh intervals offered, in minutes, as on Windows. +pub const INTERVALS: [u32; 6] = [5, 15, 30, 60, 120, 360]; +pub const DEFAULT_REFRESH_MINUTES: u32 = 15; +const MINIMUM_REFRESH_MINUTES: u32 = 5; +const MAXIMUM_REFRESH_MINUTES: u32 = 24 * 60; +/// A settings file is small; anything larger is not ours. +const MAXIMUM_BYTES: u64 = 256 * 1024; + +pub struct Settings { + file: PathBuf, +} + +impl Settings { + pub fn new(file: impl Into) -> Self { + Self { file: file.into() } + } + + pub fn file(&self) -> &Path { + &self.file + } + + /// How often widgets without a schedule of their own are refreshed. + pub fn refresh_minutes(&self) -> u32 { + self.read().get("refreshMinutes").and_then(Value::as_u64).map_or(DEFAULT_REFRESH_MINUTES, |minutes| minutes.clamp(u64::from(MINIMUM_REFRESH_MINUTES), u64::from(MAXIMUM_REFRESH_MINUTES)) as u32) + } + + pub fn set_refresh_minutes(&self, minutes: u32) -> std::io::Result<()> { + let mut values = self.read(); + values.insert("refreshMinutes".into(), Value::from(minutes.clamp(MINIMUM_REFRESH_MINUTES, MAXIMUM_REFRESH_MINUTES))); + let mut text = serde_json::to_string_pretty(&Value::Object(values)).map_err(std::io::Error::other)?; + text.push('\n'); + write_atomically(&self.file, text.as_bytes(), 0o600) + } + + /// The interval of [`INTERVALS`] the setting selects: the first that is not shorter. + pub fn selected_interval(&self) -> u32 { + let current = self.refresh_minutes(); + INTERVALS.iter().copied().find(|minutes| *minutes >= current).unwrap_or(INTERVALS[INTERVALS.len() - 1]) + } + + /// A missing, oversized or malformed file reads as no settings. + fn read(&self) -> Map { + let readable = std::fs::metadata(&self.file).is_ok_and(|metadata| metadata.is_file() && metadata.len() <= MAXIMUM_BYTES); + if !readable { + return Map::new(); + } + match std::fs::read_to_string(&self.file).ok().and_then(|text| serde_json::from_str::(&text).ok()) { + Some(Value::Object(values)) => values, + _ => Map::new(), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::testing::Scratch; + + #[test] + fn the_interval_is_saved_and_other_keys_are_kept() { + let scratch = Scratch::new("settings"); + let settings = Settings::new(scratch.path("settings.json")); + assert_eq!(settings.refresh_minutes(), 15, "no file yet"); + assert_eq!(settings.selected_interval(), 15); + + std::fs::write(settings.file(), r#"{"locked":true,"refreshMinutes":45,"future":{"a":[1,2]}}"#).unwrap(); + assert_eq!(settings.refresh_minutes(), 45); + assert_eq!(settings.selected_interval(), 60); + settings.set_refresh_minutes(120).unwrap(); + assert_eq!(settings.refresh_minutes(), 120); + let saved: Value = serde_json::from_str(&std::fs::read_to_string(settings.file()).unwrap()).unwrap(); + assert_eq!(saved, serde_json::json!({"locked": true, "refreshMinutes": 120, "future": {"a": [1, 2]}})); + assert_eq!(std::fs::read_dir(scratch.root()).unwrap().count(), 1, "no temporary file is left"); + } + + #[test] + fn values_out_of_range_and_broken_files_fall_back() { + let scratch = Scratch::new("settings-range"); + let settings = Settings::new(scratch.path("settings.json")); + for (text, expected) in [(r#"{"refreshMinutes":1}"#, 5), (r#"{"refreshMinutes":99999}"#, 1440), (r#"{"refreshMinutes":"soon"}"#, 15), (r#"{"refreshMinutes":-3}"#, 15), ("[1,2]", 15), ("{ not json", 15), ("", 15)] { + std::fs::write(settings.file(), text).unwrap(); + assert_eq!(settings.refresh_minutes(), expected, "{text}"); + } + settings.set_refresh_minutes(0).unwrap(); + assert_eq!(settings.refresh_minutes(), 5); + settings.set_refresh_minutes(1_000_000).unwrap(); + assert_eq!(settings.refresh_minutes(), 1440); + assert_eq!(settings.selected_interval(), 360); + } +} diff --git a/Linux/crates/sw-studio/src/strings.rs b/Linux/crates/sw-studio/src/strings.rs new file mode 100644 index 00000000..5f70ed98 --- /dev/null +++ b/Linux/crates/sw-studio/src/strings.rs @@ -0,0 +1,340 @@ +//! Every text the management window shows, in English and Simplified +//! Chinese. The keys follow `Windows/src/ScriptWidget.Studio/Services/Loc.cs` +//! where the two windows say the same thing. + +use std::sync::atomic::{AtomicU8, Ordering}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Language { + English, + Chinese, +} + +/// Key, English, Simplified Chinese. `{0}` and `{1}` are filled by [`tf`]. +pub const TEXT: &[(&str, &str, &str)] = &[ + ("app.title", "ScriptWidget", "ScriptWidget"), + ("nav.library", "My widgets", "我的小组件"), + ("nav.templates", "Templates", "模板"), + ("nav.settings", "Settings", "设置"), + // + ("library.title", "My widgets", "我的小组件"), + ("library.new", "New widget", "新建小组件"), + ("library.import", "Import…", "导入…"), + ("library.empty.title", "No widgets yet", "还没有小组件"), + ("library.empty.detail", "Start from a template, or create an empty widget and write your own.", "从模板开始,或者新建一个空白小组件自己写。"), + ("library.empty.templates", "Browse templates", "浏览模板"), + ("library.unreadable", "Its widget.json cannot be read.", "它的 widget.json 无法读取。"), + ("library.failed", "Your widgets could not be listed: {0}", "无法列出你的小组件:{0}"), + // + ("action.edit", "Edit", "编辑"), + ("action.addToDesktop", "Add to desktop", "添加到桌面"), + ("action.export", "Export…", "导出…"), + ("action.delete", "Remove", "移除"), + ("action.exportNamed", "Export {0}", "导出“{0}”"), + ("action.removeNamed", "Remove {0}", "移除“{0}”"), + ("action.use", "Use this template", "使用此模板"), + ("action.save", "Save", "保存"), + ("action.back", "Back", "返回"), + ("action.cancel", "Cancel", "取消"), + ("action.ok", "OK", "确定"), + // + ("templates.title", "Templates", "模板"), + ("templates.detail", "Ready-made widgets. Open one to see what it shows and how it is written, then make it yours.", "现成的小组件。点开可以看它的效果和代码,再变成你自己的。"), + ("templates.none", "No templates are installed with this program.", "这个程序没有附带模板。"), + ("templates.group.template", "Widgets", "小组件"), + ("templates.group.component", "Component examples", "组件示例"), + ("templates.group.api", "API examples", "API 示例"), + ("templates.network", "Uses the network", "会访问网络"), + // + ("detail.code", "Code", "代码"), + ("detail.readOnly", "Read only", "只读"), + ("detail.network", "Connects to {0}", "会访问 {0}"), + ("detail.noNetwork", "Does not use the network", "不访问网络"), + // + ("editor.files", "Files", "文件"), + ("editor.code", "Code editor", "代码编辑器"), + ("editor.preview", "Preview", "预览"), + ("editor.dark", "Dark", "深色"), + ("editor.light", "Light", "浅色"), + ("editor.appearance", "Preview appearance: {0}", "预览外观:{0}"), + ("editor.unsaved", "Unsaved changes", "有未保存的修改"), + ("editor.saved", "Saved", "已保存"), + ("editor.saveFailed", "Could not save: {0}", "保存失败:{0}"), + ("editor.running", "Running…", "运行中…"), + ("editor.ok", "Ran in {0} ms", "运行用时 {0} 毫秒"), + ("editor.logs", "Output", "输出"), + ("editor.noLogs", "console.log output appears here.", "console.log 的输出显示在这里。"), + ("editor.binary", "This file is not text and cannot be edited here.", "这个文件不是文本,不能在这里编辑。"), + ("editor.missing", "“{0}” is not one of your widgets.", "“{0}”不在你的小组件里。"), + ("editor.discard.title", "Discard unsaved changes?", "放弃未保存的修改?"), + ("editor.discard.detail", "Your changes to {0} have not been saved.", "{0} 的修改还没有保存。"), + ("editor.discard.confirm", "Discard", "放弃"), + ("editor.saveHint", "Ctrl+S", "Ctrl+S"), + // + ("size.small", "Small", "小"), + ("size.medium", "Medium", "中"), + ("size.large", "Large", "大"), + ("size.extraLarge", "Extra large", "特大"), + // + ("dialog.new.title", "New widget", "新建小组件"), + ("dialog.new.hint", "Name", "名称"), + ("dialog.new.default", "My Widget", "我的小组件"), + ("dialog.delete.title", "Remove “{0}”?", "移除“{0}”?"), + ("dialog.delete.detail", "The widget and its files are deleted from this computer. This cannot be undone.", "这个小组件和它的文件会从这台电脑上删除,无法恢复。"), + ("dialog.import.title", "Import this widget?", "导入这个小组件?"), + ("dialog.import.detail", "“{0}” will be added to your widgets. Only import widgets from people you trust.", "“{0}”会加入你的小组件。请只导入你信任的来源。"), + ("dialog.import.contents", "{0} files, {1}", "{0} 个文件,{1}"), + ("dialog.import.file", "From {0}", "来自 {0}"), + ("dialog.import.confirm", "Import", "导入"), + ("dialog.import.refused", "This file cannot be imported: {0}", "这个文件无法导入:{0}"), + ("dialog.path.import", "Import a widget", "导入小组件"), + ("dialog.path.import.hint", "Path of a .swt file", ".swt 文件的路径"), + ("dialog.path.export", "Export “{0}”", "导出“{0}”"), + ("dialog.path.export.hint", "Where to save the .swt file", ".swt 文件保存到哪里"), + ("dialog.error.title", "That did not work", "操作没有成功"), + // + ("toast.added", "Added to the desktop", "已添加到桌面"), + ("toast.exported", "Exported to {0}", "已导出到 {0}"), + ("toast.imported", "“{0}” was added to your widgets", "“{0}”已加入你的小组件"), + ("toast.removed", "“{0}” was removed", "已移除“{0}”"), + ("toast.hostMissing", "The ScriptWidget background service is not running, so the widget could not be placed. Start it, then try again.", "ScriptWidget 后台服务没有运行,无法放置小组件。请先启动它再试。"), + ("toast.cliMissing", "The scriptwidget command was not found, so the widget could not be placed.", "找不到 scriptwidget 命令,无法放置小组件。"), + ("toast.addFailed", "The widget could not be placed: {0}", "无法放置小组件:{0}"), + // + ("settings.title", "Settings", "设置"), + ("settings.refresh", "Refresh interval", "刷新间隔"), + ("settings.refresh.detail", "How often widgets without a schedule of their own run again.", "没有自己时间表的小组件多久重新运行一次。"), + ("settings.minutes", "{0} minutes", "{0} 分钟"), + ("settings.hours", "{0} hours", "{0} 小时"), + ("settings.hour", "1 hour", "1 小时"), + ("settings.failed", "The setting could not be saved: {0}", "设置保存失败:{0}"), + ("settings.data", "Data folder", "数据文件夹"), + ("settings.about", "About", "关于"), + ("settings.version", "ScriptWidget for Linux {0} (in development)", "ScriptWidget Linux 版 {0}(开发中)"), + ("settings.licence", "ScriptWidget is licensed under Apache-2.0.", "ScriptWidget 以 Apache-2.0 许可证发布。"), + ( + "settings.licences", + "Built with GPUI and gpui-kit (Apache-2.0), QuickJS-NG (MIT), Skia (BSD-3-Clause), Lucide icons (ISC) and the Inter typeface (SIL OFL 1.1).", + "使用了 GPUI 和 gpui-kit(Apache-2.0)、QuickJS-NG(MIT)、Skia(BSD-3-Clause)、Lucide 图标(ISC)和 Inter 字体(SIL OFL 1.1)。", + ), + ("settings.website", "Website", "官方网站"), + // + ("startup.failed", "ScriptWidget could not start: {0}", "ScriptWidget 无法启动:{0}"), + ("bytes.b", "{0} bytes", "{0} 字节"), + ("bytes.kb", "{0} KB", "{0} KB"), + ("bytes.mb", "{0} MB", "{0} MB"), +]; + +static LANGUAGE: AtomicU8 = AtomicU8::new(0); + +/// The language a locale asks for: the window follows the system language +/// when it is one of the two it has, and is English for every other. +/// `variable` reads an environment variable. `SCRIPTWIDGET_LANGUAGE=zh` or +/// `en` chooses for one run, as on Windows; otherwise the first of `LC_ALL`, +/// `LC_MESSAGES` and `LANG` that is set decides, as gettext does. +pub fn detect(variable: impl Fn(&str) -> Option) -> Language { + let locale = ["SCRIPTWIDGET_LANGUAGE", "LC_ALL", "LC_MESSAGES", "LANG"].into_iter().find_map(|name| variable(name).filter(|value| !value.is_empty())).unwrap_or_default(); + language_of(&locale) +} + +/// Simplified Chinese for a Chinese locale written in simplified characters +/// (`zh`, `zh_CN`, `zh-Hans`, `zh_SG`), English for everything else: other +/// languages, Traditional Chinese (`zh_TW`, `zh_HK`, `zh-Hant`), `C`, `POSIX` +/// and anything that is not a locale name. +pub fn language_of(locale: &str) -> Language { + // language[_territory][.codeset][@modifier], or a BCP 47 tag with hyphens. + let name = locale.split(['.', '@']).next().unwrap_or_default().to_ascii_lowercase(); + let mut parts = name.split(['_', '-']); + if parts.next() != Some("zh") { + return Language::English; + } + let rest: Vec<&str> = parts.collect(); + let traditional = rest.iter().any(|part| matches!(*part, "hant" | "tw" | "hk" | "mo")); + let known = rest.iter().all(|part| matches!(*part, "hans" | "hant" | "cn" | "sg" | "my" | "tw" | "hk" | "mo")); + if known && !traditional { Language::Chinese } else { Language::English } +} + +pub fn set_language(language: Language) { + LANGUAGE.store(language as u8, Ordering::Relaxed); +} + +pub fn language() -> Language { + if LANGUAGE.load(Ordering::Relaxed) == Language::Chinese as u8 { Language::Chinese } else { Language::English } +} + +pub fn text_in(language: Language, key: &str) -> Option<&'static str> { + TEXT.iter().find(|(name, _, _)| *name == key).map(|(_, english, chinese)| if language == Language::Chinese { *chinese } else { *english }) +} + +/// The text for `key` in the current language. An unknown key shows as itself, so a mistake is visible. +pub fn t(key: &str) -> String { + text_in(language(), key).map_or_else(|| key.to_owned(), str::to_owned) +} + +/// [`t`] with `{0}`, `{1}`… replaced. The arguments are inserted as they are, never interpreted again. +pub fn tf(key: &str, arguments: &[&str]) -> String { + fill(&t(key), arguments) +} + +fn fill(pattern: &str, arguments: &[&str]) -> String { + let mut result = String::with_capacity(pattern.len() + 16); + let mut rest = pattern; + while let Some(open) = rest.find('{') { + result.push_str(&rest[..open]); + let after = &rest[open + 1..]; + match after.find('}').and_then(|close| after[..close].parse::().ok().map(|index| (index, close))) { + Some((index, close)) => { + result.push_str(arguments.get(index).copied().unwrap_or("")); + rest = &after[close + 1..]; + } + None => { + result.push('{'); + rest = after; + } + } + } + result.push_str(rest); + result +} + +/// The name of a widget family ("small", "extraLarge"). +pub fn family(name: &str) -> String { + t(&format!("size.{name}")) +} + +pub fn bytes(count: u64) -> String { + if count < 1024 { + tf("bytes.b", &[&count.to_string()]) + } else if count < 1024 * 1024 { + tf("bytes.kb", &[&format!("{:.1}", count as f64 / 1024.0)]) + } else { + tf("bytes.mb", &[&format!("{:.1}", count as f64 / (1024.0 * 1024.0))]) + } +} + +/// The label of a refresh interval in minutes. +pub fn interval(minutes: u32) -> String { + match minutes { + 60 => t("settings.hour"), + minutes if minutes > 60 && minutes % 60 == 0 => tf("settings.hours", &[&(minutes / 60).to_string()]), + minutes => tf("settings.minutes", &[&minutes.to_string()]), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::BTreeSet; + + fn placeholders(text: &str) -> BTreeSet { + let mut found = BTreeSet::new(); + let mut rest = text; + while let Some(open) = rest.find('{') { + let after = &rest[open + 1..]; + if let Some(close) = after.find('}') { + found.insert(after[..close].to_owned()); + } + rest = after; + } + found + } + + #[test] + fn every_key_has_both_languages_and_the_same_placeholders() { + let mut seen = BTreeSet::new(); + for (key, english, chinese) in TEXT { + assert!(seen.insert(*key), "{key} is listed twice"); + assert!(!english.trim().is_empty(), "{key} has no English text"); + assert!(!chinese.trim().is_empty(), "{key} has no Chinese text"); + assert_eq!(placeholders(english), placeholders(chinese), "{key} takes different arguments in the two languages"); + } + } + + #[test] + fn chinese_texts_are_translated() { + // The same text in both languages is right only for names and key names. + let same: Vec<&str> = TEXT.iter().filter(|(_, english, chinese)| english == chinese).map(|(key, _, _)| *key).collect(); + assert_eq!(same, ["app.title", "editor.saveHint", "bytes.kb", "bytes.mb"]); + } + + /// The keys the window's source asks for, read from the source itself. + fn used_keys(source: &str) -> Vec { + let mut keys = Vec::new(); + for call in ["t(\"", "tf(\""] { + let mut rest = source; + while let Some(at) = rest.find(call) { + // `t(` must be the whole name of the function called. + let named = rest[..at].chars().next_back().is_some_and(|before| before.is_alphanumeric() || before == '_'); + let after = &rest[at + call.len()..]; + if let (false, Some(end)) = (named, after.find('"')) { + keys.push(after[..end].to_owned()); + } + rest = after; + } + } + keys + } + + #[test] + fn every_key_the_window_uses_exists() { + let sources = [ + include_str!("ui/mod.rs"), + include_str!("ui/window.rs"), + include_str!("ui/library.rs"), + include_str!("ui/templates.rs"), + include_str!("ui/editor.rs"), + include_str!("ui/settings.rs"), + include_str!("ui/preview_pane.rs"), + include_str!("main.rs"), + ]; + let mut used = BTreeSet::new(); + for source in sources { + for key in used_keys(source) { + assert!(text_in(Language::English, &key).is_some(), "the window asks for the unknown text {key}"); + used.insert(key); + } + } + assert!(used.len() > 40, "the scan found only {} keys", used.len()); + // Keys that are built at run time or used by this module. + let indirect = |key: &str| key.starts_with("size.") || key.starts_with("bytes.") || key.starts_with("templates.group.") || ["settings.hour", "settings.hours", "settings.minutes"].contains(&key); + let unused: Vec<&str> = TEXT.iter().map(|(key, _, _)| *key).filter(|key| !used.contains(*key) && !indirect(key)).collect(); + assert!(unused.is_empty(), "texts nothing shows: {unused:?}"); + } + + #[test] + fn a_supported_locale_chooses_its_language_and_every_other_is_english() { + for locale in ["zh", "zh_CN", "zh_CN.UTF-8", "zh-CN", "zh-Hans", "zh_Hans_CN", "zh-Hans-CN", "zh_SG.UTF-8", "ZH_cn.utf8", "zh_CN@pinyin"] { + assert_eq!(language_of(locale), Language::Chinese, "{locale}"); + } + for locale in ["en_US.UTF-8", "en", "C", "C.UTF-8", "POSIX", "", "fr_FR.UTF-8", "ja_JP.UTF-8", "de", "ko_KR", "zh_TW.UTF-8", "zh_HK", "zh-Hant", "zh_Hant_TW", "zh_XX", "zhx", "zha_CN", "not a locale", "\u{0}\u{ff}", "_CN", ".UTF-8"] { + assert_eq!(language_of(locale), Language::English, "{locale:?}"); + } + } + + #[test] + fn the_environment_names_the_locale() { + let with = |pairs: &[(&str, &str)]| { + let pairs: Vec<(String, String)> = pairs.iter().map(|(name, value)| (name.to_string(), value.to_string())).collect(); + detect(move |name| pairs.iter().find(|(key, _)| key == name).map(|(_, value)| value.clone())) + }; + assert_eq!(with(&[]), Language::English, "no locale at all"); + assert_eq!(with(&[("LANG", "zh_CN.UTF-8")]), Language::Chinese); + assert_eq!(with(&[("LANG", "fr_FR.UTF-8")]), Language::English, "a language the window does not have"); + assert_eq!(with(&[("LANG", "zh_CN.UTF-8"), ("LC_ALL", "en_US.UTF-8")]), Language::English, "LC_ALL wins"); + assert_eq!(with(&[("LANG", "en_US.UTF-8"), ("LC_MESSAGES", "zh_CN.UTF-8")]), Language::Chinese); + assert_eq!(with(&[("LANG", "zh_CN.UTF-8"), ("LC_ALL", "")]), Language::Chinese, "an empty variable is unset"); + assert_eq!(with(&[("LANG", "en_US.UTF-8"), ("SCRIPTWIDGET_LANGUAGE", "zh")]), Language::Chinese); + assert_eq!(with(&[("LANG", "zh_CN.UTF-8"), ("SCRIPTWIDGET_LANGUAGE", "en")]), Language::English); + } + + #[test] + fn arguments_are_inserted_and_never_read_again() { + assert_eq!(fill("Remove “{0}”?", &["Clock"]), "Remove “Clock”?"); + assert_eq!(fill("{0} files, {1}", &["3", "2 KB"]), "3 files, 2 KB"); + assert_eq!(fill("{0} and {0}", &["{1}", "never"]), "{1} and {1}", "a package name with braces stays text"); + assert_eq!(fill("{1}", &["only"]), ""); + assert_eq!(fill("{x} {", &[]), "{x} {"); + assert_eq!(interval(15), fill(text_in(language(), "settings.minutes").unwrap(), &["15"])); + assert_eq!(interval(120), fill(text_in(language(), "settings.hours").unwrap(), &["2"])); + } +} diff --git a/Linux/crates/sw-studio/src/testing.rs b/Linux/crates/sw-studio/src/testing.rs new file mode 100644 index 00000000..21dc1a8d --- /dev/null +++ b/Linux/crates/sw-studio/src/testing.rs @@ -0,0 +1,121 @@ +//! Shared by the tests: a scratch folder, a small templates folder and hand-made archives. +//! The tests need the files of `node Linux/scripts/build-shared-js.mjs`. + +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU32, Ordering}; + +use sw_core::DataPaths; +use sw_core::packages::{archive, manifests}; + +use crate::model::Studio; + +pub struct Scratch(PathBuf); + +impl Scratch { + pub fn new(name: &str) -> Self { + Self::under(&std::env::temp_dir(), "scriptwidget-studio-test", name) + } + + /// A folder with a short path: the path of a Unix socket is limited to about 100 bytes. + pub fn short(name: &str) -> Self { + Self::under(Path::new("/tmp"), "sws", name) + } + + fn under(base: &Path, prefix: &str, name: &str) -> Self { + static COUNTER: AtomicU32 = AtomicU32::new(0); + let root = base.join(format!("{prefix}-{name}-{}-{}", std::process::id(), COUNTER.fetch_add(1, Ordering::SeqCst))); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + // The real path: macOS keeps its temporary folder behind a link. + Self(std::fs::canonicalize(root).unwrap()) + } + + pub fn root(&self) -> &Path { + &self.0 + } + + pub fn path(&self, relative: &str) -> PathBuf { + self.0.join(relative) + } + + pub fn write(&self, relative: &str, bytes: &[u8]) -> PathBuf { + let path = self.path(relative); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(&path, bytes).unwrap(); + path + } +} + +impl Drop for Scratch { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +pub fn runtime_files() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets/scriptwidget") +} + +/// A studio over an empty data folder and three templates: one that runs, one that asks the network, one that needs an API Linux lacks. +pub fn studio(scratch: &Scratch) -> Studio { + scratch.write("templates/template/Clock/main.jsx", b"$import('lib/label.js');\n$render({label()});"); + scratch.write("templates/template/Clock/lib/label.js", b"function label() { return 'clock'; }"); + scratch.write("templates/template/Clock/meta.json", br#"{"description":"A clock"}"#); + scratch.write("templates/template/Quote/main.jsx", b"const quote = await fetch(\"https://zenquotes.io/api/today\");\n$render(q);"); + scratch.write("templates/component/steps/main.jsx", b"$render({String(await $health.steps())});"); + scratch.write("templates/api/storage/main.jsx", b"$render({$storage.getString('a')});"); + let paths = DataPaths::new(Some(&scratch.path("data"))).unwrap(); + Studio::with(&paths, &runtime_files(), Some(scratch.path("templates"))).expect("the runtime files: run node Linux/scripts/build-shared-js.mjs") +} + +pub fn manifest(name: &str) -> String { + String::from_utf8(manifests::encode(&manifests::new_manifest(name))).unwrap() +} + +/// A ZIP archive with the files stored uncompressed. +pub fn zip(files: &[(&str, &str)]) -> Vec { + let mut archive_bytes = Vec::new(); + let mut directory = Vec::new(); + for (name, content) in files { + let offset = archive_bytes.len() as u32; + let crc = archive::crc32(content.as_bytes()); + let size = content.len() as u32; + let header = |signature: u32, central: bool| { + let mut bytes = Vec::new(); + bytes.extend(signature.to_le_bytes()); + if central { + bytes.extend(20u16.to_le_bytes()); + } + bytes.extend(20u16.to_le_bytes()); + bytes.extend(0x0800u16.to_le_bytes()); + bytes.extend(0u16.to_le_bytes()); + bytes.extend(0u32.to_le_bytes()); + bytes.extend(crc.to_le_bytes()); + bytes.extend(size.to_le_bytes()); + bytes.extend(size.to_le_bytes()); + bytes.extend((name.len() as u16).to_le_bytes()); + bytes.extend(0u16.to_le_bytes()); + if central { + bytes.extend([0u8; 6]); + bytes.extend((0o100644u32 << 16).to_le_bytes()); + bytes.extend(offset.to_le_bytes()); + } + bytes.extend(name.as_bytes()); + bytes + }; + archive_bytes.extend(header(0x0403_4b50, false)); + archive_bytes.extend(content.as_bytes()); + directory.extend(header(0x0201_4b50, true)); + } + let start = archive_bytes.len() as u32; + let length = directory.len() as u32; + archive_bytes.extend(directory); + archive_bytes.extend(0x0605_4b50u32.to_le_bytes()); + archive_bytes.extend([0u8; 4]); + archive_bytes.extend((files.len() as u16).to_le_bytes()); + archive_bytes.extend((files.len() as u16).to_le_bytes()); + archive_bytes.extend(length.to_le_bytes()); + archive_bytes.extend(start.to_le_bytes()); + archive_bytes.extend(0u16.to_le_bytes()); + archive_bytes +} diff --git a/Linux/crates/sw-studio/src/ui/editor.rs b/Linux/crates/sw-studio/src/ui/editor.rs new file mode 100644 index 00000000..2e3524e1 --- /dev/null +++ b/Linux/crates/sw-studio/src/ui/editor.rs @@ -0,0 +1,224 @@ +//! The editor: the files of one widget, the code editor of gpui-kit with +//! completion from the shared API metadata, and the live preview. + +use std::rc::Rc; +use std::sync::Arc; + +use gpui_kit::component::button::{Button, ButtonVariants as _}; +use gpui_kit::component::input::{CompletionProvider, Editor, EditorState, InputEvent, Rope}; +use gpui_kit::component::scroll::ScrollableElement as _; +use gpui_kit::component::{ActiveTheme as _, StyledExt as _, IconName, Selectable as _, Sizable as _, h_flex, v_flex}; +use gpui_kit::{App, AppContext as _, Context, Entity, Focusable as _, InteractiveElement as _, IntoElement, ParentElement as _, Result, SharedString, Styled as _, Subscription, Task, Window, div, px}; +use lsp_types::{CompletionContext, CompletionItem, CompletionItemKind, CompletionResponse, CompletionTextEdit, Documentation, Position, Range, TextEdit}; + +use super::preview_pane::PreviewPane; +use super::window::{Page, StudioWindow}; +use crate::completion::{ApiReference, Kind}; +use crate::strings::{t, tf}; + +pub(super) struct EditorSession { + /// The folder name of the widget. + pub name: String, + files: Vec, + /// The file that is open. + pub path: String, + /// `None` for a file that is not text. + editor: Option>, + pub dirty: bool, + /// The outcome of the last save, shown beside the file name. + status: Option<(bool, String)>, + pub pane: PreviewPane, + _changes: Option, +} + +impl EditorSession { + /// The text of the open file when it differs from what is saved, for the preview to lay over the files on disk. + pub fn unsaved(&self, cx: &App) -> Vec<(String, String)> { + match &self.editor { + Some(editor) if self.dirty => vec![(self.path.clone(), editor.read(cx).value().to_string())], + _ => Vec::new(), + } + } +} + +/// Feeds the editor's completion menu from [`ApiReference`]. +struct ApiCompletions(Arc); + +impl CompletionProvider for ApiCompletions { + fn completions(&self, text: &Rope, offset: usize, _: CompletionContext, _: &mut Window, _: &mut App) -> Task> { + let text = text.to_string(); + let Some(completions) = self.0.complete(&text, offset) else { return Task::ready(Ok(CompletionResponse::Array(Vec::new()))) }; + // The editor takes positions as line and UTF-16 column. + let position = |byte: usize| { + let before = &text[..byte]; + let line_start = before.rfind('\n').map_or(0, |index| index + 1); + Position::new(before.matches('\n').count() as u32, before[line_start..].encode_utf16().count() as u32) + }; + let range = Range::new(position(completions.from), position(offset.min(text.len()))); + let items = completions + .items + .into_iter() + .map(|item| CompletionItem { + label: item.label, + detail: (!item.detail.is_empty()).then_some(item.detail), + documentation: (!item.documentation.is_empty()).then_some(Documentation::String(item.documentation)), + kind: Some(match item.kind { + Kind::Component => CompletionItemKind::CLASS, + Kind::Property => CompletionItemKind::PROPERTY, + Kind::Value => CompletionItemKind::ENUM_MEMBER, + Kind::Function => CompletionItemKind::FUNCTION, + Kind::Global => CompletionItemKind::VARIABLE, + }), + text_edit: Some(CompletionTextEdit::Edit(TextEdit { range, new_text: item.insert })), + ..Default::default() + }) + .collect(); + Task::ready(Ok(CompletionResponse::Array(items))) + } + + fn is_completion_trigger(&self, _: usize, new_text: &str, _: &mut App) -> bool { + ApiReference::is_trigger(new_text) + } +} + +fn is_script(path: &str) -> bool { + path.ends_with(".js") || path.ends_with(".jsx") || path.ends_with(".mjs") +} + +impl StudioWindow { + /// Opens a widget in the editor at `path`, or at its entry. + pub(super) fn open_editor(&mut self, name: &str, path: Option, window: &mut Window, cx: &mut Context) { + let (Some(card), Ok(files)) = (self.studio.widget(name).filter(|card| card.readable), self.studio.files(name)) else { + self.page = Page::Library; + self.reload_library(cx); + return self.notify_error(tf("editor.missing", &[name]), window, cx); + }; + let path = path.filter(|path| files.contains(path)).unwrap_or_else(|| self.studio.entry(&card.name)); + // The preview keeps its size and appearance while moving between the files of one widget. + let pane = match self.editor.take() { + Some(previous) if previous.name == card.name => previous.pane, + _ => PreviewPane::new(card.families.clone(), self.dark(cx)), + }; + let text = self.studio.read_text(&card.name, &path).ok().flatten(); + let editor = text.map(|text| { + let api = self.api.clone(); + let script = is_script(&path); + cx.new(|cx| { + let mut state = EditorState::new(window, cx).line_number(true).default_value(text); + if script { + state = state.language("javascript"); + state.lsp_mut().completion_provider = Some(Rc::new(ApiCompletions(api))); + } + state + }) + }); + let changes = editor.as_ref().map(|editor| { + cx.subscribe(editor, |this, _, event: &InputEvent, cx| { + if matches!(event, InputEvent::Change) { + this.text_changed(cx); + } + }) + }); + if let Some(editor) = &editor { + let focus = editor.focus_handle(cx); + window.defer(cx, move |window, cx| focus.focus(window, cx)); + } + self.editor = Some(EditorSession { name: card.name, files, path, editor, dirty: false, status: None, pane, _changes: changes }); + self.page = Page::Editor; + self.request_preview(true, cx); + } + + fn text_changed(&mut self, cx: &mut Context) { + if let Some(session) = &mut self.editor { + session.dirty = true; + session.status = None; + } + self.request_preview(false, cx); + } + + /// Saves the open file through the package store. + pub(super) fn save(&mut self, _: &mut Window, cx: &mut Context) { + let Some(session) = self.editor.as_mut().filter(|_| self.page == Page::Editor) else { return }; + let Some(editor) = &session.editor else { return }; + let text = editor.read(cx).value().to_string(); + match self.studio.save_text(&session.name, &session.path, &text) { + Ok(()) => { + session.dirty = false; + session.status = Some((true, t("editor.saved"))); + let name = session.name.clone(); + self.forget_thumbnail(&name); + // The files on disk now are what the preview shows; run it once more from them. + self.request_preview(true, cx); + } + Err(error) => session.status = Some((false, tf("editor.saveFailed", &[&error.0]))), + } + cx.notify(); + } + + fn open_file(&mut self, path: String, window: &mut Window, cx: &mut Context) { + let Some(session) = &self.editor else { return }; + if session.path == path { + return; + } + let name = session.name.clone(); + if !session.dirty { + return self.open_editor(&name, Some(path), window, cx); + } + // Each file is saved on its own: leaving one with changes asks first. + let this = cx.weak_entity(); + let current = session.path.clone(); + gpui_kit::component::WindowExt::open_alert_dialog(window, cx, move |alert, _, _| { + let (this, name, path) = (this.clone(), name.clone(), path.clone()); + alert.title(t("editor.discard.title")).description(tf("editor.discard.detail", &[¤t])).confirm().ok_text(t("editor.discard.confirm")).cancel_text(t("action.cancel")).on_ok(move |_, window, cx| { + let _ = this.update(cx, |this, cx| this.open_editor(&name, Some(path.clone()), window, cx)); + true + }) + }); + } + + pub(super) fn render_editor(&self, cx: &mut Context) -> impl IntoElement { + let Some(session) = &self.editor else { return div().into_any_element() }; + let theme = cx.theme(); + let (muted, border, danger, success) = (theme.muted_foreground, theme.border, theme.danger, theme.success); + let name = session.name.clone(); + let family = session.pane.family.clone(); + let status = match (&session.status, session.dirty) { + (Some((false, message)), _) => div().text_color(danger).child(message.clone()), + (_, true) => div().text_color(muted).child(t("editor.unsaved")), + (Some((true, message)), false) => div().text_color(success).child(message.clone()), + (None, false) => div(), + }; + let header = h_flex() + .p_3() + .gap_2() + .border_b_1() + .border_color(border) + .child(Button::new("editor-back").icon(IconName::ArrowLeft).ghost().tooltip(t("action.back")).accessibility_label(t("action.back")).on_click(cx.listener(|this, _, window, cx| this.back(window, cx)))) + .child( + v_flex() + .flex_1() + .min_w_0() + .child(div().font_semibold().truncate().child(SharedString::from(session.name.clone()))) + .child(h_flex().gap_2().text_sm().child(div().text_color(muted).truncate().child(SharedString::from(session.path.clone()))).child(status)), + ) + .child(Button::new("editor-export").label(t("action.export")).outline().on_click(cx.listener({ + let name = name.clone(); + move |this, _, window, cx| this.export(name.clone(), window, cx) + }))) + .child(Button::new("editor-add").label(t("action.addToDesktop")).outline().on_click(cx.listener(move |this, _, window, cx| this.add_to_desktop(name.clone(), Some(family.clone()), window, cx)))) + .child(Button::new("editor-save").label(t("action.save")).primary().tooltip(t("editor.saveHint")).on_click(cx.listener(|this, _, window, cx| this.save(window, cx)))); + + let files = v_flex().w(px(190.)).h_full().flex_shrink_0().border_r_1().border_color(border).child(div().px_3().py_2().text_sm().font_semibold().text_color(muted).child(t("editor.files"))).child( + div().id("editor-files").flex_1().min_h_0().overflow_y_scrollbar().child(v_flex().px_1().gap_0p5().children(session.files.iter().enumerate().map(|(index, path)| { + let target = path.clone(); + // File names come from the package: shown as text. + Button::new(("editor-file", index)).child(div().w_full().truncate().child(SharedString::from(path.clone()))).small().ghost().w_full().selected(*path == session.path).on_click(cx.listener(move |this, _, window, cx| this.open_file(target.clone(), window, cx))) + }))), + ); + let code = match &session.editor { + Some(editor) => div().flex_1().min_w_0().h_full().child(Editor::new(editor).bordered(false).aria_label(t("editor.code")).h_full()), + None => div().flex_1().min_w_0().h_full().p_6().text_color(muted).child(t("editor.binary")), + }; + v_flex().size_full().child(header).child(h_flex().flex_1().min_h_0().items_start().child(files).child(code).child(self.render_pane(&session.pane, cx))).into_any_element() + } +} diff --git a/Linux/crates/sw-studio/src/ui/library.rs b/Linux/crates/sw-studio/src/ui/library.rs new file mode 100644 index 00000000..d02f3fda --- /dev/null +++ b/Linux/crates/sw-studio/src/ui/library.rs @@ -0,0 +1,135 @@ +//! "My widgets": the installed widgets as cards with a small picture each. + +use gpui_kit::component::button::{Button, ButtonVariants as _}; +use gpui_kit::component::input::InputState; +use gpui_kit::component::notification::Notification; +use gpui_kit::component::scroll::ScrollableElement as _; +use gpui_kit::component::{ActiveTheme as _, StyledExt as _, Disableable as _, IconName, Sizable as _, WindowExt as _, h_flex, v_flex}; +use gpui_kit::prelude::FluentBuilder as _; +use gpui_kit::{AppContext as _, Context, InteractiveElement as _, IntoElement, ParentElement as _, SharedString, Styled as _, Window, div, img, px}; + +use super::preview_pane::Shown; +use super::window::{StudioWindow, Target}; +use crate::model::WidgetCard; +use crate::strings::{t, tf}; + +/// The inside of a card is as wide as a medium widget. +pub(super) const CARD_WIDTH: f32 = 329.0; +pub(super) const CARD_PICTURE_HEIGHT: f32 = 170.0; + +/// The frame around a card's picture, shared with the template cards. +pub(super) fn picture_frame(shown: Option<&Shown>, placeholder: impl IntoElement, background: gpui_kit::Hsla) -> impl IntoElement { + div().w(px(CARD_WIDTH)).h(px(CARD_PICTURE_HEIGHT)).flex().items_center().justify_center().rounded_lg().bg(background).map(|frame| match shown { + Some(shown) => { + let (width, height) = shown.fitted(CARD_WIDTH, CARD_PICTURE_HEIGHT); + frame.child(img(shown.image.clone()).w(px(width)).h(px(height))) + } + None => frame.child(placeholder), + }) +} + +impl StudioWindow { + pub(super) fn render_library(&self, cx: &mut Context) -> impl IntoElement { + let muted = cx.theme().muted_foreground; + let header = h_flex() + .justify_between() + .child(div().text_2xl().font_semibold().child(t("library.title"))) + .child( + h_flex() + .gap_2() + .child(Button::new("library-import").label(t("library.import")).outline().on_click(cx.listener(|this, _, window, cx| this.choose_import(window, cx)))) + .child(Button::new("library-new").icon(IconName::Plus).label(t("library.new")).primary().on_click(cx.listener(|this, _, window, cx| this.ask_new_widget(window, cx)))), + ); + let body = if let Some(error) = &self.library_error { + div().text_color(cx.theme().danger).child(tf("library.failed", &[error])).into_any_element() + } else if self.widgets.is_empty() { + v_flex() + .items_center() + .gap_2() + .pt_16() + .child(div().text_lg().font_semibold().child(t("library.empty.title"))) + .child(div().text_color(muted).child(t("library.empty.detail"))) + .child(Button::new("library-browse").label(t("library.empty.templates")).primary().on_click(cx.listener(|this, _, window, cx| this.navigate(Target::Templates, window, cx)))) + .into_any_element() + } else { + let cards: Vec<_> = self.widgets.iter().enumerate().map(|(index, card)| self.widget_card(index, card, cx)).collect(); + h_flex().flex_wrap().items_start().gap_4().children(cards).into_any_element() + }; + div().id("library").size_full().overflow_y_scrollbar().child(v_flex().p_6().gap_5().child(header).child(body)) + } + + fn widget_card(&self, index: usize, card: &WidgetCard, cx: &mut Context) -> gpui_kit::AnyElement { + let theme = cx.theme(); + let (muted, border, secondary, danger) = (theme.muted_foreground, theme.border, theme.secondary, theme.danger); + let name = card.name.clone(); + let detail = if card.readable { card.description.clone() } else { t("library.unreadable") }; + let (for_add, for_edit, for_export, for_remove) = (name.clone(), name.clone(), name.clone(), name); + let add = cx.listener(move |this, _: &gpui_kit::ClickEvent, window, cx| this.add_to_desktop(for_add.clone(), None, window, cx)); + let edit = cx.listener(move |this, _: &gpui_kit::ClickEvent, window, cx| this.navigate(Target::Editor(for_edit.clone()), window, cx)); + let export = cx.listener(move |this, _: &gpui_kit::ClickEvent, window, cx| this.export(for_export.clone(), window, cx)); + let remove = cx.listener(move |this, _: &gpui_kit::ClickEvent, window, cx| this.confirm_remove(for_remove.clone(), window, cx)); + v_flex() + .p_3() + .gap_2() + .border_1() + .border_color(border) + .rounded_xl() + .child(picture_frame(self.thumbnails.get(&format!("w/{}", card.name)), div(), secondary)) + // A widget's name and description come from its package: shown as text, one line each. + .child(div().w(px(CARD_WIDTH)).font_semibold().truncate().child(SharedString::from(card.name.clone()))) + .child(div().w(px(CARD_WIDTH)).h_5().text_sm().text_color(if card.readable { muted } else { danger }).truncate().child(SharedString::from(detail))) + .child( + h_flex() + .w(px(CARD_WIDTH)) + .gap_2() + .child(Button::new(("card-add", index)).label(t("action.addToDesktop")).small().primary().disabled(!card.readable).on_click(add)) + .child(Button::new(("card-edit", index)).label(t("action.edit")).small().outline().disabled(!card.readable).on_click(edit)) + .child(div().flex_1()) + .child( + Button::new(("card-export", index)) + .icon(IconName::ExternalLink) + .small() + .ghost() + .disabled(!card.readable) + .tooltip(t("action.export")) + .accessibility_label(tf("action.exportNamed", &[&card.name])) + .on_click(export), + ) + .child( + Button::new(("card-remove", index)) + .icon(IconName::Delete) + .small() + .ghost() + .tooltip(t("action.delete")) + .accessibility_label(tf("action.removeNamed", &[&card.name])) + .on_click(remove), + ), + ) + .into_any_element() + } + + fn ask_new_widget(&mut self, window: &mut Window, cx: &mut Context) { + let input = cx.new(|cx| InputState::new(window, cx).placeholder(t("dialog.new.hint")).default_value(t("dialog.new.default"))); + self.ask_text(t("dialog.new.title"), input, window, cx, |this, name, window, cx| match this.studio.new_widget(&name) { + Ok(created) => this.go(Target::Editor(created), window, cx), + Err(error) => this.notify_error(error.0, window, cx), + }); + } + + fn confirm_remove(&mut self, name: String, window: &mut Window, cx: &mut Context) { + let this = cx.weak_entity(); + window.open_alert_dialog(cx, move |alert, _, _| { + let (this, name) = (this.clone(), name.clone()); + alert.title(tf("dialog.delete.title", &[&name])).description(t("dialog.delete.detail")).confirm().ok_text(t("action.delete")).cancel_text(t("action.cancel")).on_ok(move |_, window, cx| { + let _ = this.update(cx, |this, cx| { + match this.studio.remove(&name) { + Ok(()) => window.push_notification(Notification::success(tf("toast.removed", &[&name])), cx), + Err(error) => this.notify_error(error.0, window, cx), + } + this.reload_library(cx); + }); + true + }) + }); + } +} diff --git a/Linux/crates/sw-studio/src/ui/mod.rs b/Linux/crates/sw-studio/src/ui/mod.rs new file mode 100644 index 00000000..32f2b403 --- /dev/null +++ b/Linux/crates/sw-studio/src/ui/mod.rs @@ -0,0 +1,60 @@ +//! The window, on GPUI and gpui-kit. One entity, [`window::StudioWindow`], +//! holds what is shown; each page is a file that draws its part of it. +//! Everything slow (compiling, running and drawing a package, reading and +//! writing archives, the `scriptwidget` child process) runs on background +//! threads and comes back as a message. + +mod editor; +mod library; +mod preview_pane; +mod settings; +mod templates; +mod window; + +use std::sync::Arc; + +use futures::channel::mpsc::UnboundedReceiver; +use gpui_kit::{App, AppContext as _, Bounds, KeyBinding, TitlebarOptions, WindowBounds, WindowOptions, px, size}; + +use crate::instance::Request; +use crate::launch::StartPage; +use crate::model::Studio; +use crate::strings::t; + +pub use window::StudioWindow; + +gpui_kit::actions!(studio, [Save, Back]); + +/// What the Wayland compositor and the desktop file know the window by. +pub const APP_ID: &str = "scriptwidget-studio"; + +/// Opens the window and runs until it is closed. `requests` carries what later starts of the program ask for. +pub fn run(studio: Arc, first: Request, page: Option, requests: UnboundedReceiver) { + gpui_kit::application().with_assets(gpui_kit::assets::Assets).run(move |cx: &mut App| { + gpui_kit::init(cx); + cx.bind_keys([KeyBinding::new("ctrl-s", Save, None), KeyBinding::new("alt-left", Back, None)]); + cx.on_window_closed(|cx, _| { + if cx.windows().is_empty() { + cx.quit(); + } + }) + .detach(); + cx.activate(true); + + let options = WindowOptions { + window_bounds: Some(WindowBounds::Windowed(Bounds::centered(None, size(px(1280.), px(800.)), cx))), + titlebar: Some(TitlebarOptions { title: Some(t("app.title").into()), ..Default::default() }), + app_id: Some(APP_ID.to_owned()), + window_min_size: Some(size(px(960.), px(600.))), + ..Default::default() + }; + let opened = gpui_kit::open_window(options, cx, move |window, cx| { + window.set_window_title(&t("app.title")); + cx.new(|cx| StudioWindow::new(studio, first, page, requests, window, cx)) + }); + if let Err(error) = opened { + eprintln!("{}", crate::strings::tf("startup.failed", &[&error.to_string()])); + cx.quit(); + } + }); +} diff --git a/Linux/crates/sw-studio/src/ui/preview_pane.rs b/Linux/crates/sw-studio/src/ui/preview_pane.rs new file mode 100644 index 00000000..06194822 --- /dev/null +++ b/Linux/crates/sw-studio/src/ui/preview_pane.rs @@ -0,0 +1,141 @@ +//! The preview beside the code: the widget as `sw-render` draws it at a +//! chosen size, and under it what the script printed or why it failed. + +use std::sync::Arc; + +use gpui_kit::component::button::{Button, ButtonVariants as _}; +use gpui_kit::component::scroll::ScrollableElement as _; +use gpui_kit::component::{ActiveTheme as _, StyledExt as _, IconName, Selectable as _, Sizable as _, h_flex, v_flex}; +use gpui_kit::prelude::FluentBuilder as _; +use gpui_kit::{Context, InteractiveElement as _, IntoElement, ParentElement as _, RenderImage, SharedString, Styled as _, div, img, px}; +use image::{Frame, RgbaImage}; +use smallvec::SmallVec; + +use super::window::StudioWindow; +use crate::model::{Picture, PreviewResult}; +use crate::strings::{self, t, tf}; + +/// The widest a preview is shown; a wider size is scaled down to it. +pub const PANE_WIDTH: f32 = 372.0; + +/// A picture GPUI can draw, with its size in points. +#[derive(Clone)] +pub struct Shown { + pub image: Arc, + pub width: f32, + pub height: f32, +} + +impl Shown { + /// Hands the pixels to GPUI as they are (it wants BGRA), without encoding them. + pub fn from_picture(picture: Picture) -> Option { + let Picture { mut pixels, pixel_width, pixel_height, width, height } = picture; + for pixel in pixels.chunks_exact_mut(4) { + pixel.swap(0, 2); + } + let buffer = RgbaImage::from_raw(pixel_width, pixel_height, pixels)?; + Some(Self { image: Arc::new(RenderImage::new(SmallVec::from_elem(Frame::new(buffer), 1))), width, height }) + } + + /// The size to draw at so that it fits `maximum_width` and `maximum_height`. + pub fn fitted(&self, maximum_width: f32, maximum_height: f32) -> (f32, f32) { + let factor = (maximum_width / self.width).min(maximum_height / self.height).min(1.0); + (self.width * factor, self.height * factor) + } +} + +pub struct PreviewPane { + pub family: String, + pub families: Vec<&'static str>, + pub dark: bool, + pub shown: Option, + pub error: Option<(String, String)>, + pub logs: Vec<(String, String)>, + pub elapsed: Option, + pub running: bool, + /// The request whose result this pane is waiting for. + pub ticket: u64, +} + +impl PreviewPane { + pub fn new(families: Vec<&'static str>, dark: bool) -> Self { + let family = if families.contains(&"medium") { "medium" } else { families.first().copied().unwrap_or("medium") }; + Self { family: family.to_owned(), families, dark, shown: None, error: None, logs: Vec::new(), elapsed: None, running: false, ticket: 0 } + } + + /// Takes a result; returns the picture it replaces, for the caller to release. + pub fn take(&mut self, result: PreviewResult) -> Option { + self.running = false; + self.error = result.error; + self.logs = result.logs; + self.elapsed = Some(result.elapsed_milliseconds); + let shown = result.picture.and_then(Shown::from_picture); + std::mem::replace(&mut self.shown, shown) + } +} + +impl StudioWindow { + pub(super) fn render_pane(&self, pane: &PreviewPane, cx: &mut Context) -> impl IntoElement { + let theme = cx.theme(); + let (muted, danger, border, secondary, mono) = (theme.muted_foreground, theme.danger, theme.border, theme.secondary, theme.mono_font_family.clone()); + let appearance = if pane.dark { t("editor.dark") } else { t("editor.light") }; + + let sizes = h_flex().gap_1().flex_wrap().children(pane.families.iter().map(|family| { + let family = *family; + Button::new(SharedString::from(format!("family-{family}"))) + .label(strings::family(family)) + .small() + .map(|button| if pane.family == family { button.primary() } else { button.outline() }) + .selected(pane.family == family) + .on_click(cx.listener(move |this, _, _, cx| this.set_preview_family(family, cx))) + })); + + let picture = div().w_full().h(px(250.)).flex().items_center().justify_center().rounded_lg().bg(secondary).map(|area| match &pane.shown { + Some(shown) => { + let (width, height) = shown.fitted(PANE_WIDTH - 24.0, 226.0); + area.child(img(shown.image.clone()).w(px(width)).h(px(height))) + } + None => area.child(div().text_sm().text_color(muted).child(if pane.running { t("editor.running") } else { String::new() })), + }); + + let status = match (&pane.error, pane.elapsed) { + (Some((_, message)), _) => div().text_sm().text_color(danger).child(message.clone()), + (None, _) if pane.running && pane.elapsed.is_none() => div().text_sm().text_color(muted).child(t("editor.running")), + (None, Some(elapsed)) => div().text_sm().text_color(muted).child(tf("editor.ok", &[&elapsed.to_string()])), + (None, None) => div(), + }; + let logs = if pane.logs.is_empty() { + div().text_sm().text_color(muted).child(t("editor.noLogs")) + } else { + v_flex().gap_0p5().children(pane.logs.iter().map(|(level, message)| { + let color = if level == "error" { danger } else { cx.theme().foreground }; + // What a script printed is text and nothing else. + div().text_sm().font_family(mono.clone()).text_color(color).child(SharedString::from(message.clone())) + })) + }; + + v_flex() + .w(px(PANE_WIDTH)) + .h_full() + .flex_shrink_0() + .p_3() + .gap_3() + .border_l_1() + .border_color(border) + .child( + h_flex().justify_between().child(div().font_semibold().child(t("editor.preview"))).child( + Button::new("preview-appearance") + .icon(if pane.dark { IconName::Moon } else { IconName::Sun }) + .small() + .ghost() + .tooltip(tf("editor.appearance", &[&appearance])) + .accessibility_label(tf("editor.appearance", &[&appearance])) + .on_click(cx.listener(|this, _, _, cx| this.toggle_preview_appearance(cx))), + ), + ) + .child(sizes) + .child(picture) + .child(div().text_sm().font_semibold().text_color(muted).child(t("editor.logs"))) + .child(div().id("preview-output").flex_1().min_h_0().overflow_y_scrollbar().child(v_flex().gap_2().child(status).child(logs))) + } +} diff --git a/Linux/crates/sw-studio/src/ui/settings.rs b/Linux/crates/sw-studio/src/ui/settings.rs new file mode 100644 index 00000000..42f545f0 --- /dev/null +++ b/Linux/crates/sw-studio/src/ui/settings.rs @@ -0,0 +1,42 @@ +//! "Settings": the refresh interval, where the data is, and what the program is made of. + +use gpui_kit::component::button::{Button, ButtonVariants as _}; +use gpui_kit::component::scroll::ScrollableElement as _; +use gpui_kit::component::{ActiveTheme as _, StyledExt as _, Selectable as _, Sizable as _, h_flex, v_flex}; +use gpui_kit::prelude::FluentBuilder as _; +use gpui_kit::{Context, InteractiveElement as _, IntoElement, ParentElement as _, SharedString, Styled as _, Window, div}; + +use super::window::StudioWindow; +use crate::settings::INTERVALS; +use crate::strings::{self, t, tf}; + +const WEBSITE: &str = "https://atomsprout.com/scriptwidget/"; + +impl StudioWindow { + fn set_refresh(&mut self, minutes: u32, window: &mut Window, cx: &mut Context) { + match self.settings.set_refresh_minutes(minutes) { + Ok(()) => self.refresh_minutes = minutes, + Err(error) => self.notify_error(tf("settings.failed", &[&error.to_string()]), window, cx), + } + cx.notify(); + } + + pub(super) fn render_settings(&self, cx: &mut Context) -> impl IntoElement { + let theme = cx.theme(); + let (muted, border) = (theme.muted_foreground, theme.border); + let section = || v_flex().p_4().gap_2().border_1().border_color(border).rounded_xl(); + let intervals = h_flex().gap_1().flex_wrap().children(INTERVALS.iter().map(|minutes| { + let minutes = *minutes; + Button::new(("refresh", minutes as usize)).label(strings::interval(minutes)).small().map(|button| if self.refresh_minutes == minutes { button.primary() } else { button.outline() }).selected(self.refresh_minutes == minutes).on_click(cx.listener(move |this, _, window, cx| this.set_refresh(minutes, window, cx))) + })); + let refresh = section().child(div().font_semibold().child(t("settings.refresh"))).child(div().text_sm().text_color(muted).child(t("settings.refresh.detail"))).child(intervals); + let data = section().child(div().font_semibold().child(t("settings.data"))).child(div().text_sm().text_color(muted).child(SharedString::from(self.studio.paths().root().display().to_string()))); + let about = section() + .child(div().font_semibold().child(t("settings.about"))) + .child(div().child(tf("settings.version", &[crate::VERSION]))) + .child(div().text_sm().text_color(muted).child(t("settings.licence"))) + .child(div().text_sm().text_color(muted).child(t("settings.licences"))) + .child(h_flex().child(Button::new("settings-website").label(t("settings.website")).small().link().on_click(cx.listener(|_, _, _, cx| cx.open_url(WEBSITE))))); + div().id("settings").size_full().overflow_y_scrollbar().child(v_flex().p_6().gap_4().max_w(gpui_kit::px(760.)).child(div().text_2xl().font_semibold().child(t("settings.title"))).child(refresh).child(data).child(about)) + } +} diff --git a/Linux/crates/sw-studio/src/ui/templates.rs b/Linux/crates/sw-studio/src/ui/templates.rs new file mode 100644 index 00000000..5d967ab0 --- /dev/null +++ b/Linux/crates/sw-studio/src/ui/templates.rs @@ -0,0 +1,108 @@ +//! "Templates": the bundled templates in their groups, and one template +//! opened with its preview beside its code. + +use gpui_kit::component::button::{Button, ButtonVariants as _}; +use gpui_kit::component::input::{Editor, EditorState}; +use gpui_kit::component::scroll::ScrollableElement as _; +use gpui_kit::component::{ActiveTheme as _, StyledExt as _, IconName, h_flex, v_flex}; +use gpui_kit::{AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, ParentElement as _, SharedString, StatefulInteractiveElement as _, Styled as _, Window, div, px}; + +use super::library::{CARD_WIDTH, picture_frame}; +use super::preview_pane::PreviewPane; +use super::window::{Page, StudioWindow, Target}; +use crate::model::{TemplateCard, TemplateDetail}; +use crate::strings::{t, tf}; + +pub(super) struct TemplateSession { + pub detail: TemplateDetail, + code: Entity, + pub pane: PreviewPane, +} + +impl StudioWindow { + pub(super) fn render_templates(&self, cx: &mut Context) -> impl IntoElement { + let muted = cx.theme().muted_foreground; + let mut page = v_flex().p_6().gap_5().child(v_flex().gap_1().child(div().text_2xl().font_semibold().child(t("templates.title"))).child(div().text_color(muted).child(t("templates.detail")))); + if self.template_groups.is_empty() { + page = page.child(div().pt_8().text_color(muted).child(t("templates.none"))); + } + let mut index = 0; + for group in &self.template_groups { + let cards: Vec<_> = group + .templates + .iter() + .map(|card| { + index += 1; + self.template_card(index, card, cx) + }) + .collect(); + page = page.child(v_flex().gap_3().child(div().text_lg().font_semibold().child(t(&format!("templates.group.{}", group.group)))).child(h_flex().flex_wrap().items_start().gap_4().children(cards))); + } + div().id("templates").size_full().overflow_y_scrollbar().child(page) + } + + fn template_card(&self, index: usize, card: &TemplateCard, cx: &mut Context) -> gpui_kit::AnyElement { + let theme = cx.theme(); + let (muted, border, secondary, hover) = (theme.muted_foreground, theme.border, theme.secondary, theme.accent); + let reference = card.reference.clone(); + let placeholder = if card.network { h_flex().gap_1().text_sm().text_color(muted).child(IconName::Globe).child(t("templates.network")) } else { h_flex() }; + v_flex() + .id(("template", index)) + .p_3() + .gap_2() + .border_1() + .border_color(border) + .rounded_xl() + .cursor_pointer() + .hover(move |style| style.bg(hover)) + .tab_index(0) + .aria_label(SharedString::from(card.name.clone())) + .on_click(cx.listener(move |this, _, window, cx| this.navigate(Target::Template(reference.clone()), window, cx))) + .child(picture_frame(self.thumbnails.get(&format!("t/{}", card.reference)), placeholder, secondary)) + .child(div().w(px(CARD_WIDTH)).font_semibold().truncate().child(SharedString::from(card.name.clone()))) + .into_any_element() + } + + pub(super) fn open_template(&mut self, reference: &str, window: &mut Window, cx: &mut Context) { + let Some(detail) = self.studio.template(reference) else { + self.page = Page::Templates; + return; + }; + let source = detail.source.clone(); + let code = cx.new(|cx| EditorState::new(window, cx).language("javascript").line_number(true).default_value(source)); + self.template = Some(TemplateSession { detail, code, pane: PreviewPane::new(sw_core::library::FAMILIES.to_vec(), self.dark(cx)) }); + self.page = Page::Template; + self.request_preview(true, cx); + } + + fn use_template(&mut self, window: &mut Window, cx: &mut Context) { + let Some(reference) = self.template.as_ref().map(|session| session.detail.reference.clone()) else { return }; + match self.studio.use_template(&reference) { + Ok(name) => self.go(Target::Editor(name), window, cx), + Err(error) => self.notify_error(error.0, window, cx), + } + } + + pub(super) fn render_template(&self, cx: &mut Context) -> impl IntoElement { + let Some(session) = &self.template else { return div().into_any_element() }; + let theme = cx.theme(); + let (muted, border) = (theme.muted_foreground, theme.border); + let network = if session.detail.hosts.is_empty() { t("detail.noNetwork") } else { tf("detail.network", &[&session.detail.hosts.join(", ")]) }; + let about = if session.detail.description.is_empty() { network } else { format!("{} · {network}", session.detail.description) }; + let header = h_flex() + .p_3() + .gap_3() + .border_b_1() + .border_color(border) + .child(Button::new("template-back").icon(IconName::ArrowLeft).ghost().tooltip(t("action.back")).accessibility_label(t("action.back")).on_click(cx.listener(|this, _, window, cx| this.back(window, cx)))) + .child(v_flex().flex_1().min_w_0().child(div().font_semibold().truncate().child(SharedString::from(session.detail.name.clone()))).child(div().text_sm().text_color(muted).truncate().child(SharedString::from(about)))) + .child(Button::new("template-use").label(t("action.use")).primary().on_click(cx.listener(|this, _, window, cx| this.use_template(window, cx)))); + let code = v_flex() + .flex_1() + .min_w_0() + .h_full() + .child(h_flex().px_3().py_2().justify_between().child(div().font_semibold().child(t("detail.code"))).child(div().text_sm().text_color(muted).child(t("detail.readOnly")))) + .child(div().flex_1().min_h_0().child(Editor::new(&session.code).readonly(true).bordered(false).aria_label(t("detail.code")).h_full())); + v_flex().size_full().child(header).child(h_flex().flex_1().min_h_0().items_start().child(code).child(self.render_pane(&session.pane, cx))).into_any_element() + } +} diff --git a/Linux/crates/sw-studio/src/ui/window.rs b/Linux/crates/sw-studio/src/ui/window.rs new file mode 100644 index 00000000..a11fba51 --- /dev/null +++ b/Linux/crates/sw-studio/src/ui/window.rs @@ -0,0 +1,546 @@ +//! The window's state, its navigation and what the pages share. + +use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::Arc; +use std::time::Duration; + +use futures::StreamExt as _; +use futures::channel::mpsc::{UnboundedReceiver, unbounded}; +use gpui_kit::component::button::{Button, ButtonVariants as _}; +use gpui_kit::component::input::{Input, InputState}; +use gpui_kit::component::notification::Notification; +use gpui_kit::component::{ActiveTheme as _, IconName, Selectable as _, Theme, WindowExt as _, h_flex, v_flex}; +use gpui_kit::{App, AppContext as _, Context, Entity, FocusHandle, Focusable, InteractiveElement as _, IntoElement, ParentElement as _, Render, SharedString, Styled as _, Subscription, Task, Window, div, px}; + +use super::editor::EditorSession; +use super::preview_pane::{PreviewPane, Shown}; +use super::templates::TemplateSession; +use super::{Back, Save}; +use crate::completion::ApiReference; +use crate::debounce::Debouncer; +use crate::instance::Request; +use crate::launch::StartPage; +use crate::model::{PreviewRequest, PreviewResult, Studio, TemplateGroup, WidgetCard}; +use crate::service::{self, AddOutcome}; +use crate::settings::Settings; +use crate::strings::{t, tf}; + +/// How long after the last key the preview runs again. +const PREVIEW_DELAY: Duration = Duration::from_millis(400); +const ADD_LIMIT: Duration = Duration::from_secs(20); + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(super) enum Page { + Library, + Templates, + Template, + Editor, + Settings, +} + +/// Where a navigation goes. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(super) enum Target { + Library, + Templates, + Settings, + Template(String), + Editor(String), +} + +pub struct StudioWindow { + pub(super) studio: Arc, + pub(super) api: Arc, + pub(super) settings: Settings, + cli: PathBuf, + pub(super) page: Page, + pub(super) widgets: Vec, + pub(super) library_error: Option, + pub(super) template_groups: Vec, + /// Small pictures of widgets ("w/") and templates ("t/"). + pub(super) thumbnails: HashMap, + thumbnail_run: u64, + pub(super) template: Option, + pub(super) editor: Option, + pub(super) previews: Debouncer, + /// Pictures that are no longer shown; their textures are released at the next frame. + retired: Vec, + pub(super) scale: f32, + pub(super) refresh_minutes: u32, + focus: FocusHandle, + _subscriptions: Vec, + _tasks: Vec>, +} + +impl StudioWindow { + pub fn new(studio: Arc, first: Request, page: Option, requests: UnboundedReceiver, window: &mut Window, cx: &mut Context) -> Self { + Theme::sync_system_appearance(Some(window), cx); + let appearance = cx.observe_window_appearance(window, |_, window, cx| Theme::sync_system_appearance(Some(window), cx)); + + // Previews are made on a thread of their own and come back here. + let (results, mut arrived) = unbounded::<(u64, PreviewResult)>(); + let engine = studio.clone(); + let previews = Debouncer::start(PREVIEW_DELAY, move |request: PreviewRequest| engine.preview(&request), move |ticket, result| { + let _ = results.unbounded_send((ticket, result)); + }); + let preview_task = cx.spawn(async move |this, cx| { + while let Some((ticket, result)) = arrived.next().await { + if this.update(cx, |this, cx| this.preview_arrived(ticket, result, cx)).is_err() { + break; + } + } + }); + // What later starts of the program ask for. + let mut requests = requests; + let request_task = cx.spawn_in(window, async move |this, cx| { + while let Some(request) = requests.next().await { + let handled = this.update_in(cx, |this, window, cx| { + window.activate_window(); + this.handle_request(request, window, cx); + }); + if handled.is_err() { + break; + } + } + }); + + let settings = Settings::new(studio.paths().settings()); + let mut this = Self { + api: Arc::new(ApiReference::embedded()), + refresh_minutes: settings.selected_interval(), + settings, + cli: service::command_line(|name| std::env::var_os(name), std::env::current_exe().ok().as_deref()), + page: Page::Library, + widgets: Vec::new(), + library_error: None, + template_groups: studio.template_groups(), + thumbnails: HashMap::new(), + thumbnail_run: 0, + template: None, + editor: None, + previews, + retired: Vec::new(), + scale: window.scale_factor(), + focus: cx.focus_handle(), + studio, + _subscriptions: vec![appearance], + _tasks: vec![preview_task, request_task], + }; + this.reload_library(cx); + match page { + Some(StartPage::Library) | None => {} + Some(StartPage::Templates) => this.go(Target::Templates, window, cx), + Some(StartPage::Settings) => this.go(Target::Settings, window, cx), + Some(StartPage::Template(reference)) => this.go(Target::Template(reference), window, cx), + Some(StartPage::Editor(name)) => this.go(Target::Editor(name), window, cx), + } + this.handle_request(first, window, cx); + this + } + + pub(super) fn handle_request(&mut self, request: Request, window: &mut Window, cx: &mut Context) { + match request { + Request::Show => {} + Request::Edit(name) => self.navigate(Target::Editor(name), window, cx), + Request::Open(path) => self.offer_import(path, window, cx), + } + } + + // Navigation + + /// Goes to `target`, asking first when the editor holds unsaved changes. + pub(super) fn navigate(&mut self, target: Target, window: &mut Window, cx: &mut Context) { + let unsaved = self.editor.as_ref().filter(|session| self.page == Page::Editor && session.dirty).map(|session| session.path.clone()); + let Some(path) = unsaved else { return self.go(target, window, cx) }; + let this = cx.weak_entity(); + window.open_alert_dialog(cx, move |alert, _, _| { + let (this, target) = (this.clone(), target.clone()); + alert.title(t("editor.discard.title")).description(tf("editor.discard.detail", &[&path])).confirm().ok_text(t("editor.discard.confirm")).cancel_text(t("action.cancel")).on_ok(move |_, window, cx| { + let _ = this.update(cx, |this, cx| this.go(target.clone(), window, cx)); + true + }) + }); + } + + /// Goes to `target` without asking. + pub(super) fn go(&mut self, target: Target, window: &mut Window, cx: &mut Context) { + if let Some(session) = self.editor.take() { + self.retired.extend(session.pane.shown); + } + if let Some(session) = self.template.take() { + self.retired.extend(session.pane.shown); + } + match target { + Target::Library => { + self.page = Page::Library; + self.reload_library(cx); + } + Target::Templates => { + self.page = Page::Templates; + self.make_thumbnails(cx); + } + Target::Settings => { + self.refresh_minutes = self.settings.selected_interval(); + self.page = Page::Settings; + } + Target::Template(reference) => self.open_template(&reference, window, cx), + Target::Editor(name) => self.open_editor(&name, None, window, cx), + } + self.focus.focus(window, cx); + cx.notify(); + } + + pub(super) fn dark(&self, cx: &App) -> bool { + cx.theme().is_dark() + } + + // The list of widgets and the small pictures + + pub(super) fn reload_library(&mut self, cx: &mut Context) { + match self.studio.widgets() { + Ok(widgets) => { + self.widgets = widgets; + self.library_error = None; + } + Err(error) => { + self.widgets.clear(); + self.library_error = Some(error.0); + } + } + let listed: Vec = self.widgets.iter().map(|card| format!("w/{}", card.name)).collect(); + let gone: Vec = self.thumbnails.keys().filter(|key| key.starts_with("w/") && !listed.contains(key)).cloned().collect(); + for key in gone { + self.retired.extend(self.thumbnails.remove(&key)); + } + self.make_thumbnails(cx); + cx.notify(); + } + + /// Forgets the picture of a widget whose files changed. + pub(super) fn forget_thumbnail(&mut self, name: &str) { + self.retired.extend(self.thumbnails.remove(&format!("w/{name}"))); + } + + /// Draws the pictures the current page still lacks, one after the other, off this thread. + pub(super) fn make_thumbnails(&mut self, cx: &mut Context) { + use crate::model::PreviewSource; + let dark = self.dark(cx); + let mut wanted: Vec<(String, PreviewRequest)> = Vec::new(); + let request = |source, family: &str| PreviewRequest { source, family: family.to_owned(), dark, scale: self.scale }; + match self.page { + Page::Templates => { + // A template that asks the network is drawn only when it is opened. + for card in self.template_groups.iter().flat_map(|group| &group.templates).filter(|card| !card.network) { + wanted.push((format!("t/{}", card.reference), request(PreviewSource::Bundled(card.reference.clone()), "medium"))); + } + } + _ => { + for card in self.widgets.iter().filter(|card| card.readable) { + wanted.push((format!("w/{}", card.name), request(PreviewSource::Installed { name: card.name.clone(), edits: vec![] }, card.card_family()))); + } + } + } + wanted.retain(|(key, _)| !self.thumbnails.contains_key(key)); + self.thumbnail_run += 1; + let run = self.thumbnail_run; + let studio = self.studio.clone(); + cx.spawn(async move |this, cx| { + for (key, request) in wanted { + let engine = studio.clone(); + let result = cx.background_executor().spawn(async move { engine.preview(&request) }).await; + let keep_going = this.update(cx, |this, cx| { + if let Some(shown) = result.picture.and_then(Shown::from_picture) { + this.retired.extend(this.thumbnails.insert(key, shown)); + cx.notify(); + } + // A newer run has taken over when the page changed. + this.thumbnail_run == run + }); + if !matches!(keep_going, Ok(true)) { + break; + } + } + }) + .detach(); + } + + // Previews of the open template or widget + + pub(super) fn pane_mut(&mut self) -> Option<&mut PreviewPane> { + match self.page { + Page::Editor => self.editor.as_mut().map(|session| &mut session.pane), + Page::Template => self.template.as_mut().map(|session| &mut session.pane), + _ => None, + } + } + + /// Asks for the preview of what is open: after a pause while typing, or at once. + pub(super) fn request_preview(&mut self, now: bool, cx: &mut Context) { + use crate::model::PreviewSource; + let source = match self.page { + Page::Editor => self.editor.as_ref().map(|session| PreviewSource::Installed { name: session.name.clone(), edits: session.unsaved(cx) }), + Page::Template => self.template.as_ref().map(|session| PreviewSource::Bundled(session.detail.reference.clone())), + _ => None, + }; + let scale = self.scale; + let Some((source, pane)) = source.zip(self.pane_mut()) else { return }; + let request = PreviewRequest { source, family: pane.family.clone(), dark: pane.dark, scale }; + pane.running = true; + let ticket = if now { self.previews.request_now(request) } else { self.previews.request(request) }; + if let Some(pane) = self.pane_mut() { + pane.ticket = ticket; + } + cx.notify(); + } + + fn preview_arrived(&mut self, ticket: u64, result: PreviewResult, cx: &mut Context) { + // A result for something that is no longer open, or that was typed over, is dropped. + let Some(pane) = self.pane_mut().filter(|pane| pane.ticket == ticket) else { return }; + let replaced = pane.take(result); + self.retired.extend(replaced); + cx.notify(); + } + + pub(super) fn set_preview_family(&mut self, family: &'static str, cx: &mut Context) { + if let Some(pane) = self.pane_mut() { + pane.family = family.to_owned(); + } + self.request_preview(true, cx); + } + + pub(super) fn toggle_preview_appearance(&mut self, cx: &mut Context) { + if let Some(pane) = self.pane_mut() { + pane.dark = !pane.dark; + } + self.request_preview(true, cx); + } + + // What several pages do + + pub(super) fn notify_error(&self, message: impl Into, window: &mut Window, cx: &mut App) { + window.push_notification(Notification::error(message).title(t("dialog.error.title")), cx); + } + + /// Asks the background service to place a widget, through the `scriptwidget` command. + pub(super) fn add_to_desktop(&mut self, name: String, family: Option, window: &mut Window, cx: &mut Context) { + let cli = self.cli.clone(); + cx.spawn_in(window, async move |this, cx| { + let outcome = cx.background_executor().spawn(async move { service::add_to_desktop(&cli, &name, family.as_deref(), ADD_LIMIT) }).await; + let _ = this.update_in(cx, |this, window, cx| match outcome { + AddOutcome::Added => window.push_notification(Notification::success(t("toast.added")), cx), + AddOutcome::ServiceNotRunning => window.push_notification(Notification::warning(t("toast.hostMissing")), cx), + AddOutcome::CommandMissing => this.notify_error(t("toast.cliMissing"), window, cx), + AddOutcome::Failed(reason) => this.notify_error(tf("toast.addFailed", &[&reason]), window, cx), + }); + }) + .detach(); + } + + /// Asks where to save, then writes the `.swt` file. + pub(super) fn export(&mut self, name: String, window: &mut Window, cx: &mut Context) { + let folder = downloads_folder(); + let suggested = format!("{name}.swt"); + let chosen = cx.prompt_for_new_path(&folder, Some(&suggested)); + cx.spawn_in(window, async move |this, cx| { + let target = match chosen.await { + Ok(Ok(Some(target))) => target, + Ok(Ok(None)) => return, + // No file chooser on this desktop: ask for the path in the window. + Ok(Err(_)) | Err(_) => { + let _ = this.update_in(cx, |this, window, cx| { + let title = tf("dialog.path.export", &[&name]); + this.ask_path(title, t("dialog.path.export.hint"), folder.join(&suggested), window, cx, move |this, target, window, cx| this.write_export(name.clone(), target, window, cx)); + }); + return; + } + }; + let _ = this.update_in(cx, |this, window, cx| this.write_export(name, target, window, cx)); + }) + .detach(); + } + + fn write_export(&mut self, name: String, target: PathBuf, window: &mut Window, cx: &mut Context) { + let studio = self.studio.clone(); + let target = if target.extension().is_some() { target } else { target.with_extension("swt") }; + cx.spawn_in(window, async move |this, cx| { + let written = target.clone(); + let result = cx.background_executor().spawn(async move { studio.export(&name, &written) }).await; + let _ = this.update_in(cx, |this, window, cx| match result { + Ok(()) => window.push_notification(Notification::success(tf("toast.exported", &[&target.display().to_string()])), cx), + Err(error) => this.notify_error(error.0, window, cx), + }); + }) + .detach(); + } + + /// Asks which `.swt` file to import. + pub(super) fn choose_import(&mut self, window: &mut Window, cx: &mut Context) { + let chosen = cx.prompt_for_paths(gpui_kit::PathPromptOptions { files: true, directories: false, multiple: false, prompt: Some(t("dialog.import.confirm").into()) }); + cx.spawn_in(window, async move |this, cx| { + let _ = match chosen.await { + Ok(Ok(Some(paths))) => this.update_in(cx, |this, window, cx| { + if let Some(path) = paths.into_iter().next() { + this.offer_import(path, window, cx); + } + }), + Ok(Ok(None)) => Ok(()), + Ok(Err(_)) | Err(_) => this.update_in(cx, |this, window, cx| { + this.ask_path(t("dialog.path.import"), t("dialog.path.import.hint"), downloads_folder().join(""), window, cx, |this, path, window, cx| this.offer_import(path, window, cx)); + }), + }; + }) + .detach(); + } + + /// A dialog with one line for a path, for desktops without a file chooser. + fn ask_path(&mut self, title: String, hint: String, initial: PathBuf, window: &mut Window, cx: &mut Context, then: impl Fn(&mut Self, PathBuf, &mut Window, &mut Context) + 'static) { + let input = cx.new(|cx| InputState::new(window, cx).placeholder(hint.clone()).default_value(initial.display().to_string())); + self.ask_text(title, input, window, cx, move |this, text, window, cx| { + let path = PathBuf::from(text); + if path.is_absolute() { + then(this, path, window, cx); + } + }); + } + + /// A dialog with one text field; `then` receives the trimmed text when it is not empty. + pub(super) fn ask_text(&mut self, title: String, input: Entity, window: &mut Window, cx: &mut Context, then: impl Fn(&mut Self, String, &mut Window, &mut Context) + 'static) { + let this = cx.weak_entity(); + let then = std::rc::Rc::new(then); + let focused = std::rc::Rc::new(std::cell::Cell::new(false)); + window.open_alert_dialog(cx, move |alert, window, cx| { + if !focused.replace(true) { + input.focus_handle(cx).focus(window, cx); + } + let (this, then, field) = (this.clone(), then.clone(), input.clone()); + alert.title(title.clone()).child(Input::new(&input).aria_label(title.clone())).confirm().ok_text(t("action.ok")).cancel_text(t("action.cancel")).on_ok(move |_, window, cx| { + let text = field.read(cx).value().trim().to_owned(); + if text.is_empty() { + return false; + } + let _ = this.update(cx, |this, cx| then(this, text, window, cx)); + true + }) + }); + } + + /// Reads what a `.swt` file holds, shows it, and imports it when the user agrees. + pub(super) fn offer_import(&mut self, path: PathBuf, window: &mut Window, cx: &mut Context) { + let studio = self.studio.clone(); + cx.spawn_in(window, async move |this, cx| { + let file = path.clone(); + let summary = cx.background_executor().spawn(async move { studio.inspect(&file) }).await; + let _ = this.update_in(cx, |this, window, cx| { + let summary = match summary { + Ok(summary) => summary, + Err(error) => return this.notify_error(tf("dialog.import.refused", &[&error.0]), window, cx), + }; + let entity = cx.weak_entity(); + let mut lines = vec![tf("dialog.import.file", &[&path.display().to_string()]), tf("dialog.import.contents", &[&summary.files.to_string(), &crate::strings::bytes(summary.bytes)])]; + lines.push(if summary.hosts.is_empty() { t("detail.noNetwork") } else { tf("detail.network", &[&summary.hosts.join(", ")]) }); + window.open_alert_dialog(cx, move |alert, _, cx| { + let (entity, path) = (entity.clone(), path.clone()); + let muted = cx.theme().muted_foreground; + alert + .title(t("dialog.import.title")) + .description(tf("dialog.import.detail", &[&summary.name])) + // What the archive says about itself is shown as text. + .child(v_flex().gap_1().text_sm().text_color(muted).children(lines.iter().map(|line| div().child(SharedString::from(line.clone()))))) + .confirm() + .ok_text(t("dialog.import.confirm")) + .cancel_text(t("action.cancel")) + .on_ok(move |_, window, cx| { + let _ = entity.update(cx, |this, cx| this.import(path.clone(), window, cx)); + true + }) + }); + }); + }) + .detach(); + } + + fn import(&mut self, path: PathBuf, window: &mut Window, cx: &mut Context) { + let studio = self.studio.clone(); + cx.spawn_in(window, async move |this, cx| { + let result = cx.background_executor().spawn(async move { studio.import(&path) }).await; + let _ = this.update_in(cx, |this, window, cx| match result { + Ok(name) => { + window.push_notification(Notification::success(tf("toast.imported", &[&name])), cx); + if this.page == Page::Library { + this.reload_library(cx); + } else if this.page != Page::Editor { + this.go(Target::Library, window, cx); + } + } + Err(error) => this.notify_error(tf("dialog.import.refused", &[&error.0]), window, cx), + }); + }) + .detach(); + } + + fn sidebar(&self, cx: &mut Context) -> impl IntoElement { + let theme = cx.theme(); + let (background, border) = (theme.sidebar, theme.sidebar_border); + let entry = |id: &'static str, icon: IconName, label: String, selected: bool, target: Target, cx: &mut Context| { + Button::new(id).child(h_flex().w_full().gap_2().child(gpui_kit::component::Icon::new(icon)).child(label)).ghost().selected(selected).w_full().on_click(cx.listener(move |this, _, window, cx| this.navigate(target.clone(), window, cx))) + }; + let library = entry("nav-library", IconName::LayoutDashboard, t("nav.library"), matches!(self.page, Page::Library | Page::Editor), Target::Library, cx); + let templates = entry("nav-templates", IconName::GalleryVerticalEnd, t("nav.templates"), matches!(self.page, Page::Templates | Page::Template), Target::Templates, cx); + let settings = entry("nav-settings", IconName::Settings, t("nav.settings"), self.page == Page::Settings, Target::Settings, cx); + v_flex().w(px(208.)).h_full().flex_shrink_0().p_2().gap_1().bg(background).border_r_1().border_color(border).child(library).child(templates).child(div().flex_1()).child(settings) + } +} + +/// Where exports go by default: the user's download folder, or the home folder. +fn downloads_folder() -> PathBuf { + let home = std::env::var_os("HOME").map(PathBuf::from).filter(|path| path.is_absolute()).unwrap_or_else(|| PathBuf::from("/")); + let named = std::env::var_os("XDG_DOWNLOAD_DIR").map(PathBuf::from).filter(|path| path.is_absolute()); + named.or_else(|| Some(home.join("Downloads")).filter(|folder| folder.is_dir())).unwrap_or(home) +} + +impl Focusable for StudioWindow { + fn focus_handle(&self, _: &App) -> FocusHandle { + self.focus.clone() + } +} + +impl Render for StudioWindow { + fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { + for shown in self.retired.drain(..) { + cx.drop_image(shown.image, Some(window)); + } + self.scale = window.scale_factor(); + let theme = cx.theme(); + let (background, foreground) = (theme.background, theme.foreground); + let page = match self.page { + Page::Library => self.render_library(cx).into_any_element(), + Page::Templates => self.render_templates(cx).into_any_element(), + Page::Template => self.render_template(cx).into_any_element(), + Page::Editor => self.render_editor(cx).into_any_element(), + Page::Settings => self.render_settings(cx).into_any_element(), + }; + h_flex() + .id("studio") + .size_full() + .items_start() + .bg(background) + .text_color(foreground) + .track_focus(&self.focus) + .on_action(cx.listener(|this, _: &Save, window, cx| this.save(window, cx))) + .on_action(cx.listener(|this, _: &Back, window, cx| this.back(window, cx))) + .child(self.sidebar(cx)) + .child(div().flex_1().min_w_0().h_full().child(page)) + } +} + +impl StudioWindow { + /// The page above the one shown: the list a template or a widget was opened from. + pub(super) fn back(&mut self, window: &mut Window, cx: &mut Context) { + match self.page { + Page::Editor => self.navigate(Target::Library, window, cx), + Page::Template => self.navigate(Target::Templates, window, cx), + _ => {} + } + } +} diff --git a/Linux/omarchy-plugin/ArrangeWindow.qml b/Linux/omarchy-plugin/ArrangeWindow.qml new file mode 100644 index 00000000..684246ba --- /dev/null +++ b/Linux/omarchy-plugin/ArrangeWindow.qml @@ -0,0 +1,182 @@ +import QtQuick +import Quickshell +import Quickshell.Wayland +import qs.Commons as Commons + +// Arrange mode, one per screen: a window over the whole screen that shows the +// widgets of that screen and lets each be dragged, with a menu on right +// click. It takes the pointer only on the pictures, and on the whole screen +// while a button is held, so a fast drag cannot leave it: Hyprland stops +// sending pointer motion to a small surface once the pointer is outside it +// when no window has the keyboard, which is the usual state of an empty +// desktop. +PanelWindow { + id: canvas + + required property var modelData + required property var service + property bool held: false + + screen: modelData + visible: service.canvasShown + anchors { top: true; bottom: true; left: true; right: true } + color: "transparent" + exclusionMode: ExclusionMode.Ignore + WlrLayershell.namespace: "scriptwidget-arrange" + WlrLayershell.keyboardFocus: WlrKeyboardFocus.None + WlrLayershell.layer: service.peek ? WlrLayer.Overlay : WlrLayer.Bottom + + mask: held ? null : pictures + + Region { + id: pictures + regions: canvas.pictureRegions + } + + property list pictureRegions + + function collectRegions() { + var found = [] + for (var index = 0; index < regionMaker.count; index++) { + var region = regionMaker.objectAt(index) + if (region) found.push(region) + } + pictureRegions = found + } + + // Nothing is built, and no picture loaded, while the window is not shown. + readonly property var ids: !visible ? [] : service.ids.filter(function (id) { + var screen = canvas.service.screenFor(canvas.service.widgets[id] ? canvas.service.widgets[id].screen : "") + return !!screen && screen.name === canvas.modelData.name + }) + + Instantiator { + id: regionMaker + model: canvas.ids + delegate: Region { + required property string modelData + readonly property var widget: canvas.service.widgets[modelData] || canvas.service.noWidget + x: widget.x + y: widget.y + width: widget.width + height: widget.height + } + onObjectAdded: canvas.collectRegions() + onObjectRemoved: canvas.collectRegions() + } + + Repeater { + model: canvas.ids + + Item { + id: picture + + required property string modelData + readonly property var widget: canvas.service.widgets[modelData] || canvas.service.noWidget + + // Where the picture is drawn relative to the place the service last + // named, during a drag and until the service answers it. + property real offsetX: 0 + property real offsetY: 0 + property real knownX: 0 + property real knownY: 0 + + x: widget.x + offsetX + y: widget.y + offsetY + z: pointer.pressed ? 1 : 0 + width: widget.width + height: widget.height + + onWidgetChanged: { + if (pointer.moved) return + if (widget.x !== knownX || widget.y !== knownY) { + offsetX = 0 + offsetY = 0 + unanswered.stop() + } + knownX = widget.x + knownY = widget.y + } + + // The service answers a move with the widget's new place. If it does + // not, the picture goes back to where the service has it. + Timer { + id: unanswered + interval: 3000 + onTriggered: { picture.offsetX = 0; picture.offsetY = 0 } + } + + Image { + anchors.fill: parent + source: canvas.service.imageUrl(picture.widget.image, picture.widget.stamp) + } + + Rectangle { + anchors.fill: parent + color: "transparent" + radius: Math.min(width, height) * 0.13 + border.width: 2 + border.color: Commons.Color.accent + } + + MouseArea { + id: pointer + + property real startX: 0 + property real startY: 0 + property real startOffsetX: 0 + property real startOffsetY: 0 + property bool moved: false + + anchors.fill: parent + acceptedButtons: Qt.LeftButton | Qt.RightButton + cursorShape: Qt.SizeAllCursor + + onPressed: function (mouse) { + if (mouse.button !== Qt.LeftButton) return + var at = mapToItem(null, mouse.x, mouse.y) + startX = at.x + startY = at.y + startOffsetX = picture.offsetX + startOffsetY = picture.offsetY + moved = false + canvas.held = true + canvas.service.refreshKeepOut() + } + + onPositionChanged: function (mouse) { + if (!(mouse.buttons & Qt.LeftButton)) return + var at = mapToItem(null, mouse.x, mouse.y) + if (!moved && Math.abs(at.x - startX) + Math.abs(at.y - startY) < 6) return + moved = true + picture.offsetX = startOffsetX + at.x - startX + picture.offsetY = startOffsetY + at.y - startY + } + + onReleased: function (mouse) { + var widget = picture.widget + if (mouse.button === Qt.RightButton) { + var at = mapToItem(null, mouse.x, mouse.y) + canvas.service.openMenu(picture.modelData, canvas.modelData, at.x, at.y) + return + } + canvas.held = false + if (!moved) return + moved = false + var placed = canvas.service.place(picture.modelData, canvas.modelData, picture.x, picture.y, widget.width, widget.height) + if (placed.screen === canvas.modelData.name) { + picture.offsetX = placed.x - widget.x + picture.offsetY = placed.y - widget.y + } + unanswered.restart() + } + + onCanceled: { + canvas.held = false + moved = false + unanswered.restart() + } + } + } + } +} diff --git a/Linux/omarchy-plugin/BarWidget.qml b/Linux/omarchy-plugin/BarWidget.qml new file mode 100644 index 00000000..832f1278 --- /dev/null +++ b/Linux/omarchy-plugin/BarWidget.qml @@ -0,0 +1,125 @@ +import QtQuick +import qs.Commons +import qs.Commons as Commons +import qs.Ui + +// The bar entry. Left click opens ScriptWidget; right click opens a card with +// the two switches. It only reads a few values off the service and calls +// three of its functions, so it also works next to an older or newer service +// (the service survives a plugin reload, this widget does not). +BarWidget { + id: root + + readonly property string pluginId: "com.atomsprout.scriptwidget" + property var service: null + property bool popupOpen: false + + readonly property string status: service && typeof service.status === "string" ? service.status : "unavailable" + readonly property bool running: status === "running" + readonly property string statusText: running ? "" : strings.t("status." + status, strings.t("status.unavailable")) + + function findService() { + service = bar && bar.shell && typeof bar.shell.serviceFor === "function" ? bar.shell.serviceFor(pluginId) : null + } + + function close() { popupOpen = false } + + onBarChanged: findService() + Component.onCompleted: findService() + + // The service may be loaded after the bar, and is replaced when the plugin + // is turned off and on. + Timer { + interval: 2000 + running: !root.service + repeat: true + onTriggered: root.findService() + } + + Commons.Translations { + id: strings + directory: Qt.resolvedUrl("i18n") + } + + implicitWidth: button.implicitWidth + implicitHeight: button.implicitHeight + + BarIconButton { + id: button + anchors.fill: parent + bar: root.bar + text: "\u{F056E}" + dimmed: !root.running + active: root.status === "outdated" + tooltipText: root.running ? "ScriptWidget" : root.statusText + + onPressed: function (pressed) { + if (pressed === Qt.RightButton) { + root.popupOpen = !root.popupOpen + return + } + // Without the service there is nothing to open; say why instead. + if (root.running || root.status === "stopped") { + if (root.service) root.service.openStudio() + if (root.running) return + } + root.popupOpen = true + } + } + + PopupCard { + id: popup + anchorItem: root + bar: root.bar + owner: root + open: root.popupOpen + contentWidth: popup.fittedContentWidth(Style.space(300)) + contentHeight: popup.fittedContentHeight(column.implicitHeight) + + Column { + id: column + anchors.fill: parent + spacing: Style.space(10) + + Text { + width: parent.width + textFormat: Text.PlainText + text: "ScriptWidget" + color: root.bar ? root.bar.foreground : Commons.Color.foreground + font.family: root.bar ? root.bar.fontFamily : Style.font.family + font.pixelSize: Style.font.subtitle + font.bold: true + elide: Text.ElideRight + } + + Text { + width: parent.width + visible: !root.running + textFormat: Text.PlainText + text: root.statusText + color: root.status === "outdated" ? Commons.Color.urgent : (root.bar ? root.bar.foreground : Commons.Color.foreground) + font.family: root.bar ? root.bar.fontFamily : Style.font.family + font.pixelSize: Style.font.body + wrapMode: Text.WordWrap + } + + Toggle { + width: parent.width + visible: root.running + label: strings.t("bar.peek") + description: strings.t("bar.peek.description") + checked: !!root.service && root.service.peek === true + onClicked: if (root.service) root.service.togglePeek() + } + + Toggle { + width: parent.width + visible: root.running + label: strings.t("bar.arrange") + description: strings.t("bar.arrange.description") + checked: !!root.service && root.service.arrange === true + onClicked: if (root.service) root.service.toggleArrange() + } + } + } +} diff --git a/Linux/omarchy-plugin/LICENSE b/Linux/omarchy-plugin/LICENSE new file mode 100644 index 00000000..d6456956 --- /dev/null +++ b/Linux/omarchy-plugin/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/Linux/omarchy-plugin/README.md b/Linux/omarchy-plugin/README.md new file mode 100644 index 00000000..0ee63312 --- /dev/null +++ b/Linux/omarchy-plugin/README.md @@ -0,0 +1,105 @@ +# ScriptWidget for Omarchy + +The display end of ScriptWidget on [Omarchy](https://omarchy.org): an Omarchy shell plugin that shows the widgets ScriptWidget draws on the desktop layer, passes clicks and drags back to it, and adds an entry to the bar. + +The plugin draws nothing itself and runs no widget code. The `scriptwidget` program and its background service do that; the plugin shows the PNG pictures they write and tells them what the pointer did. Without ScriptWidget installed the plugin only shows a bar entry that says so. + +Status: in development with the rest of the Linux version. It was tested against the stand-in in `tests/`, not yet against the real background service. + +## Files + +| File | What it is | +| --- | --- | +| `manifest.json` | Plugin `com.atomsprout.scriptwidget`, kinds `service` and `bar-widget`, `keepLoaded` | +| `Service.qml` | Runs `scriptwidget watch`, checks every event, keeps the list of widgets, sends the commands, draws the arrange menu | +| `WidgetWindow.qml` | One widget: a layer-shell window with the picture, taking the pointer only on its tap regions | +| `ArrangeWindow.qml` | Arrange mode: one window per screen in which the widgets can be dragged | +| `BarWidget.qml` | The bar entry | +| `i18n/` | English and Simplified Chinese strings | +| `tests/` | A stand-in for `scriptwidget` and the checks. Not part of the installed plugin | + +## Installing for development + +``` +mkdir -p ~/.config/omarchy/plugins/com.atomsprout.scriptwidget +rsync -a --delete --exclude tests Linux/omarchy-plugin/ ~/.config/omarchy/plugins/com.atomsprout.scriptwidget/ +omarchy plugin validate ~/.config/omarchy/plugins/com.atomsprout.scriptwidget +omarchy plugin enable com.atomsprout.scriptwidget +omarchy restart shell +``` + +**Restart the shell after changing the plugin.** The manifest sets `keepLoaded`, which makes the shell keep the service alive when it reloads plugins (it reloads all of them whenever any local plugin file changes); without it every such reload would close and reopen all widgets. The price is that an edited `Service.qml`, `WidgetWindow.qml` or `ArrangeWindow.qml` is not picked up by the reload. On Omarchy 4.5.0 turning the plugin off and on did not load the new code either; only `omarchy restart shell` did. + +The folder may also be a symbolic link to `Linux/omarchy-plugin`; `omarchy plugin validate` refuses links inside the folder, not the folder itself. + +### Environment variables + +Read from the environment of the shell process with `Quickshell.env`, for development and tests. The shell is started by Hyprland and inherits its environment, so set them with `hyprctl eval 'hl.env("NAME", "value")'` and restart the shell. An empty value counts as not set. + +| Variable | Default | Meaning | +| --- | --- | --- | +| `SCRIPTWIDGET_PLUGIN_BIN` | `scriptwidget`, found on `PATH` | The program to run | +| `SCRIPTWIDGET_PLUGIN_STUDIO_BIN` | `scriptwidget-studio`, found on `PATH` | The management window | +| `SCRIPTWIDGET_PLUGIN_FRAMES` | `$XDG_RUNTIME_DIR/scriptwidget/frames` | The only directory pictures are read from | + +## What it does + +The contract is section 4.8 of [`docs/linux/plan.md`](../../docs/linux/plan.md). The plugin runs `scriptwidget watch` and reads one JSON event per line. + +- **Widgets.** Each `widget` event becomes one layer-shell window, namespace `scriptwidget`, on the bottom layer (above the wallpaper, below every window), on the screen the event names or the first screen. It is transparent, reserves no space and never takes the keyboard. +- **Taps.** The input region of a widget window is exactly its tap regions. A click in a region runs `scriptwidget tap `; a click anywhere else on the widget goes to whatever is below it. Where regions overlap, the one named last wins. +- **Show on top** (`mode.peek`). The same windows move to the overlay layer and back. +- **Arrange** (`mode.arrange`). The widget windows are replaced by one window per screen, namespace `scriptwidget-arrange`, that shows the same pictures with an outline. A widget can be dragged; letting go runs `scriptwidget move `. Within 40 px of an edge of the usable area a widget snaps to 16 px from it. The usable area is the screen without the bar and without the Omarchy dock: the plugin asks Hyprland (`hyprctl -j layers`) where the `omarchy-dock` layer is each time arrange mode starts and each time a drag starts. A right click opens a menu that runs `scriptwidget menu ` with `resize-small`, `resize-medium`, `resize-large`, `resize-extraLarge`, `edit`, `refresh` or `remove`. +- **Frames.** A widget with `frames` cycles through them. The cycle stops while a fullscreen window is on the workspace in front of the widget (and "show on top" is off), while arranging, and while the service is not running. It does not stop when ordinary windows cover the widget or when the session is locked: Quickshell has no cheap signal for either. +- **Reports.** After every `hello`, and when they change: `scriptwidget screen ` for each screen (the scale Hyprland reports; logical size) and `scriptwidget theme `, judged from the brightness of the theme's background color. +- **Bar entry.** Left click opens the management window. Right click opens a card with "Show widgets on top" and "Arrange widgets", which run `scriptwidget peek` and `scriptwidget arrange` and show the state the last `mode` event gave. +- **When something is wrong.** `watch` ends, or cannot be started: the last pictures stay, the plugin tries again after 2, 4, 8, 16 and then every 30 seconds, and the bar entry is dimmed and says that ScriptWidget is not running, or not installed. `hello` with a protocol other than 1: nothing is shown on the desktop and the bar entry says "Please update ScriptWidget". +- **Status.** `omarchy-shell com.atomsprout.scriptwidget status` prints one line of JSON: `protocol`, `status` (`starting`, `running`, `stopped`, `missing`, `outdated`), `arrange`, `peek`, `dark`, the number of `widgets`, how many are `animating`, and whether the `menu` is open. It changes nothing. + +### What the plugin expects of the service, beyond the table in the plan + +- Every line of `watch` is a JSON object with a string field `event`. +- `id` matches `^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$`. Other ids are ignored. +- `x`, `y`, `width`, `height` and the fields of `regions` are JSON numbers (not strings); they are rounded to whole logical pixels. `x` and `y` are measured from the top left corner of the screen, bar included. `regions` are measured from the top left corner of the widget. `width` and `height` are 1 to 8192. +- `image` is an absolute path below the frames directory that ends in `.png`; every path component consists of letters, digits, `.`, `_` and `-`. Anything else is ignored, and so is the widget. +- A new picture has a new `version`. The file name may stay the same or change. +- `frames`, when present, has 2 to 60 entries `{image, ms}`; `image` is still required and is shown where animation is off. One invalid frame turns the animation off. `ms` below 20 is taken as 20. +- After `hello` the service names every widget again within 12 seconds (a widget whose script is still running has no picture yet and is named late). Widgets the plugin still shows from before and that are not named by then are removed. +- `scriptwidget peek` and `scriptwidget arrange` without arguments switch the mode over, and the service answers with a `mode` event. +- The service answers `move`, and a `menu` resize, with a `widget` event. Until then the picture stays where it was dropped, for at most 3 seconds. +- `watch` ends with a non-zero exit code or any other way when there is no service; the plugin does not look at the code. +- At most 64 widgets, 256 regions per widget, and 262144 characters per line. + +## Security + +The plugin runs inside the Omarchy shell without a sandbox, so it is kept small: + +- It never reads a package and never receives package content: only picture paths, numbers and ids. +- Every command is an argument vector (`Quickshell.execDetached`, `Process`); no shell string is built anywhere. The words it sends are an id it was given and checked, a number, a screen name from Quickshell, or a word from a fixed list. +- It opens only `.png` files inside the frames directory (see above), and nothing the events name outside it. Qt decides the image format from the file's content, not its name, so the directory must stay writable by the service only (mode 0700). +- All text it shows is its own, with `textFormat: Text.PlainText`. +- It runs three programs: `scriptwidget`, `scriptwidget-studio` and `hyprctl -j layers`. + +## Tests + +`tests/check.sh` runs on a live Omarchy session (one monitor, scale 1) against `tests/scriptwidget`, a stand-in that speaks the protocol, draws pictures with ImageMagick and logs every command it receives. `tests/pointer.py` is a virtual pointer through `/dev/uinput` and needs `sudo` without a password. See the head of `check.sh` for what it changes on the machine and puts back. + +``` +rsync -a --delete Linux/omarchy-plugin/ testmachine:sw-plugin/ +ssh testmachine 'bash ~/sw-plugin/tests/check.sh' +``` + +`tests/out/` holds the screenshots and the output of the last run on the test machine. + +## Known limits + +- Not tested: display scales above 1, more than one monitor (including dragging a widget to another monitor), a bar on another edge than the top, a third-party bar, right-to-left languages. +- In arrange mode the pictures are on the same layer as before (bottom, or overlay with "show on top"). On the bottom layer a drag ends where the pointer reaches a window, the bar or a notification, because the compositor gives the pointer to that surface. +- The dock is only avoided where Hyprland reports its layer. Widgets are not moved when the dock appears later or changes edge. +- The cycle of frames does not pause behind ordinary windows or on the lock screen. +- A click on the bar entry starts `scriptwidget-studio` without checking that it exists. +- There is no command that tells the service a screen went away. + +## License + +Apache License 2.0, the same as the rest of the repository. See `LICENSE`. diff --git a/Linux/omarchy-plugin/Service.qml b/Linux/omarchy-plugin/Service.qml new file mode 100644 index 00000000..b05a178b --- /dev/null +++ b/Linux/omarchy-plugin/Service.qml @@ -0,0 +1,547 @@ +import QtQuick +import Quickshell +import Quickshell.Hyprland +import Quickshell.Io +import Quickshell.Wayland +import qs.Commons as Commons + +// The display end of ScriptWidget on Omarchy. It runs `scriptwidget watch`, +// shows the pictures that command names on the desktop layer, and passes +// clicks and drags back as `scriptwidget tap`, `move`, `menu`, `screen` and +// `theme`. The protocol is section 4.8 of docs/linux/plan.md. +// +// This code runs unsandboxed inside the Omarchy shell, so it does as little +// as it can: it never reads a package, every command is an argument vector +// (no shell string is ever built), and the only files it opens are PNG +// pictures inside the frames directory of the background service. +Item { + id: root + + // Filled in by the shell. + property var shell: null + property var manifest: null + + readonly property int protocol: 1 + + // Development overrides, read from the environment of the shell process. + readonly property string program: Quickshell.env("SCRIPTWIDGET_PLUGIN_BIN") || "scriptwidget" + readonly property string studioProgram: Quickshell.env("SCRIPTWIDGET_PLUGIN_STUDIO_BIN") || "scriptwidget-studio" + readonly property string framesDirectory: { + var named = String(Quickshell.env("SCRIPTWIDGET_PLUGIN_FRAMES") || "") + if (named === "") { + var runtime = String(Quickshell.env("XDG_RUNTIME_DIR") || "") + named = runtime === "" ? "" : runtime + "/scriptwidget/frames" + } + named = named.replace(/\/+$/, "") + return named.charAt(0) === "/" ? named : "" + } + + // "starting", "running", "stopped" (installed, no service), "missing" (the + // program could not be started) or "outdated" (another protocol version). + property string status: "starting" + property bool arrange: false + property bool peek: false + + // Arrange mode hands the pictures over from the widget windows to + // ArrangeWindow and back. For a moment both show the same picture in the + // same place, so nothing blinks: `canvasShown` turns on first and off last, + // `arranging` (the widget windows are hidden) the other way round. + property bool canvasShown: false + property bool arranging: false + readonly property bool dark: { + var color = Commons.Color.background + return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b < 0.5 + } + + // Widget ids in the order they arrived, and the checked copy of the last + // `widget` event of each. + property var ids: [] + property var widgets: ({}) + readonly property var noWidget: ({ id: "", screen: "", x: 0, y: 0, width: 0, height: 0, image: "", version: 0, stamp: "", regions: [], frames: [] }) + + // Counts connections, so a picture path that comes back with the same + // version after the service restarted is not answered from Qt's cache. A + // widget keeps the count it arrived with: its picture is not loaded again + // when the service reconnects, only when the service names it again. + property int epoch: 0 + property bool greeted: false + // About the current run of `watch`: whether it started at all, and whether + // it spoke another protocol version. + property bool started: false + property bool refused: false + property var announced: ({}) + property var reportedScreens: ({}) + property int retryDelay: 2000 + + // Layers the widgets should stay out of, by screen name, in screen + // coordinates: the Omarchy dock while it is shown. + property var keepOut: ({}) + + property string menuWidget: "" + property var menuScreen: null + property real menuX: 0 + property real menuY: 0 + + readonly property var limits: ({ widgets: 64, regions: 256, frames: 60, side: 8192, line: 262144 }) + readonly property var menuWords: ["resize-small", "resize-medium", "resize-large", "resize-extraLarge", "edit", "refresh", "remove"] + + Commons.Translations { + id: strings + directory: Qt.resolvedUrl("i18n") + } + + // ---------------------------------------------------------------- checks + + function validId(value) { + return typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(value) + } + + function whole(value, low, high) { + var number = Number(value) + if (typeof value !== "number" || !isFinite(number)) return null + number = Math.round(number) + return number < low || number > high ? null : number + } + + // A picture is accepted only when it is a .png inside the frames directory, + // named with plain path components. Anything else is ignored. + function framePath(value) { + if (typeof value !== "string" || framesDirectory === "" || value.length > 4096) return "" + var prefix = framesDirectory + "/" + if (value.indexOf(prefix) !== 0 || !/\.png$/.test(value)) return "" + var parts = value.substring(prefix.length).split("/") + for (var index = 0; index < parts.length; index++) { + if (!/^[A-Za-z0-9._-]+$/.test(parts[index]) || /^\.+$/.test(parts[index])) return "" + } + return value + } + + // The query only keeps Qt from answering with a picture it loaded earlier + // from the same path; `stamp` is the connection count and the version. + function imageUrl(path, stamp) { + return path === "" ? "" : "file://" + path + "?v=" + stamp + } + + function checkedWidget(event) { + if (!validId(event.id)) return null + var width = whole(event.width, 1, limits.side), height = whole(event.height, 1, limits.side) + var x = whole(event.x, -65536, 65536), y = whole(event.y, -65536, 65536) + var version = whole(event.version, 0, 9007199254740991) + var image = framePath(event.image) + if (width === null || height === null || x === null || y === null || version === null || image === "") return null + + var regions = [] + var given = Array.isArray(event.regions) ? event.regions : [] + for (var index = 0; index < given.length && regions.length < limits.regions; index++) { + var region = given[index] + if (!region || typeof region !== "object") continue + var n = whole(region.n, 0, 2147483647) + var left = whole(region.x, -limits.side, limits.side), top = whole(region.y, -limits.side, limits.side) + var across = whole(region.width, 1, limits.side), down = whole(region.height, 1, limits.side) + if (n === null || left === null || top === null || across === null || down === null) continue + regions.push({ n: n, x: left, y: top, width: across, height: down }) + } + + // One bad frame turns the animation off; the first picture still shows. + var frames = [] + var steps = Array.isArray(event.frames) ? event.frames : [] + if (steps.length > 1 && steps.length <= limits.frames) { + for (var step = 0; step < steps.length; step++) { + var path = steps[step] && typeof steps[step] === "object" ? framePath(steps[step].image) : "" + var ms = steps[step] && typeof steps[step] === "object" ? whole(steps[step].ms, 0, 600000) : null + if (path === "" || ms === null) { frames = []; break } + frames.push({ image: path, ms: Math.max(20, ms) }) + } + } + + return { + id: event.id, screen: typeof event.screen === "string" ? event.screen.substring(0, 128) : "", + x: x, y: y, width: width, height: height, image: image, version: version, stamp: epoch + "." + version, + regions: regions, frames: frames + } + } + + // --------------------------------------------------------------- protocol + + function handle(line) { + if (line.length === 0 || line.length > limits.line) return + var event + try { event = JSON.parse(line) } catch (error) { return } + if (!event || typeof event !== "object" || typeof event.event !== "string") return + + if (!greeted) { + if (event.event !== "hello" || refused) return + if (event.protocol !== protocol) { + // Another version: show nothing, and wait for an update. + refused = true + status = "outdated" + dropAll() + return + } + greeted = true + epoch += 1 + retryDelay = 2000 + arrange = event.arrange === true + peek = event.peek === true + status = "running" + // The service names every widget again after hello. What it has not + // named a moment later is gone. + announced = ({}) + sweep.restart() + reportedScreens = ({}) + for (var index = 0; index < screenReports.instances.length; index++) screenReports.instances[index].send() + reportTheme() + return + } + + if (event.event === "widget") { + var widget = checkedWidget(event) + if (!widget) return + if (ids.indexOf(widget.id) < 0) { + if (ids.length >= limits.widgets) return + ids = ids.concat([widget.id]) + } + var next = Object.assign({}, widgets) + next[widget.id] = widget + widgets = next + announced[widget.id] = true + } else if (event.event === "remove") { + if (validId(event.id)) drop([event.id]) + } else if (event.event === "mode") { + arrange = event.arrange === true + peek = event.peek === true + if (!arrange) closeMenu() + } else if (event.event === "bye") { + // The pictures stay; the retry below finds the service again. + greeted = false + status = "stopped" + closeMenu() + } + } + + function drop(gone) { + var left = ids.filter(function (id) { return gone.indexOf(id) < 0 }) + if (left.length === ids.length) return + ids = left + var next = ({}) + for (var index = 0; index < left.length; index++) next[left[index]] = widgets[left[index]] + widgets = next + if (gone.indexOf(menuWidget) >= 0) closeMenu() + } + + function dropAll() { + ids = [] + widgets = ({}) + closeMenu() + } + + function watchEnded() { + greeted = false + sweep.stop() + closeMenu() + if (!started) status = "missing" + else if (!refused) status = "stopped" + retry.interval = retryDelay + retry.restart() + retryDelay = Math.min(30000, retryDelay * 2) + } + + // Commands of the display protocol. They mean nothing without the service. + function send(words) { + if (status !== "running") return + Quickshell.execDetached([program].concat(words.map(String))) + } + + function tap(id, n) { send(["tap", id, n]) } + function togglePeek() { send(["peek"]) } + function toggleArrange() { send(["arrange"]) } + function openStudio() { Quickshell.execDetached([studioProgram]) } + + function reportTheme() { send(["theme", dark ? "dark" : "light"]) } + onDarkChanged: reportTheme() + + function reportScreen(name, scale, width, height) { + if (status !== "running" || name === "" || !(width > 0) || !(height > 0)) return + var figures = [String(Math.round(scale * 10000) / 10000), String(width), String(height)] + if (reportedScreens[name] === figures.join(" ")) return + reportedScreens[name] = figures.join(" ") + send(["screen", name].concat(figures)) + } + + function screenFor(name) { + var screens = Quickshell.screens + for (var index = 0; index < screens.length; index++) if (screens[index].name === name) return screens[index] + return screens.length > 0 ? screens[0] : null + } + + // --------------------------------------------------------------- arranging + + readonly property int snapDistance: 24 + readonly property int edgeInset: 16 + + // The part of a screen a widget may be dropped on: not under the bar and + // not under the dock. + function usableArea(screen) { + var area = { left: 0, top: 0, right: screen.width, bottom: screen.height } + var bar = shell ? shell.bar : null + if (bar && !bar.barHidden) { + var thickness = typeof bar.barSizeFor === "function" ? bar.barSizeFor(screen.name) : Number(bar.barSize) || 0 + if (bar.position === "top") area.top += thickness + else if (bar.position === "bottom") area.bottom -= thickness + else if (bar.position === "left") area.left += thickness + else if (bar.position === "right") area.right -= thickness + } + var strips = keepOut[screen.name] || [] + for (var index = 0; index < strips.length; index++) { + var strip = strips[index] + if (strip.width >= strip.height) { + if (strip.y + strip.height / 2 < screen.height / 2) area.top = Math.max(area.top, strip.y + strip.height) + else area.bottom = Math.min(area.bottom, strip.y) + } else { + if (strip.x + strip.width / 2 < screen.width / 2) area.left = Math.max(area.left, strip.x + strip.width) + else area.right = Math.min(area.right, strip.x) + } + } + return area + } + + function settle(value, low, high) { + if (high - low < 2 * edgeInset) return Math.round(low) + if (value < low + edgeInset + snapDistance) return Math.round(low + edgeInset) + if (value > high - edgeInset - snapDistance) return Math.round(high - edgeInset) + return Math.round(value) + } + + // The end of a drag: picks the screen under the middle of the widget, snaps + // near the edges of its usable area, and tells the service. + function place(id, from, x, y, width, height) { + var screen = from + var screens = Quickshell.screens + var middleX = from.x + x + width / 2, middleY = from.y + y + height / 2 + for (var index = 0; index < screens.length; index++) { + var other = screens[index] + if (middleX >= other.x && middleX < other.x + other.width && middleY >= other.y && middleY < other.y + other.height) screen = other + } + x += from.x - screen.x + y += from.y - screen.y + var area = usableArea(screen) + var placed = { screen: screen.name, x: settle(x, area.left, area.right - width), y: settle(y, area.top, area.bottom - height) } + send(["move", id, placed.screen, placed.x, placed.y]) + return placed + } + + function refreshKeepOut() { if (!layers.running) layers.running = true } + + onArrangeChanged: { + if (arrange) { + canvasShown = true + refreshKeepOut() + } else { + arranging = false + } + handover.restart() + } + + function readLayers(text) { + var found = ({}) + try { + var monitors = JSON.parse(text) + for (var name in monitors) { + var screen = screenFor(name) + if (!screen || screen.name !== name) continue + var levels = monitors[name].levels || ({}) + for (var level in levels) { + for (var index = 0; index < levels[level].length; index++) { + var layer = levels[level][index] + if (layer.namespace !== "omarchy-dock" || !(layer.w > 0) || !(layer.h > 0)) continue + if (!found[name]) found[name] = [] + found[name].push({ x: layer.x - screen.x, y: layer.y - screen.y, width: layer.w, height: layer.h }) + } + } + } + } catch (error) { found = ({}) } + keepOut = found + } + + function openMenu(id, screen, x, y) { + if (!arrange || !widgets[id]) return + menuScreen = screen + menuX = x + menuY = y + menuWidget = id + } + + function closeMenu() { menuWidget = "" } + + function choose(word) { + var id = menuWidget + closeMenu() + if (menuWords.indexOf(word) >= 0 && widgets[id]) send(["menu", id, word]) + } + + // ------------------------------------------------------------------ parts + + function connect() { + if (watch.running) return + started = false + refused = false + watch.running = true + } + + Component.onCompleted: connect() + + Process { + id: watch + command: [root.program, "watch"] + stdout: SplitParser { onRead: function (line) { root.handle(line) } } + onStarted: root.started = true + onRunningChanged: if (!running) root.watchEnded() + } + + Timer { + id: retry + onTriggered: root.connect() + } + + Timer { + id: handover + interval: 150 + onTriggered: { + if (root.arrange) root.arranging = true + else root.canvasShown = false + } + } + + Timer { + id: sweep + // A widget whose script is still running after a service restart has no + // picture yet and is named late: wait longer than a script may take. + interval: 12000 + onTriggered: root.drop(root.ids.filter(function (id) { return !root.announced[id] })) + } + + // Asks the compositor where the dock is. Read-only, and its answer is only + // used to keep widgets from being dropped under it. + Process { + id: layers + command: ["hyprctl", "-j", "layers"] + stdout: StdioCollector { onStreamFinished: root.readLayers(text) } + } + + Variants { + id: screenReports + model: Quickshell.screens + + QtObject { + required property var modelData + readonly property var monitor: Hyprland.monitorFor(modelData) + readonly property real scale: monitor && monitor.scale > 0 ? monitor.scale : modelData.devicePixelRatio + readonly property string figures: modelData.name + " " + scale + " " + modelData.width + " " + modelData.height + function send() { root.reportScreen(modelData.name, scale, modelData.width, modelData.height) } + onFiguresChanged: send() + } + } + + Variants { + id: windows + model: root.ids + + WidgetWindow { service: root } + } + + Variants { + model: Quickshell.screens + + ArrangeWindow { service: root } + } + + // `omarchy-shell com.atomsprout.scriptwidget status`: what the display end + // is doing, for `scriptwidget doctor` and the tests. Read-only. + IpcHandler { + target: "com.atomsprout.scriptwidget" + + function status(): string { + var animating = 0 + for (var index = 0; index < windows.instances.length; index++) if (windows.instances[index].animating) animating += 1 + return JSON.stringify({ + protocol: root.protocol, status: root.status, arrange: root.arrange, peek: root.peek, dark: root.dark, + widgets: root.ids.length, animating: animating, menu: root.menuWidget !== "" + }) + } + } + + // The menu of a widget in arrange mode: one window over the whole screen, + // so a click anywhere else closes it. + PanelWindow { + id: menu + + visible: root.menuWidget !== "" + screen: root.menuScreen + anchors { top: true; bottom: true; left: true; right: true } + color: "transparent" + exclusionMode: ExclusionMode.Ignore + WlrLayershell.namespace: "scriptwidget-menu" + WlrLayershell.keyboardFocus: WlrKeyboardFocus.None + WlrLayershell.layer: WlrLayer.Overlay + + MouseArea { + anchors.fill: parent + acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MiddleButton + onPressed: root.closeMenu() + } + + Rectangle { + id: card + x: Math.max(0, Math.min(root.menuX, menu.width - width)) + y: Math.max(0, Math.min(root.menuY, menu.height - height)) + width: Commons.Style.space(180) + height: rows.implicitHeight + 2 * Commons.Style.spacing.sm + radius: Commons.Style.cornerRadius + color: Commons.Color.popups.background + border.color: Commons.Color.popups.border + border.width: 1 + + MouseArea { anchors.fill: parent; acceptedButtons: Qt.AllButtons } + + Column { + id: rows + x: Commons.Style.spacing.sm + y: Commons.Style.spacing.sm + width: parent.width - 2 * x + + Repeater { + model: root.menuWords + + Rectangle { + id: row + required property string modelData + width: rows.width + height: Commons.Style.spacing.popupRowHeight + radius: Math.max(0, card.radius - Commons.Style.spacing.sm) + color: hover.containsMouse ? Commons.Style.hoverFill : "transparent" + + Text { + anchors.verticalCenter: parent.verticalCenter + x: Commons.Style.spacing.controlPaddingX + width: parent.width - 2 * x + textFormat: Text.PlainText + text: strings.t("menu." + row.modelData) + color: row.modelData === "remove" ? Commons.Color.urgent : Commons.Color.popups.text + font.family: Commons.Style.font.family + font.pixelSize: Commons.Style.font.body + elide: Text.ElideRight + } + + MouseArea { + id: hover + anchors.fill: parent + hoverEnabled: true + cursorShape: Qt.PointingHandCursor + onClicked: root.choose(row.modelData) + } + } + } + } + } + } +} diff --git a/Linux/omarchy-plugin/WidgetWindow.qml b/Linux/omarchy-plugin/WidgetWindow.qml new file mode 100644 index 00000000..c721399b --- /dev/null +++ b/Linux/omarchy-plugin/WidgetWindow.qml @@ -0,0 +1,96 @@ +import QtQuick +import Quickshell +import Quickshell.Hyprland +import Quickshell.Wayland + +// One widget: a picture on the desktop layer. Only its tap regions take the +// pointer; everywhere else a click goes to whatever is below. While widgets +// are being arranged, ArrangeWindow shows the pictures instead. +PanelWindow { + id: panel + + required property string modelData + required property var service + + readonly property var widget: service.widgets[modelData] || service.noWidget + readonly property var frames: widget.frames + property int frame: 0 + + // The only cheap sign that a widget cannot be seen: a fullscreen window on + // the workspace in front of it. + readonly property var monitor: Hyprland.monitorFor(screen) + readonly property bool covered: !service.peek && !!monitor && !!monitor.activeWorkspace && monitor.activeWorkspace.hasFullscreen === true + // Without the service the frame files may be gone, so the frame on show stays. + readonly property bool animating: visible && frames.length > 1 && !covered && service.status === "running" + + screen: service.screenFor(widget.screen) + visible: widget.width > 0 && !service.arranging + anchors { top: true; left: true } + margins { left: widget.x; top: widget.y } + implicitWidth: Math.max(1, widget.width) + implicitHeight: Math.max(1, widget.height) + color: "transparent" + exclusionMode: ExclusionMode.Ignore + WlrLayershell.namespace: "scriptwidget" + WlrLayershell.keyboardFocus: WlrKeyboardFocus.None + WlrLayershell.layer: service.peek ? WlrLayer.Overlay : WlrLayer.Bottom + + // The input region: the tap regions and nothing else. + mask: Region { regions: panel.tapRegions } + + property list tapRegions + + function collectRegions() { + var found = [] + for (var index = 0; index < regionMaker.count; index++) { + var region = regionMaker.objectAt(index) + if (region) found.push(region) + } + tapRegions = found + } + + Instantiator { + id: regionMaker + model: panel.widget.regions + delegate: Region { + required property var modelData + x: modelData.x + y: modelData.y + width: modelData.width + height: modelData.height + } + onObjectAdded: panel.collectRegions() + onObjectRemoved: panel.collectRegions() + } + + onFramesChanged: if (frame >= frames.length) frame = 0 + + Timer { + interval: panel.frames.length > 0 ? panel.frames[panel.frame % panel.frames.length].ms : 1000 + running: panel.animating + repeat: true + onTriggered: panel.frame = (panel.frame + 1) % panel.frames.length + } + + Image { + anchors.fill: parent + source: panel.service.imageUrl(panel.frames.length > 1 ? panel.frames[panel.frame % panel.frames.length].image : panel.widget.image, panel.widget.stamp) + } + + MouseArea { + anchors.fill: parent + cursorShape: Qt.PointingHandCursor + + onClicked: function (mouse) { + // Where regions overlap, the one named last is on top. + var regions = panel.widget.regions + for (var index = regions.length - 1; index >= 0; index--) { + var region = regions[index] + if (mouse.x >= region.x && mouse.x < region.x + region.width && mouse.y >= region.y && mouse.y < region.y + region.height) { + panel.service.tap(panel.modelData, region.n) + return + } + } + } + } +} diff --git a/Linux/omarchy-plugin/i18n/en.json b/Linux/omarchy-plugin/i18n/en.json new file mode 100644 index 00000000..4da4af45 --- /dev/null +++ b/Linux/omarchy-plugin/i18n/en.json @@ -0,0 +1,18 @@ +{ + "status.starting": "Connecting to ScriptWidget…", + "status.stopped": "ScriptWidget is not running. The widgets come back when its background service is started.", + "status.missing": "ScriptWidget is not installed. This plugin only shows the widgets; install the scriptwidget package to use it.", + "status.outdated": "Please update ScriptWidget. The installed version and this plugin do not match, so no widgets are shown.", + "status.unavailable": "The ScriptWidget plugin is not loaded.", + "bar.peek": "Show widgets on top", + "bar.peek.description": "Above every window, until switched off", + "bar.arrange": "Arrange widgets", + "bar.arrange.description": "Drag to move, right-click for more", + "menu.resize-small": "Small", + "menu.resize-medium": "Medium", + "menu.resize-large": "Large", + "menu.resize-extraLarge": "Extra large", + "menu.edit": "Edit", + "menu.refresh": "Refresh", + "menu.remove": "Remove" +} diff --git a/Linux/omarchy-plugin/i18n/zh-CN.json b/Linux/omarchy-plugin/i18n/zh-CN.json new file mode 100644 index 00000000..8b7cd104 --- /dev/null +++ b/Linux/omarchy-plugin/i18n/zh-CN.json @@ -0,0 +1,18 @@ +{ + "status.starting": "正在连接 ScriptWidget…", + "status.stopped": "ScriptWidget 没有在运行。启动它的后台服务后,小组件会重新出现。", + "status.missing": "还没有安装 ScriptWidget。这个插件只负责显示小组件,请先安装 scriptwidget 软件包。", + "status.outdated": "请更新 ScriptWidget。已安装的版本和这个插件不匹配,所以没有显示小组件。", + "status.unavailable": "ScriptWidget 插件没有加载。", + "bar.peek": "把小组件提到最上层", + "bar.peek.description": "显示在所有窗口之上,直到关闭", + "bar.arrange": "摆放小组件", + "bar.arrange.description": "拖动来移动,右键有更多选项", + "menu.resize-small": "小", + "menu.resize-medium": "中", + "menu.resize-large": "大", + "menu.resize-extraLarge": "特大", + "menu.edit": "编辑", + "menu.refresh": "刷新", + "menu.remove": "移除" +} diff --git a/Linux/omarchy-plugin/manifest.json b/Linux/omarchy-plugin/manifest.json new file mode 100644 index 00000000..c3164221 --- /dev/null +++ b/Linux/omarchy-plugin/manifest.json @@ -0,0 +1,25 @@ +{ + "schemaVersion": 1, + "id": "com.atomsprout.scriptwidget", + "name": "ScriptWidget", + "version": "0.1.0", + "author": "AtomSprout", + "description": "Shows the widgets ScriptWidget draws on the desktop layer and passes clicks back to it", + "license": "Apache-2.0", + "kinds": [ + "service", + "bar-widget" + ], + "keepLoaded": true, + "entryPoints": { + "service": "Service.qml", + "barWidget": "BarWidget.qml" + }, + "barWidget": { + "displayName": "ScriptWidget", + "description": "Opens ScriptWidget; shows widgets on top or arranges them", + "category": "Status", + "allowMultiple": false, + "defaultSection": "right" + } +} diff --git a/Linux/omarchy-plugin/tests/check.sh b/Linux/omarchy-plugin/tests/check.sh new file mode 100755 index 00000000..8bec9adb --- /dev/null +++ b/Linux/omarchy-plugin/tests/check.sh @@ -0,0 +1,395 @@ +#!/bin/bash +# Checks the Omarchy plugin on a live Omarchy session, against the stand-in +# `scriptwidget` in this folder. Run it on the test machine, for example over +# SSH: bash tests/check.sh +# +# It needs: one monitor at scale 1, sudo without a password (for the virtual +# pointer, pointer.py), and jq, grim, ImageMagick, rsync and foot. +# +# What it changes, and puts back when it ends: it installs this plugin as a +# local plugin and enables it, sets three SCRIPTWIDGET_PLUGIN_* variables in +# the session (so the shell finds the stand-in), restarts the shell a few +# times, moves the bar entry to the left section, and opens one terminal +# window on the test workspace. At the end the plugin is disabled, the +# stand-in is stopped, the variables are emptied and the shell restarted. +# CHECK_WORKSPACE names the workspace to use (default 2); CHECK_KEEP=1 leaves +# everything running for a look. +# +# Exit code: 0 when every check passed, 1 when one failed, 2 when the machine +# cannot run the checks. + +set -u +here=$(cd "$(dirname "$0")" && pwd) +source_dir=$(dirname "$here") +id=com.atomsprout.scriptwidget + +export LANG=C.UTF-8 +export OMARCHY_PATH=${OMARCHY_PATH:-/usr/share/omarchy} +export PATH=$OMARCHY_PATH/bin:$PATH +export XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR:-/run/user/$(id -u)} +if [ -z "${WAYLAND_DISPLAY:-}" ]; then + WAYLAND_DISPLAY=$(ls "$XDG_RUNTIME_DIR" | grep -E '^wayland-[0-9]+$' | head -1); export WAYLAND_DISPLAY +fi +if [ -z "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]; then + HYPRLAND_INSTANCE_SIGNATURE=$(ls -t "$XDG_RUNTIME_DIR/hypr" 2>/dev/null | head -1); export HYPRLAND_INSTANCE_SIGNATURE +fi + +T=$XDG_RUNTIME_DIR/scriptwidget-plugin-test +export SCRIPTWIDGET_PLUGIN_FRAMES=$T/frames +sw=$here/scriptwidget +plugin_dir=$HOME/.config/omarchy/plugins/$id +workspace=${CHECK_WORKSPACE:-2} +out=$here/out +tmp=$(mktemp -d) +failures=0 +catcher_pid="" + +for tool in jq grim magick rsync foot hyprctl omarchy-shell omarchy-restart-shell python3; do + command -v "$tool" >/dev/null || { echo "cannot run: $tool is missing" >&2; exit 2; } +done +sudo -n true 2>/dev/null || { echo "cannot run: sudo needs a password (the virtual pointer needs /dev/uinput)" >&2; exit 2; } +monitors=$(hyprctl -j monitors) +[ "$(jq length <<< "$monitors")" = 1 ] || { echo "cannot run: these checks are written for one monitor" >&2; exit 2; } +screen=$(jq -r '.[0].name' <<< "$monitors") +W=$(jq -r '.[0].width' <<< "$monitors"); H=$(jq -r '.[0].height' <<< "$monitors") +jq -e '.[0].scale == 1' <<< "$monitors" >/dev/null || { echo "cannot run: these checks are written for scale 1" >&2; exit 2; } +home_workspace=$(hyprctl -j activeworkspace | jq -r .id) + +pass() { echo "PASS $1"; } +fail() { echo "FAIL $1${2:+ -- $2}"; failures=$((failures + 1)); } +# check : passes when the command succeeds. +check() { local name=$1; shift; if "$@"; then pass "$name"; else fail "$name" "${DETAIL:-}"; fi; DETAIL=""; } +skip() { echo "SKIP $1 -- $2"; } + +status() { omarchy-shell "$id" status 2>/dev/null; } +field() { status | jq -r ".$1" 2>/dev/null; } +# until_true +until_true() { + local deadline=$((SECONDS + $1)); shift + while ! "$@" >/dev/null 2>&1; do [ $SECONDS -ge $deadline ] && return 1; sleep 0.2; done +} +is() { [ "$(field "$1")" = "$2" ]; } +layers() { hyprctl -j layers | jq -c '[to_entries[] | .key as $m | .value.levels | to_entries[] | .key as $level | .value[] | {monitor: $m, level: $level, namespace, x, y, w, h}]'; } +# widget_layers: "level x y w h" of every widget window, sorted. +widget_layers() { layers | jq -r '.[] | select(.namespace == "scriptwidget") | "\(.level) \(.x) \(.y) \(.w) \(.h)"' | sort; } +has_layer() { widget_layers | grep -qx "$1"; } +layer_count() { layers | jq "[.[] | select(.namespace == \"$1\")] | length"; } +# count_is +count_is() { [ "$(layer_count "$1")" = "$2" ]; } +workspace_empty() { [ "$(hyprctl -j workspaces | jq "[.[] | select(.id == $workspace) | .windows] | add // 0")" = 0 ]; } +logged() { grep -qE "$1" "$T/log"; } +clear_log() { : > "$T/log"; } +# The pointer and the screenshots act on whatever workspace is in front, so +# each first makes sure it is the test workspace (something else may have +# switched away in the meantime). +in_front() { + [ "$(hyprctl -j activeworkspace | jq -r .id)" = "$workspace" ] && return + hyprctl dispatch "hl.dsp.focus({ workspace = \"$workspace\" })" >/dev/null; sleep 0.5 +} +ptr() { in_front; sudo -n python3 "$here/pointer.py" "$W" "$H" "$@"; } +# The pointer first goes somewhere else: a "move" to where it already is moves +# nothing, and the compositor then does not look again at what is under it. +click() { ptr move $(($1 + 9)) $(($2 + 9)) sleep 0.1 move "$1" "$2" sleep 0.3 down up sleep 0.4; } +right_click() { ptr move $(($1 + 9)) $(($2 + 9)) sleep 0.1 move "$1" "$2" sleep 0.3 rdown rup sleep 0.6; } +shot() { in_front; grim -g "$1" "$2"; } +# differ : true when more than 20 pixels differ. +differ() { local count; count=$(magick compare -metric AE "$1" "$2" null: 2>&1 | awk '{print int($1)}'); [ "${count:-0}" -gt 20 ]; } +# changed_box : "x y w h" of what differs, inside the pictures. +changed_box() { + magick "$1" "$2" -compose difference -composite -colorspace gray -threshold 8% -trim -format '%X %Y %w %h' info: 2>/dev/null | tr -d '+' +} +session_env() { hyprctl eval "hl.env(\"$1\", \"$2\")" >/dev/null; } +watch_pid() { cat "$T/watch.pid" 2>/dev/null; } +stop_watch() { local pid; pid=$(watch_pid); [ -n "$pid" ] && kill "$pid" 2>/dev/null; } +connected() { is status running && is widgets "${1:-2}"; } + +cleanup() { + [ -n "$catcher_pid" ] && kill "$catcher_pid" 2>/dev/null + [ -f "$sw.off" ] && mv "$sw.off" "$sw" + rm -f "$T/down" "$T/protocol" + rm -rf "$tmp" + if [ "${CHECK_KEEP:-0}" = 1 ]; then echo "left running (CHECK_KEEP=1)"; return; fi + omarchy plugin disable "$id" >/dev/null 2>&1 + sleep 0.5 + stop_watch + session_env SCRIPTWIDGET_PLUGIN_BIN "" + session_env SCRIPTWIDGET_PLUGIN_STUDIO_BIN "" + session_env SCRIPTWIDGET_PLUGIN_FRAMES "" + omarchy-restart-shell >/dev/null 2>&1 + hyprctl dispatch "hl.dsp.focus({ workspace = \"$home_workspace\" })" >/dev/null +} +trap cleanup EXIT + +# ------------------------------------------------------------------ set up + +mkdir -p "$out" "$T" +rm -f "$T/down" "$T/protocol" "$T/clicks" +clear_log +session_env SCRIPTWIDGET_PLUGIN_BIN "$sw" +session_env SCRIPTWIDGET_PLUGIN_STUDIO_BIN "$here/scriptwidget-studio" +session_env SCRIPTWIDGET_PLUGIN_FRAMES "$T/frames" +hyprctl dispatch "hl.dsp.focus({ workspace = \"$workspace\" })" >/dev/null +workspace_empty || { echo "cannot run: workspace $workspace is not empty" >&2; exit 2; } + +mkdir -p "$plugin_dir" +rsync -a --delete --exclude tests "$source_dir/" "$plugin_dir/" + +check "validate: omarchy plugin validate accepts the plugin" omarchy plugin validate "$plugin_dir" +omarchy-shell shell rescanPlugins >/dev/null 2>&1; sleep 1 +omarchy plugin enable "$id" >/dev/null 2>&1 +check "enable: omarchy plugin list shows it enabled" sh -c "omarchy plugin list | grep -E '^$id +enabled' >/dev/null" +omarchy bar move "$id" --section left >/dev/null 2>&1 +# A restart, so the shell has the variables above and the code just copied. +omarchy-restart-shell >/dev/null 2>&1 +check "connect: the service runs watch and shows two widgets" until_true 20 connected + +# ------------------------------------------------------- layer and geometry + +sleep 0.5 +DETAIL=$(widget_layers | tr '\n' ';') +check "layer: widget a is on the bottom layer at 80,80 329x170" has_layer "1 80 80 329 170" +check "layer: widget b is on the bottom layer at 460,80 170x170" has_layer "1 460 80 170 170" +check "layer: no other widget window" count_is scriptwidget 2 +shot "0,0 1000x420" "$out/1-desktop.png" + +check "report: scriptwidget screen " logged "command screen $screen 1 $W $H\$" +check "report: scriptwidget theme dark or light" logged 'command theme (dark|light)$' + +# ---------------------------------------------------------------- pictures + +shot "80,80 329x170" "$tmp/a1.png" +"$sw" tap a 1; sleep 0.8 +shot "80,80 329x170" "$tmp/a2.png" +check "swap: a new picture file is shown within a second" differ "$tmp/a1.png" "$tmp/a2.png" +"$sw" menu a refresh; sleep 0.8 +shot "80,80 329x170" "$tmp/a3.png" +check "swap: a new version under the same file name is shown" differ "$tmp/a2.png" "$tmp/a3.png" + +shot "460,80 170x170" "$tmp/b1.png"; sleep 0.3 +shot "460,80 170x170" "$tmp/b2.png" +check "frames: the pictures of b change" differ "$tmp/b1.png" "$tmp/b2.png" +check "frames: one widget is animating" is animating 1 + +# -------------------------------------------------------- taps on the desktop + +clear_log +click 244 207 +check "tap: a click in the region sends scriptwidget tap a 1" until_true 3 logged 'command tap a 1$' +clear_log +click 120 100; click 545 165 +check "tap: clicks outside a region send nothing" [ ! -s "$T/log" ] + +# ---------------------------- peek, and clicks passing through to a window + +hyprctl dispatch "hl.dsp.exec_cmd(\"foot --app-id=sw-click-catcher $here/click-catcher.sh $T/clicks\")" >/dev/null +until_true 8 sh -c "hyprctl -j clients | jq -e '.[] | select(.class == \"sw-click-catcher\")'" +catcher_pid=$(hyprctl -j clients | jq -r '.[] | select(.class == "sw-click-catcher") | .pid') +if [ -n "$catcher_pid" ] && until_true 5 test -e "$T/clicks"; then + received() { stat -c %s "$T/clicks"; } + sleep 0.5 + "$sw" peek + check "peek: the widgets move to the overlay layer" until_true 3 has_layer "3 460 80 170 170" + shot "0,0 1000x420" "$out/2-peek-over-window.png" + + before=$(received); click 545 165 + check "pass through: a click on a widget without regions reaches the window below" [ "$(received)" -gt "$before" ] + before=$(received); click 120 100 + check "pass through: a click beside the region reaches the window below" [ "$(received)" -gt "$before" ] + before=$(received); clear_log; click 244 207 + check "pass through: a click in the region is a tap" until_true 3 logged 'command tap a 1$' + check "pass through: and does not reach the window below" [ "$(received)" = "$before" ] + + # The other way round, so the three checks above can be seen to fail. + "$sw" arrange; until_true 3 is arrange true; sleep 0.5 + before=$(received); click 545 165 + check "pass through: in arrange mode the click stays with the widget" [ "$(received)" = "$before" ] + "$sw" arrange; until_true 3 is arrange false; sleep 0.4 + + "$sw" peek + check "peek: and back to the bottom layer" until_true 3 has_layer "1 460 80 170 170" + + hyprctl dispatch 'hl.dsp.focus({ window = "class:sw-click-catcher" })' >/dev/null + hyprctl dispatch 'hl.dsp.window.fullscreen({ mode = "fullscreen" })' >/dev/null + check "frames: paused behind a fullscreen window" until_true 3 is animating 0 + hyprctl dispatch 'hl.dsp.window.fullscreen({ mode = "fullscreen" })' >/dev/null + check "frames: running again after it" until_true 3 is animating 1 +else + fail "pass through: the test window did not open" +fi +[ -n "$catcher_pid" ] && kill "$catcher_pid" 2>/dev/null +catcher_pid="" +until_true 5 workspace_empty + +# ----------------------------------------------------------------- arrange + +"$sw" arrange +check "arrange: the mode event turns arrange mode on" until_true 3 is arrange true +sleep 0.5 +check "arrange: one window over the screen shows the widgets" count_is scriptwidget-arrange 1 +check "arrange: in place of the widget windows" count_is scriptwidget 0 + +clear_log +# Held at 150,120, which is 70,40 inside a, and let go at 750,520 in one jump. +ptr move 150 120 sleep 0.3 down sleep 0.2 move 750 520 sleep 0.3 up sleep 0.5 +DETAIL=$(grep move "$T/log") +check "arrange: a drag sends scriptwidget move a 680 480" logged "command move a $screen 680 480\$" + +# The dock, where it is mapped, takes a strip at the bottom of the screen. +dock_top=$(layers | jq '[.[] | select(.namespace == "omarchy-dock" and .w > .h and .y > 300) | .y] | min // empty') +floor=${dock_top:-$H} +clear_log +ptr move 500 150 sleep 0.3 down sleep 0.2 move 60 $((H - 5)) sleep 0.3 up sleep 0.5 +DETAIL="$(grep move "$T/log") (bottom of the usable area: $floor)" +check "arrange: near the left edge and the bottom it snaps to 16 px from them, above the dock" logged "command move b $screen 16 $((floor - 16 - 170))\$" + +clear_log +# Away from every edge nothing snaps: b from 16, by its middle to 1000,600. +ptr move 100 $((floor - 100)) sleep 0.3 down sleep 0.2 move 1084 686 sleep 0.3 up sleep 0.5 +DETAIL=$(grep move "$T/log") +check "arrange: in the middle of the screen it stays where it is dropped" logged "command move b $screen 1000 600\$" + +right_click 1050 650 +check "menu: a right click opens the menu" is menu true +shot "600,400 700x460" "$out/3-arrange-menu.png" +clear_log +click 1400 300 +check "menu: a click elsewhere closes it and sends nothing" sh -c "[ \"\$(omarchy-shell $id status | jq -r .menu)\" = false ] && ! grep -q 'command menu' '$T/log'" +right_click 1050 650 +clear_log +# Rows are 28 px high and start 4 px below the top of the menu: the second. +click 1080 $((650 + 4 + 28 + 14)) +DETAIL=$(cat "$T/log") +check "menu: the second row sends scriptwidget menu b resize-medium" until_true 3 logged 'command menu b resize-medium$' + +"$sw" arrange +check "arrange: off again" until_true 3 is arrange false +sleep 0.5 +DETAIL=$(widget_layers | tr '\n' ';') +check "arrange: the widget windows are back, where they were dropped" has_layer "1 680 480 329 170" +check "menu: b now has the medium size" has_layer "1 1000 600 329 170" + +# ------------------------------------------------- remove, and bad messages + +"$sw" menu b remove +check "remove: the remove event takes the window away" until_true 3 count_is scriptwidget 1 + +cp "$T/frames/$(ls "$T/frames" | grep '^a-' | head -1)" "$T/outside.png" +good=$T/frames/$(ls "$T/frames" | grep '^a-' | head -1) +"$sw" raw "{\"event\":\"widget\",\"id\":\"x\",\"screen\":\"\",\"x\":900,\"y\":100,\"width\":170,\"height\":170,\"image\":\"$T/outside.png\",\"version\":1,\"regions\":[]}" +"$sw" raw "{\"event\":\"widget\",\"id\":\"y\",\"screen\":\"\",\"x\":900,\"y\":100,\"width\":170,\"height\":170,\"image\":\"$T/frames/../outside.png\",\"version\":1,\"regions\":[]}" +"$sw" raw "{\"event\":\"widget\",\"id\":\"-z\",\"screen\":\"\",\"x\":900,\"y\":100,\"width\":170,\"height\":170,\"image\":\"$good\",\"version\":1,\"regions\":[]}" +"$sw" raw "{\"event\":\"widget\",\"id\":\"w\",\"screen\":\"\",\"x\":900,\"y\":100,\"width\":0,\"height\":170,\"image\":\"$good\",\"version\":1,\"regions\":[]}" +"$sw" raw 'not json at all' +sleep 0.8 +check "bad input: pictures outside the frames directory, bad ids and bad sizes are ignored" connected 1 +"$sw" raw "{\"event\":\"widget\",\"id\":\"c\",\"screen\":\"no-such-screen\",\"x\":900,\"y\":100,\"width\":170,\"height\":170,\"image\":\"$good\",\"version\":1,\"regions\":[]}" +check "screen: a widget for an unknown screen is shown on the first screen" until_true 3 has_layer "1 900 100 170 170" +"$sw" raw '{"event":"remove","id":"c"}' +until_true 3 count_is scriptwidget 1 + +# -------------------------------------- service gone and back; the bar entry + +bar_strip="0,0 ${W}x40" +click 960 300 +shot "$bar_strip" "$tmp/bar-running.png" +old_pid=$(watch_pid) +touch "$T/down"; stop_watch +check "stopped: with the service gone the bar entry knows it is not running" until_true 5 is status stopped +check "stopped: the last picture stays on the desktop" count_is scriptwidget 1 +sleep 0.3 +shot "$bar_strip" "$tmp/bar-stopped.png" +read -r ix iy iw ih <<< "$(changed_box "$tmp/bar-running.png" "$tmp/bar-stopped.png")" +if [ "${iw:-0}" -ge 4 ] && [ "${iw:-0}" -le 60 ]; then + pass "bar: the icon looks different while the service is not running" + icon_x=$((ix + iw / 2)); icon_y=$((iy + ih / 2)) + clear_log + click "$icon_x" "$icon_y"; sleep 0.5 + shot "0,0 700x300" "$out/5-bar-not-running.png" + click 960 300 +else + fail "bar: the icon looks different while the service is not running" "changed area: ${ix:-} ${iy:-} ${iw:-} ${ih:-}" + icon_x="" +fi +rm -f "$T/down" +check "reconnect: the service is found again after it comes back" until_true 25 connected 2 +check "reconnect: by a new run of watch" [ "$(watch_pid)" != "$old_pid" ] +check "reconnect: screen and theme are reported again" until_true 3 sh -c "grep -q 'command screen' '$T/log' && grep -q 'command theme' '$T/log'" + +if [ -n "$icon_x" ]; then + clear_log + click "$icon_x" "$icon_y" + check "bar: a click opens the management window" until_true 3 logged 'studio opened' + shot "0,0 440x400" "$tmp/popup-closed.png" + right_click "$icon_x" "$icon_y" + shot "0,0 440x400" "$tmp/popup-open.png" + shot "0,0 700x300" "$out/4-bar-menu.png" + read -r px py pw ph <<< "$(changed_box "$tmp/popup-closed.png" "$tmp/popup-open.png")" + if [ "${pw:-0}" -ge 150 ] && [ "${ph:-0}" -ge 120 ]; then + pass "bar: a right click opens the card with the two switches" + clear_log + click $((px + pw / 2)) $((py + ph * 38 / 100)) + check "bar: the first switch runs scriptwidget peek" until_true 3 logged 'command peek' + check "bar: and the widgets go to the overlay layer" until_true 3 sh -c "hyprctl -j layers | jq -e '.[].levels[\"3\"][] | select(.namespace == \"scriptwidget\")'" + click $((px + pw / 2)) $((py + ph * 38 / 100)) + check "bar: again, and they are back on the bottom layer" until_true 3 is peek false + clear_log + click $((px + pw / 2)) $((py + ph * 76 / 100)) + check "bar: the second switch runs scriptwidget arrange" until_true 3 logged 'command arrange' + until_true 3 is arrange true + click $((px + pw / 2)) $((py + ph * 76 / 100)) + check "bar: again, and arrange mode is off" until_true 3 is arrange false + else + fail "bar: a right click opens the card with the two switches" "changed area: ${px:-} ${py:-} ${pw:-} ${ph:-}" + fi + click 960 300 +else + skip "bar: clicks on the entry" "the icon was not found" +fi + +"$sw" bye +check "bye: after a bye event the service counts as not running" until_true 3 is status stopped +check "bye: and watch is run again" until_true 15 connected 2 + +# ------------------------------------------------------------ not installed + +old_pid=$(watch_pid) +mv "$sw" "$sw.off"; kill "$old_pid" +check "missing: a program that cannot be started is reported as not installed" until_true 5 is status missing +check "missing: the pictures stay" count_is scriptwidget 2 +mv "$sw.off" "$sw" +check "missing: found again once it is there" until_true 25 connected 2 + +# --------------------------------------------------------- protocol mismatch + +echo 2 > "$T/protocol"; stop_watch +check "protocol: hello with protocol 2 is refused" until_true 10 is status outdated +check "protocol: nothing is shown on the desktop" until_true 3 count_is scriptwidget 0 +if [ -n "$icon_x" ]; then + click "$icon_x" "$icon_y"; sleep 0.5 + shot "0,0 700x300" "$out/6-bar-please-update.png" + click 960 300 +fi +rm -f "$T/protocol"; stop_watch +check "protocol: with protocol 1 again the widgets are back" until_true 25 connected 2 + +# ------------------------------------------------------------ shell restart + +old_pid=$(watch_pid) +omarchy-restart-shell >/dev/null 2>&1 +check "restart: after omarchy-restart-shell the widgets are back" until_true 25 connected 2 +check "restart: the old watch was ended and a new one started" sh -c "! kill -0 $old_pid 2>/dev/null && [ \"\$(cat $T/watch.pid)\" != $old_pid ]" +sleep 0.5 +check "restart: two widget windows on the bottom layer" [ "$(widget_layers | grep -c '^1 ')" = 2 ] + +# ------------------------------------------------------------------ disable + +if [ "${CHECK_KEEP:-0}" != 1 ]; then + old_pid=$(watch_pid) + omarchy plugin disable "$id" >/dev/null 2>&1 + check "disable: the widget windows go away" until_true 5 count_is scriptwidget 0 + check "disable: watch is ended" until_true 5 sh -c "! kill -0 $old_pid 2>/dev/null" +fi + +echo +if [ "$failures" = 0 ]; then echo "all checks passed"; else echo "$failures failed"; fi +[ "$failures" = 0 ] diff --git a/Linux/omarchy-plugin/tests/click-catcher.sh b/Linux/omarchy-plugin/tests/click-catcher.sh new file mode 100755 index 00000000..375414eb --- /dev/null +++ b/Linux/omarchy-plugin/tests/click-catcher.sh @@ -0,0 +1,7 @@ +#!/bin/sh +# Runs inside a terminal window that the tests put below the widgets: turns on +# mouse reporting and writes what the terminal receives to the file named, so +# a click that reaches this window leaves a line like "<0;12;7M" there. +printf '\033[?1000h\033[?1006h' +stty raw -echo +exec cat > "$1" diff --git a/Linux/omarchy-plugin/tests/out/1-desktop.png b/Linux/omarchy-plugin/tests/out/1-desktop.png new file mode 100644 index 00000000..17079711 Binary files /dev/null and b/Linux/omarchy-plugin/tests/out/1-desktop.png differ diff --git a/Linux/omarchy-plugin/tests/out/2-peek-over-window.png b/Linux/omarchy-plugin/tests/out/2-peek-over-window.png new file mode 100644 index 00000000..028c19d4 Binary files /dev/null and b/Linux/omarchy-plugin/tests/out/2-peek-over-window.png differ diff --git a/Linux/omarchy-plugin/tests/out/3-arrange-menu.png b/Linux/omarchy-plugin/tests/out/3-arrange-menu.png new file mode 100644 index 00000000..ffb433b8 Binary files /dev/null and b/Linux/omarchy-plugin/tests/out/3-arrange-menu.png differ diff --git a/Linux/omarchy-plugin/tests/out/4-bar-menu.png b/Linux/omarchy-plugin/tests/out/4-bar-menu.png new file mode 100644 index 00000000..ab63c689 Binary files /dev/null and b/Linux/omarchy-plugin/tests/out/4-bar-menu.png differ diff --git a/Linux/omarchy-plugin/tests/out/5-bar-not-running.png b/Linux/omarchy-plugin/tests/out/5-bar-not-running.png new file mode 100644 index 00000000..5e718a89 Binary files /dev/null and b/Linux/omarchy-plugin/tests/out/5-bar-not-running.png differ diff --git a/Linux/omarchy-plugin/tests/out/6-bar-please-update.png b/Linux/omarchy-plugin/tests/out/6-bar-please-update.png new file mode 100644 index 00000000..bf5fead7 Binary files /dev/null and b/Linux/omarchy-plugin/tests/out/6-bar-please-update.png differ diff --git a/Linux/omarchy-plugin/tests/out/check.log b/Linux/omarchy-plugin/tests/out/check.log new file mode 100644 index 00000000..fe4446bc --- /dev/null +++ b/Linux/omarchy-plugin/tests/out/check.log @@ -0,0 +1,66 @@ +PASS validate: omarchy plugin validate accepts the plugin +PASS enable: omarchy plugin list shows it enabled +PASS connect: the service runs watch and shows two widgets +PASS layer: widget a is on the bottom layer at 80,80 329x170 +PASS layer: widget b is on the bottom layer at 460,80 170x170 +PASS layer: no other widget window +PASS report: scriptwidget screen +PASS report: scriptwidget theme dark or light +PASS swap: a new picture file is shown within a second +PASS swap: a new version under the same file name is shown +PASS frames: the pictures of b change +PASS frames: one widget is animating +PASS tap: a click in the region sends scriptwidget tap a 1 +PASS tap: clicks outside a region send nothing +PASS peek: the widgets move to the overlay layer +PASS pass through: a click on a widget without regions reaches the window below +PASS pass through: a click beside the region reaches the window below +PASS pass through: a click in the region is a tap +PASS pass through: and does not reach the window below +PASS pass through: in arrange mode the click stays with the widget +PASS peek: and back to the bottom layer +PASS frames: paused behind a fullscreen window +PASS frames: running again after it +PASS arrange: the mode event turns arrange mode on +PASS arrange: one window over the screen shows the widgets +PASS arrange: in place of the widget windows +PASS arrange: a drag sends scriptwidget move a 680 480 +PASS arrange: near the left edge and the bottom it snaps to 16 px from them, above the dock +PASS arrange: in the middle of the screen it stays where it is dropped +PASS menu: a right click opens the menu +PASS menu: a click elsewhere closes it and sends nothing +PASS menu: the second row sends scriptwidget menu b resize-medium +PASS arrange: off again +PASS arrange: the widget windows are back, where they were dropped +PASS menu: b now has the medium size +PASS remove: the remove event takes the window away +PASS bad input: pictures outside the frames directory, bad ids and bad sizes are ignored +PASS screen: a widget for an unknown screen is shown on the first screen +PASS stopped: with the service gone the bar entry knows it is not running +PASS stopped: the last picture stays on the desktop +PASS bar: the icon looks different while the service is not running +PASS reconnect: the service is found again after it comes back +PASS reconnect: by a new run of watch +PASS reconnect: screen and theme are reported again +PASS bar: a click opens the management window +PASS bar: a right click opens the card with the two switches +PASS bar: the first switch runs scriptwidget peek +PASS bar: and the widgets go to the overlay layer +PASS bar: again, and they are back on the bottom layer +PASS bar: the second switch runs scriptwidget arrange +PASS bar: again, and arrange mode is off +PASS bye: after a bye event the service counts as not running +PASS bye: and watch is run again +PASS missing: a program that cannot be started is reported as not installed +PASS missing: the pictures stay +PASS missing: found again once it is there +PASS protocol: hello with protocol 2 is refused +PASS protocol: nothing is shown on the desktop +PASS protocol: with protocol 1 again the widgets are back +PASS restart: after omarchy-restart-shell the widgets are back +PASS restart: the old watch was ended and a new one started +PASS restart: two widget windows on the bottom layer +PASS disable: the widget windows go away +PASS disable: watch is ended + +all checks passed diff --git a/Linux/omarchy-plugin/tests/pointer.py b/Linux/omarchy-plugin/tests/pointer.py new file mode 100644 index 00000000..f844ccbf --- /dev/null +++ b/Linux/omarchy-plugin/tests/pointer.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +"""Test helper for the Omarchy plugin: a virtual absolute pointer through /dev/uinput. +Run as root inside the test machine. pointer.py ... +Steps: move X Y | down | up | rdown | rup | sleep SECONDS +(down and up are the left button, rdown and rup the right one.)""" +import fcntl, os, struct, sys, time + +EV_SYN, EV_KEY, EV_ABS = 0, 1, 3 +ABS_X, ABS_Y, BTN_LEFT, BTN_RIGHT = 0, 1, 0x110, 0x111 +UI_SET_EVBIT, UI_SET_KEYBIT, UI_SET_ABSBIT, UI_SET_PROPBIT = 0x40045564, 0x40045565, 0x40045567, 0x4004556e +UI_DEV_SETUP, UI_ABS_SETUP, UI_DEV_CREATE, UI_DEV_DESTROY = 0x405c5503, 0x401c5504, 0x5501, 0x5502 + +width, height = int(sys.argv[1]), int(sys.argv[2]) +fd = os.open("/dev/uinput", os.O_WRONLY | os.O_NONBLOCK) +for bit in (EV_KEY, EV_ABS): fcntl.ioctl(fd, UI_SET_EVBIT, bit) +for button in (BTN_LEFT, BTN_RIGHT): fcntl.ioctl(fd, UI_SET_KEYBIT, button) +for axis, limit in ((ABS_X, width - 1), (ABS_Y, height - 1)): + fcntl.ioctl(fd, UI_SET_ABSBIT, axis) + fcntl.ioctl(fd, UI_ABS_SETUP, struct.pack("HHiiiiii", axis, 0, 0, 0, limit, 0, 0, 0)) +fcntl.ioctl(fd, UI_DEV_SETUP, struct.pack("HHHH80sI", 3, 0x1234, 0x5678, 1, b"scriptwidget-test-pointer", 0)) +fcntl.ioctl(fd, UI_DEV_CREATE) +time.sleep(1.0) + +def event(kind, code, value): + os.write(fd, struct.pack("llHHi", 0, 0, kind, code, value)) + +steps = sys.argv[3:] +index = 0 +while index < len(steps): + step = steps[index]; index += 1 + if step == "move": + event(EV_ABS, ABS_X, int(steps[index])); event(EV_ABS, ABS_Y, int(steps[index + 1])); index += 2 + elif step == "down": event(EV_KEY, BTN_LEFT, 1) + elif step == "up": event(EV_KEY, BTN_LEFT, 0) + elif step == "rdown": event(EV_KEY, BTN_RIGHT, 1) + elif step == "rup": event(EV_KEY, BTN_RIGHT, 0) + elif step == "sleep": time.sleep(float(steps[index])); index += 1; continue + event(EV_SYN, 0, 0) + time.sleep(0.03) +time.sleep(0.3) +fcntl.ioctl(fd, UI_DEV_DESTROY) diff --git a/Linux/omarchy-plugin/tests/scriptwidget b/Linux/omarchy-plugin/tests/scriptwidget new file mode 100755 index 00000000..23b0e8db --- /dev/null +++ b/Linux/omarchy-plugin/tests/scriptwidget @@ -0,0 +1,166 @@ +#!/bin/bash +# A stand-in for the `scriptwidget` program, for testing the Omarchy plugin +# without the real background service. It is not installed with the plugin. +# +# It speaks the display protocol of docs/linux/plan.md section 4.8: +# scriptwidget watch prints one JSON event per line +# scriptwidget tap \ +# scriptwidget move | +# scriptwidget screen +# scriptwidget theme | logged, and passed to `watch` +# scriptwidget menu | through a named pipe +# scriptwidget peek | arrange / +# and, for the tests only: +# scriptwidget raw `watch` prints the line as it is +# scriptwidget bye `watch` prints a bye event and exits +# +# Its files are in the folder that holds the frames directory named by +# SCRIPTWIDGET_PLUGIN_FRAMES (the same variable the plugin reads): +# frames/ the pictures, drawn with ImageMagick +# log every command received, one per line +# ctl the named pipe +# down if this file exists, the "service" is not running: exit code 69 +# protocol if this file exists, the protocol number `hello` announces +# +# It shows two widgets: `a` (medium, with a "+1" button, tap region 1) and +# `b` (small, four frames, no tap region). + +frames=${SCRIPTWIDGET_PLUGIN_FRAMES:-${XDG_RUNTIME_DIR:?}/scriptwidget-plugin-test/frames} +dir=${frames%/frames} +fifo="$dir/ctl" +log="$dir/log" +pidfile="$dir/watch.pid" + +stamp() { date +%H:%M:%S.%N | cut -c1-12; } +note() { echo "$(stamp) $*" >> "$log"; } +serving() { [ -f "$pidfile" ] && [ -p "$fifo" ] && kill -0 "$(cat "$pidfile")" 2>/dev/null; } + +verb=${1:-} +[ -n "$verb" ] || { echo "usage: scriptwidget " >&2; exit 2; } +shift + +if [ "$verb" != watch ]; then + mkdir -p "$dir" + if [ -e "$dir/down" ] || ! serving; then + note "refused $verb $*" + echo "error: [service_unavailable] the background service is not running" >&2 + exit 69 + fi + note "command $verb $*" + printf '%s\n' "$verb $*" > "$fifo" + exit 0 +fi + +# ---------------------------------------------------------------- watch + +mkdir -p -m 700 "$dir" "$frames" +if [ -e "$dir/down" ]; then + note "watch refused: down" + echo "error: [service_unavailable] the background service is not running" >&2 + exit 69 +fi +[ -f "$pidfile" ] && kill "$(cat "$pidfile")" 2>/dev/null +echo $$ > "$pidfile" +rm -f "$fifo" "$frames"/*.png +mkfifo -m 600 "$fifo" +exec 3<>"$fifo" + +protocol=1 +[ -f "$dir/protocol" ] && protocol=$(tr -cd '0-9' < "$dir/protocol") +arrange=false; peek=false +ascreen=""; ax=80; ay=80; aw=329; ah=170; aversion=0; count=0 +bscreen=""; bx=460; by=80; bw=170; bh=170; bversion=0 +colors=("#e64553" "#fe640b" "#40a02b" "#1e66f5") + +note "watch started pid $$ protocol $protocol" +echo "{\"event\":\"hello\",\"protocol\":$protocol,\"dark\":true,\"arrange\":$arrange,\"peek\":$peek}" + +# draw_a : the medium widget, at twice its logical size. +draw_a() { + local w=$((aw * 2)) h=$((ah * 2)) left=$(((aw - 128) / 2 * 2)) top=$(((ah - 65) * 2)) + magick -size "${w}x${h}" xc:none -fill "#1e1e2ee6" -draw "roundrectangle 0,0 $((w - 1)),$((h - 1)) 44,44" \ + -fill white -pointsize 44 -gravity north -annotate +0+24 "count $count" \ + -pointsize 44 -annotate +0+84 "version $aversion" \ + -fill "#1e66f5" -draw "roundrectangle $left,$top $((left + 256)),$((top + 90)) 24,24" \ + -fill white -pointsize 40 -gravity northwest -annotate "+$((left + 100))+$((top + 22))" "+1" \ + "$dir/tmp-a.png" && mv "$dir/tmp-a.png" "$1" +} + +# emit_a [same]: a new picture of `a`. With `same`, under the file name it had. +emit_a() { + local file + aversion=$((aversion + 1)) + if [ "${1:-}" = same ] && [ -f "$afile" ]; then file=$afile; else file="$frames/a-$aversion.png"; fi + draw_a "$file" + echo "{\"event\":\"widget\",\"id\":\"a\",\"screen\":\"$ascreen\",\"x\":$ax,\"y\":$ay,\"width\":$aw,\"height\":$ah,\"image\":\"$file\",\"version\":$aversion,\"regions\":[{\"n\":1,\"x\":$(((aw - 128) / 2)),\"y\":$((ah - 65)),\"width\":128,\"height\":45}]}" + note "emit a version $aversion $file" + [ "$afile" != "$file" ] && [ -n "$afile" ] && { sleep 0.3; rm -f "$afile"; } + afile=$file +} +afile="" + +emit_b() { + local list="" index file + bversion=$((bversion + 1)) + for index in 0 1 2 3; do + file="$frames/b-$bversion-$index.png" + magick -size "$((bw * 2))x$((bh * 2))" xc:none -fill "${colors[$index]}" \ + -draw "roundrectangle 0,0 $((bw * 2 - 1)),$((bh * 2 - 1)) 44,44" \ + -fill white -pointsize 120 -gravity center -annotate 0 "$index" "$dir/tmp-b.png" && mv "$dir/tmp-b.png" "$file" + list="$list{\"image\":\"$file\",\"ms\":250}," + done + echo "{\"event\":\"widget\",\"id\":\"b\",\"screen\":\"$bscreen\",\"x\":$bx,\"y\":$by,\"width\":$bw,\"height\":$bh,\"image\":\"$frames/b-$bversion-0.png\",\"version\":$bversion,\"regions\":[],\"frames\":[${list%,}]}" + note "emit b version $bversion" + [ "$bversion" -gt 1 ] && { sleep 0.3; rm -f "$frames"/b-$((bversion - 1))-*.png; } +} + +number() { case "$1" in ""|*[!0-9-]*) return 1 ;; esac; } + +resize() { + case "$1" in + resize-small) echo "170 170" ;; + resize-medium) echo "329 170" ;; + resize-large) echo "329 345" ;; + resize-extraLarge) echo "639 345" ;; + esac +} + +aalive=true; balive=true +emit_a; emit_b + +while read -r -u 3 verb one two three four; do + case "$verb" in + tap) + [ "$one" = a ] && [ "$two" = 1 ] && $aalive && { count=$((count + 1)); emit_a; } ;; + move) + number "$three" && number "$four" || continue + if [ "$one" = a ] && $aalive; then ascreen=$two; ax=$three; ay=$four; emit_a same + elif [ "$one" = b ] && $balive; then bscreen=$two; bx=$three; by=$four; emit_b; fi ;; + menu) + case "$two" in + resize-*) + read -r w h <<< "$(resize "$two")" + [ -n "$w" ] || continue + if [ "$one" = a ] && $aalive; then aw=$w; ah=$h; emit_a + elif [ "$one" = b ] && $balive; then bw=$w; bh=$h; emit_b; fi ;; + refresh) + if [ "$one" = a ] && $aalive; then emit_a same; elif [ "$one" = b ] && $balive; then emit_b; fi ;; + remove) + if [ "$one" = a ]; then aalive=false; elif [ "$one" = b ]; then balive=false; else continue; fi + echo "{\"event\":\"remove\",\"id\":\"$one\"}" ;; + esac ;; + peek) + if $peek; then peek=false; else peek=true; fi + echo "{\"event\":\"mode\",\"arrange\":$arrange,\"peek\":$peek}" ;; + arrange) + if $arrange; then arrange=false; else arrange=true; fi + echo "{\"event\":\"mode\",\"arrange\":$arrange,\"peek\":$peek}" ;; + raw) + printf '%s\n' "$one${two:+ $two}${three:+ $three}${four:+ $four}" ;; + bye) + echo '{"event":"bye","reason":"test"}' + note "watch said bye" + rm -f "$pidfile" + exit 0 ;; + esac +done diff --git a/Linux/omarchy-plugin/tests/scriptwidget-studio b/Linux/omarchy-plugin/tests/scriptwidget-studio new file mode 100755 index 00000000..947ce258 --- /dev/null +++ b/Linux/omarchy-plugin/tests/scriptwidget-studio @@ -0,0 +1,6 @@ +#!/bin/bash +# A stand-in for the management window, for the tests: it only writes a line +# to the stand-in's log. +frames=${SCRIPTWIDGET_PLUGIN_FRAMES:-${XDG_RUNTIME_DIR:?}/scriptwidget-plugin-test/frames} +mkdir -p "${frames%/frames}" +echo "$(date +%H:%M:%S.%N | cut -c1-12) studio opened $*" >> "${frames%/frames}/log" diff --git a/Linux/packaging/icons/hicolor/128x128/apps/scriptwidget-studio.png b/Linux/packaging/icons/hicolor/128x128/apps/scriptwidget-studio.png new file mode 100644 index 00000000..1444d094 Binary files /dev/null and b/Linux/packaging/icons/hicolor/128x128/apps/scriptwidget-studio.png differ diff --git a/Linux/packaging/icons/hicolor/256x256/apps/scriptwidget-studio.png b/Linux/packaging/icons/hicolor/256x256/apps/scriptwidget-studio.png new file mode 100644 index 00000000..14897f50 Binary files /dev/null and b/Linux/packaging/icons/hicolor/256x256/apps/scriptwidget-studio.png differ diff --git a/Linux/packaging/icons/hicolor/48x48/apps/scriptwidget-studio.png b/Linux/packaging/icons/hicolor/48x48/apps/scriptwidget-studio.png new file mode 100644 index 00000000..d07b0585 Binary files /dev/null and b/Linux/packaging/icons/hicolor/48x48/apps/scriptwidget-studio.png differ diff --git a/Linux/packaging/make-icons.sh b/Linux/packaging/make-icons.sh new file mode 100644 index 00000000..cb59df3a --- /dev/null +++ b/Linux/packaging/make-icons.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# Makes the icons of the management window from the shared app icon +# (Resource/AppIcon/ScriptWidget-AppIcon-2026.png), the one the Windows and +# web builds use. The output is checked in; run this again only when the app +# icon changes. Needs ImageMagick (`magick`) or, on a Mac, `sips`. +# +# sh Linux/packaging/make-icons.sh +# +# A package installs icons/hicolor//apps/scriptwidget-studio.png under +# /usr/share/icons/hicolor, scriptwidget-studio.desktop under +# /usr/share/applications and scriptwidget-studio.xml under +# /usr/share/mime/packages. +set -eu +repository=$(cd "$(dirname "$0")/../.." && pwd) +source="$repository/Resource/AppIcon/ScriptWidget-AppIcon-2026.png" +for size in 48 128 256; do + target="$repository/Linux/packaging/icons/hicolor/${size}x${size}/apps" + mkdir -p "$target" + if command -v magick >/dev/null 2>&1; then + magick "$source" -resize "${size}x${size}" "$target/scriptwidget-studio.png" + else + sips -s format png -z "$size" "$size" "$source" --out "$target/scriptwidget-studio.png" >/dev/null + fi +done diff --git a/Linux/packaging/release/README.txt b/Linux/packaging/release/README.txt new file mode 100644 index 00000000..b0307e03 --- /dev/null +++ b/Linux/packaging/release/README.txt @@ -0,0 +1,28 @@ +ScriptWidget for Linux (preview) +================================ + +Desktop widgets written in JavaScript and JSX. This preview shows widgets on +the desktop of Omarchy 4.5; on other desktops the command line and the +management window work, but nothing is shown on the desktop yet. + +Install (for your user, no root): + + sh install.sh + +It puts the programs in ~/.local/bin, starts the background service as a +systemd user unit and, on Omarchy, enables the display plugin. + +Then open "ScriptWidget" from the launcher (or run scriptwidget-studio), pick +a template, and choose "Add to desktop". To move widgets, right-click the +ScriptWidget icon in the bar and turn on "Arrange widgets". + +Needs, from your distribution: ICU 78 (libicu), fontconfig, FreeType, and a +Vulkan driver for the management window. Chinese, Japanese, Korean and emoji +text uses your system fonts. + +Remove: + + sh uninstall.sh keeps your widgets + sh uninstall.sh --purge deletes them too + +Documentation: https://github.com/everettjf/scriptwidget/tree/main/docs/linux diff --git a/Linux/packaging/release/THIRD-PARTY-NOTICES.txt b/Linux/packaging/release/THIRD-PARTY-NOTICES.txt new file mode 100644 index 00000000..673ce2bc --- /dev/null +++ b/Linux/packaging/release/THIRD-PARTY-NOTICES.txt @@ -0,0 +1,25 @@ +ScriptWidget for Linux contains or links these third-party works. The exact +versions of the Rust crates are in Linux/Cargo.lock of the source repository, +https://github.com/everettjf/scriptwidget; each crate's licence text is in its +own source distribution on crates.io. + +Built into the programs +- QuickJS-NG (MIT) - JavaScript engine +- Skia, through skia-safe and skia-bindings (BSD-3-Clause, MIT) - drawing and text layout +- GPUI (gpui-pre) and gpui-kit / gpui-component (Apache-2.0) - management window +- Lucide icons (ISC) - management window icons +- rustls, ring, rustls-native-certs, webpki (Apache-2.0 / ISC / MIT, ring: see its LICENSE) - TLS +- serde, serde_json, url, sha2, flate2, miniz_oxide, rustix, libc, httparse, + unicode-* and the other crates listed in Cargo.lock (MIT and/or Apache-2.0 + unless their own files say otherwise) +- Inter (SIL Open Font License 1.1, see OFL-Inter.txt) + +Shipped as files (share/scriptwidget/runtime) +- Babel standalone (MIT) - JSX compiler +- Moment.js (MIT) + +Used from your system, not shipped +- ICU, fontconfig, FreeType, Vulkan loader and drivers, system fonts + +The full machine-generated list of every crate licence is not part of this +preview archive. diff --git a/Linux/packaging/release/install.sh b/Linux/packaging/release/install.sh new file mode 100644 index 00000000..181b0074 --- /dev/null +++ b/Linux/packaging/release/install.sh @@ -0,0 +1,100 @@ +#!/bin/sh +# Installs ScriptWidget for the current user. No root needed. +# +# sh install.sh [--prefix ] [--no-plugin] [--no-service] +# +# Default prefix: ~/.local (programs in /bin, data in /share). +# The background service becomes a systemd user unit; on Omarchy the display +# plugin is copied to ~/.config/omarchy/plugins and enabled. +set -eu + +here=$(cd "$(dirname "$0")" && pwd) +prefix="$HOME/.local" +plugin=1 +service=1 +while [ "$#" -gt 0 ]; do + case "$1" in + --prefix) [ "$#" -ge 2 ] || { echo "--prefix needs a folder" >&2; exit 2; }; prefix=$2; shift 2 ;; + --no-plugin) plugin=0; shift ;; + --no-service) service=0; shift ;; + -h|--help) sed -n '2,9p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; + *) echo "unknown option: $1" >&2; exit 2 ;; + esac +done +case "$prefix" in /*) ;; *) echo "the prefix must be an absolute path" >&2; exit 2 ;; esac +if [ "$(id -u)" -eq 0 ]; then + echo "run this as your own user, not as root: the service and the widgets belong to a user" >&2 + exit 2 +fi + +plugin_id=com.atomsprout.scriptwidget +config="${XDG_CONFIG_HOME:-$HOME/.config}" +unit_folder="$config/systemd/user" +plugin_folder="$config/omarchy/plugins/$plugin_id" + +# Stop a running copy before its files are replaced. +if command -v systemctl >/dev/null 2>&1; then + systemctl --user stop scriptwidget.service >/dev/null 2>&1 || true +fi + +echo "Installing ScriptWidget into $prefix" +mkdir -p "$prefix/bin" "$prefix/share" +for program in scriptwidget scriptwidget-studio; do + [ -f "$here/bin/$program" ] || continue + # Write beside the target and rename, so a running program is never half replaced. + cp "$here/bin/$program" "$prefix/bin/.$program.new" + chmod 755 "$prefix/bin/.$program.new" + mv -f "$prefix/bin/.$program.new" "$prefix/bin/$program" +done +rm -rf "$prefix/share/scriptwidget/runtime" "$prefix/share/scriptwidget/templates" +mkdir -p "$prefix/share/scriptwidget" +cp -R "$here/share/scriptwidget/runtime" "$here/share/scriptwidget/templates" "$prefix/share/scriptwidget/" +for part in applications icons mime; do + [ -d "$here/share/$part" ] && cp -R "$here/share/$part" "$prefix/share/" +done +command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$prefix/share/applications" >/dev/null 2>&1 || true +command -v update-mime-database >/dev/null 2>&1 && update-mime-database "$prefix/share/mime" >/dev/null 2>&1 || true + +if [ "$service" -eq 1 ] && command -v systemctl >/dev/null 2>&1; then + mkdir -p "$unit_folder" + sed "s|^ExecStart=.*|ExecStart=$prefix/bin/scriptwidget host run|" "$here/lib/systemd/user/scriptwidget.service" > "$unit_folder/scriptwidget.service" + systemctl --user daemon-reload + if systemctl --user enable --now scriptwidget.service >/dev/null 2>&1; then + echo "The background service is running (systemctl --user status scriptwidget)." + else + echo "The background service could not be started now. Start it from a graphical session with:" >&2 + echo " systemctl --user enable --now scriptwidget.service" >&2 + fi +else + echo "The background service was not set up. Start it by hand with: $prefix/bin/scriptwidget host run" +fi + +if [ "$plugin" -eq 1 ]; then + if command -v omarchy >/dev/null 2>&1; then + rm -rf "$plugin_folder" + mkdir -p "$plugin_folder" + cp -R "$here/omarchy-plugin/." "$plugin_folder/" + omarchy-shell shell rescanPlugins >/dev/null 2>&1 || true + # The shell needs a moment to find the new folder before it can be enabled. + enabled=0 + for attempt in 1 2 3 4 5; do + sleep 1 + if omarchy plugin enable "$plugin_id" >/dev/null 2>&1; then enabled=1; break; fi + done + if [ "$enabled" -eq 1 ]; then + echo "The Omarchy plugin is enabled. If widgets do not appear, run: omarchy restart shell" + else + echo "The Omarchy plugin was copied to $plugin_folder. Enable it from a graphical session with:" >&2 + echo " omarchy plugin enable $plugin_id" >&2 + fi + else + echo "Omarchy was not found: the display plugin was not installed, so widgets will not show on the desktop." + echo "The command line and the management window work without it." + fi +fi + +case ":$PATH:" in + *":$prefix/bin:"*) ;; + *) echo "Note: $prefix/bin is not on your PATH." ;; +esac +echo "Done. Open ScriptWidget from the launcher, or run: scriptwidget-studio" diff --git a/Linux/packaging/release/uninstall.sh b/Linux/packaging/release/uninstall.sh new file mode 100644 index 00000000..e206ab34 --- /dev/null +++ b/Linux/packaging/release/uninstall.sh @@ -0,0 +1,52 @@ +#!/bin/sh +# Removes what install.sh installed. +# +# sh uninstall.sh [--prefix ] [--purge] +# +# --purge also deletes your widgets and settings (~/.local/share/scriptwidget +# data folders and the cache). Without it they are kept. +set -eu + +prefix="$HOME/.local" +purge=0 +while [ "$#" -gt 0 ]; do + case "$1" in + --prefix) [ "$#" -ge 2 ] || { echo "--prefix needs a folder" >&2; exit 2; }; prefix=$2; shift 2 ;; + --purge) purge=1; shift ;; + -h|--help) sed -n '2,7p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; + *) echo "unknown option: $1" >&2; exit 2 ;; + esac +done +case "$prefix" in /*) ;; *) echo "the prefix must be an absolute path" >&2; exit 2 ;; esac + +plugin_id=com.atomsprout.scriptwidget +config="${XDG_CONFIG_HOME:-$HOME/.config}" + +if command -v omarchy >/dev/null 2>&1; then + omarchy plugin disable "$plugin_id" >/dev/null 2>&1 || true +fi +rm -rf "$config/omarchy/plugins/$plugin_id" +command -v omarchy-shell >/dev/null 2>&1 && omarchy-shell shell rescanPlugins >/dev/null 2>&1 || true + +if command -v systemctl >/dev/null 2>&1; then + systemctl --user disable --now scriptwidget.service >/dev/null 2>&1 || true +fi +rm -f "$config/systemd/user/scriptwidget.service" +command -v systemctl >/dev/null 2>&1 && systemctl --user daemon-reload >/dev/null 2>&1 || true + +rm -f "$prefix/bin/scriptwidget" "$prefix/bin/scriptwidget-studio" +rm -rf "$prefix/share/scriptwidget/runtime" "$prefix/share/scriptwidget/templates" "$prefix/share/scriptwidget/fonts" +rm -f "$prefix/share/applications/scriptwidget-studio.desktop" "$prefix/share/mime/packages/scriptwidget-studio.xml" +find "$prefix/share/icons/hicolor" -name 'scriptwidget-studio.png' -exec rm -f {} + 2>/dev/null || true +command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$prefix/share/applications" >/dev/null 2>&1 || true +command -v update-mime-database >/dev/null 2>&1 && update-mime-database "$prefix/share/mime" >/dev/null 2>&1 || true + +data="${SCRIPTWIDGET_DATA:-${XDG_DATA_HOME:-$HOME/.local/share}/scriptwidget}" +cache="${XDG_CACHE_HOME:-$HOME/.cache}/scriptwidget" +if [ "$purge" -eq 1 ]; then + rm -rf "$data" "$cache" + echo "ScriptWidget and your widgets were removed." +else + rmdir "$prefix/share/scriptwidget" 2>/dev/null || true + echo "ScriptWidget was removed. Your widgets and settings are kept in $data (delete with --purge)." +fi diff --git a/Linux/packaging/scriptwidget-studio.desktop b/Linux/packaging/scriptwidget-studio.desktop new file mode 100644 index 00000000..34cd65e6 --- /dev/null +++ b/Linux/packaging/scriptwidget-studio.desktop @@ -0,0 +1,19 @@ +[Desktop Entry] +Type=Application +Version=1.5 +Name=ScriptWidget +Name[zh_CN]=ScriptWidget +GenericName=Widget Editor +GenericName[zh_CN]=小组件编辑器 +Comment=Make desktop widgets with JavaScript and JSX +Comment[zh_CN]=用 JavaScript 和 JSX 制作桌面小组件 +# %f: one local path. A .swt file is shown first (name, files, network hosts) and imported only when confirmed. +Exec=scriptwidget-studio %f +Icon=scriptwidget-studio +Terminal=false +Categories=Development; +Keywords=widget;desktop;javascript;jsx; +MimeType=application/x-scriptwidget-package; +# The app_id of the window (Linux/crates/sw-studio/src/ui/mod.rs). +StartupWMClass=scriptwidget-studio +StartupNotify=true diff --git a/Linux/packaging/scriptwidget-studio.xml b/Linux/packaging/scriptwidget-studio.xml new file mode 100644 index 00000000..8cdda2f8 --- /dev/null +++ b/Linux/packaging/scriptwidget-studio.xml @@ -0,0 +1,13 @@ + + + + + ScriptWidget widget + ScriptWidget 小组件 + + + + + diff --git a/Linux/packaging/systemd/scriptwidget.service b/Linux/packaging/systemd/scriptwidget.service new file mode 100644 index 00000000..1964719b --- /dev/null +++ b/Linux/packaging/systemd/scriptwidget.service @@ -0,0 +1,33 @@ +# The ScriptWidget background service, as a systemd user unit. +# +# Install as /usr/lib/systemd/user/scriptwidget.service (a package) or +# ~/.config/systemd/user/scriptwidget.service (by hand), then: +# +# systemctl --user enable --now scriptwidget.service +# +# It starts with the graphical session and stops with it. The service opens +# no window; the display (the Omarchy plugin) connects to its socket at +# $XDG_RUNTIME_DIR/scriptwidget/host.sock. + +[Unit] +Description=ScriptWidget background service +Documentation=https://github.com/everettjf/ScriptWidget/blob/main/docs/linux/README.md +PartOf=graphical-session.target +After=graphical-session.target + +[Service] +Type=exec +# A package installs the program in /usr/bin; change the path for another prefix. +ExecStart=/usr/bin/scriptwidget host run +# On SIGTERM the service tells every display "bye", removes its socket and exits with 0. +TimeoutStopSec=10 +Restart=on-failure +RestartSec=2 +# Only the service itself is stopped. A browser that a link in a widget +# started, and the management window, are the user's programs: they must not +# be closed when the service is restarted or updated. +KillMode=process +Slice=app.slice + +[Install] +WantedBy=graphical-session.target diff --git a/Linux/scripts/asan-test.sh b/Linux/scripts/asan-test.sh new file mode 100755 index 00000000..a0d3f808 --- /dev/null +++ b/Linux/scripts/asan-test.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# Runs the engine binding tests with AddressSanitizer in the C side (QuickJS and +# the binding). Linux only. The sanitizer runtime is preloaded into the test +# programs alone, not into cargo or the compiler. +set -eu +cd "$(dirname "$0")/.." +host=$(rustc -vV | sed -n 's/^host: //p') +runner=CARGO_TARGET_$(printf '%s' "$host" | tr 'a-z-' 'A-Z_')_RUNNER +runtime=$(cc -print-file-name=libasan.so) +[ -e "$runtime" ] || { echo "libasan.so not found: install the compiler's sanitizer runtime" >&2; exit 1; } +export SW_QUICKJS_ASAN=1 +export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-target/asan}" +# Leak detection is off: the Rust test harness itself is not instrumented. +export "$runner=env LD_PRELOAD=$runtime ASAN_OPTIONS=detect_leaks=0:abort_on_error=1" +exec cargo test --locked -p sw-quickjs --tests "$@" diff --git a/Linux/scripts/build-shared-js.mjs b/Linux/scripts/build-shared-js.mjs new file mode 100644 index 00000000..dc6089ad --- /dev/null +++ b/Linux/scripts/build-shared-js.mjs @@ -0,0 +1,42 @@ +// Generates the JavaScript assets the Linux runtime shares with the web +// preview engine, the Android app and the Windows app. Run after changing +// Web/preview-engine/src, the support bundle, or Android/core/src/main/js: +// +// node Linux/scripts/build-shared-js.mjs +// +// Output (not checked in): Linux/assets/scriptwidget/ and the parity corpus +// in Linux/tests/assets/. +import fs from "node:fs"; +import path from "node:path"; +import { pathToFileURL } from "node:url"; +import { buildSharedJS, repository } from "../../Scripts/lib/shared-js.mjs"; + +await buildSharedJS({ + output: path.join(repository, "Linux/assets/scriptwidget"), + banner: "// Generated by Linux/scripts/build-shared-js.mjs. Do not edit.\n", + compilerFingerprint: "scriptwidget-linux-babel-v1", + // The QuickJS host files are shared with Android and Windows. API files + // are evaluated in name order after host.js; the fallbacks in apis.js come + // last. + hostFiles: [ + ["android-host.js", "host.js"], + ["android-apis-datasource.js", "apis-10-datasource.js"], + ["android-apis-weather.js", "apis-20-weather.js"], + ["android-apis.js", "apis-90.js"], + ["android-compiler.js", "compiler.js"], + ], + // The Android directory keeps the module paths inside render.js identical + // to the Android and Windows bundles. + bundleDirectory: path.join(repository, "Android"), + corpusOutput: path.join(repository, "Linux/tests/assets"), +}); + +// What the management window's code editor completes from: the metadata the +// other editors use (Editor/editorfe/src/scriptWidgetAPI.js), as data, the +// way Windows/scripts/build-shared-js.mjs writes it. Linux/crates/sw-studio +// builds it into the program (see its build.rs). +const { scriptWidgetAPI } = await import(pathToFileURL(path.join(repository, "Editor/editorfe/src/scriptWidgetAPI.js")).href); +fs.writeFileSync( + path.join(repository, "Linux/assets/scriptwidget/api-reference.json"), + JSON.stringify({ functions: scriptWidgetAPI.functions, components: scriptWidgetAPI.components, globalProperties: scriptWidgetAPI.globalProperties }) + "\n", +); diff --git a/Linux/scripts/package.sh b/Linux/scripts/package.sh new file mode 100755 index 00000000..886ce21a --- /dev/null +++ b/Linux/scripts/package.sh @@ -0,0 +1,58 @@ +#!/bin/sh +# Builds the release archive of ScriptWidget for Linux: +# +# Linux/artifacts/scriptwidget--linux-.tar.gz and its .sha256 +# +# sh Linux/scripts/package.sh [--no-studio] +# +# Run on the architecture the archive is for, after +# `node Linux/scripts/build-shared-js.mjs`. Needs the Rust toolchain and the +# development files the build needs (see docs/linux/README.md). +set -eu + +studio=1 +[ "${1:-}" = "--no-studio" ] && studio=0 +repository=$(cd "$(dirname "$0")/../.." && pwd) +cd "$repository/Linux" + +version=$(sed -n '/^\[workspace.package\]/,/^\[/s/^version = "\(.*\)"/\1/p' Cargo.toml) +arch=$(uname -m) +name="scriptwidget-$version-linux-$arch" +target="${CARGO_TARGET_DIR:-$repository/Linux/target}" +stage="$repository/Linux/artifacts/$name" + +cargo build --locked --release -p sw-cli +[ "$studio" -eq 1 ] && cargo build --locked --release -p sw-studio --features ui + +rm -rf "$stage" "$stage.tar.gz" "$stage.tar.gz.sha256" +mkdir -p "$stage/bin" "$stage/share/applications" "$stage/share/mime/packages" "$stage/lib/systemd/user" +cp "$target/release/scriptwidget" "$stage/bin/" +[ "$studio" -eq 1 ] && cp "$target/release/scriptwidget-studio" "$stage/bin/" +strip "$stage"/bin/* 2>/dev/null || true +sh "$repository/Linux/scripts/stage-resources.sh" "$stage/share/scriptwidget" +cp packaging/scriptwidget-studio.desktop "$stage/share/applications/" +cp packaging/scriptwidget-studio.xml "$stage/share/mime/packages/" +cp -R packaging/icons "$stage/share/icons" +cp packaging/systemd/scriptwidget.service "$stage/lib/systemd/user/" +mkdir -p "$stage/omarchy-plugin" +for item in manifest.json Service.qml WidgetWindow.qml ArrangeWindow.qml BarWidget.qml README.md LICENSE i18n; do + cp -R "omarchy-plugin/$item" "$stage/omarchy-plugin/" +done +cp packaging/release/install.sh packaging/release/uninstall.sh packaging/release/README.txt "$stage/" +cp packaging/release/THIRD-PARTY-NOTICES.txt "$stage/" +cp "$repository/LICENSE" "$stage/" +cp "$repository/Android/core/src/main/assets/fonts/OFL-Inter.txt" "$stage/" +chmod 755 "$stage/install.sh" "$stage/uninstall.sh" +find "$stage" -name '.DS_Store' -delete + +# The archive must not carry the six display fonts (see sw-render/src/fonts.rs). +if find "$stage" -iname '*.ttf' -o -iname '*.otf' | grep -q .; then + echo "font files found in the archive" >&2 + exit 1 +fi + +cd "$repository/Linux/artifacts" +tar --owner=0 --group=0 --numeric-owner -czf "$name.tar.gz" "$name" 2>/dev/null || tar -czf "$name.tar.gz" "$name" +if command -v sha256sum >/dev/null 2>&1; then sha256sum "$name.tar.gz" > "$name.tar.gz.sha256"; else shasum -a 256 "$name.tar.gz" > "$name.tar.gz.sha256"; fi +ls -l "$name.tar.gz" +cat "$name.tar.gz.sha256" diff --git a/Linux/scripts/stage-resources.sh b/Linux/scripts/stage-resources.sh new file mode 100644 index 00000000..5f870e2d --- /dev/null +++ b/Linux/scripts/stage-resources.sh @@ -0,0 +1,48 @@ +#!/bin/sh +# Assembles the read-only data directory the Linux programs look for at run +# time (see Linux/crates/sw-core/src/resources.rs): +# +# /runtime/ the generated JavaScript (Linux/assets/scriptwidget) +# /templates/ the bundled templates, as the Windows build ships them: +# Script.bundle without __Build, dot-files and preview.png +# +# sh Linux/scripts/stage-resources.sh +# +# A package installs the result as /usr/share/scriptwidget; an unpacked +# release has it as share/scriptwidget next to bin/. For development, +# SCRIPTWIDGET_RESOURCES= points the programs at it. +# Run `node Linux/scripts/build-shared-js.mjs` first. +set -eu + +if [ "$#" -ne 1 ]; then + echo "usage: stage-resources.sh " >&2 + exit 2 +fi +destination=$1 +repository=$(cd "$(dirname "$0")/../.." && pwd) +assets="$repository/Linux/assets/scriptwidget" +bundle="$repository/Shared/ScriptWidgetRuntime/Resource/Script.bundle" + +if [ ! -f "$assets/kernel.js" ]; then + echo "$assets/kernel.js is missing: run node Linux/scripts/build-shared-js.mjs" >&2 + exit 1 +fi +if [ ! -d "$bundle/template" ]; then + echo "$bundle is missing" >&2 + exit 1 +fi + +rm -rf "$destination/runtime" "$destination/templates" +mkdir -p "$destination/runtime" "$destination/templates" +cp "$assets"/*.js "$assets"/contract.json "$destination/runtime/" +for group in template component api; do + cp -R "$bundle/$group" "$destination/templates/$group" +done +# What is not part of a shipped template. +find "$destination/templates" -depth \( -name '__Build' -o -name '.*' -o -name 'preview.png' \) -exec rm -rf {} + +# Links are never read as package files; none should be shipped either. +find "$destination/templates" -type l -exec rm -f {} + +find "$destination" -type d -exec chmod 755 {} + +find "$destination" -type f -exec chmod 644 {} + + +echo "staged $(ls "$destination/runtime" | wc -l | tr -d ' ') runtime files and $(find "$destination/templates" -name main.jsx | wc -l | tr -d ' ') template entries in $destination" diff --git a/docs/linux/README.md b/docs/linux/README.md new file mode 100644 index 00000000..83d88946 --- /dev/null +++ b/docs/linux/README.md @@ -0,0 +1,180 @@ +# ScriptWidget for Linux + +Status: preview, for [Omarchy](https://omarchy.org) 4.5. An archive is on [GitHub Releases](https://github.com/everettjf/scriptwidget/releases) (tag `linux-v`). What was tested and on which machine is in [testing.md](testing.md); what is left is in [TODO.md](TODO.md); the plan is in [plan.md](plan.md) (Chinese); the messages between the service and the display are in [protocol.md](protocol.md). + +ScriptWidget for Linux puts the same Package 2.0 widgets that run on iPhone, iPad, Mac, Android and Windows on the Linux desktop. It is written in Rust and runs the same shared JavaScript as Android and Windows in the same QuickJS-NG sources. A widget is a picture the background service draws; on Omarchy a small plugin of the Omarchy shell shows it on the desktop layer, below your windows, and passes clicks back. + +![Widgets on the Omarchy desktop](images/desktop.png) + +On other desktops the command line and the management window work, but nothing is shown on the desktop yet. + +## What is in the archive + +| Part | What it does | +| --- | --- | +| `scriptwidget` | The command line, and the background service (`scriptwidget host run`): it runs the scripts on a schedule, draws the pictures and handles clicks. It opens no window | +| `scriptwidget-studio` | The management window: your widgets, the templates, the code editor with a live preview, the settings. Closing it frees its memory; the widgets keep running | +| Omarchy plugin `com.atomsprout.scriptwidget` | Shows the pictures on the desktop layer and adds an entry to the bar. It only shows pictures and forwards numbers and coordinates | + +## Pictures + +The management window on Omarchy 4.5 (screenshots of the real window in the test machine): + +| | | +| --- | --- | +| ![My widgets](images/studio-library.png) | ![Templates](images/studio-templates.png) | +| ![A template opened](images/studio-template.png) | ![Editor with live preview](images/studio-editor.png) | +| ![Settings](images/studio-settings.png) | ![My widgets in Chinese](images/studio-library-zh.png) | + +The interface follows the system language when it is English or Simplified Chinese, and is English otherwise. + +## Installing it + +Needs Omarchy 4.5 (or another Arch-based system with the same libraries: ICU 78, fontconfig, FreeType, and a Vulkan driver for the management window). Download `scriptwidget--linux-.tar.gz` from [GitHub Releases](https://github.com/everettjf/scriptwidget/releases), check it against the SHA-256 the release lists, unpack it and run: + +``` +sh install.sh +``` + +- **It installs for you only**, without root: programs in `~/.local/bin`, data in `~/.local/share/scriptwidget`, a systemd user unit `scriptwidget.service` that starts with the graphical session, a launcher entry, and on Omarchy the plugin in `~/.config/omarchy/plugins`, enabled. +- **The archive is not signed.** Each release lists the SHA-256 of its files. +- **Which archive.** `aarch64` was built and tested on an Omarchy 4.5 machine. `x86_64` is built and tested without a desktop by CI on Arch Linux; nobody has seen it show widgets on a desktop. +- **It is built against ICU 78.** When the distribution moves to a newer ICU the programs stop starting until a new archive is published; see [TODO.md](TODO.md). +- **Updating.** There is no automatic update. Run `install.sh` of the newer archive; it stops the service, replaces the programs and keeps your widgets. +- **Removing it.** `sh uninstall.sh` keeps your widgets; `sh uninstall.sh --purge` deletes them too. + +## Using it + +- **Add a widget.** Open ScriptWidget from the launcher or the bar entry, open a template, choose "Use this template", then "Add to desktop". From a terminal: `scriptwidget install `, then `scriptwidget add --family medium`. +- **Move a widget.** Right-click the ScriptWidget entry in the bar and turn on "Arrange widgets" (or run `scriptwidget arrange`). Drag widgets; they snap to the screen edges and stay out of the bar and the dock. Turn it off when done. Outside this mode only buttons, toggles and links react; clicks anywhere else on a widget go through to the desktop. +- **Change its size, edit or remove it.** In arrange mode, right-click the widget. +- **See widgets above your windows.** "Show widgets on top" in the bar entry, or `scriptwidget peek`; once more puts them back. +- **Edit.** The preview next to the code is drawn by the same painter as the desktop widget. `scriptwidget dev ` watches a folder and refreshes the placed widgets of that package on save. +- **Move a widget to another device.** Export it as a `.swt` file and open that file in ScriptWidget on the other device; a `.swt` from another device is imported the same way. + +## Parts in the source + +| Part | Crate or folder | +| --- | --- | +| Engine: QuickJS-NG behind a safe wrapper | `Linux/crates/sw-quickjs` | +| Runtime: packages, archives, compiler, executor, `Intl`, native APIs, Data Source Plugins, timeline | `Linux/crates/sw-core` | +| Painter: element tree to bitmap and tap regions, on Skia | `Linux/crates/sw-render` | +| Background service | `Linux/crates/sw-host` | +| Command line `scriptwidget` | `Linux/crates/sw-cli` | +| Management window, on GPUI and gpui-kit | `Linux/crates/sw-studio` | +| Omarchy plugin (QML) | `Linux/omarchy-plugin` | +| Archive, installer, systemd unit, desktop entry | `Linux/packaging`, `Linux/scripts/package.sh` | + +## Building + +Needs Rust 1.85 or later, a C compiler, `pkg-config`, the development files of ICU, fontconfig and FreeType, and Node 24 or later for the shared JavaScript. + +``` +cd Web/studio && npm ci # once, for esbuild +node Linux/scripts/build-shared-js.mjs # shared JavaScript -> Linux/assets, parity corpus -> Linux/tests/assets +cd Linux +cargo test --locked --workspace +cargo build --locked --release -p sw-cli # target/release/scriptwidget +cargo build --locked --release -p sw-studio --features ui # target/release/scriptwidget-studio +sh scripts/asan-test.sh # engine binding tests with AddressSanitizer in QuickJS (Linux only) +sh scripts/package.sh # Linux/artifacts/scriptwidget--linux-.tar.gz +``` + +The management window is behind the `ui` feature, off by default, so that `cargo test --workspace` needs no graphical libraries; with it the build also needs the development files of libxkbcommon, libxcb, Wayland and Vulkan. The "Linux release archive" workflow builds the x86_64 archive on Arch Linux; the aarch64 archive is built with `package.sh` on an aarch64 Omarchy machine. + +Run `node Linux/scripts/build-shared-js.mjs` again after changing `Web/preview-engine/src`, the support bundle or `Android/core/src/main/js`. Those sources are shared with Android and Windows: a change there needs their suites as well. + +`Linux/assets`, `Linux/tests/assets`, `Linux/target` and `Linux/artifacts` are generated and not checked in. + +### Where the program finds its files + +The program reads the generated JavaScript and the bundled templates from a data directory, never from the repository: + +``` +/bin/scriptwidget +/share/scriptwidget/runtime/ kernel.js and the other generated files +/share/scriptwidget/templates/ template/ component/ api/ +``` + +`sh Linux/scripts/stage-resources.sh /share/scriptwidget` assembles it. The first location that holds `runtime/kernel.js` wins: `SCRIPTWIDGET_RESOURCES` if set; `../share/scriptwidget` next to the executable; `scriptwidget` under each entry of `XDG_DATA_DIRS`. For development, `SCRIPTWIDGET_RUNTIME_ASSETS` and `SCRIPTWIDGET_TEMPLATES` override each half. + +User data is in `$XDG_DATA_HOME/scriptwidget` (`Packages`, `Storage`, `Plugins`, `Staging`), by default `~/.local/share/scriptwidget`; caches are in `$XDG_CACHE_HOME/scriptwidget`. `SCRIPTWIDGET_DATA` or `--data` names another folder. + +## The command line + +Every command prints one JSON report on standard output; errors and warnings are repeated on standard error as `error: [code] message`. Color is used only on a terminal and never when `NO_COLOR` is set. + +| Command | What it does | +| --- | --- | +| `scriptwidget new [--template ] [--directory ]` | Creates a package from a template, or from a starter | +| `scriptwidget check [--fail-on-warning]` | Validates the manifest and compiles; runs nothing | +| `scriptwidget run [--family F] [--param P] [--dark]` | Executes; reports the element tree, logs and errors | +| `scriptwidget render --output [--family F] [--scale N] [--now ]` | Executes and draws a PNG; reports size, tap regions and timings. Needs no graphical session | +| `scriptwidget timeline [--now ]` | Reports the timeline | +| `scriptwidget pack [file.swt] [--force]` | Exports a `.swt` | +| `scriptwidget install <.swt or dir>`, `list`, `templates` | Installed packages and bundled templates | +| `scriptwidget doctor` | Checks the installation: files, engine, painter, fonts, data folder | +| `scriptwidget host run` | The background service. Normally started by the systemd user unit | +| `scriptwidget add [--family F]`, `remove `, `widgets`, `refresh [id]` | Widgets on the desktop | +| `scriptwidget dev ` | Watches a package folder and refreshes its placed widgets on save | +| `scriptwidget peek`, `arrange` | Toggle "on top" and arrange mode | +| `watch`, `tap`, `move`, `screen`, `theme`, `menu`, `session` | Used by the display; see [protocol.md](protocol.md) | + +The commands from `add` on need the background service; without it they exit with 69 and say how to start it. + +Families: `small` 170×170, `medium` 329×170, `large` 329×345, `extraLarge` 639×345 points, multiplied by `--scale` (default 2). + +| Exit code | Meaning | +| --- | --- | +| 0 | Passed | +| 1 | The package, archive or script is at fault | +| 2 | Usage or environment: missing folder, unwritable or unsafe output, missing runtime files | +| 3 | Internal error | +| 69 | The background service is not running | + +Every report has `reportVersion` (1), `command`, `status` (`passed`, `failed`, `error`), `ok`, `tool`, `error` and `diagnostics`. Package commands add `backend` (`linux-native`), `package`, `sourceHash`, `manifest` and `checks` (`package`, `compile`, `execute`, `render`, `export`, each `passed`, `failed` or `notRun`). + +Differences from the Mac and Windows command lines: reports are always JSON; `render --output` names one PNG file, as on Windows (the Mac writes a folder with a light and a dark picture); there is no `preview` command and there are no fixtures, the APIs are real; a script error still writes the picture of the error card and exits with 1. + +## How it runs a widget + +As on Android and Windows: + +1. **Compile.** The package's JSX is compiled by the shared Babel bundle inside QuickJS. The manifest is checked by the shared validator. +2. **Execute.** The script runs in a fresh QuickJS context with a 64 MB memory limit, a stack limit and a 5-second deadline. It reaches the system only through host calls, which check the manifest's permissions and network domains first. +3. **Lay out.** The shared renderer runs in a second QuickJS context that only ever sees the element tree, never package code. Text is measured by the Skia paragraph code that later draws it. +4. **Draw.** The laid-out tree is painted with Skia on a CPU surface into a bitmap with per-pixel transparency. + +Every bundled package that needs no live data produces the same element tree on Linux as on iPhone, byte for byte; the tests check this. + +## Differences from the other platforms + +`$system.platform()` returns `"linux"`. Anything not listed is meant to work as on Windows (see [the Windows page](../windows/README.md)). + +| Area | On Linux | +| --- | --- | +| Rendering modes | Always `fullColor`; `backgroundMaterial` uses the opaque `background` fallback | +| Fonts | Inter is built into the program. The six display fonts of the other platforms are not shipped, because the repository records no licence for them: text that asks for one is drawn with Inter, unless the font file is put in `~/.local/share/scriptwidget/fonts` (or the folder `SCRIPTWIDGET_FONTS` names). Chinese, Japanese, Korean and emoji use system fonts found through fontconfig; the order of the Chinese, Japanese and Korean faces follows the locale. Monospace and serif are the user's fontconfig choices | +| `animation` | The first frame, as on Android and Windows | +| `` | Animated on the desktop, at most 60 frames; `render` and the previews show the first frame | +| `` | A running timer is redrawn every second and a clock time every minute | +| Timeline | The script runs again at each transition | +| Links | `http`, `https` and `mailto` links open with `xdg-open`. Other schemes are refused | +| Refresh | Every 15 minutes by default (5 minutes to 24 hours in the settings), at timeline transitions, after the theme or a screen changes, after sleep, and on demand | +| Dark mode | Follows the brightness of the Omarchy theme | +| Where widgets live | On the desktop layer of Omarchy, placed anywhere; not in a panel or dashboard | +| `$device`, `$system` | `model` is `"PC"`, `name` and `hostName` are the host name, `brightness` is `1`, `thermalState` is `"nominal"`; the battery is the first system battery under `/sys/class/power_supply`, and a machine without one reports level 1 and `"full"`; `systemVersion` is `VERSION_ID` from `os-release`, or the kernel release where there is none | +| `$storage`, `$file`, `fetch`, `$http`, `$xml`, `$dataSource`, `$weather` | Yes, with the same permissions, network-domain declarations, private-address refusal and budgets. `$weather` is the MET Norway forecast | +| `$calendar`, `$reminders`, `$health`, `$photos`, `$contacts`, `$documents`, `$home`, `$icloud`, `$alarms`, `$maps`, `$services`, `$music`, `$notifications`, `$location` | No. `isAvailable()` returns `false` and calls reject with `[unsupported_platform]`. Templates that use one are left out of the template list | + +## Security + +Scripts, imported archives and network responses are untrusted, as on every platform. The Linux-specific parts: + +- **Package files** are opened relative to the package folder with `openat2` (`RESOLVE_BENEATH`, no symlinks, no magic links) and must be regular files with one link. Where the kernel has no `openat2` the path is walked one component at a time with `O_NOFOLLOW`. Traversal, absolute paths, backslashes, symlinks, hard links, case collisions and names that are not UTF-8 are refused. Files are created with mode 0600, folders 0700. +- **Archives** are checked whole before anything is extracted (entry count, file size, expanded size, names), extracted through the same reading of the archive that checked them, into a unique staging folder on the same filesystem, and published with a rename that refuses to replace anything. +- **Network.** Requests go over a small HTTP/1.1 client on rustls. It resolves the host itself, refuses private, loopback and link-local addresses, and connects only to the address it checked. Redirects are followed by hand with the same checks at every hop, and a redirect to another origin does not receive the `Authorization` or `Cookie` header the script set. The system proxy is not used. Requests name the client as `ScriptWidget-Linux/` unless the script sets its own `User-Agent`. +- **Engine isolation.** Package code runs inside the program's process, as on the other platforms: QuickJS exposes no system interface, and memory, stack and time are bounded. +- **Service socket.** `$XDG_RUNTIME_DIR/scriptwidget/host.sock` is readable by the user only, and the service checks that the peer is the same user. Requests are single lines that name packages, widget ids, integers and fixed words; nothing received is run as code or used as a path. +- **The display plugin** runs inside the Omarchy shell, unsandboxed like every plugin, so it is kept small: it shows PNG files from the service's own folder, ignores any other path, and sends back only a widget id with a region number or coordinates. Which action a region stands for is looked up in the service. +- **Command line output.** `render --output` and `pack` write a private temporary file and rename it; they refuse a symlink at the output name and any output inside the package. diff --git a/docs/linux/TODO.md b/docs/linux/TODO.md new file mode 100644 index 00000000..78a28efc --- /dev/null +++ b/docs/linux/TODO.md @@ -0,0 +1,58 @@ +# ScriptWidget for Linux: what is left + +The plan is in [plan.md](plan.md) (Chinese); what was tested is in [testing.md](testing.md). + +## Known limits of the preview + +- **Tested on one machine.** Everything a person can see was tested in one aarch64 Omarchy 4.5 virtual machine with a virtual GPU, one 1920×1080 display at scale 1, and a virtual pointer instead of a hand. Not tested: a real computer, a real GPU, display scales above 1, several displays, a light theme, sleep and wake, a restart of the computer with the service enabled, an input method. +- **The x86_64 archive** is built and tested without a desktop by CI on Arch Linux. Nobody has seen it show widgets. +- **ICU.** The programs link the system's ICU 78 by its versioned name. When Arch and Omarchy move to ICU 79 they stop starting until a new archive is built. Fix: link ICU statically, or publish an AUR source package that is rebuilt on the user's machine. +- **Display fonts.** The six display fonts of the other platforms (Apple ][, Larabiefont Free, Monofonto, White Rabbit, OneSlot, Unispace) are not shipped: the repository records no licence for them, and the notice inside `Unispace.ttf` forbids redistribution. Text that asks for one is drawn with Inter. The same files ship in the iPhone, Android and Windows apps; their licences need to be settled there too. +- **The archive is not signed**, and its third-party notices are a hand-written summary, not a generated list of every crate licence. +- **No AUR package and no entry in the Omarchy plugin catalog.** The plugin is installed by `install.sh` from the archive. + +## Still to do + +- **Lock screen.** Omarchy locks through its own shell and sends nothing on logind, and the plugin does not report it, so the service keeps refreshing behind the lock screen. +- **Screens.** No message tells the service a display went away; scales above 1 and moving widgets between displays have code but never ran. +- **Time zone changes** are not noticed until the next refresh. +- **Management window:** closing it with unsaved changes does not ask (moving to another page does); no "new file" button; the components and API reference pages of the Windows app are missing; changing the refresh interval writes the setting but does not tell the running service; in the test machine the editor's blinking cursor costs a quarter of a core under software rendering. +- **Plugin:** frames do not pause behind ordinary windows or on the lock screen; the bar entry starts `scriptwidget-studio` without checking that it exists; widgets are not moved when the dock appears later; the Chinese strings were not seen on screen. +- **Service:** real suspend and resume, timeline transitions end to end, and the path that drops a slow display were not exercised; `doctor` does not ask the plugin for its status. +- `docs/platform-support.md` lists Linux; the website and the user READMEs do not yet. + +## Runtime + +- **Out of memory is recognised by its message.** A script that ends with the engine's "out of memory", or with a `null` exception (what the engine leaves when it cannot even build the error), is reported as `resourceLimit`. A script that itself throws `null` is therefore reported as a memory-budget error. The exact fix is a flag from the allocator in `sw-quickjs`. +- **A lone UTF-16 surrogate** that reaches Rust becomes U+FFFD; Windows keeps it. +- **`PackageFiles::paths()` cannot fail**, so a package with more than 500 files lists as empty through the trait instead of reporting `too_many_entries`. +- **The driver name in `host.js`** (`__sw_`) is visible to package code through `Object.getOwnPropertyNames(globalThis)` on every QuickJS host. It only lets a script pump, resolve or time out its own execution; shared with Android and Windows, not changed here. +- **Network:** one connection per request, no keep-alive; brotli bodies are passed through undecoded; a `$weather` request is not cancelled when the execution ends. No request to a real Internet host has been made by a test. +- **Unsupported-API filter** of the template list reads only `main.jsx`, as on Windows, so "Local Weather (Location)" is listed. +- **Remote images** in `render` are fetched under the package's network policy; there is no offline switch, and a tree nested deeper than about 64 elements gets placeholders. +- **`$music` (MPRIS) and `$notifications`** are planned for after the first version. +- **Engine in a separate restricted process** (Landlock, seccomp): planned, not started. + +## Painter + +Differences from the Windows painter, or things not compared: + +- No picture has been compared pixel for pixel with Windows or iPhone; about 25 pictures were looked at by eye. +- Live date slots, motion slots and scrolling-list slots are not ported: Linux draws again instead, and a `` is drawn in place with only its visible tap targets. +- GIF: every frame keeps its own delay (at most 60 frames, 8 MB, 16 million pixels); Windows samples 24 frames. The service caches decoded frames while it draws an animation. +- Images are decoded at full size before being reduced (limit 40 megapixels). HEIC is probably not decodable; not tested. +- Rounded corners of the widget itself are left to the display, as on Windows. +- Gradients interpolate premultiplied (as CSS and Windows; Android does not). Conic gradients use Skia's sweep gradient. +- Drop shadows use a Skia image filter; softness follows Android's formula and was not compared. +- `rotate3d` uses the Windows matrix. SVG mask luminance is taken on non-linear sRGB, as on Android. +- Monospace and serif fonts are the user's fontconfig choices, so widths differ from Windows. +- Painting has no time limit (layout has 8 seconds). +- In "Personal Dashboard" at the medium size the title touches the top edge; not yet checked against the other platforms. +- A machine with no Chinese, Japanese, Korean or emoji font, and `extraLarge` with real templates, were not tested. + +## Tests and tooling + +- `clippy` reports style warnings in `sw-render`; CI runs it without failing on warnings. CI does not build the management window (`ui` feature); the release workflow does. +- AddressSanitizer covers the engine binding tests only. +- The 14 bundled templates that ask the network were not rendered anywhere. +- Not tested: a real `/usr/share/scriptwidget` installation, two `install` commands at once, the `openat2` fallback being chosen by a kernel without it, a filesystem without `RENAME_NOREPLACE`. diff --git a/docs/linux/images/desktop.png b/docs/linux/images/desktop.png new file mode 100644 index 00000000..83420644 Binary files /dev/null and b/docs/linux/images/desktop.png differ diff --git a/docs/linux/images/studio-editor-zh.png b/docs/linux/images/studio-editor-zh.png new file mode 100644 index 00000000..73293a43 Binary files /dev/null and b/docs/linux/images/studio-editor-zh.png differ diff --git a/docs/linux/images/studio-editor.png b/docs/linux/images/studio-editor.png new file mode 100644 index 00000000..0e34a12c Binary files /dev/null and b/docs/linux/images/studio-editor.png differ diff --git a/docs/linux/images/studio-library-zh.png b/docs/linux/images/studio-library-zh.png new file mode 100644 index 00000000..06b4c038 Binary files /dev/null and b/docs/linux/images/studio-library-zh.png differ diff --git a/docs/linux/images/studio-library.png b/docs/linux/images/studio-library.png new file mode 100644 index 00000000..43ce5aef Binary files /dev/null and b/docs/linux/images/studio-library.png differ diff --git a/docs/linux/images/studio-settings.png b/docs/linux/images/studio-settings.png new file mode 100644 index 00000000..82566b65 Binary files /dev/null and b/docs/linux/images/studio-settings.png differ diff --git a/docs/linux/images/studio-template.png b/docs/linux/images/studio-template.png new file mode 100644 index 00000000..3a63b29a Binary files /dev/null and b/docs/linux/images/studio-template.png differ diff --git a/docs/linux/images/studio-templates.png b/docs/linux/images/studio-templates.png new file mode 100644 index 00000000..7519b8fe Binary files /dev/null and b/docs/linux/images/studio-templates.png differ diff --git a/docs/linux/plan.md b/docs/linux/plan.md index 385d2699..97837674 100644 --- a/docs/linux/plan.md +++ b/docs/linux/plan.md @@ -1,258 +1,421 @@ -# ScriptWidget Linux 版(Omarchy):开发计划 +# ScriptWidget Linux 版:开发计划 -状态:**已搁置**(2026-11-16 决定先做 Windows,见 [Windows 开发计划](../windows/plan.md))。本文保留作为记录;其中"和 Windows 共用 Rust 核心"的设想不再成立,Windows 版改用 C++。计划稿写于 2026-11-16,尚未写任何 Linux 代码,也没有在 Omarchy 机器上运行过任何东西。 +状态:**选型已定,等待阶段 0**,分支 `linux`。本文写于 2026-11-17,取代 2026-11-16 的旧稿(旧稿设想命令行加 QML 面板,且当时已搁置)。目前还没有写任何 Linux 代码,也没有在 Omarchy 上运行过任何东西。 -标注约定:**[已核实]** 本次直接读到官方页面或本仓库源码;**[二手]** 来自第三方文章;**[待验证]** 没有可靠来源,或需要在 Omarchy 真机上确认,不能当作事实。 +标注约定:**[已核实]** 本次读到官方文档、官方仓库源码或本仓库源码;**[待验证]** 来自记忆或推断,要在 Omarchy 虚拟机里确认后才能当作事实。 -## 1. 目标与定位 +## 1. 已定的决定 -**目标**:同一个 Package 2.0 小组件(同一份 `main.jsx` 和 `widget.json`)在 iPhone、iPad、Mac、Android 和 Linux 上都能运行。 +| 决定 | 内容 | 谁定的 | +| --- | --- | --- | +| 语言 | 从零用 Rust 写,尽量用原生语言。不复用 Windows 的 C# 核心 | 你 | +| 小组件的显示 | 用 Omarchy 官方的方式:一个 QML 插件把 Rust 画好的图片显示在桌面层。在 Omarchy 上 QML 就是原生的界面语言,缩放、多显示器、点击、拖动都由外壳处理 | 你(2026-11-17 改,原先定的是 Rust 自己开窗口) | +| 位置 | 小组件贴在桌面层(壁纸之上、所有窗口之下),可拖动摆放;另有一个快捷键把它们临时提到最上层 | 你 | +| 管理窗口 | 完整的图形管理窗口,用 GPUI(Zed 编辑器的 Rust 界面框架)写 | 你 | +| 发到手机 | 第一版只导出 `.swt` 文件;扫码导入放到之后 | 你 | +| 测试机 | 这台 Mac 上的 RiftVM(Omarchy aarch64 虚拟机) | 你 | + +下面两项是我定的,理由写在对应章节,你可以否决: -**目标用户**:使用 Omarchy 的人。他们在桌面上写小组件、看到效果,再把同一个小组件装到自己的手机上。Linux 版是入口,手机版是落点。 +- 绘制库用 Skia(`skia-safe`),不用纯 Rust 的 `tiny-skia`(第 4.3 节)。 +- 第一版执行脚本的引擎和后台服务在同一个进程里,和 iPhone、Android、Windows 现状相同;独立的受限进程放到之后(第 6 节)。 -由此得出三条产品原则: +没有采用的方向: -1. **Linux 上不做图形界面的应用。** Omarchy 用户用终端和 Neovim,用自己的 Coding Agent。Linux 版是一个命令行工具、一个后台进程和一个 Omarchy 插件。编辑器、AI 生成、Gallery 界面都不搬过来。 -2. **"发到手机"是一等功能,不是附属功能。** 如果桌面上写好的小组件不能在一分钟内出现在手机上,这个版本就没有达到目的。 -3. **不和 Omarchy 自己的插件比"谁更能定制桌面"。** QML 插件能做的事比我们多,而且没有沙箱。我们提供的是它们没有的两点:跨到手机,以及可以安全安装别人写的小组件。 +- **把 JSX 直接变成 QML 或 GTK 的控件**(AGS 的做法)。排版和文字交给界面框架后,同一个小组件在 Linux 和手机上会不一样,一致性验收也做不了。 +- **基于 AGS 做**。Omarchy 用户没有装它;它跑在 GNOME 的 JavaScript 引擎上,和手机端的 QuickJS 行为不一致;它的脚本能访问整个系统,和"可以安全安装别人写的小组件"冲突。AGS 只作为参照。 +- **GTK 4 的管理窗口**。作为 GPUI 走不通时的退路保留(第 4.5 节)。 -## 2. Omarchy 4 的现状 +## 2. 调查得到的事实 + +### 2.1 Omarchy | 事实 | 来源 | | --- | --- | -| Omarchy 是 Arch Linux 加 Hyprland 加 Quickshell。4.0 于 2026-08-14 发布,文章写作时最新为 4.0.6(2026-10-19) | [二手] [starryhope.com](https://www.starryhope.com/linux/what-is-omarchy/) | -| 4.0 用一个 Quickshell 进程替换了 Waybar、Walker、Mako、SwayOSD、hyprlock、hypridle、swaybg、polkit-gnome | [已核实] [PR 6231](https://github.com/basecamp/omarchy/pull/6231) | -| 外壳的每个部分都是插件。第三方插件是一个 Git 仓库,根目录有 `manifest.json`,入口是 QML 文件 | [已核实] [Develop a Plugin](https://plugins.omarchy.org/develop.html) | -| 插件种类:`bar-widget`、`bar`、`panel`(浮动窗口)、`overlay`(全屏)、`menu`、`service`(无界面的常驻单例) | [已核实] 同上,及 [omarchy-shell.md](https://github.com/omacom/omarchy/blob/quattro/docs/omarchy-shell.md) | -| 安装:`omarchy plugin add `,装到 `~/.config/omarchy/plugins//`;另有 `enable`、`disable`、`update`、`remove`、`validate` | [已核实] 同上 | -| 插件"以用户权限运行,没有沙箱",和外壳共用一个进程;文档要求不要为插件再启动第二个 Quickshell 进程 | [已核实] 同上 | -| 调色板在主题的 `colors.toml`,QML 里通过 `Commons.Color` 读取(`foreground`、`background`、`accent`、`urgent` 及各表面的角色) | [已核实] omarchy-shell.md | -| IPC:`omarchy-shell shell summon `、`hide`、`toggle`、`call ` | [已核实] 同上 | -| 上架插件目录:公开的 GitHub 仓库,根目录有合法的 `manifest.json`、README 和许可证,通过 GitHub issue 表单提交,自动校验后由维护者批准 | [已核实] [Publish](https://plugins.omarchy.org/publish.html) | -| 插件目录对外部二进制依赖、商业产品、AI 生成代码有没有限制 | **[待验证]** 发布页没有写。提交前要问维护者 | -| 插件能否自己创建壁纸层(background layer)的窗口,也就是真正的"桌面小组件" | **[待验证]** 文档只列了上面六种,没有提 | -| 装软件包:`omarchy pkg add`,AUR 包用 `omarchy pkg aur add` | [已核实] Omarchy 自带的 Skill 文档;该文档部分内容还是 4.0 之前的,命令要在真机上确认 | -| 4.0 第一个月下载超过 30 万次,第一周出现一千多个插件 | [二手] 项目自己公布的数字 | -| 硬件:x86 PC 和 Intel Mac;Apple 芯片的 Omarchy M 还没完成 | [二手] starryhope.com | - -三点影响方案的结论: - -- **展示端应当是一个 Omarchy 插件,而不是自己开窗口。** 这是 Omarchy 用户安装东西的标准方式,也能自动跟随主题。 -- **平铺式桌面上,壁纸层的小组件大部分时间被窗口盖住。** 更合适的形态是一个按快捷键呼出的"小组件面板"(`panel`),加上状态栏上的一个入口(`bar-widget`)。壁纸层小组件作为可选项,等验证了可行性再做。 -- **这里已有的同类不是 AGS,而是上千个 QML 插件。** 之前的对比对象要换。 - -## 3. 仓库里可以直接复用的东西 - -| 资产 | 位置 | 在 Linux 上的用法 | +| 最新版是 4.5.0 "Quattro RS",2026-11-05 发布。仓库已迁到 `omacom/omarchy`,默认分支 `quattro` | [已核实] GitHub 发布页 | +| 4.5 起官方支持 ARM:有 aarch64 ISO,可原生装在 M1、M2、M3 的 Mac 上;M4 及以后还不支持 | [已核实] 4.5 发布说明、仓库里的 `install/omarchy-aarch64-*.packages` | +| 4.5 新增传统的浮动桌面模式(带 Dock) | [已核实] 4.5 发布说明 | +| 外壳是一个 Quickshell 进程(Omarchy 自己的构建,0.3.2),Qt 6.12 | [已核实] 软件源数据库 | +| 插件有六种:`bar-widget`、`bar`、`panel`、`overlay`、`menu`、`service`。没有专门的"桌面小组件"种类 | [已核实] `docs/omarchy-shell.md` | +| 桌面层小组件已经很常见:插件声明一个 `service`,在里面自己创建 `PanelWindow` 并放到 `WlrLayer.Bottom`。目录里有 49 个这样的插件 | [已核实] omaclock、omarchy-vitrine、OmaWidgets 的源码 | +| 插件没有沙箱,和外壳同进程;文档要求插件不要再启动第二个 Quickshell 进程 | [已核实] 同上 | +| 插件安装不执行任何安装脚本,不要 sudo;清单里没有声明系统依赖的字段 | [已核实] 插件开发手册 | +| 本地开发:把文件夹放进 `~/.config/omarchy/plugins//`,运行 `omarchy-shell shell rescanPlugins` 再 `omarchy plugin enable `;保存文件即热重载 | [已核实] 同上 | +| 插件目录约 6,700 个条目,其中 5,645 个是状态栏小组件 | [已核实] 目录数据,2026-11-17 | +| 上架:公开的 GitHub 仓库,根目录是一个插件,有 README 和许可证;自动安全扫描后由维护者批准。捆绑可执行文件、安装器、systemd 单元不会被拒,但会转人工审核;"下载后直接执行"会被拦 | [已核实] 插件目录的 `SUBMISSION.md`、`SECURITY.md` | +| 插件配一个独立二进制的惯例:先 `omarchy pkg aur add <包>-bin`,再 `omarchy plugin add <仓库>`;二进制没装时插件显示"未安装" | [已核实] ai-usagebar(784 星)的做法 | +| 仓库 47,145 星;官网称第一年 ISO 下载 139 万次。活跃安装数没有公开数字 | [已核实] GitHub API、官网;下载数是项目自己公布的 | +| 对商业关联产品、AI 生成代码有没有限制 | **没查到规定**,提交前要问维护者 | + +**已有的同类产品**:[Omalauncher](https://github.com/SimonSchubert/omalauncher-omarchy-plugin)(11 星)。它用自己的 `.owl` 语言写单文件小组件,保存即刷新,有审核过的小组件商店,能扫码发到它的 Android 启动器,有 AUR 包。也就是说"桌面上写、发到手机"这个卖点已经有人在做,但只到 Android,而且是它自己的语言。我们的不同点是:同一个包能跑在 iPhone、iPad、Mac、Android、Windows 上,用的是 JavaScript 和 JSX,包的权限和网络域名要声明。 + +### 2.2 仓库里现成的东西 + +| 资产 | 位置 | 在 Linux 上怎么用 | | --- | --- | --- | -| 执行内核,通过 `host` 接口注入求值、文件、日志、超时 | `Web/preview-engine/src/kernel.js` | 原样复用,和 Android 一样在 QuickJS 里运行 | -| 布局和渲染(propose/size/place,文本度量走 `measurer` 抽象) | `Web/preview-engine/src/render/` | 原样复用 | -| 在 QuickJS 里跑渲染器、把结果记录成元素树的入口 | `Android/core/src/main/js/render/entry.mjs` | 不含 Android 专有代码,可以共用 | -| QuickJS 宿主(去掉原生入口、补 `Intl` 子集、启动函数) | `Android/core/src/main/js/android-host.js` | 大部分与平台无关,抽成共用文件 | -| QuickJS-NG 源码 | `Android/core/src/main/cpp/quickjs` | 用同一份源码编译,保证两端引擎行为一致 | -| 包校验、清单、压缩包处理 | `Web/preview-engine/src/package/` | JS 版可直接在 QuickJS 里用;路径和压缩包的安全检查在原生侧再做一遍 | -| 164 棵一致性树 | `Web/preview-engine/test/parity/native-trees.json` | 作为 Linux 运行时的验收门槛 | -| 把元素树画成位图的画笔 | `Android/core/.../render/`(`DomPainter.kt`、`SvgPainter.kt`、`Css.kt`、`TextEngine.kt`,约 1,300 行) | 依赖 `android.graphics`,不能复用,需要移植。**这是最大的一块新工作** | -| 给 Coding Agent 用的创作 Skill 和 MCP 服务 | `Web/preview-engine/bin/scriptwidget-mcp.mjs`、`docs/mac-cli.md` 里的 `scriptwidget-creator` | Omarchy 用户自己有 Agent,直接提供这两样,不做内置 AI | -| Mac CLI 的命令设计(`check`、`render`、`pack`、`doctor`,JSON 报告,退出码) | `docs/mac-cli.md` | Linux CLI 用同样的命令和报告格式 | +| 执行内核、布局渲染器、包校验器 | `Web/preview-engine/src/` | 原样复用,在 QuickJS 里运行 | +| 打包这些共用 JavaScript 的脚本 | `Scripts/lib/shared-js.mjs` | 已经支持非 Windows 平台,加一个 Linux 的输出目录 | +| QuickJS-NG 0.18.0 源码 | `Android/core/src/main/cpp/quickjs` | 用同一份源码编译,三端引擎行为一致 | +| QuickJS 的 C 绑定 | `Windows/native/swcore.c`(382 行) | 引擎部分是可移植的 C。接口设计(创建、求值、字节码、截止时间、内存上限、宿主回调)照搬到 Rust 的 FFI 层 | +| 画笔 | `Android/core/.../render/`(Kotlin)和 `Windows/src/ScriptWidget.Core/Render/`(C#,约 2,700 行) | 不能复用,要移植成 Rust。有两份实现可以对照 | +| 运行时其余部分(编译、执行、时间线、包存储、压缩包、`fetch`、存储、天气、数据源插件) | `Windows/src/ScriptWidget.Core/`(约 8,400 行) | 作为 Rust 移植的参照。Windows 版是照着 Android 逐文件移植的 | +| 测试 | `Windows/tests/`(约 239 个)、`Android/core` 的测试 | 逐个移植成 Rust 测试 | +| 一致性树(164 棵,其中 127 个包不需要实时数据) | `Web/preview-engine/test/parity/native-trees.json` | Linux 运行时的验收门槛:每棵树逐字节相同 | +| CodeMirror 编辑器 | `Editor/editorfe/` | 管理窗口的编辑器区域复用它 | +| API 和组件的参考数据 | `Editor/editorfe/src/scriptWidgetAPI.js` | 管理窗口的参考页由它生成,和 Windows 版一样 | + +Windows 版已经踩过、Linux 上要留意的两个坑: + +- `Intl` 靠 ICU 补。发行版的 ICU 版本不同,输出可能有细微差别,会影响"逐字节一致"。 +- QuickJS 的本地时间在夏令时切换前后算错,Windows 版改为向 ICU 询问那个日期的偏移。Linux 上没有这个问题:QuickJS 向 C 库问每个时刻的偏移,源码不用打补丁。[已核实] 阶段 0,见 [testing.md](testing.md) + +### 2.3 RiftVM + +| 事实 | 来源 | +| --- | --- | +| 已安装 0.6.10;这台 Mac 会分到 6 核、16 GB 内存、64 GB 稀疏磁盘;镜像下载约 4.4 GB | [已核实] RiftVM 源码 | +| 镜像是 Omarchy 4.0.6(aarch64),不是最新的 4.5.0 | [已核实] `sources.env` | +| 命令行只能查询(`list`、`inspect`、`validate`、`doctor`)。创建、启动、停止都只能在窗口里做,没有别的无界面方式 | [已核实] | +| 网络只有 NAT,客户机地址在 192.168.64.0/24,Mac 可以直接连过去 | [已核实] 网络模式;地址互通是 [待验证] | +| 装了 openssh 但默认不开;用 Omarchy 自带的 `omarchy-setup-security-sshd --key=…` 开启 | [已核实] | +| 共享文件夹用 virtiofs 挂在 `/mnt/mac/<名字>`,没有 `noexec` | [已核实] | +| 自带 gcc、clang、git、base-devel、grim、Qt 6、Quickshell、`omarchy plugin`。没有 Rust、cmake、Node | [已核实] 镜像的包清单 | +| 图形只有 OpenGL ES 3.0;Vulkan 是软件渲染。Omarchy 自己的外壳能正常运行 | [已核实] RiftVM 的验证记录 | +| 关掉 RiftVM 窗口,虚拟机就关机;每次开机要在锁屏输入密码 | [已核实] 前者;后者 [待验证] | +| 150 秒无操作出屏保,300 秒锁屏,会挡住截图;`omarchy-toggle-idle stay-awake` 可以关掉 | [已核实] | + +## 3. 产品形态 + +| 部分 | 程序 | 语言 | 作用 | +| --- | --- | --- | --- | +| 后台服务 | `scriptwidget-host` | Rust | 一直在后台运行。按计划执行脚本,把每个小组件画成图片,保存位置和尺寸,处理点击对应的动作。自己不开窗口 | +| Omarchy 插件 | 一个 QML 插件 | QML | 把后台服务画好的图片显示在桌面层,转发点击和拖动;状态栏上有一个入口。不执行任何包代码,不解释任何来自包的内容 | +| 管理窗口 | `scriptwidget-studio` | Rust(GPUI) | 需要时才打开。我的小组件、模板、组件和 API 参考、编辑器加实时预览、设置。关掉后释放内存,小组件继续运行 | +| 命令行 | `scriptwidget` | Rust | 新建、检查、渲染、打包、添加到桌面、实时预览。不需要图形会话的命令可以在 CI 里跑 | + +第一版只有 Omarchy 上能把小组件显示在桌面上。后台服务和显示是分开的,以后要支持其他桌面,只需要再加一个显示端(例如一个直接用 layer-shell 协议的 Rust 程序),不用改后台服务。命令行和管理窗口不依赖 Omarchy。 ## 4. 架构 ``` -~/Widgets/MyWidget/ scriptwidget(一个二进制) Omarchy 外壳 - widget.json ──读取──▶ ┌──────────────────────────┐ ┌─────────────────────┐ - main.jsx │ 包存储与校验 │ │ 插件 │ - │ QuickJS:内核 + 宿主 │ PNG + │ service:连接后台 │ - │ QuickJS:共用渲染器 │ 点击区 │ panel:小组件面板 │ - │ 画笔:元素树 → PNG │ ───────▶ │ bar-widget:入口 │ - │ 时间线与刷新调度 │ ◀─────── │ │ - │ 网络、存储、文件(受限) │ 动作号 └─────────────────────┘ - └──────────────────────────┘ - Unix socket,仅本用户可访问 +Linux/ + crates/ + sw-quickjs QuickJS-NG 的 FFI 和安全封装 + sw-core 包、编译、执行、时间线、API、数据源插件 + sw-render 画笔:元素树 → 位图和点击区域 + sw-host 后台服务(调度、位置、动作、给显示端供图) + sw-cli 命令行 + sw-studio 管理窗口(GPUI) + omarchy-plugin/ QML 插件的源码:桌面层显示和状态栏入口(发布时同步到单独的公开仓库) + packaging/ AUR 的 PKGBUILD、systemd 用户单元、桌面文件 ``` -### 4.1 后台进程和命令行 +### 4.1 引擎(`sw-quickjs`) -一个二进制 `scriptwidget`,既是命令行也是后台进程(`scriptwidget daemon`,由 systemd 用户服务启动)。 +- 用 `cc` 在构建时编译仓库里那份 QuickJS-NG 源码,不用系统的 `quickjs-ng` 包,也不用第三方的 Rust 绑定,保证三端是同一份引擎。 +- 每次执行一个新的上下文:64 MB 内存上限、栈上限、5 秒截止时间,和其他平台相同。 +- 包代码只在"执行"上下文里跑;布局渲染器在另一个上下文里跑,只看得到元素树。 +- 引擎绑定的测试在 CI 里用 AddressSanitizer 再跑一遍,和 Windows 版一样。 -- **语言:Rust。** 理由:它要解析不可信的压缩包、清单和网络响应,内存安全有实际价值;能产出单个静态链接的二进制,便于分发。 -- **JS 引擎:QuickJS-NG,用仓库里已有的那份源码编译。** 和 Android 相同的中断回调和内存上限。不在 Quickshell 自带的 QML JS 引擎里运行包代码:那里没有内存上限和中断,语义也和 JavaScriptCore 不一致,而且等于把不可信脚本放进外壳进程。 -- **`Intl`:** QuickJS 没有 `Intl`。Android 用设备上的 ICU 补了一个子集。Linux 上用系统的 ICU 或 ICU4X,哪个能通过 Android 那套 `Intl` 测试就用哪个。**[待验证]** -- **画笔:** 把共用渲染器记录下来的元素树画成 PNG,同时输出可点击区域。两个候选:纯 Rust 的 `tiny-skia` 加 `cosmic-text`,或者 Skia。前者体积小、不依赖系统库,后者的文字和绘制更成熟但构建更重。**由阶段 0 的对比结果决定**,判断标准是和浏览器预览截图的差异。原先考虑过系统的 Cairo 加 Pango,因为这套核心也要用于 Windows(见 [Windows 可行性调研](../windows/feasibility.md)),而它们在 Windows 上不是合适的依赖,所以不再作为候选;对比时要同时在 Linux 和 Windows 的字体上测中日韩文字和表情。 -- **文本度量和绘制必须是同一套代码。** 布局在画之前就要知道每段文字的宽度,度量和绘制不一致会导致错误的换行和截断。这也是不让 QML 来画的原因。 +### 4.2 运行时(`sw-core`) -命令(和 Mac CLI 对齐): +按 Windows 版的文件逐个移植:包路径规则、压缩包预检和事务式安装、清单校验(走共用的 JS 校验器)、Babel 编译、执行器、时间线、`$storage`、`$file`、`fetch`、`$http`、`$xml`、`$weather`、`$dataSource`。 -| 命令 | 作用 | -| --- | --- | -| `scriptwidget new ` | 从模板创建一个包 | -| `scriptwidget check ` | 校验清单并编译,不执行脚本 | -| `scriptwidget render --family medium --output ` | 执行并输出 PNG 和报告。不需要图形会话,可以在 CI 里跑 | -| `scriptwidget dev ` | 监视文件,保存后刷新面板里的小组件 | -| `scriptwidget add ` / `remove` / `list` | 管理已安装的小组件 | -| `scriptwidget pack ` | 导出 `.swt` | -| `scriptwidget send ` | 发到手机,见第 5 节 | -| `scriptwidget doctor` | 环境自检 | +- **`Intl`**:链接系统的 ICU(Arch 的 `icu` 在 `core` 里,一定存在),通过一小段 C 垫片调用,垫片用头文件解决符号带版本号后缀的问题。输出要和一致性树逐字节相同。阶段 0 的结果:用系统 ICU(78.3,另在 79.1 上也跑过)127 个包全部相同,所以不随程序带 ICU 数据。[已核实] +- **网络**:自己解析主机名,拒绝私有、回环、链路本地地址后,只连接检查过的那个地址;重定向手动跟随,每一跳都重新检查;跨源重定向不带脚本设置的 `Authorization` 和 `Cookie`。和 Windows 版相同。 +- **数据目录**:`$XDG_DATA_HOME/scriptwidget`(`Packages`、`Storage`、`Plugins`、`widgets.json`、`settings.json`),缓存在 `$XDG_CACHE_HOME/scriptwidget`。 + +### 4.3 画笔(`sw-render`) + +把共用渲染器给出的元素树画成带透明度的位图,同时输出点击区域。文字的度量和绘制必须是同一套代码,否则换行和截断会错。 + +**选 Skia(`skia-safe`)。** 理由: + +- Android 的画笔就是画在 Skia 上的,Windows 的画笔也是照着 Android 的 `Canvas` 形状写的。用 Skia 可以近乎逐行移植,保真度风险最小。 +- 画笔用到的透视变换、模糊和阴影、锥形渐变、SVG 亮度蒙版,Skia 都直接支持。纯 Rust 的 `tiny-skia` 没有透视变换和滤镜,要自己补。 +- 文字用 Skia 的段落排版(自带断行、中日韩和表情的字体回退),不用自己写断行。 +- 代价:Skia 是 C++ 库,Rust 通过绑定调用;二进制会增大十几 MB;aarch64 上有预编译包,干净构建 8 秒,程序约 17 MB。[已核实] 阶段 0。中文字体的顺序要自己指定:测试机上系统回退选到了韩文字形的字体。 -### 4.2 Omarchy 插件 +如果阶段 0 发现 Skia 在 aarch64 上构建或打包走不通,退回纯 Rust 方案(`tiny-skia` 加 `cosmic-text`,缺的效果自己实现)。 -一个很小的 QML 插件,只做显示和转发,不执行任何包代码。 +字体:随程序携带 Inter 和六种展示字体,和 Android、Windows 相同;中日韩文字和表情用系统字体(经 fontconfig 查找)。 -- `service`:连接后台进程的 socket,接收"某个小组件有新图了"的通知。 -- `panel`:小组件面板,按快捷键呼出,按网格排列已安装的小组件。每个小组件是一张图,上面盖着后台给出的点击区域。 -- `bar-widget`:状态栏上的入口,点击打开面板。后续可以让一个小尺寸的小组件直接显示在状态栏上。 -- 点击时只把一个数字发回后台,由后台从自己保存的动作表里查出对应的动作。插件和 socket 上都不传动作内容。这和 Android 列表行点击的规则相同。 -- 主题:插件把 `Commons.Color` 的当前配色发给后台,后台据此给脚本提供深色或浅色外观。 +### 4.4 后台服务(`sw-host`)和显示端 -插件要能连接 Unix socket 或启动进程,Quickshell 提供这类类型(`Process`、`Socket`)。**[待验证]** 这是凭记忆,本次没有读到 Quickshell 的类型文档;Omarchy 的能力外观(facade)是否对第三方插件开放它们,也要在真机上确认。 +后台服务不开窗口。它把每个小组件画成 PNG,放在 `$XDG_RUNTIME_DIR/scriptwidget/` 下,并告诉显示端"哪个小组件有新图、放在哪、多大、哪些区域可以点"。 -### 4.3 平台契约 +- **和显示端通信**:插件用 Quickshell 的 `Process` 运行 `scriptwidget watch`,这条命令连上后台服务,把事件一行一个 JSON 地打印出来;点击和拖动结束时插件运行 `scriptwidget tap <小组件> <编号>`、`scriptwidget move <小组件> `。`Process` 是外壳自己和很多插件都在用的方式。Quickshell 也有 `Socket` 类型,但外壳和我读过的插件都没用它,所以不依赖它。 +- **显示**:插件声明 `service`(`keepLoaded: true`)和 `bar-widget`。`service` 里为每个小组件创建一个 `PanelWindow`,放在 `WlrLayer.Bottom`,里面是一个 `Image`。图片更新时在地址后面加版本号来绕过 Qt 的图片缓存,这是外壳自己的做法。 +- **缩放**:插件把每个显示器的缩放比例告诉后台服务,后台服务按那个比例画图,QML 按逻辑尺寸显示。 +- **提到最上层**:把这些窗口的层从 `Bottom` 改成 `Overlay`,再按一次改回去。由状态栏入口、`scriptwidget peek` 或用户绑定的快捷键触发。Quickshell 文档只写了"命名空间在窗口连接后不能改",运行中改层是可以的,Quickshell 0.3.2 上实测生效。[已核实] 阶段 0 +- **摆放模式**:平时小组件只响应按钮、开关、链接的点击,其余位置点击穿透到桌面。进入摆放模式后可以拖动,靠近屏幕边缘吸附;右键菜单可以改尺寸、编辑、移除。 +- **点击**:插件只发回一个编号,后台服务从自己保存的动作表里查出动作。链接只允许 `http`、`https`、`mailto`,其他协议拒绝。 +- **刷新**:默认每 15 分钟,时间线切换点,从睡眠恢复后,时钟或主题改变后,以及手动。会话锁定时暂停。睡眠和锁定通过 logind 的 D-Bus 信号得知。 +- **深浅色**:插件从 `Commons.Color` 判断当前主题的明暗,告诉后台服务。 +- **外壳重启或插件热重载**:窗口会消失再重建,小组件闪一下。位置、尺寸、内容都在后台服务里,不会丢。 +- **用户换了第三方状态栏**:状态栏入口会失去和 `service` 的联系,但桌面上的小组件不受影响;这时用命令行或管理窗口操作。 +- **开机启动**:一个 systemd 用户单元,随图形会话启动。插件发现后台服务没运行时,显示安装或启动的提示,不显示空白。 +- **socket**:`$XDG_RUNTIME_DIR/scriptwidget/host.sock`,权限仅限本用户,并核对对端的用户 ID。请求只包含包名、小组件编号和命令,不传任何会被当作代码执行的内容。 -- `$system.platform()` 返回 `"linux"`。内核现在的默认值是 `"ios"`(`kernel.js` 第 538 行),由宿主传入。 -- 支持 small、medium、large、extraLarge 四种尺寸;没有锁屏尺寸。 -- 没有对应物的能力返回 `[unsupported_platform]`,和 Android 的做法一致。所有差异写进 `docs/platform-support.md` 的新增一列。 +### 4.5 管理窗口(`sw-studio`) -| API | Linux 第一版 | 说明 | +**用 GPUI 加 gpui-component。** + +- GPUI 是 Zed 编辑器的界面框架,纯 Rust,Apache-2.0 许可。[已核实] Zed 仓库里 `crates/gpui/Cargo.toml` 的许可字段 +- gpui-component 是它上面的一套控件,Apache-2.0,带一个代码编辑器(Tree-sitter 语法高亮,诊断、补全、悬停提示)。项目自己说还在早期,接口会经常变。[已核实] 项目 README。它已经搬到 `longbridge/gpui-kit`,发布在 crates.io 上(`gpui-kit`,依赖 `gpui-pre`),不需要钉 Zed 仓库的提交;每周发一个版本,要锁定版本。[已核实] 阶段 0 +- 这样管理窗口从界面到编辑器全部是 Rust,不需要 WebKit,也不需要 GTK。实时预览直接调用 `sw-render`,和桌面上的小组件是同一个画笔。 + +页面和 Windows 版对应:我的小组件、模板(打开后先看预览和代码,再"使用此模板")、组件、API 参考、编辑器、设置。界面语言跟随系统,英文和简体中文。补全和参考页的数据来自 `Editor/editorfe/src/scriptWidgetAPI.js`,构建时转成 Rust 能读的数据。 + +要在阶段 0 确认的风险: + +- **图形接口**。GPUI 在 Linux 上的渲染从 2026 年 2 月起改用 wgpu([提交 46758](https://git.secluded.site/zed/commit/af8ea0d6c26192c45f44f473c0d4a7d6f72ed018))。RiftVM 只有 OpenGL ES 3.0,Vulkan 是软件渲染。管理窗口在这台虚拟机里能不能跑、流不流畅,要实测。跑不动不代表真机不行,但意味着我没法在这里测它。 +- **版本**。crates.io 上的 GPUI 停在 2025 年 10 月的版本,比 Zed 主线旧很多;新的渲染在 Git 主线上。要固定依赖 Zed 仓库的某个提交。Zed 团队表示 GPUI 只优先做编辑器自己需要的功能。[已核实] 第三方文章,未对照 Zed 仓库 +- **中文输入**。字符能输入和显示;输入法的组字在虚拟机里没法测(没有中文输入法),要在真实电脑上测。**[待验证]** +- **编辑器的补全**。gpui-component 的补全是按语言服务器的方式设计的,我们要把 `scriptWidgetAPI.js` 的数据接进去,接口够用:补全接口拿到文本和位置,返回补全项,阶段 0 里接了三项固定数据。[已核实] +- 编辑器的行为会和 Mac、Windows 上的 CodeMirror 不完全一样(快捷键、补全的细节)。 + +**退路**:如果 GPUI 在阶段 0 不通过,改用 GTK 4 加 libadwaita,编辑器区域用 WebKitGTK 放现有的 CodeMirror。 + +### 4.6 命令行(`sw-cli`) + +和 Mac、Windows 的命令行对齐,输出 JSON 报告,退出码固定。 + +| 命令 | 作用 | 需要后台服务在运行 | | --- | --- | --- | -| `$device`、`$system`、`$storage`、`$file`、`fetch`、`$http`、`$xml` | 支持 | 权限、域名声明、私有地址拒绝、请求预算和其他平台相同 | -| `$dataSource`(数据源插件) | 支持 | 纯声明式的 HTTPS 映射,逻辑可复用 | -| 时间线、按钮和开关 | 支持 | 后台进程自己调度,没有系统的刷新配额 | -| `$music` | 之后 | 通过 MPRIS(D-Bus)读取正在播放的媒体 | -| `$notifications` | 之后 | 通过桌面通知接口 | -| `$weather`、`$location` | 之后 | 需要先确定数据来源 | -| `$calendar`、`$reminders`、`$health`、`$photos`、`$contacts`、`$home`、`$icloud`、`$alarms`、`$maps`、`$services` | 不支持 | 返回 `[unsupported_platform]` | -| Live Activity、Control Widget、Siri 动作、推送刷新 | 不支持 | | +| `scriptwidget new <名字>` | 从模板创建一个包 | 否 | +| `scriptwidget check <目录>` | 校验清单并编译,不执行 | 否 | +| `scriptwidget run <目录>` | 执行,输出元素树、日志、错误 | 否 | +| `scriptwidget render <目录> --family medium --output ` | 执行并画成图片 | 否 | +| `scriptwidget timeline <目录>` | 输出时间线 | 否 | +| `scriptwidget pack <目录>` | 导出 `.swt` | 否 | +| `scriptwidget install <.swt 或目录>`、`list`、`templates` | 管理已安装的包 | 否 | +| `scriptwidget add <包> [--family]`、`remove`、`widgets`、`refresh` | 管理桌面上的小组件 | 是 | +| `scriptwidget dev <目录>` | 监视文件,保存后刷新桌面上的小组件 | 是 | +| `scriptwidget peek`、`arrange` | 提到最上层、进入摆放模式 | 是 | +| `scriptwidget watch`、`tap`、`move`、`screen`、`theme`、`menu` | 给显示端用,见 4.8 | 是 | +| `scriptwidget doctor` | 环境自检 | 否 | + +### 4.7 Omarchy 插件 + +``` +omarchy-plugin/ + manifest.json schemaVersion 1,id、name、version、author、description、license、 + kinds: ["service", "bar-widget"],keepLoaded: true + Service.qml 运行 watch,维护小组件列表,为每个小组件建一个窗口 + WidgetWindow.qml 一个 PanelWindow:图片、点击区、摆放模式下的拖动和右键菜单 + BarWidget.qml 状态栏入口 + README.md、LICENSE +``` -这意味着依赖健康、日历这类数据的小组件在桌面上只能显示"此平台不支持"。对目标用户来说可以接受:他们在桌面上写和预览,数据在手机上才有。但 `render` 要支持模拟数据(Mac CLI 已有 `--fixtures`),否则这类小组件在桌面上没法调试。 +种类 `service` 加 `bar-widget`。`service` 负责桌面层的显示(见 4.4)。`bar-widget` 是状态栏上的入口:点击打开管理窗口;菜单里有"提到最上层"和"摆放模式"。 -## 5. 发到手机 +插件越小越好。它和外壳在同一个进程里,没有沙箱,所以它只显示图片、转发编号和坐标,不读包的内容,不拼接命令行字符串(`Process` 的命令是参数列表,不经过 shell)。 + +本地开发:把 `Linux/omarchy-plugin/` 链接到 `~/.config/omarchy/plugins//`,保存即热重载。 -这是整个计划里决定成败的部分,也是目前最不确定的部分。 +### 4.8 显示协议 -现状: +后台服务和显示端之间只有下面这些消息。显示端拿不到包的任何内容,只有图片路径、几何信息和编号。 -- Android 应用能打开 `.swt` 文件,并注册了 `scriptwidget://` 协议(`AndroidManifest.xml`)。**[已核实]** -- iOS 应用注册了 `scriptwidget://` 协议(`Info.plist`)。它现在能处理哪些地址,本次没有核实;一份旧调研文档说只支持 `reload-all`。**[待验证]** -- iOS 应用有 Web Studio 配对:手机上开启后,同一局域网的电脑用六位配对码连接,推送包里的文本文件(`docs/web-studio.md`,客户端在 `Web/preview-engine/src/cli/deviceSync.js`)。**[已核实]** Android 有没有同样的接口,本次没有核实。**[待验证]** +`scriptwidget watch` 的输出,一行一个 JSON: -方案:`scriptwidget send ` +| 事件 | 字段 | 含义 | +| --- | --- | --- | +| `hello` | `protocol`(整数,第一版是 1)、`dark`、`arrange`、`peek` | 连接建立后的第一行 | +| `widget` | `id`、`screen`、`x`、`y`、`width`、`height`(逻辑像素)、`image`(PNG 的绝对路径)、`version`(整数)、`regions`(`[{n, x, y, width, height}]`)、`frames`(可选,见下) | 新增或更新一个小组件。`version` 每次换图加一 | +| `remove` | `id` | 移除一个小组件 | +| `mode` | `arrange`、`peek` | 摆放模式或"提到最上层"的开关变了 | +| `bye` | `reason` | 后台服务要退出 | + +显示端发回的命令,都是参数列表,不经过 shell: + +| 命令 | 含义 | +| --- | --- | +| `scriptwidget tap ` | 用户点了编号为 `n` 的区域 | +| `scriptwidget move ` | 拖动结束后的新位置 | +| `scriptwidget screen ` | 显示器的名字、缩放和逻辑尺寸;启动时和变化时各报一次 | +| `scriptwidget theme ` | 当前主题的明暗 | +| `scriptwidget menu ` | 右键菜单里选了一项。可选的词是写死的一个列表 | + +规则: + +- `id` 是后台服务生成的随机标识,`n` 是整数。后台服务对两者都做校验,不认识的直接拒绝。 +- 图片放在 `$XDG_RUNTIME_DIR/scriptwidget/frames/` 下,目录权限 0700。新图先写临时文件再改名,显示端不会读到写了一半的文件。旧图在显示端换图之后删除。 +- 协议版本不匹配时,显示端显示"请更新 ScriptWidget",不尝试解析。 +- `watch` 进程退出时,显示端每隔几秒重连,期间保留最后一张图。 + +**GIF**:由后台服务用 Rust 解码,不交给 QML 解析(GIF 来自包,不可信;外壳进程不应该解析它)。上限:每个小组件最多 60 帧,解码后的总像素有上限,超出的只显示第一帧。后台服务把每一帧按完整排版画成图片,在 `frames` 里给出 `[{image, ms}]`,显示端按时间循环换图。窗口被遮住、锁屏或"提到最上层"关闭且有全屏窗口时暂停。 -1. 打包成 `.swt`,在局域网上临时提供一次下载,地址里带一次性令牌。 -2. 在终端里显示一个二维码,内容是 `scriptwidget://import?...`,带下载地址、文件大小和 SHA-256。 -3. 手机相机扫码,打开应用。应用下载文件,核对大小和 SHA-256,然后走**现有的不可信压缩包导入流程**:预检、限额、权限预览、用户确认后才安装。 -4. 下载完成或超时后,电脑端关闭这次下载。 +### 4.9 平台契约 -需要的应用侧改动:iOS 和 Android 各加一个 `import` 地址处理。这会改动两个已上架应用,属于新增契约,要有安全评审和回归测试: +- `$system.platform()` 返回 `"linux"`。 +- 尺寸:`small` 170×170、`medium` 329×170、`large` 329×345、`extraLarge` 639×345 点,乘以显示缩放。和 Mac、Windows 相同,没有锁屏尺寸。 +- 渲染模式固定 `fullColor`;`backgroundMaterial` 用不透明的 `background` 回退;时间线在每个切换点重新执行脚本。和 Windows 相同。 +- `` 会动(见 4.8);`animation` 属性的过渡动画第一版不做,显示最终状态。 +- `` 的计时样式每秒重画,时钟样式每分钟重画。 -- 只接受用户主动扫码触发的导入,不能由小组件脚本触发。 -- 导入的包和其他来源的包一样不可信,不因为来自"自己的电脑"而放宽任何检查。 -- 脚本的网络规则(拒绝私有和本地地址)不变;这里访问局域网的是应用自己的导入功能,不是脚本。iOS 上会触发"本地网络"权限提示。 -- 二维码内容不进日志。 +| API | Linux 第一版 | +| --- | --- | +| `$device`、`$system`、`$storage`、`$file`、`fetch`、`$http`、`$xml` | 支持,权限、域名声明、私有地址拒绝、预算和其他平台相同 | +| `$dataSource` | 支持 | +| `$weather` | 支持,和 Android、Windows 一样用 MET Norway | +| `$music`(MPRIS)、`$notifications` | 之后 | +| `$calendar`、`$reminders`、`$health`、`$photos`、`$contacts`、`$documents`、`$home`、`$icloud`、`$alarms`、`$maps`、`$services`、`$location` | 不支持。`isAvailable()` 返回 `false`,调用以 `[unsupported_platform]` 拒绝 | +| Live Activity、Control Widget、Siri 动作、推送刷新 | 不支持 | + +用到最后两行 API 的模板不出现在模板列表里。所有差异写进 `docs/linux/README.md` 和 `docs/platform-support.md` 的新增一列。 + +## 5. 发到手机 -备选方案(不需要改应用,但体验差):`scriptwidget pack` 后用户自己把 `.swt` 传到手机,例如通过 LocalSend 或聊天软件。阶段 0 用它来验证流程是否有人愿意用。 +第一版:管理窗口和 `scriptwidget pack` 导出 `.swt`,用户自己传到手机打开。iOS 和 Android 应用现在就能打开 `.swt`,不需要改应用。 -以后可以考虑的:从 Git 仓库地址安装。Omarchy 用户习惯把配置放在 Git 里,但这是一个新的安装来源,要单独做信任设计,不在这份计划里。 +之后:扫码导入。需要给 iOS 和 Android 各加一个导入入口,属于新增契约,要单独做安全评审和回归测试,并等两个应用过审。设计见旧稿,不在第一版范围内。 ## 6. 安全 -脚本、导入的压缩包、网络响应仍然全部视为不可信。Linux 上要重新落实的不变量: +脚本、导入的压缩包、网络响应仍然全部视为不可信。Linux 上要重新落实并带测试的: -- 包路径规范化,拒绝穿越、绝对路径、符号链接、反斜杠、大小写冲突。 -- 压缩包的条目数、单文件大小、展开后总大小限制;先预检再解压;用唯一的临时目录;事务式安装。 -- 网络需要 `network` 权限和匹配的 `networkDomains`;拒绝私有和本地地址;请求和响应预算。 -- `$file` 限制在包内,`$storage` 按包隔离并有上限。 +- **包路径**:规范化后拒绝穿越、绝对路径、符号链接、硬链接、反斜杠、大小写冲突。打开文件用 `openat2` 的 `RESOLVE_BENEATH` 和 `O_NOFOLLOW`,读取前检查链接数。 +- **压缩包**:条目数、单文件大小、展开后总大小限制;先预检再解压;唯一的临时目录;事务式安装。 +- **网络**:见 4.2。 +- **`$file`** 限制在包内,**`$storage`** 按包隔离并有上限。 - 清单存在但格式错误时直接失败,不当作旧格式处理。 -- 以上每一条都要有对应的测试,从 Android 和 Swift 的测试用例移植。 +- **socket**:仅本用户可访问,核对对端用户 ID。 +- **显示端**:插件只收到图片路径、位置、尺寸和点击区域的编号,见 4.8。 +- **引擎隔离**:第一版包代码在后台服务的进程里运行,QuickJS 不暴露任何系统接口,内存、栈、时间都有上限,和其他平台现状相同。之后把执行放进单独的子进程,用 Landlock 和 seccomp 限制。 +- 不做 AI 客户端的话不保存任何密钥;如果之后把 AI 生成搬过来,密钥放 Secret Service(系统钥匙串),不写进配置文件。第一版的管理窗口**不含 AI 生成**。 -Linux 特有的: - -- socket 放在 `$XDG_RUNTIME_DIR` 下,权限仅限本用户。 -- 包代码只在后台进程里运行,不进入外壳进程。Omarchy 插件本身没有沙箱,所以它越小越好,并且不解释任何来自包的内容。 -- 执行脚本的子进程用 Landlock 和 seccomp 再加一层系统级限制。**[待验证]** 放在阶段 4,不阻塞第一版;第一版依靠 QuickJS 不暴露任何系统接口这一点。 -- 不做 AI 客户端,所以 Linux 版不保存任何 API 密钥。 +每条安全规则都要有一个测试;安全修复要有修复前会失败的回归测试。 ## 7. 分发 | 内容 | 渠道 | | --- | --- | -| `scriptwidget` 二进制 | GitHub Releases(x86_64,之后加 aarch64);AUR 包 `scriptwidget-bin` | -| Omarchy 插件 | 一个单独的公开仓库(根目录要有 `manifest.json`),源码在本仓库 `Linux/omarchy-plugin/`,发布时同步过去 | -| 插件目录上架 | 通过 plugins.omarchy.org 的 issue 表单提交 | +| 三个程序 | GitHub Releases,标签 `linux-v<版本>`,x86_64 和 aarch64 各一个压缩包,附 SHA-256 | +| Arch 和 Omarchy 用户 | AUR 包 `scriptwidget-bin`(装程序、systemd 用户单元、桌面文件、`.swt` 的文件关联) | +| Omarchy 插件 | 一个单独的公开仓库,根目录是插件;通过插件目录的表单提交 | -未决的问题: +安装是两步,和 ai-usagebar 的惯例相同:`omarchy pkg aur add scriptwidget-bin`,然后 `omarchy plugin add <插件仓库> --enable`。两样都要装:没有插件,小组件显示不到桌面上;没有程序,插件只显示安装提示。 -- 插件依赖一个外部二进制。Omarchy 的清单没有声明系统依赖的字段,文档只要求在 README 里写清楚。所以安装是两步:先装二进制,再 `omarchy plugin add`。插件在后台没运行时要显示清楚的安装提示,而不是空白。 -- **提交上架前先问插件目录的维护者**:是否接受依赖外部二进制的插件,是否接受关联商业应用的插件。发布页没有写这两点,不能假设可以。 +按仓库的规则,预览版的 release 不标 Latest。 ## 8. 阶段 -估算是单人加 AI 辅助的粗估,没有依据可以校准,只用来排顺序。 +不给时间估算:没有可以校准的依据。每个阶段有通过标准,没通过就不进下一个。 + +### 阶段 0:验证(在 RiftVM 里) + +**状态(2026-11-17):已做完。** 引擎、Skia、QML 显示端、GPUI 都通过;没能测的是中文输入法、真实显卡、高分屏和多显示器;Omarchy 升到了 4.5.0-3,但有一个迁移步骤失败。详细结果在 [testing.md](testing.md)。引擎那部分直接写成了正式代码(`Linux/crates/sw-quickjs`、`Linux/crates/sw-core`),其余三项在 `Linux/spikes/`。 -### 阶段 0:技术验证(1 到 2 周) +验证用的代码放在 `Linux/spikes/`,每个问题一个小工程,通过后删掉或并入正式代码。按下面的顺序做,前一项不通过不做后一项: -回答五个问题,任何一个是"否"就停下来重新评估。 +1. 连上虚拟机,装 Rust、cmake、Node;记录 Omarchy、Quickshell、Qt、ICU 的版本。 +2. `spikes/quickjs`:编译仓库里的 QuickJS-NG,跑共用内核,对比一致性树(参考文件 164 棵,其中不需要实时数据的 127 个包参加对比)。 +3. `spikes/skia`:用 `skia-safe` 画文字和 10 个模板。 +4. `spikes/plugin`:一个本地 QML 插件加一个假的 `watch`(只打印写死的事件),验证显示、换图、点击、拖动、换层、GIF 换帧。 +5. `spikes/gpui`:一个带编辑器的 GPUI 窗口。 +6. 把结果写进 `docs/linux/testing.md`,每项写明通过、不通过或没法测,并附截图。 -| 问题 | 通过标准 | + +| 要回答的问题 | 通过标准 | | --- | --- | -| 共用内核在 Linux 的 QuickJS 里结果是否一致 | 164 棵一致性树全部逐字节相同 | -| 画笔的保真度够不够 | 选 10 个有代表性的模板,和浏览器预览的截图对比,差异在 Android 当时接受的范围内;中文、表情、图表各至少一个 | -| Omarchy 插件能不能做展示端 | 在 Omarchy 4 真机上:面板能显示后台给的图,保存文件后 1 秒内更新,点击能传回后台 | -| 发到手机的流程走不走得通 | 先用备选方案(`pack` 后手动传),iPhone 和 Android 各成功导入并显示一次 | -| 有没有人要 | 把原型的短视频发到 Omarchy 社区,看反馈。由你判断,我给不出阈值 | +| 我能不能从 Mac 的命令行驱动虚拟机 | 通过 SSH 同步代码、编译、运行图形程序、用 `grim` 截图传回,全程不需要你操作 | +| 引擎结果是否一致 | 不需要实时数据的 127 个包的一致性树在 aarch64 上逐字节相同(含 `Intl` 和本地时间) | +| Skia 能不能用 | `skia-safe` 在 aarch64 的 Arch 上能构建并画出文字;画 10 个有代表性的模板,和 Windows 版 `swcli` 画的参考图对比,中文、表情、图表各至少一个 | +| QML 显示端是否可行 | 一个本地插件把一张图显示在 Omarchy 的桌面层,位置正确;文件更新后 1 秒内换图;点击和拖动能通过 `Process` 传回;`peek` 能提到最上层再放回;外壳重启后恢复 | +| GPUI 能不能用 | 一个 GPUI 窗口在虚拟机里能打开,里面的 gpui-component 编辑器能显示一段 JSX、高亮、弹出我们给的补全、输入中文。不通过就改用退路(4.5) | +| Omarchy 版本 | 虚拟机里的 4.0.6 能不能升到 4.5;不能的话记录下来,以 4.0.6 为测试基线 | + +任何一项不通过,先停下来告诉你,再决定换方案还是调整范围。 + +### 阶段 1:运行时和命令行 + +**状态(2026-11-17):做完,通过标准已满足。** 四个 crate 都在,测试虚拟机上 418 个测试通过,`scriptwidget render` 在没有图形会话的情况下画出了 116 个不需要联网的内置模板。CI 在 x86_64 和 aarch64 上都通过了(PR 77 的第一次运行)。详见 [testing.md](testing.md) 和 [TODO.md](TODO.md);用法见 [README.md](README.md)。 + +`sw-quickjs`、`sw-core`、`sw-render`、`sw-cli`。移植 Windows 版的测试。GitHub Actions 加 Linux 任务(x86_64 和 aarch64)。 + +通过标准:`scriptwidget render` 在没有图形会话的机器上画出所有不依赖系统数据的内置模板;一致性树、包和压缩包的安全测试、网络策略测试在 CI 的两种架构上通过。 + +### 阶段 2:后台服务和显示端 -顺带确认:壁纸层窗口是否可行;第三方插件能否使用 socket 或进程;`omarchy pkg aur add` 在 4.0 上是否还是这个命令。 +**状态(2026-11-17):做完。** 后台服务(`sw-host`,由 `scriptwidget host run` 启动)、需要它的命令、Omarchy 插件都在;在 Omarchy 4.5 上从发布包安装后做了端到端检查。和本计划不一致的地方以 [protocol.md](protocol.md) 和代码为准:服务和命令行是同一个程序;摆放模式用每块屏幕一个窗口;`keepLoaded` 的插件改了文件不会热重载,要重启外壳。 -### 阶段 1:命令行(2 到 3 周) +`sw-host`、需要它的那些命令,以及 Omarchy 插件的桌面层显示和状态栏入口。包括 GIF 动画和每秒重画的计时。 -`Linux/` 目录和 Rust 工程;QuickJS 宿主抽成 Android 和 Linux 共用;包存储和校验;`new`、`check`、`render`、`pack`、`doctor`;网络、存储、文件模块和它们的权限检查;安全测试移植;GitHub Actions 上的 Linux 任务。 +通过标准(在 RiftVM 的 Omarchy 上):桌面上放多个小组件,按时间线刷新,按钮和开关生效,能拖动摆放,`peek` 可用,外壳重启、后台服务重启、虚拟机重启后都能恢复,锁屏时暂停。有一个不看屏幕就能跑的自检,像 Windows 版的 `desktop-check`。 -完成标准:`scriptwidget render` 能在没有图形会话的机器上渲染全部内置模板中不依赖系统数据的那部分,一致性树和安全测试在 CI 里通过。这一阶段结束时,CLI 本身已经可以单独发布。 +### 阶段 3:管理窗口 -### 阶段 2:后台进程和插件(2 到 3 周) +**状态(2026-11-17):做完第一版。** GPUI 加 gpui-kit 0.10.0(版本锁定)。我的小组件、模板、带实时预览的编辑器、设置都有;组件和 API 参考页没做。中文输入法和真实显卡仍然没法在虚拟机里测。 -`daemon`、socket 协议、时间线调度、按钮和开关;Omarchy 插件的 `service`、`panel`、`bar-widget`;`dev` 的实时刷新;`add`、`remove`、`list`;深浅色跟随主题。 +`sw-studio`。 -完成标准:在 Omarchy 4 真机上,装好插件后面板里能放多个小组件,按时间线刷新,点击按钮生效,外壳重启和后台重启后都能恢复。 +通过标准:从模板添加一个小组件到桌面、编辑并看到实时预览、导出 `.swt`、导入 `.swt`,每一步都截图记录;中英文界面各检查一遍。 -### 阶段 3:发到手机(2 到 3 周) +### 阶段 4:打包、发布预览版 -`send` 和二维码;iOS 和 Android 的 `import` 地址处理,及各自的安全测试;发布包含它的 iOS 和 Android 版本。 +**状态(2026-11-17):安装包和发布流程做完。** 发布物是一个压缩包加 `install.sh`,装在用户目录下,不需要 root;插件随包安装,没有单独的插件仓库。AUR 包和插件目录的提交没有做,见 [TODO.md](TODO.md)。 -完成标准:在 iPhone 和 Android 真机上,从终端里的二维码到主屏幕上出现小组件,全程不离开手机相机和应用。 +插件的公开仓库;AUR 包;`docs/linux/README.md`、`testing.md`、`TODO.md`;`docs/platform-support.md` 加 Linux 列;仓库 README 加 Linux。发布 `linux-v0.1.0` 预览版。 -这一阶段依赖应用商店审核。Android 还要先拿到正式发布权限。 +通过标准:在一台干净的 Omarchy 虚拟机上,按 README 的两步安装后能用;卸载后不留文件。 -### 阶段 4:发布(1 到 2 周) +### 之后 -AUR 包;插件仓库和上架申请;`docs/linux/README.md`、`testing.md`、`TODO.md`;`docs/platform-support.md` 加 Linux 列;官网和 README 加 Linux;`$music`(MPRIS);系统级沙箱的评估。 +扫码发到手机;`$music`;受限的执行子进程;`animation` 过渡动画;提交插件目录;其他桌面的显示端(直接用 layer-shell 协议的 Rust 程序覆盖 Sway、KDE、niri 等,GNOME 扩展加 `.deb` 包覆盖默认的 Ubuntu 和 Fedora)。 ## 9. 测试 -- **CI 里能跑的**:一致性树、包和压缩包的安全测试、网络策略测试、`render` 的截图回归。这些不需要图形会话。 -- **只能在真机上做的**:插件的显示、刷新、点击、主题切换、外壳重启后的恢复;HiDPI 和分数缩放;多显示器。每次记录到 `docs/linux/testing.md`,写明 Omarchy 版本和机器。 -- **改动共用代码时**:`Web/preview-engine` 和 QuickJS 宿主被三端共用,改动后要跑预览引擎自己的测试、Android 的 JVM 和设备测试、Linux 的测试。 +- **CI 里跑的**:一致性树、安全测试、网络策略测试、`render` 的截图回归。后台服务和命令行之间的协议在 CI 里测;显示端只能在 Omarchy 里测。**[待验证]** +- **RiftVM 里跑的**:桌面层显示、缩放、点击、拖动、主题切换、恢复、管理窗口。每次记录到 `docs/linux/testing.md`,写明 Omarchy 版本、RiftVM 版本、架构。 +- **改动共用代码时**:`Web/preview-engine` 和 QuickJS 宿主脚本被四端共用,改动后要跑预览引擎自己的测试、Android 的 JVM 和设备测试、Windows 的测试、Linux 的测试。 + +**测不到的,不会写成测过的**: + +- x86_64 的真机。RiftVM 是 aarch64,x86_64 只有 CI 上的构建和无界面测试,窗口行为没有人看过。 +- 真实显卡。RiftVM 是虚拟的 OpenGL ES 3.0。 +- 多显示器、睡眠和唤醒。RiftVM 只有一个显示器,睡眠和唤醒本身还没验证过。 +- Omarchy 之外的桌面:第一版没有显示端,只有命令行和管理窗口。 +- 如果 GPUI 在虚拟机里跑不动,管理窗口在这里就测不了,只能靠你在真机上看。 + +## 10. 需要你做的 + +**现在就需要(阶段 0 的前提),都在 RiftVM 的窗口里,我不能代做:** + +1. 打开 RiftVM,点 **Prepare Omarchy**,等约 4.4 GB 的镜像下载和校验完成。 +2. 点 **Start Omarchy**。 +3. 填首次开机表单:用户名、密码(两遍)、键盘、主机名、时区。 +4. 先在 Mac 的终端里把公钥放进共享文件夹:`mkdir -p ~/riftvm-shared && cp ~/.ssh/id_ed25519.pub ~/riftvm-shared/`。然后在虚拟机里按 Command-Return 打开终端,运行三条命令(没有在虚拟机里试过,报错的话把输出发给我): + - 开启 SSH 并装入公钥:`omarchy-setup-security-sshd --key="$(cat /mnt/mac/riftvm-shared/id_ed25519.pub)"` + - 给这个用户免密码的 sudo(装包要用;这只是一台测试虚拟机):`echo "$USER ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/99-dev` + - 关掉屏保和自动锁屏:`omarchy-toggle-idle stay-awake` +5. RiftVM 的窗口保持开着,可以放在后台。虚拟机或 Mac 重启后,需要你重新点 Start 并输入密码。 + +**之后需要:** -## 10. 需要你提供或决定的 +6. 一个 AUR 账号,用来发布 `scriptwidget-bin`(阶段 4)。注册和上传是你的账号,我不代做。 +7. 一个放 Omarchy 插件的公开仓库(阶段 4)。你说可以的话我用 `gh` 建。 +8. 提交插件目录前,问维护者对关联商业应用的插件有没有限制。我可以起草。 -1. **一台装了 Omarchy 4 的 x86 机器。** 当前这台 Mac 上验证不了插件,Apple 芯片上的 Omarchy 还没完成。没有它,阶段 0 的第三个问题和阶段 2 都做不了,我也不能声称任何插件行为是验证过的。 -2. **是否接受改动两个手机应用来支持扫码导入。** 这是阶段 3 的前提。 -3. **优先级。** 仓库当前的优先事项是稳定已有契约和把 Android 送上 Google Play。Android 封闭测试要等 14 天,阶段 0 可以放在这段等待期里;阶段 1 之后的投入建议等阶段 0 的第五个问题有了答案再决定。 -4. **插件目录的政策。** 上架前要问维护者,见第 7 节。 +**可以否决的两个选择**:Skia、第一版引擎和后台服务同进程。见第 1 节。 ## 11. 不做的事 -- Linux 上的图形界面应用、编辑器、Gallery 界面、内置 AI。 -- Omarchy 3 及更早版本(Waybar)。 -- 承诺支持其他发行版和窗口管理器。命令行在任何 Linux 上都能用;展示端第一版只有 Omarchy 插件。因为它是 Quickshell 的 QML,之后做一个独立的 Quickshell 配置给非 Omarchy 用户是可能的,但不在这份计划里。 -- 新的 JSX 标签或只有 Linux 才有的能力(例如读取 Hyprland 工作区、使用主题调色板里的具体颜色)。这些是新的契约范围,等第一版有用户之后再提。 +- 第一版不做:扫码发到手机、AI 生成、Gallery 界面、`$music`、`animation` 过渡动画。 +- 不支持 Omarchy 3 及更早版本、X11、GNOME。 +- 不做只有 Linux 才有的新标签或新能力(例如读取 Hyprland 工作区)。这是新的契约范围,等第一版有用户之后再提。 +- 不用 QML 排版或绘制小组件的内容(QML 只显示画好的图片),不在 Quickshell 的 JS 引擎里执行包代码。 +- 第一版不支持 Omarchy 之外的桌面显示小组件。 ## 资料来源 -- [Omarchy 4 "Quattro" 的合并请求](https://github.com/basecamp/omarchy/pull/6231) -- [omarchy-shell.md(外壳参考)](https://github.com/omacom/omarchy/blob/quattro/docs/omarchy-shell.md) -- [Develop a Plugin](https://plugins.omarchy.org/develop.html) -- [Publish a Plugin](https://plugins.omarchy.org/publish.html) -- [Omarchy 自带的 Skill 文档](https://raw.githubusercontent.com/basecamp/omarchy/master/default/omarchy-skill/SKILL.md) -- [What is Omarchy(第三方文章)](https://www.starryhope.com/linux/what-is-omarchy/) -- [Quickshell](https://quickshell.org/) -- [Android 版的调研与规划](../android/android-port-research-and-plan.md) +- [Omarchy 仓库](https://github.com/omacom/omarchy)(分支 `quattro`)及其 `docs/omarchy-shell.md` +- [Omarchy 4.5 发布说明](https://omarchy.org/news/2026/11/omarchy-4-5/) +- [插件开发](https://plugins.omarchy.org/develop.html)、[插件上架](https://plugins.omarchy.org/publish.html) +- [Quickshell 0.3.2 文档](https://quickshell.org/docs/v0.3.2/) +- [gpui-component,现名 gpui-kit](https://github.com/longbridge/gpui-kit)、[GPUI 改用 wgpu 的提交](https://git.secluded.site/zed/commit/af8ea0d6c26192c45f44f473c0d4a7d6f72ed018) +- [AGS](https://github.com/Aylur/ags)(参照对象,本次未重新查证) +- [Omalauncher](https://github.com/SimonSchubert/omalauncher-omarchy-plugin)、[ai-usagebar](https://github.com/akitaonrails/ai-usagebar)、[omaclock](https://github.com/ubeyidah/omaclock) +- RiftVM 源码及其 `docs/` +- [ScriptWidget for Windows](../windows/README.md)、[Android 版的调研与规划](../android/android-port-research-and-plan.md) diff --git a/docs/linux/protocol.md b/docs/linux/protocol.md new file mode 100644 index 00000000..5409372f --- /dev/null +++ b/docs/linux/protocol.md @@ -0,0 +1,223 @@ +# ScriptWidget for Linux: the display protocol + +How the background service and a display talk to each other. The contract is sections 4.4 and 4.8 of [plan.md](plan.md) (Chinese); this page writes it out exactly and records every decision the plan left open. Those are marked **Decision**. + +A display is whatever puts the pictures on the desktop: today the Omarchy plugin (`Linux/omarchy-plugin`). It never runs or reads package content. It receives picture paths, rectangles and numbers, and sends back numbers and words from fixed lists. + +Protocol version: **1**. + +## The parts + +| Part | What it is | +| --- | --- | +| Service | `scriptwidget host run`, a long-running process of the user's session. Code: `Linux/crates/sw-host` | +| Socket | `$XDG_RUNTIME_DIR/scriptwidget/host.sock` | +| Pictures | PNG files in `$XDG_RUNTIME_DIR/scriptwidget/frames/` | +| Commands | `scriptwidget watch`, `tap`, `move`, `screen`, `theme`, `menu`, `session`: each connects to the socket, sends one request and prints the answer | + +**Decision: the service is the `scriptwidget` program itself** (`scriptwidget host run`), not a second program `scriptwidget-host`. One file is installed, and the command line can never be a different version from the service it talks to; every binary links the engine and Skia statically, so a second one would double the installed size. A systemd user unit starts it (`Linux/packaging/systemd/scriptwidget.service`). When the program is installed or linked under the name `scriptwidget-host` it behaves as `scriptwidget host run`. + +A display should run the commands, not open the socket itself: the commands are the stable interface. The socket format is written down below for the management window and for tests. + +## Starting and stopping + +- `scriptwidget host run [--data ]` needs `XDG_RUNTIME_DIR`; there is no fallback to `/tmp`. It prints nothing on standard output; a few lines for the journal go to standard error (identifiers, error kinds and its own paths, never package content or addresses of links). +- Exit codes: `0` after `SIGTERM` or `SIGINT`; `2` when it cannot start (no runtime directory, no runtime files, or a service is already running in this session: the message says "already running"). +- A second service in the same session is refused through a lock on `host.lock` (`flock`). The service that holds the lock removes a socket file left by a service that crashed, and all pictures of an earlier run. +- On `SIGTERM` or `SIGINT` the service sends `bye` to every watcher, waits up to one second for those lines to be written, removes the socket and the pictures, and exits. +- The unit is `PartOf` and `WantedBy` `graphical-session.target`. It uses `KillMode=process`: a browser started by a link in a widget, and the management window, are not stopped with the service. + +## Events: what `scriptwidget watch` prints + +One JSON object a line, UTF-8, each with a string member `event`. A display ignores events and members it does not know. + +| Event | Members | +| --- | --- | +| `hello` | `protocol` (integer, 1), `dark`, `arrange`, `peek` (booleans), `service` (the service's version, text) | +| `widget` | `id`, `screen`, `x`, `y`, `width`, `height`, `image`, `version`, `regions`, optional `frames`, `family`, `families` | +| `remove` | `id` | +| `mode` | `arrange`, `peek` | +| `bye` | `reason` | + +Examples, exactly as printed: + +``` +{"event":"hello","protocol":1,"dark":false,"arrange":false,"peek":false,"service":"0.1.0"} +{"event":"widget","id":"5ef2d5c170089ecf","screen":"eDP-1","x":40,"y":40,"width":329,"height":170,"image":"/run/user/1000/scriptwidget/frames/5ef2d5c170089ecf-1d639a55-3.png","version":3,"regions":[{"n":0,"x":16,"y":120,"width":96,"height":34}],"family":"medium","families":["small","medium","large"]} +{"event":"widget","id":"c45467fe6c9c2b4a","screen":"","x":68,"y":68,"width":170,"height":170,"image":"/run/user/1000/scriptwidget/frames/c45467fe6c9c2b4a-1d639a55-1-0.png","version":1,"regions":[],"frames":[{"image":"/run/user/1000/scriptwidget/frames/c45467fe6c9c2b4a-1d639a55-1-0.png","ms":200},{"image":"/run/user/1000/scriptwidget/frames/c45467fe6c9c2b4a-1d639a55-1-1.png","ms":300}],"family":"small","families":["small","medium","large","extraLarge"]} +{"event":"remove","id":"5ef2d5c170089ecf"} +{"event":"mode","arrange":true,"peek":true} +{"event":"bye","reason":"shutdown"} +``` + +### `hello` + +The first line of every connection. A display that reads another `protocol` shows "update ScriptWidget" and parses nothing else. + +**Decision: after `hello` the service sends one `widget` event for every widget that has a picture**, immediately, so a display that connects later has the whole state. A widget whose first picture is still being drawn (just after the service started, or just added) is sent when the picture is ready. A display that removes widgets it was not told about again should therefore wait a few seconds after `hello`; a script may take up to five seconds, longer with network requests. + +**Decision: after every `hello` the display reports every screen and the theme again** (`screen`, `theme`). The service remembers the last report across restarts (in its cache directory) so that it draws at the right scale at once, but the display is the authority. + +### `widget` + +A new widget, or a change to one. Every member is always present except `frames`. + +| Member | Meaning | +| --- | --- | +| `id` | 16 lower-case hexadecimal digits, random, made by the service | +| `screen` | The display's own name of the screen, as the display reported it in `move`. **Decision:** empty text until the display has placed the widget; the display then uses its primary screen. A name the display does not know is treated the same way | +| `x`, `y` | Integers: offset of the widget's top-left corner from the screen's top-left corner in logical pixels | +| `width`, `height` | Integers: logical size. 170×170, 329×170, 329×345 or 639×345 | +| `image` | Absolute path of a PNG. Its pixel size is the logical size times the screen's scale | +| `version` | Integer from 1; one more for every new picture of this widget since the service started | +| `regions` | `[{n, x, y, width, height}]`: tappable areas, integers in logical pixels from the widget's top-left corner, in the order they were painted. **Decision:** where two overlap, the later one is on top. `n` is the number to send in `tap`. At most 256. Nothing else is told about a region | +| `frames` | Only for a widget that moves: `[{image, ms}]`, 2 to 60 entries. Show each `image` for `ms` milliseconds, then start again. `image` beside it is the first frame | +| `family` | **Addition.** `small`, `medium`, `large` or `extraLarge`: the current size, for the menu | +| `families` | **Addition.** The sizes the package supports, for the menu. The service refuses a resize to any other | + +A `widget` event is sent when the picture changed (new `version` and new `image`) and when only the place changed (after `move`: same `version`, same `image`). **Decision:** a script that runs again and draws the same picture produces no event. A `menu` resize is answered with a `widget` event, also when the widget already has that size. + +`version` starts again at 1 when the service restarts; the path never repeats (see below), so a display should key its image cache on the path, or on the connection and the version. + +### `mode` + +Sent when `arrange` or `peek` changed. Both are state of the service, not saved across restarts; a new service starts with both off. + +**Decision: turning arrange on turns peek on**, because on the bottom layer a drag ends where the pointer reaches a window. Turning arrange off puts peek back to what it was before, unless `peek` was set while arranging; then that choice stays. + +### `bye` + +The last line. Reasons: + +| `reason` | Written by | Meaning | Exit code of `watch` | +| --- | --- | --- | --- | +| `shutdown` | the service | It was asked to stop | 0 | +| `not-running` | `scriptwidget watch` | There was no service to connect to. This is the only line | 69 | +| `lost` | `scriptwidget watch` | The connection ended without a `bye`: the service was killed, or it dropped this watcher for being too far behind | 69 | + +**Decision:** `watch` always ends its output with a `bye` line, so a display has one thing to look for; when there is no service it also prints `error: [service.unavailable] …` on standard error with how to start it. After any `bye` the display keeps the last pictures (they are in its memory; the files are gone) and runs `watch` again after a few seconds. + +A watcher that has more than 1024 events waiting is dropped. + +## Pictures + +- Directory `$XDG_RUNTIME_DIR/scriptwidget/frames/`, mode 0700; files mode 0600. +- **Decision: names.** `--.png`, and `---