An open-source, self-hosted system for apps built with web technologies.
import assert from "node:assert/strict"
import { mkdtemp, readFile, rm } from "node:fs/promises"
import { resolve } from "node:path"
import FileManager from "@libs/file-manager"
import UploadManager from "@server/core/upload-manager"
import { systemWallpapers } from "@phreshos/core"
import uploadView from "@server/view/http/uploads"
import type Application from "@server/core/application"
import { Hono } from "hono"
import { test } from "vitest"
import { systemFiles } from "./system-files"
test("uploads contract", async () => {
const directory = await mkdtemp(resolve(".verify-uploads-"))
const uploads = new UploadManager(new FileManager(directory), systemFiles)
try {
const file = await uploads.write("txt", new Blob(["hello uploads"]).stream())
const upload = uploads.stat(file)
assert(upload)
assert.equal(upload.size, 13)
assert.equal(typeof upload.modifiedAt, "number")
assert.equal(await readFile(uploads.path(file), "utf8"), "hello uploads")
assert.equal(uploads.stat("00000000-0000-0000-0000-000000000000.txt"), null)
assert.throws(() => uploads.stat("../outside.txt"), /not an upload file/)
const view = new Hono()
const application = {
uploads,
linkManager: {
authManager: {
verify(value: unknown) {
if (value !== "allowed") throw new Error("Unauthorized")
},
async upload(authorization: unknown, content: ReadableStream<Uint8Array> | null, extension: string, signal?: AbortSignal) {
this.verify(authorization)
const written = await uploads.write(extension, content, signal)
const upload = uploads.stat(written)
assert(upload)
return { file: written, ...upload }
}
}
}
} as unknown as Application
view.route("/uploads", uploadView(application))
const response = await view.request("http://system/uploads", {
method: "POST",
headers: {
authorization: "allowed",
"content-disposition": "attachment; filename=value.json",
"content-type": "application/json"
},
body: JSON.stringify({ ready: true })
})
assert.equal(response.status, 200)
const created = await response.json()
assert.equal(created.file.endsWith(".json"), true)
assert.equal(created.size, 14)
assert.equal(typeof created.modifiedAt, "number")
const described = await view.request(`http://system/uploads/${created.file}/stat`)
const downloaded = await view.request(`http://system/uploads/${created.file}`)
assert.equal(described.status, 200)
assert.equal(downloaded.status, 200)
assert.equal(described.headers.get("cache-control"), "public, max-age=31536000, immutable")
assert.equal(downloaded.headers.get("cache-control"), "public, max-age=31536000, immutable")
assert.deepEqual(await described.json(), { size: created.size, modifiedAt: created.modifiedAt })
assert.deepEqual(JSON.parse(await downloaded.text()), { ready: true })
const htmlSource = "<!doctype html><script>document.body.textContent = 'offline wallpaper'</script>"
const htmlFile = await uploads.write("html", new Blob([htmlSource]).stream())
const wallpaper = await view.request(`http://system/uploads/wallpaper/${htmlFile}`)
assert.equal(wallpaper.status, 200)
assert.equal(wallpaper.headers.get("cache-control"), "public, max-age=31536000, immutable")
assert.equal(wallpaper.headers.get("content-type"), "text/html; charset=utf-8")
assert.match(wallpaper.headers.get("content-security-policy") ?? "", /connect-src 'none'/)
assert.match(wallpaper.headers.get("content-security-policy") ?? "", /script-src 'unsafe-inline' data: blob:/)
assert.match(wallpaper.headers.get("content-security-policy") ?? "", /sandbox allow-scripts/)
assert.equal(await wallpaper.text(), htmlSource)
assert.equal((await view.request(`http://system/uploads/wallpaper/${created.file}`)).status, 400)
// A key the System keeps reads the file it comes with, asked for again after a release.
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.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.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 {
await rm(directory, { recursive: true, force: true })
}
console.log("uploads verified")
}, 120_000)