An open-source, self-hosted system for apps built with web technologies.
Kept wallpaper files are named by key with their format's extension; an unshowable held wallpaper becomes the System's own
zohayrslileh1f10e6b30ced
diff --git a/assets/wallpapers/desktop-dark.webp b/assets/wallpapers/DESKTOP_DARK_WALLPAPER.webpsimilarity index 100%rename from assets/wallpapers/desktop-dark.webprename to assets/wallpapers/DESKTOP_DARK_WALLPAPER.webpdiff --git a/assets/wallpapers/desktop-light.webp b/assets/wallpapers/DESKTOP_LIGHT_WALLPAPER.webpsimilarity index 100%rename from assets/wallpapers/desktop-light.webprename to assets/wallpapers/DESKTOP_LIGHT_WALLPAPER.webpdiff --git a/assets/wallpapers/sign-in-dark.webp b/assets/wallpapers/SIGN_IN_DARK_WALLPAPER.webpsimilarity index 100%rename from assets/wallpapers/sign-in-dark.webprename to assets/wallpapers/SIGN_IN_DARK_WALLPAPER.webpdiff --git a/assets/wallpapers/sign-in-light.webp b/assets/wallpapers/SIGN_IN_LIGHT_WALLPAPER.webpsimilarity index 100%rename from assets/wallpapers/sign-in-light.webprename to assets/wallpapers/SIGN_IN_LIGHT_WALLPAPER.webpdiff --git a/source/client/view/structure/desktop/layers/wallpaper/wallpaper.tsx b/source/client/view/structure/desktop/layers/wallpaper/wallpaper.tsxindex ad0bb97..6a1d36a 100644--- a/source/client/view/structure/desktop/layers/wallpaper/wallpaper.tsx+++ b/source/client/view/structure/desktop/layers/wallpaper/wallpaper.tsx@@ -5,8 +5,7 @@ import { useReady } from "@libs/readiness"import { wallpaperRequirement } from "../../../readiness-requirements"import { usePreferences, useTiming } from "@phreshos/react-ui"import { useReducedMotion } from "@libs/react-motion"-import { cssEasing, type AppearanceWallpapers, type ThemedValue } from "@phreshos/core"-import { wallpaperKind, type WallpaperKind } from "@shared/wallpaper"+import { cssEasing, wallpaperKind, type AppearanceWallpapers, type ThemedValue, type WallpaperKind } from "@phreshos/core"/** Where a wallpaper is shown. */export type WallpaperPlace = keyof AppearanceWallpapersdiff --git a/source/server/core/appearance-manager.ts b/source/server/core/appearance-manager.tsindex 18dc84b..dbb50c6 100644--- a/source/server/core/appearance-manager.ts+++ b/source/server/core/appearance-manager.ts@@ -1,8 +1,7 @@import Keyv from "keyv"import { isDeepStrictEqual } from "node:util"-import { applyAppearanceUpdate, defaultAppearance, isUploadFile, type Appearance } from "@phreshos/core"+import { applyAppearanceUpdate, defaultAppearance, isUploadFile, systemWallpapers, wallpaperKind, wallpaperSizeLimit, type Appearance, type AppearanceWallpapers } from "@phreshos/core"import UploadManager from "./upload-manager"-import { wallpaperKind, wallpaperSizeLimit } from "@shared/wallpaper"const storageKey = "appearance"@@ -16,10 +15,16 @@ export default class AppearanceManager {public static async open(store: Keyv, uploads: UploadManager) {const stored = await store.get(storageKey)- const appearance = stored === undefined+ const held = stored === undefined? defaultAppearance: applyAppearanceUpdate(defaultAppearance, stored)+ // A wallpaper that can no longer be shown, such as an upload gone from disk, becomes the+ // System's own for its place, so the Desktop never shows nothing and later changes apply.+ const shown = (theme: "light" | "dark") => Object.fromEntries((Object.keys(systemWallpapers[theme]) as (keyof AppearanceWallpapers)[])+ .map(place => [place, wallpaperProblem(uploads, held.wallpapers[theme][place]) === null ? held.wallpapers[theme][place] : systemWallpapers[theme][place]]))+ const appearance = applyAppearanceUpdate(held, { wallpapers: { light: shown("light"), dark: shown("dark") } })+// Persisted Appearance values may predate newly introduced fields. They// remain overrides of the current defaults, then become canonical here.if (stored === undefined || !isDeepStrictEqual(stored, appearance)) {@@ -48,12 +53,21 @@ export default class AppearanceManager {}private validateWallpaper(file: string) {- if (!isUploadFile(file)) throw new Error("A wallpaper must be a system upload")-- const upload = this.uploads.stat(file)+ const problem = wallpaperProblem(this.uploads, file)- if (!upload) throw new Error("The wallpaper upload does not exist")- if (!wallpaperKind(file)) throw new Error("A wallpaper must be an image, video, or HTML file")- if (upload.size > wallpaperSizeLimit) throw new Error("A wallpaper cannot exceed 50 MiB")+ if (problem) throw new Error(problem)}}++/** Why an upload cannot be a wallpaper, or `null` when it can. */+function wallpaperProblem(uploads: UploadManager, file: string) {+ if (!isUploadFile(file)) return "A wallpaper must be a system upload"++ const upload = uploads.stat(file)++ if (!upload) return "The wallpaper upload does not exist"+ if (!wallpaperKind(file)) return "A wallpaper must be an image, video, or HTML file"+ if (upload.size > wallpaperSizeLimit) return "A wallpaper cannot exceed 50 MiB"++ return null+}diff --git a/source/server/core/upload-manager.ts b/source/server/core/upload-manager.tsindex 335aac6..f141b4c 100644--- a/source/server/core/upload-manager.ts+++ b/source/server/core/upload-manager.ts@@ -1,4 +1,4 @@-import { createReadStream, createWriteStream, lstatSync, mkdirSync } from "node:fs"+import { createReadStream, createWriteStream, existsSync, lstatSync, mkdirSync, readdirSync } from "node:fs"import FileManager from "@libs/file-manager"import { rename, rm } from "node:fs/promises"import { randomUUID } from "node:crypto"@@ -35,24 +35,37 @@ export class UploadTooLargeError extends Error {* interruption and refusal remove the temporary file.** The keys the System keeps for its own wallpapers read the files it comes- * with instead, so each release shows its own pictures behind them.+ * with instead, so each release shows its own pictures behind them. Each+ * such file is named by its key, with the extension of its format.*/export default class UploadManager {public readonly fileManager: FileManager+ private readonly systemFiles: ReadonlyMap<string, string>+public constructor(fileManager: FileManager, public readonly systemPath: string) {this.fileManager = fileManagermkdirSync(fileManager.path, { recursive: true })++ const named = existsSync(systemPath) ? readdirSync(systemPath) : []++ this.systemFiles = new Map(named.map(name => [name.replace(/\.[^.]+$/, ""), name]))+ }++ /** The file the System comes with behind one of its own keys, by its name in `systemPath`. */+ public systemFile(file: string) {++ return this.systemFiles.get(file) ?? file}public path(file: string) {if (!isUploadFile(file)) throw new Error("That is not an upload file")- return isSystemUploadFile(file) ? join(this.systemPath, file) : this.fileManager.join(file)+ return isSystemUploadFile(file) ? join(this.systemPath, this.systemFile(file)) : this.fileManager.join(file)}public async write(extension: string, content: ReadableStream<Uint8Array> | null, signal?: AbortSignal) {diff --git a/source/server/view/http/uploads.ts b/source/server/view/http/uploads.tsindex baa90e2..ba1a61b 100644--- a/source/server/view/http/uploads.ts+++ b/source/server/view/http/uploads.ts@@ -3,9 +3,8 @@ import { MissingUploadValueError, UploadTooLargeError, uploadLimit } from "@servimport Application from "@server/core/application"import doors from "./doors"import { Hono } from "hono"-import { isSystemUploadFile, isUploadFile } from "@phreshos/core"+import { isSystemUploadFile, isUploadFile, wallpaperKind, wallpaperSizeLimit } from "@phreshos/core"import { readFile } from "node:fs/promises"-import { wallpaperKind, wallpaperSizeLimit } from "@shared/wallpaper"const immutableCache = "public, max-age=31536000, immutable"@@ -158,7 +157,7 @@ export default function (application: Application) {const uploaded = serveStatic({ root: application.uploads.fileManager.path, rewriteRequestPath: path => path.slice(doors.uploads.length + 1) })- const kept = serveStatic({ root: application.uploads.systemPath, rewriteRequestPath: path => path.slice(doors.uploads.length + 1) })+ const kept = serveStatic({ root: application.uploads.systemPath, rewriteRequestPath: path => application.uploads.systemFile(path.slice(doors.uploads.length + 1)) })uploads.use("/:file", (context, next) => (isSystemUploadFile(context.req.param("file")) ? kept : uploaded)(context, next))diff --git a/source/shared/wallpaper.ts b/source/shared/wallpaper.tsdeleted file mode 100644index 12d360d..0000000--- a/source/shared/wallpaper.ts+++ /dev/null@@ -1,27 +0,0 @@-/** Wallpaper sources accepted by the System-owned Appearance boundary. */-export type WallpaperKind = "image" | "video" | "html"--/** Applying a wallpaper is deliberately stricter than storing an upload. */-export const wallpaperSizeLimit = 50 * 1024 * 1024--const wallpaperKinds: Readonly<Record<string, WallpaperKind>> = {- avif: "image",- bmp: "image",- gif: "image",- jpeg: "image",- jpg: "image",- png: "image",- svg: "image",- webp: "image",- mp4: "video",- ogg: "video",- ogv: "video",- webm: "video",- html: "html"-}--export function wallpaperKind(file: string): WallpaperKind | null {- const extension = file.match(/\.([a-z0-9]+)$/i)?.[1]?.toLowerCase()-- return extension ? wallpaperKinds[extension] ?? null : null-}diff --git a/tests/appearance.test.ts b/tests/appearance.test.tsindex 5d093fa..bea7ddf 100644--- a/tests/appearance.test.ts+++ b/tests/appearance.test.ts@@ -1,6 +1,6 @@import assert from "node:assert/strict"import Keyv from "keyv"-import { defaultAppearance, parseAppearance, systemWallpapers } from "@phreshos/core"+import { defaultAppearance, parseAppearance, systemWallpapers, wallpaperSizeLimit } from "@phreshos/core"import AppearanceManager from "@server/core/appearance-manager"import FileManager from "@libs/file-manager"import UploadManager from "@server/core/upload-manager"@@ -9,7 +9,6 @@ import { tmpdir } from "node:os"import { join, resolve } from "node:path"import { test } from "vitest"import { randomUUID } from "node:crypto"-import { wallpaperSizeLimit } from "@shared/wallpaper"test("appearance contract", async () => {assert.deepEqual(parseAppearance(defaultAppearance), defaultAppearance)@@ -152,5 +151,21 @@ test("appearance contract", async () => {wallpapers: { ...manager.value.wallpapers, light: { ...manager.value.wallpapers.light, desktop: oversized } }}), /50 MiB/)+ // A held wallpaper that can no longer be shown becomes the System's own for its place.+ const kept = await uploads.write("png", new Blob(["wallpaper"]).stream())+ const brokenStore = new Keyv()+ await brokenStore.set("appearance", {+ ...defaultAppearance,+ wallpapers: { light: { signIn: "sign-in-light.webp", desktop: kept }, dark: { signIn: systemWallpapers.dark.signIn, desktop: `${randomUUID()}.png` } }+ })+ const repaired = await AppearanceManager.open(brokenStore, uploads)+ assert.deepEqual(repaired.value.wallpapers, {+ light: { signIn: systemWallpapers.light.signIn, desktop: kept },+ dark: systemWallpapers.dark+ })+ assert.deepEqual((await brokenStore.get("appearance")).wallpapers, repaired.value.wallpapers)+ await repaired.update({ wallpapers: { dark: { desktop: kept } } })+ assert.equal(repaired.value.wallpapers.dark.desktop, kept)+await rm(directory, { recursive: true, force: true })}, 120_000)diff --git a/tests/uploads.test.ts b/tests/uploads.test.tsindex 0e180e6..12ef3f4 100644--- a/tests/uploads.test.ts+++ b/tests/uploads.test.ts@@ -87,8 +87,9 @@ test("uploads contract", async () => {const kept = await view.request(`http://system/uploads/${systemWallpapers.light.desktop}`)assert.equal(kept.status, 200)assert.equal(kept.headers.get("cache-control"), "no-cache")- assert.deepEqual(new Uint8Array(await kept.arrayBuffer()), new Uint8Array(await readFile(resolve("assets/wallpapers", systemWallpapers.light.desktop))))- assert.equal(uploads.stat(systemWallpapers.dark.signIn)?.size, (await readFile(resolve("assets/wallpapers", systemWallpapers.dark.signIn))).byteLength)+ assert.equal(kept.headers.get("content-type"), "image/webp")+ assert.deepEqual(new Uint8Array(await kept.arrayBuffer()), new Uint8Array(await readFile(uploads.path(systemWallpapers.light.desktop))))+ assert.equal(uploads.stat(systemWallpapers.dark.signIn)?.size, (await readFile(resolve("assets/wallpapers/SIGN_IN_DARK_WALLPAPER.webp"))).byteLength)assert.equal((await view.request("http://system/uploads/not-a-key")).status, 400)assert.equal((await view.request("http://system/uploads/00000000-0000-0000-0000-000000000000.txt")).status, 404)} finally {diff --git a/tests/wallpaper-source.test.tsx b/tests/wallpaper-source.test.tsxindex 8bd839a..eb0fb3d 100644--- a/tests/wallpaper-source.test.tsx+++ b/tests/wallpaper-source.test.tsx@@ -26,8 +26,8 @@ test("wallpaper source contract", () => {}// The System's own wallpapers are uploads like any other, each shown in its own place.- assert.match(render(systemWallpapers.dark.signIn, "signIn"), /src="\/uploads\/sign-in-dark\.webp"/)- assert.match(render(systemWallpapers.dark.desktop), /src="\/uploads\/desktop-dark\.webp"/)+ assert.match(render(systemWallpapers.dark.signIn, "signIn"), /src="\/uploads\/SIGN_IN_DARK_WALLPAPER"/)+ assert.match(render(systemWallpapers.dark.desktop), /src="\/uploads\/DESKTOP_DARK_WALLPAPER"/)const image = render("00000000-0000-0000-0000-000000000000.png")assert.match(image, /<img/)