Files
Shade/packages/shade-media/src/album.ts

92 lines
3.6 KiB
TypeScript
Raw Normal View History

/**
* Albums: one link for many files.
*
* A season of a series, a photo album, a playlist, a wall of cameras. An
* album is itself a sealed file (SMF1) whose plaintext is a list of grants,
* with the MIME type `application/vnd.shade.album+json`. So:
*
* - one capability link opens every item, and nothing else;
* - the relay stores it like any other object: it cannot tell an album
* from a film, let alone count or name what is in it;
* - an album may hold live grants (a camera wall) and other albums.
*
* Holding an album means holding every item's keys. Deleting the album
* object does not revoke them from anyone who already opened it — delete
* the items to do that.
*/
import { MediaFormatError } from './errors.js';
import { encodeGrant, parseGrant } from './grant.js';
import type { Grant, MediaGrant } from './grant.js';
import type { MediaMeta } from './sealed.js';
export const ALBUM_KIND = 'shade.album/v1';
export const ALBUM_MIME = 'application/vnd.shade.album+json';
/** Enough for any season or camera roll, small enough to parse without thought. */
export const MAX_ALBUM_ITEMS = 10_000;
const MAX_TITLE = 512;
export interface Album {
title: string;
items: Grant[];
}
const enc = new TextEncoder();
const dec = new TextDecoder('utf-8', { fatal: true });
export function encodeAlbum(album: Album): Uint8Array {
checkTitle(album.title);
if (album.items.length > MAX_ALBUM_ITEMS) throw new MediaFormatError(`album holds more than ${MAX_ALBUM_ITEMS} items`);
// Grants are embedded as objects, each in its own wire form.
const items = album.items.map((g) => JSON.parse(encodeGrant(g)) as unknown);
return enc.encode(JSON.stringify({ kind: ALBUM_KIND, title: album.title, items }));
}
/** Parse an album's plaintext, strictly: every item must be a valid grant. */
export function parseAlbum(bytes: Uint8Array): Album {
let o: unknown;
try {
o = JSON.parse(dec.decode(bytes));
} catch {
throw new MediaFormatError('album is not UTF-8 JSON');
}
if (typeof o !== 'object' || o === null || Array.isArray(o)) throw new MediaFormatError('album must be an object');
const r = o as Record<string, unknown>;
if (r.kind !== ALBUM_KIND) throw new MediaFormatError(`not an album (kind ${String(r.kind)})`);
if (typeof r.title !== 'string') throw new MediaFormatError('album.title must be a string');
checkTitle(r.title);
if (!Array.isArray(r.items)) throw new MediaFormatError('album.items must be an array');
if (r.items.length > MAX_ALBUM_ITEMS) throw new MediaFormatError(`album holds more than ${MAX_ALBUM_ITEMS} items`);
const items = r.items.map((it, i) => {
if (typeof it !== 'object' || it === null || Array.isArray(it)) throw new MediaFormatError(`album item ${i} is not a grant`);
try {
return parseGrant(JSON.stringify(it));
} catch (e) {
throw new MediaFormatError(`album item ${i}: ${(e as Error).message}`);
}
});
return { title: r.title, items };
}
/** Does this grant (by its meta preview) or this sealed meta name an album? */
export function isAlbum(x: Grant | MediaMeta | undefined): boolean {
if (x === undefined) return false;
const meta = 'kind' in x ? x.meta : x;
return meta?.mime === ALBUM_MIME;
}
/** The meta an album's sealed file carries. */
export function albumMeta(title: string): MediaMeta {
return { mime: ALBUM_MIME, name: title };
}
/** Narrow for callers that only take sealed files. */
export function isMediaGrant(g: Grant): g is MediaGrant {
return g.kind === 'shade.media-grant/v1';
}
function checkTitle(t: string): void {
if (t.length > MAX_TITLE) throw new MediaFormatError(`album title longer than ${MAX_TITLE} characters`);
}