diff --git a/deno.jsonc b/deno.jsonc index 0b36bec0..8ce23b17 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -112,6 +112,8 @@ "exports": { // .js "./common/atproto.js": "./src/common/atproto.js", + "./common/firkin/files.js": "./src/common/firkin/files.js", + "./common/firkin/session.js": "./src/common/firkin/session.js", "./common/collaboration/index.js": "./src/common/collaboration/index.js", "./common/foundation.js": "./src/common/foundation.js", "./common/facets/utils.js": "./src/common/facets/utils.js", @@ -146,8 +148,8 @@ "./components/engine/repeat-shuffle/element.js": "./src/components/engine/repeat-shuffle/element.js", "./components/engine/scope/element.js": "./src/components/engine/scope/element.js", "./components/input/common.js": "./src/components/input/common.js", - "./components/input/wnfs-firkin/element.js": "./src/components/input/wnfs-firkin/element.js", - "./components/input/wnfs-firkin/worker.js": "./src/components/input/wnfs-firkin/worker.js", + "./components/input/firkin/element.js": "./src/components/input/firkin/element.js", + "./components/input/firkin/worker.js": "./src/components/input/firkin/worker.js", "./components/input/dropbox/common.js": "./src/components/input/dropbox/common.js", "./components/input/dropbox/constants.js": "./src/components/input/dropbox/constants.js", "./components/input/dropbox/element.js": "./src/components/input/dropbox/element.js", @@ -211,7 +213,6 @@ "./components/output/bytes/wnfs/blockstore.js": "./src/components/output/bytes/wnfs/blockstore.js", "./components/output/bytes/wnfs/constants.js": "./src/components/output/bytes/wnfs/constants.js", "./components/output/bytes/wnfs/element.js": "./src/components/output/bytes/wnfs/element.js", - "./components/output/bytes/wnfs/files.js": "./src/components/output/bytes/wnfs/files.js", "./components/output/bytes/wnfs/local.js": "./src/components/output/bytes/wnfs/local.js", "./components/output/bytes/wnfs/passkey.js": "./src/components/output/bytes/wnfs/passkey.js", "./components/output/bytes/wnfs/root-tree.js": "./src/components/output/bytes/wnfs/root-tree.js", @@ -239,8 +240,8 @@ "./components/transformer/output/refiner/passkey-encryption/element.js": "./src/components/transformer/output/refiner/passkey-encryption/element.js", "./components/transformer/output/refiner/path-collections/element.js": "./src/components/transformer/output/refiner/path-collections/element.js", "./components/transformer/output/string/json/element.js": "./src/components/transformer/output/string/json/element.js", - "./components/upload/wnfs-firkin/element.js": "./src/components/upload/wnfs-firkin/element.js", - "./components/upload/wnfs-firkin/worker.js": "./src/components/upload/wnfs-firkin/worker.js", + "./components/upload/firkin/element.js": "./src/components/upload/firkin/element.js", + "./components/upload/firkin/worker.js": "./src/components/upload/firkin/worker.js", "./components/upload/dropbox/common.js": "./src/components/upload/dropbox/common.js", "./components/upload/dropbox/element.js": "./src/components/upload/dropbox/element.js", "./components/upload/dropbox/worker.js": "./src/components/upload/dropbox/worker.js", diff --git a/src/_data/elements.js b/src/_data/elements.js index c4742ba2..8f134984 100644 --- a/src/_data/elements.js +++ b/src/_data/elements.js @@ -131,10 +131,10 @@ export default { "Ephemeral blobs stored in indexedDB, resolving creates temporary Blob URLs. Not responsible for storing blobs.", }, { - url: "components/input/wnfs-firkin/element.js", - title: "Firkin + WNFS", + url: "components/input/firkin/element.js", + title: "Firkin", desc: - "Firkin-hosted audio files, referenced from an encrypted WNFS pointer map.", + "Firkin-hosted audio files, listed from the account's file registry.", }, { url: "components/input/https/element.js", @@ -398,10 +398,10 @@ export default { "Uploads and deletes files on Dropbox using the Dropbox v2 HTTP API.", }, { - url: "components/upload/wnfs-firkin/element.js", - title: "Firkin + WNFS", + url: "components/upload/firkin/element.js", + title: "Firkin", desc: - "Uploads files to Firkin and records them in an encrypted WNFS pointer map.", + "Uploads files to Firkin and records them in the account's file registry.", }, { url: "components/upload/s3/element.js", diff --git a/src/_data/facets.json b/src/_data/facets.json index 5981b3d5..7f5b74f4 100644 --- a/src/_data/facets.json +++ b/src/_data/facets.json @@ -216,7 +216,7 @@ "kind": "prelude", "category": "Data", "incomplete": true, - "desc": "Store your userdata encrypted on a [Firkin](https://tangled.org/tokono.ma/firkin) instance. Uses passkey authentication and no email. [WNFS](https://github.com/wnfs-wg/) and PRF passkeys are used for the encryption. Adds input, output and uploader capabilities." + "desc": "Store your userdata encrypted on a [Firkin](https://tangled.org/tokono.ma/firkin) instance. Uses passkey authentication and no email. [WNFS](https://github.com/wnfs-wg/) and PRF passkeys are used for the encrypted userdata; the audio input and uploader use Firkin blobs and the account's file registry directly." }, { "url": "facets/data/export-import/index.tile", diff --git a/src/common/firkin/files.js b/src/common/firkin/files.js new file mode 100644 index 00000000..97935b51 --- /dev/null +++ b/src/common/firkin/files.js @@ -0,0 +1,256 @@ +import { base64url } from "iso-base/rfc4648"; + +/** + * @import { Firkin } from "@tokono.ma/firkin" + */ + +/** + * Files stored on Firkin, listed from the account's namespaced registry. + * + * Each file is a raw Firkin blob; the registry name is a base64url-encoded + * file name (Firkin registry names reject spaces and several other filename + * characters) pointing at its CID. There is no pointer manifest and no WNFS + * involvement. + */ + +/** URI scheme for files stored on Firkin. */ +export const SCHEME = "firkin"; + +/** The single URI representing the current Firkin account. */ +export const SOURCE_URI = `${SCHEME}://me`; + +/** Path segment used in file URIs (user-facing). */ +export const URI_DIR = "files"; + +/** Registry namespace holding `encoded-file-name → blob CID`. */ +export const FILES_NAMESPACE = "diffuse.files"; + +/** Firkin's maximum registry-name length. */ +const MAX_REGISTRY_NAME_LENGTH = 512; + +/** + * How long a CID-scoped Firkin read URL stays valid. + * + * The queue-audio orchestrator re-resolves and reloads a URL shortly before + * it lapses, so this is a ceiling rather than the expected link lifetime — + * a leaked URL stays a usable bearer capability for at most this long. + */ +export const FILE_URL_TTL_SECONDS = 6 * 60 * 60; + +/** + * @typedef {Object} StorageFile + * @property {string} name + * @property {string} cid - Raw Firkin blob CID. + */ + +/** + * Encode a file name as a Firkin registry name. + * + * Registry names are restricted to `A–Z a–z 0–9 . _ - ~ @ + :`, so a + * base64url encoding keeps arbitrary (Unicode, spaces, punctuation) filenames + * addressable. + * + * @param {string} name + */ +export function encodeFileName(name) { + const encoded = base64url.encode(new TextEncoder().encode(name)); + + if (encoded.length > MAX_REGISTRY_NAME_LENGTH) { + throw new Error(`File name is too long for Firkin: ${name}`); + } + + return encoded; +} + +/** + * @param {string} encoded + */ +export function decodeFileName(encoded) { + return new TextDecoder().decode(base64url.decode(encoded)); +} + +/** + * URI of a file inside the Firkin account. + * + * @param {string} name + */ +export function buildURI(name) { + return `${SOURCE_URI}/${URI_DIR}/${encodeURIComponent(name)}`; +} + +/** + * @param {string} uri + * @returns {{ name: string } | null} + */ +export function parseURI(uri) { + const prefix = `${SOURCE_URI}/`; + if (!uri.startsWith(prefix)) return null; + + const segments = uri.slice(prefix.length).split("/"); + if (segments[0] !== URI_DIR || segments.length !== 2) return null; + + try { + const name = decodeURIComponent(segments[1]); + return name.length > 0 ? { name } : null; + } catch { + return null; + } +} + +/** @type {Record} */ +const MIME_TYPES = { + aac: "audio/aac", + aiff: "audio/aiff", + alac: "audio/mp4", + flac: "audio/flac", + m4a: "audio/mp4", + m4b: "audio/mp4", + mp3: "audio/mpeg", + mp4: "audio/mp4", + oga: "audio/ogg", + ogg: "audio/ogg", + opus: "audio/ogg", + wav: "audio/wav", + webm: "audio/webm", + wma: "audio/x-ms-wma", +}; + +/** + * @param {string} name + */ +export function mimeType(name) { + const ext = name.slice(name.lastIndexOf(".") + 1).toLowerCase(); + return MIME_TYPES[ext] ?? "application/octet-stream"; +} + +/** + * Strip path/control characters from a browser-provided file name. + * + * @param {string} name + */ +export function safeFileName(name) { + const base = name.split("/").pop() ?? ""; + const cleaned = base.replace(/[\u0000-\u001f]/g, "").trim(); + return cleaned.length > 0 ? cleaned : "audio"; +} + +/** + * Every file in the account's file registry. + * + * @param {Firkin} rb + * @returns {Promise} + */ +export async function listFiles(rb) { + const names = await rb.registryNames(FILES_NAMESPACE); + if (!names) return []; + + /** @type {StorageFile[]} */ + const files = []; + + for (const [encoded, cid] of Object.entries(names)) { + try { + files.push({ name: decodeFileName(encoded), cid }); + } catch { + // Ignore entries that were not written by encodeFileName. + } + } + + return files; +} + +/** + * Resolve one file pointer, or `undefined` when it is not registered. + * + * @param {Firkin} rb + * @param {string} name + * @returns {Promise} + */ +export async function getFile(rb, name) { + const cid = await rb.registryGet(FILES_NAMESPACE, encodeFileName(name)); + return cid ? { name, cid } : undefined; +} + +/** + * Read a file's bytes. + * + * @param {Firkin} rb + * @param {string} name + * @returns {Promise} + */ +export async function readFile(rb, name) { + const file = await getFile(rb, name); + if (!file) throw new Error(`File not found on Firkin: ${name}`); + + return await rb.get(file.cid); +} + +/** + * A CID-scoped, range-capable Firkin URL for a file — what media elements and + * the metadata workers use. + * + * @param {Firkin} rb + * @param {string} name + * @param {number} [expireIn] + * @returns {Promise} + */ +export async function fileURL(rb, name, expireIn) { + const file = await getFile(rb, name); + if (!file) throw new Error(`File not found on Firkin: ${name}`); + + return await rb.readUrl(file.cid, { + expireIn: expireIn ?? FILE_URL_TTL_SECONDS, + }); +} + +/** + * Upload the bytes as a raw Firkin blob and point the file name at it. + * + * @param {Firkin} rb + * @param {string} name + * @param {Uint8Array} bytes + * @returns {Promise<{ cid: string }>} + */ +export async function uploadFile(rb, name, bytes) { + const registryName = encodeFileName(name); + const previous = await rb.registryGet(FILES_NAMESPACE, registryName); + + // Audio bytes stay in Firkin; the registry only holds the pointer. + const { cid } = await rb.upload(bytes, { codec: 0x55 }); + await rb.registrySet(FILES_NAMESPACE, registryName, cid); + + if (previous && previous !== cid) { + await deleteIfUnreferenced(rb, previous); + } + + return { cid }; +} + +/** + * Remove a file's registry pointer and detach the blob's account claim when + * no other file name references it. + * + * @param {Firkin} rb + * @param {string} name + */ +export async function deleteFile(rb, name) { + const registryName = encodeFileName(name); + const cid = await rb.registryGet(FILES_NAMESPACE, registryName); + if (!cid) return; + + await rb.registryDelete(FILES_NAMESPACE, registryName); + await deleteIfUnreferenced(rb, cid); +} + +/** + * @param {Firkin} rb + * @param {string} cid + */ +async function deleteIfUnreferenced(rb, cid) { + const names = await rb.registryNames(FILES_NAMESPACE); + const referenced = names !== null && + Object.values(names).some((value) => value === cid); + + if (!referenced) { + await rb.delete(cid).catch(() => {}); + } +} diff --git a/src/common/firkin/session.js b/src/common/firkin/session.js new file mode 100644 index 00000000..ea91970d --- /dev/null +++ b/src/common/firkin/session.js @@ -0,0 +1,135 @@ +import * as IDB from "idb-keyval"; +import { + Firkin, + WebCryptoSigner, + delegationTokenSource, + inlineInvocation, +} from "@tokono.ma/firkin"; + +/** + * @import { BlockCache, TokenSource } from "@tokono.ma/firkin" + */ + +/** IndexedDB key for the delegated Firkin account session. */ +export const IDB_SESSION = "diffuse/firkin/session"; + +/** + * The Firkin account session shared by the storage output and the Firkin + * input/uploader components. + * + * The session is a delegated ed25519 key; the account (passkey) is only + * needed to mint a fresh delegation. + * + * @typedef {Object} Session + * @property {string} apiBase + * @property {string} did + * @property {CryptoKey} privateKey + * @property {Uint8Array} publicKey + * @property {Uint8Array} proof + * @property {string} proofCid + * @property {number} expiresAt + * @property {boolean} [inlineProofs] - The server rejected delegation publishing + * (or it failed), so tokens carry their proof inline instead of by CID. + */ + +/** + * Read the persisted session, ignoring expired or malformed records. + * + * @returns {Promise} + */ +export async function readSession() { + /** @type {Session | undefined} */ + const session = await IDB.get(IDB_SESSION); + if (!session) return undefined; + if (session.expiresAt <= Date.now()) return undefined; + if (!(session.privateKey instanceof CryptoKey)) return undefined; + + return session; +} + +/** + * @param {Session} session + */ +export async function saveSession(session) { + await IDB.set(IDB_SESSION, session); +} + +export async function deleteSession() { + await IDB.del(IDB_SESSION); +} + +/** + * Build a Firkin client for a delegated session. + * + * Reference the delegation by CID when it has been published (the SDK's + * default; smaller tokens). Servers without a delegation store get the + * bootstrap shape with the proof inline. + * + * @param {Session} session + * @param {{ cache?: BlockCache }} [options] + * @returns {Firkin} + */ +export function clientForSession(session, options = {}) { + const sessionSigner = WebCryptoSigner.fromPersisted( + session.privateKey, + session.publicKey, + ); + + /** @type {TokenSource} */ + const tokenSource = session.inlineProofs + ? async (command, tokenOptions) => + await inlineInvocation(sessionSigner, { + command, + proofs: [{ proof: session.proof, cid: session.proofCid }], + expireIn: tokenOptions?.expireIn ?? 600, + }) + : delegationTokenSource( + sessionSigner, + { cid: session.proofCid }, + { subject: session.did }, + ); + + return new Firkin( + session.apiBase, + { tokenSource }, + { cache: options.cache }, + ); +} + +/** @type {{ key: string; session: Session; rb: Firkin } | undefined} */ +let cached; + +/** + * The shared session + client for components that do not own the sign-in. + * + * Cached per worker and invalidated when the persisted session changes, so a + * sign-in or delegation refresh in the output is picked up on the next call. + * + * @param {{ cache?: BlockCache }} [options] + * @returns {Promise<{ session: Session; rb: Firkin } | undefined>} + */ +export async function getSessionClient(options = {}) { + const session = await readSession(); + if (!session) { + cached = undefined; + return undefined; + } + + const key = [ + session.apiBase, + session.did, + session.proofCid, + session.expiresAt, + session.inlineProofs ? "inline" : "published", + ].join("\0"); + + if (cached?.key !== key) { + cached = { + key, + session, + rb: clientForSession(session, options), + }; + } + + return { session: cached.session, rb: cached.rb }; +} diff --git a/src/components/index.js b/src/components/index.js index c663aa91..d74bd5d3 100644 --- a/src/components/index.js +++ b/src/components/index.js @@ -14,7 +14,7 @@ import * as _EngineAudio from "./engine/audio/element.js" import * as _EngineQueue from "./engine/queue/element.js" import * as _EngineRepeatShuffle from "./engine/repeat-shuffle/element.js" import * as _EngineScope from "./engine/scope/element.js" -import * as _InputWnfsFirkin from "./input/wnfs-firkin/element.js" +import * as _InputFirkin from "./input/firkin/element.js" import * as _InputDropbox from "./input/dropbox/element.js" import * as _InputEphemeralCache from "./input/ephemeral-cache/element.js" import * as _InputHttps from "./input/https/element.js" @@ -53,7 +53,7 @@ import * as _TransformerOutputRefinerInitialContents from "./transformer/output/ import * as _TransformerOutputRefinerPasskeyEncryption from "./transformer/output/refiner/passkey-encryption/element.js" import * as _TransformerOutputRefinerPathCollections from "./transformer/output/refiner/path-collections/element.js" import * as _TransformerOutputStringJson from "./transformer/output/string/json/element.js" -import * as _UploadWnfsFirkin from "./upload/wnfs-firkin/element.js" +import * as _UploadFirkin from "./upload/firkin/element.js" import * as _UploadDropbox from "./upload/dropbox/element.js" import * as _UploadS3 from "./upload/s3/element.js" import * as _UploadWebdav from "./upload/webdav/element.js" @@ -87,7 +87,7 @@ export const engine = { } export const input = { - wnfsFirkin: _InputWnfsFirkin, + firkin: _InputFirkin, dropbox: _InputDropbox, ephemeralCache: _InputEphemeralCache, https: _InputHttps, @@ -160,7 +160,7 @@ export const transformer = { } export const upload = { - wnfsFirkin: _UploadWnfsFirkin, + firkin: _UploadFirkin, dropbox: _UploadDropbox, s3: _UploadS3, webdav: _UploadWebdav, diff --git a/src/components/input/wnfs-firkin/element.js b/src/components/input/firkin/element.js similarity index 56% rename from src/components/input/wnfs-firkin/element.js rename to src/components/input/firkin/element.js index beeacb4b..b21a9d10 100644 --- a/src/components/input/wnfs-firkin/element.js +++ b/src/components/input/firkin/element.js @@ -1,5 +1,5 @@ import { defineElement, DiffuseElement } from "~/common/element.js"; -import { SCHEME, SOURCE_URI } from "~/components/output/bytes/wnfs/files.js"; +import { SCHEME, SOURCE_URI } from "~/common/firkin/files.js"; /** * @import {InputActions, InputSchemeProvider} from "@specs/components/input/types.d.ts" @@ -12,17 +12,17 @@ import { SCHEME, SOURCE_URI } from "~/components/output/bytes/wnfs/files.js"; //////////////////////////////////////////// /** - * Audio source backed by Firkin blobs listed in the WNFS pointer map. + * Audio source backed by Firkin blobs listed in the account's file registry. * - * The actual WNFS work happens in the storage output element; this element's - * worker reaches it over a `BroadcastChannel`. + * The worker reads the shared Firkin session from IndexedDB, so this element + * does not depend on the WNFS output or a broadcast channel. * * @implements {ProxiedActions} * @implements {InputSchemeProvider} */ -class WnfsFirkinInput extends DiffuseElement { - static NAME = "diffuse/input/wnfs-firkin"; - static WORKER_URL = "components/input/wnfs-firkin/worker.js"; +class FirkinInput extends DiffuseElement { + static NAME = "diffuse/input/firkin"; + static WORKER_URL = "components/input/firkin/worker.js"; SCHEME = SCHEME; @@ -40,29 +40,21 @@ class WnfsFirkinInput extends DiffuseElement { this.resolve = proxy.resolve; } - /** - * Pass the output element's group/namespace to the worker so it can find the - * matching broadcast channel. - */ - workerQuery() { - return { group: this.group, namespace: this.namespace ?? "" }; - } - // 🛠️ /** @param {Track[]} _tracks */ sources(_tracks) { - return [{ label: "Firkin + WNFS", uri: SOURCE_URI }]; + return [{ label: "Firkin", uri: SOURCE_URI }]; } } -export default WnfsFirkinInput; +export default FirkinInput; //////////////////////////////////////////// // REGISTER //////////////////////////////////////////// -export const CLASS = WnfsFirkinInput; -export const NAME = "di-wnfs-firkin"; +export const CLASS = FirkinInput; +export const NAME = "di-firkin"; defineElement(NAME, CLASS); diff --git a/src/components/input/wnfs-firkin/worker.js b/src/components/input/firkin/worker.js similarity index 69% rename from src/components/input/wnfs-firkin/worker.js rename to src/components/input/firkin/worker.js index 9acf9b12..1205306d 100644 --- a/src/components/input/wnfs-firkin/worker.js +++ b/src/components/input/firkin/worker.js @@ -5,12 +5,15 @@ import { isAudioFile } from "~/components/input/common.js"; import { buildURI, FILE_URL_TTL_SECONDS, + fileURL, + listFiles, mimeType, parseURI, + readFile, SCHEME, SOURCE_URI, - StorageClient, -} from "~/components/output/bytes/wnfs/files.js"; +} from "~/common/firkin/files.js"; +import { getSessionClient } from "~/common/firkin/session.js"; /** * @import { Track } from "~/definitions/types.d.ts" @@ -21,30 +24,34 @@ import { // SETUP //////////////////////////////////////////// -const params = new URL(globalThis.location.href).searchParams; - -const client = new StorageClient({ - group: params.get("group") || undefined, - namespace: params.get("namespace") || undefined, -}); +const NOT_CONNECTED = + "Firkin is not connected. Sign in on the Connect → Firkin + WNFS page first."; /** @type {Map} */ const blobUrls = new Map(); /** Cache a *definitive* status for a moment; a timeout is retried. */ const STATUS_TTL = 5_000; -/** @type {Awaited> | undefined} */ +/** @type {{ connected: boolean; did?: string; apiBase?: string } | undefined} */ let lastStatus; let lastStatusAt = 0; -/** @returns {Promise> | undefined>} */ +/** @returns {Promise<{ connected: boolean; did?: string; apiBase?: string } | undefined>} */ async function storageStatus() { if (lastStatus && Date.now() - lastStatusAt < STATUS_TTL) return lastStatus; try { - lastStatus = await client.status(); + const client = await getSessionClient(); + lastStatus = client + ? { + connected: true, + did: client.session.did, + apiBase: client.session.apiBase, + } + : { connected: false }; } catch { - // No output element answered. Don't cache: a leader may come back. + // No session yet, or IndexedDB is unavailable. Don't cache: sign-in may + // complete while this worker is alive. lastStatus = undefined; } @@ -105,7 +112,7 @@ export async function groupConsult(uris) { const grouping = available === "no" ? { available, - reason: "Firkin + WNFS is not connected", + reason: "Firkin is not connected", scheme: SCHEME, uris, } @@ -118,14 +125,24 @@ export async function groupConsult(uris) { * @type {Actions['list']} */ export async function list(cachedTracks = []) { - const status = await storageStatus(); - if (!status?.connected) return cachedTracks; + let client; + try { + client = await getSessionClient(); + } catch { + // IndexedDB/session access failed. Keep the cached tracks rather than + // dropping the user's library. + return cachedTracks; + } + + if (!client) return cachedTracks; let files; try { - files = (await client.listFiles()).filter((file) => isAudioFile(file.name)); + files = (await listFiles(client.rb)).filter((file) => + isAudioFile(file.name) + ); } catch { - // The storage became unreachable (tab closed, session lost). Keep the + // The session became unusable (expired, revoked, offline). Keep the // cached tracks rather than dropping the user's library. return cachedTracks; } @@ -152,7 +169,7 @@ export async function list(cachedTracks = []) { if (tracks.length > 0) return tracks; - // Keep the source discoverable while the directory is empty. + // Keep the source discoverable while the registry is empty. const placeholder = cachedTracks.find((track) => track.kind === "placeholder" ); @@ -180,25 +197,30 @@ export async function resolve({ uri }) { const parsed = parseURI(uri); if (!parsed) return undefined; - // Stream directly from Firkin with a presigned, CID-scoped URL: the media - // element and metadata workers get HTTP range support without WNFS or blob - // buffering in between. + const client = await getSessionClient(); + if (!client) throw new Error(NOT_CONNECTED); + + // Stream directly from Firkin with a CID-scoped URL: the media element and + // metadata workers get HTTP range support without buffering in between. try { - const url = await client.fileURL(parsed.name, FILE_URL_TTL_SECONDS); - if (url) { - return { - url, - expiresAt: Math.round(Date.now() / 1000) + FILE_URL_TTL_SECONDS, - }; - } + const url = await fileURL( + client.rb, + parsed.name, + FILE_URL_TTL_SECONDS, + ); + + return { + url, + expiresAt: Math.round(Date.now() / 1000) + FILE_URL_TTL_SECONDS, + }; } catch { - // Presigned URLs unavailable — fall back to a blob. + // Read URLs unavailable — fall back to a blob. } let url = blobUrls.get(uri); if (!url) { - const bytes = await client.read(parsed.name); + const bytes = await readFile(client.rb, parsed.name); const blob = new Blob( [/** @type {BlobPart} */ (/** @type {unknown} */ (bytes))], { type: mimeType(parsed.name) }, diff --git a/src/components/output/bytes/wnfs/constants.js b/src/components/output/bytes/wnfs/constants.js index ae6e9260..2c92554c 100644 --- a/src/components/output/bytes/wnfs/constants.js +++ b/src/components/output/bytes/wnfs/constants.js @@ -1,13 +1,12 @@ /** * IndexedDB prefixes for this output. * - * The session (delegated ed25519 key + proof) is stored separately from the - * WNFS storage pointer (root CID + imported RSA-OAEP private key) so a session - * refresh never has to re-derive the private key. + * The account session (delegated ed25519 key + proof) is shared with the + * Firkin input/uploader components and lives in `~/common/firkin/session.js`. + * The WNFS storage pointer (root CID + imported RSA-OAEP private key) is kept + * here so a session refresh never has to re-derive the private key. */ export const IDB_PREFIX = "diffuse/output/bytes/wnfs"; -/** IndexedDB key for the delegated session key + proof. */ -export const IDB_SESSION = `${IDB_PREFIX}/session`; /** IndexedDB key for the WNFS root pointer + imported exchange private key. */ export const IDB_STORAGE = `${IDB_PREFIX}/storage`; /** Root CID that the local collection cache corresponds to. */ diff --git a/src/components/output/bytes/wnfs/element.js b/src/components/output/bytes/wnfs/element.js index 27e291ce..ce86236c 100644 --- a/src/components/output/bytes/wnfs/element.js +++ b/src/components/output/bytes/wnfs/element.js @@ -19,10 +19,8 @@ import { import { ApiError, CMD, - Firkin, WebCryptoSigner, delegate, - delegationTokenSource, inlineInvocation, } from "@tokono.ma/firkin"; @@ -38,18 +36,17 @@ import { generateExchangeKeyPair, importExchangePrivateKey, } from "./passkey.js"; -import { - FILES_MANIFEST, - FILE_URL_TTL_SECONDS, - decodeManifest, - encodeManifest, -} from "./files.js"; import { readLocalCollection } from "./local.js"; +import { + clientForSession, + deleteSession, + readSession, + saveSession, +} from "~/common/firkin/session.js"; import { DEFAULT_API_BASE, IDB_LOCAL_PREFIX, IDB_LOCAL_ROOT, - IDB_SESSION, IDB_STORAGE, PASSKEY_NAMESPACE, REGISTRY_NAMESPACE, @@ -66,17 +63,8 @@ import { * @import { OutputElement, OutputManager } from "@specs/components/output/types.d.ts" * @import { WnfsFirkinOutputElement } from "@specs/components/output/bytes/wnfs/types.d.ts" * @import { CollectionName } from "~/common/self-describing.js" - * - * @typedef {Object} Session - * @property {string} apiBase - * @property {string} did - * @property {CryptoKey} privateKey - * @property {Uint8Array} publicKey - * @property {Uint8Array} proof - * @property {string} proofCid - * @property {number} expiresAt - * @property {boolean} [inlineProofs] - The server rejected delegation publishing - * (or it failed), so tokens carry their proof inline instead of by CID. + * @import { Firkin } from "@tokono.ma/firkin" + * @import { Session } from "~/common/firkin/session.js" * * @typedef {Object} Storage * @property {string} apiBase @@ -256,32 +244,7 @@ class WnfsFirkinOutput extends BroadcastedOutputElement { /** @override */ connectedCallback() { - this.replicateSavedData(this.#manager, { - storageStatus: { - strategy: "leaderOnly", - fn: () => this.#filesStatus(), - }, - storageListFiles: { - strategy: "leaderOnly", - fn: () => this.#listFiles(), - }, - storageRead: { - strategy: "leaderOnly", - fn: (name) => this.#readFile(name), - }, - storageFileURL: { - strategy: "leaderOnly", - fn: (name, expireIn) => this.#fileURL(name, expireIn), - }, - storageUploadFile: { - strategy: "leaderOnly", - fn: (name, bytes, mime) => this.#uploadFile(name, bytes, mime), - }, - storageDeleteFile: { - strategy: "leaderOnly", - fn: (name) => this.#deleteFile(name), - }, - }); + this.replicateSavedData(this.#manager); super.connectedCallback(); @@ -464,7 +427,7 @@ class WnfsFirkinOutput extends BroadcastedOutputElement { async signOut() { await this.#writeQueue.catch(() => {}); await this.#flushUploads(); - await Promise.all([IDB.del(IDB_SESSION), IDB.del(IDB_STORAGE)]); + await Promise.all([deleteSession(), IDB.del(IDB_STORAGE)]); this.#session.value = undefined; this.#storage.value = undefined; @@ -506,11 +469,8 @@ class WnfsFirkinOutput extends BroadcastedOutputElement { // RESTORE async #restore() { - /** @type {Session | undefined} */ - const session = await IDB.get(IDB_SESSION); + const session = await readSession(); if (!session) return; - if (session.expiresAt <= Date.now()) return; - if (!(session.privateKey instanceof CryptoKey)) return; await this.#publishSessionDelegation(session); @@ -786,8 +746,7 @@ class WnfsFirkinOutput extends BroadcastedOutputElement { * @returns {Promise} */ async #createSession(apiBase, did, accountSigner) { - /** @type {Session | undefined} */ - const stored = await IDB.get(IDB_SESSION); + const stored = await readSession(); const sessionSigner = stored && @@ -813,51 +772,28 @@ class WnfsFirkinOutput extends BroadcastedOutputElement { proof, proofCid: cid, expiresAt: Date.now() + SESSION_TTL_SECONDS * 1000, + // Start with inline proofs: a worker may read this session before the + // publish below completes. + inlineProofs: true, }; - await IDB.set(IDB_SESSION, session); + await saveSession(session); return session; } /** @param {Session} session */ #clientFor(session) { - const sessionSigner = WebCryptoSigner.fromPersisted( - session.privateKey, - session.publicKey, - ); - - const blockCache = { - /** @param {string} cid */ - get: (cid) => this.#blockstore?.peek(cid), - /** @param {string} cid; @param {Uint8Array} bytes */ - set: (cid, bytes) => { - this.#blockstore?.seed(cid, bytes); + return clientForSession(session, { + cache: { + /** @param {string} cid */ + get: (cid) => this.#blockstore?.peek(cid), + /** @param {string} cid; @param {Uint8Array} bytes */ + set: (cid, bytes) => { + this.#blockstore?.seed(cid, bytes); + }, + keys: () => this.#blockstore?.keys() ?? [], }, - keys: () => this.#blockstore?.keys() ?? [], - }; - - // Reference the delegation by CID when it has been published (the SDK's - // default; smaller tokens). Servers without a delegation store get the - // bootstrap shape with the proof inline. - /** @type {import("@tokono.ma/firkin").TokenSource} */ - const tokenSource = session.inlineProofs - ? async (command, options) => - await inlineInvocation(sessionSigner, { - command, - proofs: [{ proof: session.proof, cid: session.proofCid }], - expireIn: options?.expireIn ?? 600, - }) - : delegationTokenSource( - sessionSigner, - { cid: session.proofCid }, - { subject: session.did }, - ); - - return new Firkin( - session.apiBase, - { tokenSource }, - { cache: /** @type {any} */ (blockCache) }, - ); + }); } /** @@ -872,8 +808,6 @@ class WnfsFirkinOutput extends BroadcastedOutputElement { * @param {Session} session */ async #publishSessionDelegation(session) { - if (session.inlineProofs) return; - const sessionSigner = WebCryptoSigner.fromPersisted( session.privateKey, session.publicKey, @@ -907,7 +841,7 @@ class WnfsFirkinOutput extends BroadcastedOutputElement { session.inlineProofs = true; } - await IDB.set(IDB_SESSION, session); + await saveSession(session); } // COLLECTIONS @@ -986,207 +920,6 @@ class WnfsFirkinOutput extends BroadcastedOutputElement { return pending; } - // FILES - - /** @returns {import("./files.js").StorageStatus} */ - #filesStatus() { - return { - // Report the session (restored from IndexedDB immediately) rather than - // `#ready`, so a consult during the initial registry restore stays - // optimistic. Mounting/writing still wait for `#ready`. - connected: Boolean(this.#session.value), - did: this.did(), - root: this.dataRoot(), - }; - } - - /** @returns {Promise} */ - async #filesFs() { - await this.#waitForRestore(); - - if (!this.#ready.value) { - throw new Error("Firkin + WNFS is not connected"); - } - - await this.#ensureMounted(); - - const fs = this.#fs; - if (!fs) throw new Error("Firkin + WNFS could not be mounted"); - return fs; - } - - /** @returns {Promise} */ - async #listFiles() { - const fs = await this.#filesFs(); - const manifest = await this.#readManifest(fs); - - return Object.entries(manifest.files).map(([name, file]) => ({ - name, - ...file, - })); - } - - /** - * Read the manifest from the WNFS. A missing or corrupt manifest is treated - * as empty (there is no migration of pre-pointer file layouts). - * - * @param {FileSystem} fs - * @returns {Promise} - */ - async #readManifest(fs) { - const path = Path.priv(FILES_MANIFEST); - if (!(await fs.exists(path))) return { version: 1, files: {} }; - - return decodeManifest(await fs.read(path, "bytes")); - } - - /** - * @param {FileSystem} fs - * @param {import("./files.js").FilesManifest} manifest - * @returns {Promise} The new data root. - */ - async #writeManifest(fs, manifest) { - const result = await fs.write( - Path.priv(FILES_MANIFEST), - "bytes", - encodeManifest(manifest), - ); - - return result.dataRoot.toString(); - } - - /** @returns {Promise<{ fs: FileSystem; rb: Firkin }>} */ - async #filesContext() { - const fs = await this.#filesFs(); - const rb = this.#rb; - - if (!rb) throw new Error("Firkin + WNFS is not connected"); - return { fs, rb }; - } - - /** - * Fallback byte read for playback/metadata, resolving the pointer's CID - * directly against Firkin. - * - * @param {string} name - * @returns {Promise} - */ - async #readFile(name) { - const { fs, rb } = await this.#filesContext(); - const manifest = await this.#readManifest(fs); - const entry = manifest.files[name]; - - if (!entry) { - throw new Error(`File not found in Firkin + WNFS: ${name}`); - } - - return await rb.get(entry.cid); - } - - /** - * A presigned, range-capable Firkin URL for a file — what media elements and - * the metadata workers use. - * - * @param {string} name - * @param {number} [expireIn] - * @returns {Promise} - */ - async #fileURL(name, expireIn) { - const { fs, rb } = await this.#filesContext(); - const manifest = await this.#readManifest(fs); - const entry = manifest.files[name]; - - if (!entry) { - throw new Error(`File not found in Firkin + WNFS: ${name}`); - } - - return await rb.readUrl(entry.cid, { - expireIn: expireIn ?? FILE_URL_TTL_SECONDS, - }); - } - - /** - * Upload the bytes as a raw Firkin blob, then record the pointer in the WNFS. - * - * @param {string} name - * @param {Uint8Array} bytes - * @param {string} mime - * @returns {Promise<{ cid: string }>} - */ - #uploadFile(name, bytes, mime) { - const write = async () => { - const { fs, rb } = await this.#filesContext(); - const manifest = await this.#readManifest(fs); - const previous = manifest.files[name]; - - // Audio bytes stay in Firkin; only this pointer goes into the WNFS. - const { cid } = await rb.upload(bytes, { codec: 0x55 }); - - manifest.files[name] = { - cid, - size: bytes.byteLength, - mime, - modified: Date.now(), - }; - - const root = await this.#writeManifest(fs, manifest); - - // Drop the replaced blob when nothing else points at it. - if ( - previous && previous.cid !== cid && - !Object.values(manifest.files).some((file) => - file.cid === previous.cid - ) - ) { - await rb.delete(previous.cid).catch(() => {}); - } - - return { cid, root }; - }; - - const written = this.#writeQueue.then(write, write); - this.#writeQueue = written.then(() => {}, () => {}); - - return written.then(async ({ cid, root }) => { - await this.#enqueueUpload(root); - return { cid }; - }); - } - - /** - * Remove a pointer from the WNFS and detach the blob's account claim when no - * other pointer references it. - * - * @param {string} name - */ - #deleteFile(name) { - const remove = async () => { - const { fs, rb } = await this.#filesContext(); - const manifest = await this.#readManifest(fs); - const entry = manifest.files[name]; - - if (!entry) return null; - - delete manifest.files[name]; - const root = await this.#writeManifest(fs, manifest); - - if ( - !Object.values(manifest.files).some((file) => file.cid === entry.cid) - ) { - await rb.delete(entry.cid).catch(() => {}); - } - - return root; - }; - - const removed = this.#writeQueue.then(remove, remove); - this.#writeQueue = removed.then(() => {}, () => {}); - - return removed.then(async (root) => { - if (root) await this.#enqueueUpload(root); - }); - } - // GET & PUT /** diff --git a/src/components/output/bytes/wnfs/files.js b/src/components/output/bytes/wnfs/files.js deleted file mode 100644 index bd7d2766..00000000 --- a/src/components/output/bytes/wnfs/files.js +++ /dev/null @@ -1,293 +0,0 @@ -import { RpcChannel } from "~/common/worker/rpc-channel.js"; - -/** - * Browser-side access to the audio files referenced by the Firkin + WNFS. - * - * Audio bytes live in Firkin as raw blobs (content-addressed). The WNFS only - * stores a small pointer map: ` → { cid, size, mime, modified }`. Input - * and upload workers talk to the output element over a `BroadcastChannel`; the - * output owns the WNFS mount and the Firkin session. - */ - -/** URI scheme for files in the Firkin + WNFS WNFS. */ -export const SCHEME = "wnfs+firkin"; - -/** The single URI representing the current Firkin + WNFS account. */ -export const SOURCE_URI = `${SCHEME}://me`; - -/** Private WNFS file holding the `name → Firkin CID` pointer map. */ -export const FILES_MANIFEST = "diffuse-files.json"; - -/** Path segment used in file URIs (user-facing). */ -export const URI_DIR = "files"; - -/** Identifier prefix of the output element that owns the WNFS. */ -export const OUTPUT_NAME = "diffuse/output/bytes/wnfs"; - -/** - * How long a presigned Firkin read URL stays valid. - * - * The queue-audio orchestrator re-resolves and reloads a URL shortly before - * it lapses, so this is a ceiling rather than the expected link lifetime — - * a leaked URL stays a usable bearer capability for at most this long. - */ -export const FILE_URL_TTL_SECONDS = 6 * 60 * 60; - -/** - * @typedef {Object} StorageFile - * @property {string} name - * @property {string} cid - Raw Firkin blob CID. - * @property {number} size - Bytes. - * @property {string} mime - * @property {number} modified - Unix epoch milliseconds. - */ - -/** - * @typedef {Object} FilesManifest - * @property {number} version - * @property {Record>} files - */ - -/** - * @param {Uint8Array} bytes - * @returns {FilesManifest} - */ -export function decodeManifest(bytes) { - try { - const parsed = JSON.parse(new TextDecoder().decode(bytes)); - if ( - parsed && typeof parsed === "object" && parsed.files && - typeof parsed.files === "object" - ) { - return { version: 1, files: parsed.files }; - } - } catch (err) { - console.error("Failed to decode the Firkin + WNFS file manifest:", err); - } - - return { version: 1, files: {} }; -} - -/** - * @param {FilesManifest} manifest - * @returns {Uint8Array} - */ -export function encodeManifest(manifest) { - return new TextEncoder().encode(JSON.stringify(manifest)); -} - -/** - * @typedef {Object} StorageStatus - * @property {boolean} connected - * @property {string} [did] - * @property {string} [root] - */ - -/** - * `BroadcastChannel` name of the output element for the given group and - * namespace. Mirrors `DiffuseElement#identifier` for the output element. - * - * @param {{ group?: string; namespace?: string }} options - */ -export function outputChannelName({ group, namespace }) { - return `${OUTPUT_NAME}/${group || "default"}${ - namespace ? `/${namespace}` : "" - }`; -} - -/** - * URI of a file inside the Firkin + WNFS. - * - * @param {string} name - */ -export function buildURI(name) { - return `${SOURCE_URI}/${URI_DIR}/${encodeURIComponent(name)}`; -} - -/** - * @param {string} uri - * @returns {{ name: string } | null} - */ -export function parseURI(uri) { - const prefix = `${SOURCE_URI}/`; - if (!uri.startsWith(prefix)) return null; - - const segments = uri.slice(prefix.length).split("/"); - if (segments[0] !== URI_DIR || segments.length !== 2) return null; - - try { - const name = decodeURIComponent(segments[1]); - return name.length > 0 ? { name } : null; - } catch { - return null; - } -} - -/** @type {Record} */ -const MIME_TYPES = { - aac: "audio/aac", - aiff: "audio/aiff", - alac: "audio/mp4", - flac: "audio/flac", - m4a: "audio/mp4", - m4b: "audio/mp4", - mp3: "audio/mpeg", - mp4: "audio/mp4", - oga: "audio/ogg", - ogg: "audio/ogg", - opus: "audio/ogg", - wav: "audio/wav", - webm: "audio/webm", - wma: "audio/x-ms-wma", -}; - -/** - * @param {string} name - */ -export function mimeType(name) { - const ext = name.slice(name.lastIndexOf(".") + 1).toLowerCase(); - return MIME_TYPES[ext] ?? "application/octet-stream"; -} - -/** Per-action timeouts. Writes wait for the debounced `/dag/push`. */ -const TIMEOUT = { - status: 3_000, - list: 30_000, - read: 60_000, - fileURL: 10_000, - upload: 120_000, - delete: 120_000, -}; - -const NOT_REACHABLE = - "Firkin + WNFS is not reachable. Sign in on the Connect → Firkin + WNFS page first."; - -/** - * RPC client for the output element's WNFS file actions. - * - * One instance per worker; the broadcast channel is opened lazily on the first - * call and reused afterwards. - */ -export class StorageClient { - /** @type {string} */ - #channelName; - - /** @type {BroadcastChannel | undefined} */ - #channel; - - /** @type {RpcChannel<{}, StorageActions> | undefined} */ - #rpc; - - /** - * @param {{ group?: string; namespace?: string }} options - */ - constructor(options) { - this.#channelName = outputChannelName(options); - } - - /** @returns {Promise} */ - status() { - return this.#call("storageStatus", [], TIMEOUT.status); - } - - /** @returns {Promise} */ - listFiles() { - return this.#call("storageListFiles", [], TIMEOUT.list); - } - - /** - * @param {string} name - * @returns {Promise} - */ - read(name) { - return this.#call("storageRead", [name], TIMEOUT.read); - } - - /** - * A short-lived, presigned Firkin URL for this file. - * - * @param {string} name - * @param {number} [expireIn] - * @returns {Promise} - */ - fileURL(name, expireIn) { - return this.#call("storageFileURL", [name, expireIn], TIMEOUT.fileURL); - } - - /** - * Upload a file as a raw Firkin blob and record its pointer in the WNFS. - * - * @param {string} name - * @param {Uint8Array} bytes - * @param {string} mime - * @returns {Promise<{ cid: string } | null>} - */ - uploadFile(name, bytes, mime) { - return this.#call( - "storageUploadFile", - [name, bytes, mime], - TIMEOUT.upload, - ); - } - - /** - * @param {string} name - * @returns {Promise} - */ - deleteFile(name) { - return this.#call("storageDeleteFile", [name], TIMEOUT.delete); - } - - close() { - this.#channel?.close(); - this.#channel = undefined; - this.#rpc = undefined; - } - - /** - * @param {keyof StorageActions & string} method - * @param {any[]} args - * @param {number} timeout - * @returns {Promise} - */ - async #call(method, args, timeout) { - if (!this.#rpc) { - this.#channel = new BroadcastChannel(this.#channelName); - this.#rpc = new RpcChannel(this.#channel); - } - - const request = this.#rpc.callMethod(method, args); - - /** @type {ReturnType | undefined} */ - let timer; - let timedOut = false; - const deadline = new Promise((_, reject) => { - timer = setTimeout(() => { - timedOut = true; - reject(new Error(NOT_REACHABLE)); - }, timeout); - }); - - try { - return await Promise.race([request, deadline]); - } catch (err) { - // Drop the channel after a timeout: the pending request will never - // settle, and a later call should get a fresh channel (and a fresh - // chance to reach a newly-opened leader tab). - if (timedOut) this.close(); - throw err; - } finally { - clearTimeout(timer); - } - } -} - -/** - * @typedef {Object} StorageActions - * @property {() => StorageStatus | Promise} storageStatus - * @property {() => StorageFile[] | Promise} storageListFiles - * @property {(name: string) => Uint8Array | Promise} storageRead - * @property {(name: string, expireIn?: number) => string | null | Promise} storageFileURL - * @property {(name: string, bytes: Uint8Array, mime: string) => { cid: string } | null | Promise<{ cid: string } | null>} storageUploadFile - * @property {(name: string) => void | Promise} storageDeleteFile - */ diff --git a/src/components/upload/wnfs-firkin/element.js b/src/components/upload/firkin/element.js similarity index 57% rename from src/components/upload/wnfs-firkin/element.js rename to src/components/upload/firkin/element.js index 3790e53e..cf084b4e 100644 --- a/src/components/upload/wnfs-firkin/element.js +++ b/src/components/upload/firkin/element.js @@ -1,5 +1,5 @@ import { defineElement, DiffuseElement } from "~/common/element.js"; -import { SCHEME } from "~/components/output/bytes/wnfs/files.js"; +import { SCHEME } from "~/common/firkin/files.js"; /** * @import {UploadActions, UploadSchemeProvider} from "@specs/components/upload/types.d.ts" @@ -11,18 +11,22 @@ import { SCHEME } from "~/components/output/bytes/wnfs/files.js"; //////////////////////////////////////////// /** - * Uploader that stores audio in Firkin and records pointers in the WNFS. + * Uploader that stores audio on Firkin and records file names in the account's + * file registry. + * + * The worker reads the shared Firkin session from IndexedDB, so this element + * does not depend on the WNFS output or a broadcast channel. * * @implements {ProxiedActions} * @implements {UploadSchemeProvider} */ -class WnfsFirkinUpload extends DiffuseElement { - static NAME = "diffuse/upload/wnfs-firkin"; - static WORKER_URL = "components/upload/wnfs-firkin/worker.js"; +class FirkinUpload extends DiffuseElement { + static NAME = "diffuse/upload/firkin"; + static WORKER_URL = "components/upload/firkin/worker.js"; SCHEME = SCHEME; - /** Where the Firkin + WNFS output is connected. */ + /** Where the Firkin account is connected. */ CONNECT_PATH = "facets/connect/wnfs-firkin/index.tile"; constructor() { @@ -36,23 +40,15 @@ class WnfsFirkinUpload extends DiffuseElement { this.delete = proxy.delete; this.createSource = proxy.createSource; } - - /** - * Pass the output element's group/namespace to the worker so it can find the - * matching broadcast channel. - */ - workerQuery() { - return { group: this.group, namespace: this.namespace ?? "" }; - } } -export default WnfsFirkinUpload; +export default FirkinUpload; //////////////////////////////////////////// // REGISTER //////////////////////////////////////////// -export const CLASS = WnfsFirkinUpload; -export const NAME = "du-wnfs-firkin"; +export const CLASS = FirkinUpload; +export const NAME = "du-firkin"; defineElement(NAME, CLASS); diff --git a/src/components/upload/wnfs-firkin/worker.js b/src/components/upload/firkin/worker.js similarity index 52% rename from src/components/upload/wnfs-firkin/worker.js rename to src/components/upload/firkin/worker.js index 30bffd59..fbaeacc5 100644 --- a/src/components/upload/wnfs-firkin/worker.js +++ b/src/components/upload/firkin/worker.js @@ -1,25 +1,19 @@ import { ostiary, rpc } from "~/common/worker.js"; import { buildURI, - mimeType, + deleteFile, parseURI, - StorageClient, -} from "~/components/output/bytes/wnfs/files.js"; + safeFileName, + uploadFile, +} from "~/common/firkin/files.js"; +import { getSessionClient } from "~/common/firkin/session.js"; /** * @import { UploadActions as Actions } from "@specs/components/upload/types.d.ts" */ -//////////////////////////////////////////// -// SETUP -//////////////////////////////////////////// - -const params = new URL(globalThis.location.href).searchParams; - -const client = new StorageClient({ - group: params.get("group") || undefined, - namespace: params.get("namespace") || undefined, -}); +const NOT_CONNECTED = + "Firkin is not connected. Sign in on the Connect → Firkin + WNFS page first."; //////////////////////////////////////////// // ACTIONS @@ -34,8 +28,8 @@ export async function consult(uriOrScheme) { } try { - const status = await client.status(); - return { supported: true, consult: status.connected ? "yes" : "no" }; + const client = await getSessionClient(); + return { supported: true, consult: client ? "yes" : "no" }; } catch { return { supported: true, consult: "unsure" }; } @@ -47,10 +41,12 @@ export async function consult(uriOrScheme) { export async function upload({ file, uri }) { const target = parseURI(uri); const name = target?.name ?? safeFileName(file.name); - const mime = file.type || mimeType(name); + + const client = await getSessionClient(); + if (!client) throw new Error(NOT_CONNECTED); const bytes = new Uint8Array(await file.arrayBuffer()); - await client.uploadFile(name, bytes, mime); + await uploadFile(client.rb, name, bytes); return buildURI(name); } @@ -62,7 +58,10 @@ export async function deleteFn(uri) { const parsed = parseURI(uri); if (!parsed) return; - await client.deleteFile(parsed.name); + const client = await getSessionClient(); + if (!client) throw new Error(NOT_CONNECTED); + + await deleteFile(client.rb, parsed.name); } /** @@ -70,26 +69,13 @@ export async function deleteFn(uri) { */ // deno-lint-ignore require-await export async function createSource(_args) { - // The Firkin + WNFS source is the signed-in output itself; there is no - // OAuth flow that could mint one from here. + // The Firkin source is the signed-in account itself; there is no OAuth flow + // that could mint one from here. throw new Error( - "Firkin + WNFS does not use OAuth. Sign in on the Connect → Firkin + WNFS page first, then select it for syncing.", + "Firkin does not use OAuth. Sign in on the Connect → Firkin + WNFS page first, then select it for syncing.", ); } -//////////////////////////////////////////// -// 🛠️ -//////////////////////////////////////////// - -/** - * @param {string} name - */ -function safeFileName(name) { - const base = name.split("/").pop() ?? ""; - const cleaned = base.replace(/[\u0000-\u001f]/g, "").trim(); - return cleaned.length > 0 ? cleaned : "audio"; -} - //////////////////////////////////////////// // ⚡️ //////////////////////////////////////////// diff --git a/src/facets/data/file-manager/facet.js b/src/facets/data/file-manager/facet.js index 4fe1f784..624abfee 100644 --- a/src/facets/data/file-manager/facet.js +++ b/src/facets/data/file-manager/facet.js @@ -8,9 +8,9 @@ import { SCHEME as SCHEME_EPHEMERAL_CACHE, } from "~/components/input/ephemeral-cache/constants.js"; import { - SCHEME as SCHEME_WNFS_FIRKIN, - SOURCE_URI as WNFS_FIRKIN_SOURCE_URI, -} from "~/components/output/bytes/wnfs/files.js"; + SCHEME as SCHEME_FIRKIN, + SOURCE_URI as FIRKIN_SOURCE_URI, +} from "~/common/firkin/files.js"; import { isAudioFile } from "~/components/input/common.js"; import { SCHEME as SCHEME_DROPBOX } from "~/components/input/dropbox/constants.js"; import { @@ -39,7 +39,7 @@ import foundation from "~/common/foundation.js"; /** @type {Record} */ const SCHEME_LABELS = { [SCHEME_DROPBOX]: "Dropbox", - [SCHEME_WNFS_FIRKIN]: "Firkin + WNFS", + [SCHEME_FIRKIN]: "Firkin", [SCHEME_S3]: "S3", [SCHEME_WEBDAV]: "WebDAV", }; @@ -514,15 +514,15 @@ const CONNECT_CONFIGS = { }, }, - [SCHEME_WNFS_FIRKIN]: { - scheme: SCHEME_WNFS_FIRKIN, + [SCHEME_FIRKIN]: { + scheme: SCHEME_FIRKIN, fields: [], - build: () => WNFS_FIRKIN_SOURCE_URI, + build: () => FIRKIN_SOURCE_URI, caption: - "Uploaded files are stored as Firkin blobs; only their pointers are kept in your encrypted WNFS.", + "Uploaded files are stored as Firkin blobs and listed from your Firkin account registry.", unavailable: html` Not connected to a Firkin instance. Sign in on the @@ -533,8 +533,8 @@ const CONNECT_CONFIGS = { `, sourceParts: () => ({ - name: "Firkin + WNFS", - detail: "Firkin blobs + encrypted pointers", + name: "Firkin", + detail: "Firkin blobs + registry pointers", }), }, }; @@ -874,10 +874,15 @@ await (async () => { namesVersion.value++; })(); -// Load persisted sync scheme. +// Load persisted sync scheme. Drop schemes this build no longer provides +// (e.g. the old `wnfs+firkin` scheme). { const scheme = await IDB.get(SYNC_SCHEME_KEY); - if (scheme) syncScheme.value = scheme; + if (scheme && scheme in SCHEME_LABELS) { + syncScheme.value = scheme; + } else if (scheme) { + await IDB.del(SYNC_SCHEME_KEY); + } } effect(() => { diff --git a/src/facets/data/wnfs-firkin/prelude/facet.js b/src/facets/data/wnfs-firkin/prelude/facet.js index f7dcffe7..418770db 100644 --- a/src/facets/data/wnfs-firkin/prelude/facet.js +++ b/src/facets/data/wnfs-firkin/prelude/facet.js @@ -1,8 +1,8 @@ import foundation from "~/common/foundation.js"; import { effect } from "~/common/signal.js"; -import { NAME as INPUT_NAME } from "~/components/input/wnfs-firkin/element.js"; -import { NAME as UPLOAD_NAME } from "~/components/upload/wnfs-firkin/element.js"; +import { NAME as INPUT_NAME } from "~/components/input/firkin/element.js"; +import { NAME as UPLOAD_NAME } from "~/components/upload/firkin/element.js"; /** * @import InputConfigurator from "~/components/configurator/input/element.js" @@ -16,21 +16,27 @@ import { NAME as UPLOAD_NAME } from "~/components/upload/wnfs-firkin/element.js" const WNFS_OUTPUT_NAME = "dob-wnfs-firkin"; const OUTPUT_ID = "do-output__dc-output__wnfs-firkin"; -const INPUT_ID = "di-input__dc-input__wnfs-firkin"; -const UPLOAD_ID = "du-upload__dc-upload__wnfs-firkin"; +const INPUT_ID = "di-input__dc-input__firkin"; +const UPLOAD_ID = "du-upload__dc-upload__firkin"; const CONNECT_URL = "facets/connect/wnfs-firkin/index.tile"; -const NAMESPACE = "wnfs-firkin"; + +/** Namespace of the WNFS userdata output. */ +const OUTPUT_NAMESPACE = "wnfs-firkin"; +/** Namespace of the Firkin-native input/uploader components. */ +const FIRKIN_NAMESPACE = "firkin"; /** - * Adds the Firkin + WNFS components: the output (storage itself), plus an - * audio input and an upload destination that operate on the files in the same - * WNFS. + * Adds the Firkin components: the WNFS userdata output (which also owns the + * passkey sign-in), plus a Firkin-native audio input and uploader. + * + * The input and uploader read the shared Firkin session from IndexedDB and + * operate on the account's file registry directly; they do not touch the WNFS + * or the output's broadcast channel. * * This is a feature (prelude) facet, so it only runs when the user enables - * it. The input and upload elements are cheap; their workers talk to the - * output element over a `BroadcastChannel`, so the WNFS/WASM code stays in the - * output bundle. + * it. The input and upload elements are cheap; their workers only need the + * Firkin SDK, so the WNFS/WASM code stays in the output bundle. */ effect(() => { const output = foundation.signals.orchestrator.output(); @@ -38,8 +44,8 @@ effect(() => { const upload = foundation.signals.configurator.upload(); if (output) wnfsFirkin(output); - if (input) wnfsFirkinInput(input); - if (upload) wnfsFirkinUpload(upload); + if (input) firkinInput(input); + if (upload) firkinUpload(upload); if (output) output.loadSelected(); }); @@ -61,7 +67,7 @@ function wnfsFirkin(output) { storage.setAttribute("group", foundation.GROUP); storage.setAttribute("id", OUTPUT_ID); storage.setAttribute("label", "Firkin + WNFS"); - storage.setAttribute("namespace", NAMESPACE); + storage.setAttribute("namespace", OUTPUT_NAMESPACE); storage.setAttribute("connect", CONNECT_URL); output.outputConfigurator.append(storage); @@ -75,14 +81,14 @@ function wnfsFirkin(output) { /** * @param {InputConfigurator} input */ -function wnfsFirkinInput(input) { +function firkinInput(input) { if (input.querySelector(`#${INPUT_ID}`)) return; const element = document.createElement(INPUT_NAME); element.setAttribute("group", foundation.GROUP); element.setAttribute("id", INPUT_ID); - element.setAttribute("label", "Firkin + WNFS"); - element.setAttribute("namespace", NAMESPACE); + element.setAttribute("label", "Firkin"); + element.setAttribute("namespace", FIRKIN_NAMESPACE); input.append(element); } @@ -90,14 +96,14 @@ function wnfsFirkinInput(input) { /** * @param {UploadConfigurator} upload */ -function wnfsFirkinUpload(upload) { +function firkinUpload(upload) { if (upload.querySelector(`#${UPLOAD_ID}`)) return; const element = document.createElement(UPLOAD_NAME); element.setAttribute("group", foundation.GROUP); element.setAttribute("id", UPLOAD_ID); - element.setAttribute("label", "Firkin + WNFS"); - element.setAttribute("namespace", NAMESPACE); + element.setAttribute("label", "Firkin"); + element.setAttribute("namespace", FIRKIN_NAMESPACE); upload.append(element); } diff --git a/tests/common/firkin/files.test.ts b/tests/common/firkin/files.test.ts new file mode 100644 index 00000000..2bb9960e --- /dev/null +++ b/tests/common/firkin/files.test.ts @@ -0,0 +1,191 @@ +// @ts-nocheck -- the fake client intentionally implements only the subset it uses + +import { describe, it } from "@std/testing/bdd"; +import { expect } from "@std/expect"; + +import { + buildURI, + decodeFileName, + deleteFile, + encodeFileName, + FILES_NAMESPACE, + fileURL, + getFile, + listFiles, + mimeType, + parseURI, + readFile, + safeFileName, + SCHEME, + SOURCE_URI, + uploadFile, +} from "~/common/firkin/files.js"; + +/** + * @param {Record} [initial] + */ +function fakeClient(initial = {}) { + const names = new Map(Object.entries(initial)); + const blobs = new Map(); + const deleted = []; + + return { + names, + blobs, + deleted, + + /** @param {string} namespace */ + async registryNames(namespace) { + if (namespace !== FILES_NAMESPACE || names.size === 0) return null; + return Object.fromEntries(names); + }, + + /** @param {string} namespace @param {string} name */ + async registryGet(namespace, name) { + if (namespace !== FILES_NAMESPACE) return null; + return names.get(name) ?? null; + }, + + /** @param {string} namespace @param {string} name @param {string} cid */ + async registrySet(namespace, name, cid) { + if (namespace !== FILES_NAMESPACE) throw new Error("wrong namespace"); + names.set(name, cid); + }, + + /** @param {string} namespace @param {string} name */ + async registryDelete(namespace, name) { + if (namespace !== FILES_NAMESPACE) throw new Error("wrong namespace"); + names.delete(name); + }, + + /** @param {Uint8Array} bytes */ + async upload(bytes) { + const cid = `bafk-${[...bytes].join("")}`; + blobs.set(cid, bytes); + return { cid, deduplicated: false }; + }, + + /** @param {string} cid */ + async delete(cid) { + blobs.delete(cid); + deleted.push(cid); + }, + + /** @param {string} cid */ + async get(cid) { + return blobs.get(cid); + }, + + /** @param {string} cid @param {{ expireIn?: number }} [_opts] */ + async readUrl(cid, _opts) { + return `https://firkin.test/v1/blob/${cid}?token=x`; + }, + }; +} + +describe("common/firkin/files", () => { + it("builds and parses file URIs", () => { + expect(SCHEME).toBe("firkin"); + expect(SOURCE_URI).toBe("firkin://me"); + + const uri = buildURI("Artist - Title.mp3"); + expect(uri).toBe("firkin://me/files/Artist%20-%20Title.mp3"); + expect(parseURI(uri)).toEqual({ name: "Artist - Title.mp3" }); + + expect(parseURI("firkin://me/other/x.mp3")).toBeNull(); + expect(parseURI("firkin://me/files/a/b.mp3")).toBeNull(); + expect(parseURI("firkin://me/files/%E0%A4%A")).toBeNull(); + }); + + it("encodes arbitrary file names as registry names", () => { + for ( + const name of [ + "Artist - Title.mp3", + "Björk — Jóga.flac", + "a/b (live) [remix].ogg", + "quote's & hash#.wav", + ] + ) { + const encoded = encodeFileName(name); + expect(encoded).toMatch(/^[A-Za-z0-9._~@+:-]+$/); + expect(decodeFileName(encoded)).toBe(name); + } + }); + + it("guesses audio mime types from the file name", () => { + expect(mimeType("a.mp3")).toBe("audio/mpeg"); + expect(mimeType("a.flac")).toBe("audio/flac"); + expect(mimeType("a.unknown")).toBe("application/octet-stream"); + }); + + it("sanitizes browser-provided file names", () => { + expect(safeFileName("Artist - Title.mp3")).toBe("Artist - Title.mp3"); + expect(safeFileName("../../etc/passwd")).toBe("passwd"); + expect(safeFileName("\u0000")).toBe("audio"); + }); + + it("lists registered files by their decoded names", async () => { + const rb = fakeClient({ + [encodeFileName("a.mp3")]: "bafk-a", + [encodeFileName("b.flac")]: "bafk-b", + }); + + const files = await listFiles(/** @type {any} */ (rb)); + + expect(files).toEqual([ + { name: "a.mp3", cid: "bafk-a" }, + { name: "b.flac", cid: "bafk-b" }, + ]); + }); + + it("resolves, reads and links one file", async () => { + const rb = fakeClient({ [encodeFileName("a.mp3")]: "bafk-a" }); + rb.blobs.set("bafk-a", new Uint8Array([1, 2, 3])); + + const file = await getFile(/** @type {any} */ (rb), "a.mp3"); + const bytes = await readFile(/** @type {any} */ (rb), "a.mp3"); + const url = await fileURL(/** @type {any} */ (rb), "a.mp3", 60); + + expect(file).toEqual({ name: "a.mp3", cid: "bafk-a" }); + expect([...bytes]).toEqual([1, 2, 3]); + expect(url).toBe("https://firkin.test/v1/blob/bafk-a?token=x"); + }); + + it("uploads a file and replaces the pointer", async () => { + const rb = fakeClient({ [encodeFileName("a.mp3")]: "bafk-old" }); + rb.blobs.set("bafk-old", new Uint8Array([9])); + + const result = await uploadFile( + /** @type {any} */ (rb), + "a.mp3", + new Uint8Array([4, 5]), + ); + + expect(result.cid).toBe("bafk-45"); + expect(rb.names.get(encodeFileName("a.mp3"))).toBe("bafk-45"); + expect(rb.deleted).toEqual(["bafk-old"]); + }); + + it("deletes a file and detaches its blob when unreferenced", async () => { + const rb = fakeClient({ [encodeFileName("a.mp3")]: "bafk-a" }); + rb.blobs.set("bafk-a", new Uint8Array([1])); + + await deleteFile(/** @type {any} */ (rb), "a.mp3"); + + expect(rb.names.has(encodeFileName("a.mp3"))).toBe(false); + expect(rb.deleted).toEqual(["bafk-a"]); + }); + + it("keeps a shared blob when another file name references it", async () => { + const rb = fakeClient({ + [encodeFileName("a.mp3")]: "bafk-shared", + [encodeFileName("copy.mp3")]: "bafk-shared", + }); + rb.blobs.set("bafk-shared", new Uint8Array([1])); + + await deleteFile(/** @type {any} */ (rb), "a.mp3"); + + expect(rb.deleted).toEqual([]); + expect(rb.names.get(encodeFileName("copy.mp3"))).toBe("bafk-shared"); + }); +}); diff --git a/tests/common/firkin/session.test.ts b/tests/common/firkin/session.test.ts new file mode 100644 index 00000000..6d043f8c --- /dev/null +++ b/tests/common/firkin/session.test.ts @@ -0,0 +1,59 @@ +// @ts-nocheck -- the evaluated browser function intentionally uses untyped JS + +import { describe, it } from "@std/testing/bdd"; +import { expect } from "@std/expect"; + +import { testWeb } from "@tests/common/index.ts"; + +describe("common/firkin/session", () => { + it("reads the shared session and builds a client", async () => { + const result = await testWeb(async () => { + const sdk = await import("~/vendor/firkin/index.js"); + const { + deleteSession, + getSessionClient, + saveSession, + } = await import("~/common/firkin/session.js"); + + const account = await sdk.Signer.generate(); + // Only persistence and client construction are under test; no signing + // happens, so a widely-supported P-256 handle is enough here. + const keyPair = await crypto.subtle.generateKey( + { name: "ECDSA", namedCurve: "P-256" }, + false, + ["sign"], + ); + + await saveSession({ + apiBase: "http://example.invalid", + did: account.did(), + privateKey: keyPair.privateKey, + publicKey: account.pubkeyBytes, + proof: new Uint8Array([1]), + proofCid: "bafkreitest", + expiresAt: Date.now() + 60_000, + }); + + const client = await getSessionClient(); + const connected = Boolean(client); + const did = client?.session.did; + + await deleteSession(); + const afterDelete = await getSessionClient(); + + return { + connected, + did, + expectedDid: account.did(), + afterDelete: Boolean(afterDelete), + }; + }); + + expect(result).toEqual({ + connected: true, + did: result.expectedDid, + expectedDid: result.expectedDid, + afterDelete: false, + }); + }); +}); diff --git a/tests/components/input/firkin/test.ts b/tests/components/input/firkin/test.ts new file mode 100644 index 00000000..2b301bad --- /dev/null +++ b/tests/components/input/firkin/test.ts @@ -0,0 +1,87 @@ +// @ts-nocheck -- the evaluated browser function intentionally uses untyped JS + +import { describe, it } from "@std/testing/bdd"; +import { expect } from "@std/expect"; + +import { testWeb } from "@tests/common/index.ts"; + +describe("components/input/firkin", () => { + it("exposes the Firkin source", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/input/firkin/element.js"); + const { SCHEME, SOURCE_URI } = await import( + "~/common/firkin/files.js" + ); + + const input = new mod.CLASS(); + document.body.append(input); + + return { + name: mod.NAME, + scheme: input.SCHEME, + expectedScheme: SCHEME, + sourceUri: SOURCE_URI, + sources: input.sources([]), + }; + }); + + expect(result).toEqual({ + name: "di-firkin", + scheme: "firkin", + expectedScheme: "firkin", + sourceUri: "firkin://me", + sources: [{ label: "Firkin", uri: "firkin://me" }], + }); + }); + + it("reports not connected and keeps cached tracks without a session", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/input/firkin/element.js"); + const { SOURCE_URI } = await import("~/common/firkin/files.js"); + + const input = new mod.CLASS(); + document.body.append(input); + + const consult = await input.consult(SOURCE_URI); + const groupConsult = await input.groupConsult([SOURCE_URI]); + const artwork = await input.artwork(SOURCE_URI); + + const tracks = await input.list([ + { + $type: "sh.diffuse.output.track", + id: "keep-me", + uri: SOURCE_URI, + kind: "placeholder", + }, + ]); + + const detached = await input.detach({ + tracks: [ + { uri: "firkin://me/files/a.mp3" }, + { uri: "https://example.com/b.mp3" }, + ], + }); + + return { + consult, + groupConsult, + artwork, + tracks: tracks.map((track) => track.id), + detached: detached.map((track) => track.uri), + }; + }); + + expect(result.consult).toEqual({ supported: true, consult: "no" }); + expect(result.groupConsult).toEqual({ + firkin: { + available: "no", + reason: "Firkin is not connected", + scheme: "firkin", + uris: ["firkin://me"], + }, + }); + expect(result.artwork).toBe(null); + expect(result.tracks).toEqual(["keep-me"]); + expect(result.detached).toEqual(["https://example.com/b.mp3"]); + }); +}); diff --git a/tests/components/input/wnfs-firkin/test.ts b/tests/components/input/wnfs-firkin/test.ts deleted file mode 100644 index 6522a5d1..00000000 --- a/tests/components/input/wnfs-firkin/test.ts +++ /dev/null @@ -1,138 +0,0 @@ -// @ts-nocheck -- the evaluated browser function intentionally uses untyped JS - -import { describe, it } from "@std/testing/bdd"; -import { expect } from "@std/expect"; - -import { testWeb } from "@tests/common/index.ts"; - -describe("components/input/wnfs-firkin", () => { - it("lists, resolves and consults through the output broadcast channel", async () => { - const result = await testWeb(async () => { - const group = "di-wnfs-firkin-test"; - const namespace = "test"; - - const { outputChannelName, SOURCE_URI } = await import( - "~/components/output/bytes/wnfs/files.js" - ); - const { rpc } = await import("~/common/worker.js"); - const mod = await import( - "~/components/input/wnfs-firkin/element.js" - ); - - const server = new BroadcastChannel( - outputChannelName({ group, namespace }), - ); - const files = new Map([["a.mp3", new Uint8Array([1, 2, 3])]]); - - const channel = rpc(server, { - storageStatus: () => ({ connected: true, did: "did:key:test" }), - storageListFiles: () => - [...files.keys()].map((name) => ({ - name, - cid: "bafkreitest", - size: 3, - mime: "audio/mpeg", - modified: 0, - })), - storageRead: (name) => files.get(name), - storageFileURL: (name) => - `https://firkin.test/blob/bafkreitest?token=x&name=${name}`, - storageUploadFile: () => ({ cid: "bafkreitest" }), - storageDeleteFile: () => undefined, - }); - - const input = new mod.CLASS(); - input.setAttribute("group", group); - input.setAttribute("namespace", namespace); - document.body.append(input); - - const consult = await input.consult(SOURCE_URI); - const tracks = await input.list([ - { - $type: "sh.diffuse.output.track", - id: "t1", - uri: SOURCE_URI, - kind: "placeholder", - }, - ]); - const resolved = await input.resolve({ uri: tracks[0].uri }); - const artwork = await input.artwork(tracks[0].uri); - const sources = input.sources(tracks); - - channel.destroy(); - server.close(); - - return { - consult, - tracks: tracks.map((track) => ({ - hasId: typeof track.id === "string" && track.id.length > 0, - uri: track.uri, - })), - resolvedUrl: resolved && "url" in resolved ? resolved.url : null, - artwork, - sources, - }; - }); - - expect(result.consult).toEqual({ supported: true, consult: "yes" }); - expect(result.tracks).toEqual([ - { hasId: true, uri: "wnfs+firkin://me/files/a.mp3" }, - ]); - expect(result.resolvedUrl).toBe( - "https://firkin.test/blob/bafkreitest?token=x&name=a.mp3", - ); - expect(result.artwork).toBe(null); - expect(result.sources).toEqual([ - { label: "Firkin + WNFS", uri: "wnfs+firkin://me" }, - ]); - }); - - it("keeps a placeholder when the storage has no audio files", async () => { - const result = await testWeb(async () => { - const group = "di-wnfs-firkin-test"; - const namespace = "test"; - - const { outputChannelName, SOURCE_URI } = await import( - "~/components/output/bytes/wnfs/files.js" - ); - const { rpc } = await import("~/common/worker.js"); - const mod = await import( - "~/components/input/wnfs-firkin/element.js" - ); - - const server = new BroadcastChannel( - outputChannelName({ group, namespace }), - ); - - const channel = rpc(server, { - storageStatus: () => ({ connected: true }), - storageListFiles: () => [], - storageRead: () => undefined, - storageFileURL: () => null, - storageUploadFile: () => null, - storageDeleteFile: () => undefined, - }); - - const input = new mod.CLASS(); - input.setAttribute("group", group); - input.setAttribute("namespace", namespace); - document.body.append(input); - - const tracks = await input.list([ - { - $type: "sh.diffuse.output.track", - id: "keep-me", - uri: SOURCE_URI, - kind: "placeholder", - }, - ]); - - channel.destroy(); - server.close(); - - return tracks.map((track) => track.id); - }); - - expect(result).toEqual(["keep-me"]); - }); -}); diff --git a/tests/components/output/bytes/wnfs/files-client.test.ts b/tests/components/output/bytes/wnfs/files-client.test.ts deleted file mode 100644 index b092c591..00000000 --- a/tests/components/output/bytes/wnfs/files-client.test.ts +++ /dev/null @@ -1,99 +0,0 @@ -// @ts-nocheck -- the evaluated browser function intentionally uses untyped JS - -import { describe, it } from "@std/testing/bdd"; -import { expect } from "@std/expect"; - -import { testWeb } from "@tests/common/index.ts"; - -describe("components/output/bytes/wnfs/files (broadcast client)", () => { - it("calls the output element's file actions over the broadcast channel", async () => { - const result = await testWeb(async () => { - const { outputChannelName, StorageClient } = await import( - "~/components/output/bytes/wnfs/files.js" - ); - const { rpc } = await import("~/common/worker.js"); - - const options = { group: "files-test", namespace: "files-test" }; - const server = new BroadcastChannel(outputChannelName(options)); - - const files = new Map([ - [ - "a.mp3", - { - bytes: new Uint8Array([1, 2, 3]), - cid: "bafk-a", - mime: "audio/mpeg", - modified: 0, - size: 3, - }, - ], - ]); - - const channel = rpc(server, { - storageStatus: () => ({ connected: true, did: "did:key:test" }), - storageListFiles: () => - [...files.entries()].map(([name, file]) => ({ - name, - cid: file.cid, - mime: file.mime, - modified: file.modified, - size: file.size, - })), - storageRead: (name) => files.get(name)?.bytes, - storageFileURL: (name, expireIn) => - `https://firkin.test/blob/${ - files.get(name)?.cid - }?token=x&expire=${expireIn}`, - storageUploadFile: (name, bytes, mime) => { - const cid = `bafk-${name}`; - files.set(name, { - bytes, - cid, - mime, - modified: 0, - size: bytes.byteLength, - }); - return { cid }; - }, - storageDeleteFile: (name) => { - files.delete(name); - }, - }); - - const client = new StorageClient(options); - - const status = await client.status(); - const list = await client.listFiles(); - const bytes = await client.read("a.mp3"); - const url = await client.fileURL("a.mp3", 123); - const uploaded = await client.uploadFile( - "b.mp3", - new Uint8Array([4, 5]), - "audio/mpeg", - ); - await client.deleteFile("a.mp3"); - - client.close(); - channel.destroy(); - server.close(); - - return { - status, - list: list.map((item) => item.name), - bytes: [...bytes], - url, - uploaded, - remaining: [...files.keys()], - }; - }); - - expect(result).toEqual({ - status: { connected: true, did: "did:key:test" }, - list: ["a.mp3"], - bytes: [1, 2, 3], - url: "https://firkin.test/blob/bafk-a?token=x&expire=123", - uploaded: { cid: "bafk-b.mp3" }, - remaining: ["b.mp3"], - }); - }); -}); diff --git a/tests/components/output/bytes/wnfs/files.test.ts b/tests/components/output/bytes/wnfs/files.test.ts deleted file mode 100644 index 0e96be42..00000000 --- a/tests/components/output/bytes/wnfs/files.test.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { describe, it } from "@std/testing/bdd"; -import { expect } from "@std/expect"; - -import { - buildURI, - decodeManifest, - encodeManifest, - FILE_URL_TTL_SECONDS, - FILES_MANIFEST, - mimeType, - outputChannelName, - parseURI, - SCHEME, - SOURCE_URI, -} from "~/components/output/bytes/wnfs/files.js"; - -describe("components/output/bytes/wnfs/files", () => { - it("builds and parses file URIs", () => { - expect(SCHEME).toBe("wnfs+firkin"); - expect(SOURCE_URI).toBe("wnfs+firkin://me"); - - const uri = buildURI("Artist - Title.mp3"); - expect(uri).toBe("wnfs+firkin://me/files/Artist%20-%20Title.mp3"); - expect(parseURI(uri)).toEqual({ name: "Artist - Title.mp3" }); - }); - - it("rejects URIs that are not files in this storage", () => { - expect(parseURI(SOURCE_URI)).toBeNull(); - expect(parseURI("wnfs+firkin://me/other/x.mp3")).toBeNull(); - expect(parseURI("wnfs+firkin://me/files/a/b.mp3")).toBeNull(); - expect(parseURI("s3://bucket/x.mp3")).toBeNull(); - expect(parseURI("wnfs+firkin://me/files/%E0%A4%A")).toBeNull(); - }); - - it("names the output broadcast channel like the element identifier", () => { - expect(outputChannelName({ group: "g", namespace: "n" })).toBe( - "diffuse/output/bytes/wnfs/g/n", - ); - expect(outputChannelName({ group: "g" })).toBe( - "diffuse/output/bytes/wnfs/g", - ); - expect(outputChannelName({})).toBe("diffuse/output/bytes/wnfs/default"); - }); - - it("guesses audio mime types from the file name", () => { - expect(mimeType("track.MP3")).toBe("audio/mpeg"); - expect(mimeType("track.m4a")).toBe("audio/mp4"); - expect(mimeType("track.flac")).toBe("audio/flac"); - expect(mimeType("track.unknown")).toBe("application/octet-stream"); - expect(mimeType("track")).toBe("application/octet-stream"); - }); - - it("round-trips the pointer manifest", () => { - expect(FILES_MANIFEST).toBe("diffuse-files.json"); - expect(FILE_URL_TTL_SECONDS).toBe(6 * 60 * 60); - - const manifest = { - version: 1, - files: { - "a.mp3": { - cid: "bafkreitest", - size: 3, - mime: "audio/mpeg", - modified: 123, - }, - }, - }; - - expect(decodeManifest(encodeManifest(manifest))).toEqual(manifest); - }); - - it("treats a corrupt manifest as empty", () => { - expect(decodeManifest(new TextEncoder().encode("not json"))).toEqual({ - version: 1, - files: {}, - }); - }); -}); diff --git a/tests/components/upload/firkin/test.ts b/tests/components/upload/firkin/test.ts new file mode 100644 index 00000000..b276802a --- /dev/null +++ b/tests/components/upload/firkin/test.ts @@ -0,0 +1,60 @@ +// @ts-nocheck -- the evaluated browser function intentionally uses untyped JS + +import { describe, it } from "@std/testing/bdd"; +import { expect } from "@std/expect"; + +import { testWeb } from "@tests/common/index.ts"; + +describe("components/upload/firkin", () => { + it("reports not connected and rejects actions without a session", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/upload/firkin/element.js"); + const { SOURCE_URI } = await import("~/common/firkin/files.js"); + + const upload = new mod.CLASS(); + document.body.append(upload); + + const consult = await upload.consult(SOURCE_URI); + const file = new File([new Uint8Array([1, 2, 3])], "a.mp3", { + type: "audio/mpeg", + }); + + let uploadError = ""; + try { + await upload.upload({ file, uri: SOURCE_URI }); + } catch (err) { + uploadError = err instanceof Error ? err.message : String(err); + } + + let deleteError = ""; + try { + await upload.delete("firkin://me/files/a.mp3"); + } catch (err) { + deleteError = err instanceof Error ? err.message : String(err); + } + + let createError = ""; + try { + await upload.createSource({}); + } catch (err) { + createError = err instanceof Error ? err.message : String(err); + } + + return { + name: mod.NAME, + scheme: upload.SCHEME, + consult, + uploadError, + deleteError, + createError, + }; + }); + + expect(result.name).toBe("du-firkin"); + expect(result.scheme).toBe("firkin"); + expect(result.consult).toEqual({ supported: true, consult: "no" }); + expect(result.uploadError).toContain("Firkin is not connected"); + expect(result.deleteError).toContain("Firkin is not connected"); + expect(result.createError).toContain("Firkin does not use OAuth"); + }); +}); diff --git a/tests/components/upload/wnfs-firkin/test.ts b/tests/components/upload/wnfs-firkin/test.ts deleted file mode 100644 index 539e679b..00000000 --- a/tests/components/upload/wnfs-firkin/test.ts +++ /dev/null @@ -1,67 +0,0 @@ -// @ts-nocheck -- the evaluated browser function intentionally uses untyped JS - -import { describe, it } from "@std/testing/bdd"; -import { expect } from "@std/expect"; - -import { testWeb } from "@tests/common/index.ts"; - -describe("components/upload/wnfs-firkin", () => { - it("uploads and deletes through the output broadcast channel", async () => { - const result = await testWeb(async () => { - const group = "du-wnfs-firkin-test"; - const namespace = "test"; - - const { outputChannelName, SOURCE_URI } = await import( - "~/components/output/bytes/wnfs/files.js" - ); - const { rpc } = await import("~/common/worker.js"); - const mod = await import( - "~/components/upload/wnfs-firkin/element.js" - ); - - const server = new BroadcastChannel( - outputChannelName({ group, namespace }), - ); - const writes = []; - const deletes = []; - - const channel = rpc(server, { - storageStatus: () => ({ connected: true }), - storageListFiles: () => [], - storageRead: () => undefined, - storageFileURL: () => null, - storageUploadFile: (name, bytes, mime) => { - writes.push({ name, bytes: [...bytes], mime }); - return { cid: "bafkreiuploaded" }; - }, - storageDeleteFile: (name) => { - deletes.push(name); - }, - }); - - const upload = new mod.CLASS(); - upload.setAttribute("group", group); - upload.setAttribute("namespace", namespace); - document.body.append(upload); - - const consult = await upload.consult(SOURCE_URI); - const uri = await upload.upload({ - uri: SOURCE_URI, - file: new File([new Uint8Array([7, 8])], "bafktest.mp3"), - }); - await upload.delete(uri); - - channel.destroy(); - server.close(); - - return { consult, uri, writes, deletes }; - }); - - expect(result.consult).toEqual({ supported: true, consult: "yes" }); - expect(result.uri).toBe("wnfs+firkin://me/files/bafktest.mp3"); - expect(result.writes).toEqual([ - { name: "bafktest.mp3", bytes: [7, 8], mime: "audio/mpeg" }, - ]); - expect(result.deletes).toEqual(["bafktest.mp3"]); - }); -});