Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | 3x 3x 3x 3x 9x 11x 7x 7x 6x 15x 6x 12x 10x 4x 4x 6x 6x 3x 3x 1x 2x 2x 2x 7x 6x 6x 5x 4x 4x 2x 2x 3x 3x 2x 3x 3x 5x 3x 3x 1x 1x 1x 3x 4x 3x 2x 2x 2x 1x 1x 1x 1x 4x 3x 3x 3x 3x 3x 3x 1x 3x 1x 3x 2x 2x | // The `*.bemuse` chunks of the Bemuse server songs played, kept between visits so that a song played again downloads
// nothing. A chunk's file name carries the hash of its payload (`ogg.3.8ed2dece.bemuse`), so a URL always serves the
// same bytes and a kept chunk never needs asking for again. The chunks take a fixed share of the disk: once they pass
// it, the ones used longest ago go first. Which chunks are kept, their sizes and when each was last used are listed in
// `localStorage`, since Cache Storage keeps no use times and reading every entry to size it would cost a download.
/** A chunk store by URL. */
export interface BemuseChunkStore {
get(url: string): Promise<Uint8Array | undefined>;
put(url: string, bytes: Uint8Array): Promise<void>;
}
/** A kept chunk as the list names it. */
export interface BemuseChunkRecord {
readonly url: string;
readonly size: number;
/** `Date.now()` when the chunk was last read or stored. */
readonly usedAt: number;
}
export const BEMUSE_CHUNK_CACHE_NAME = 'be-music-bemuse-chunks';
/** `localStorage` key of the kept chunks' list. */
export const BEMUSE_CHUNK_LIST_STORAGE_KEY = 'be-music.bemuse-chunk-cache';
/** The disk the chunks may take: about 700 chunks, a few dozen songs. */
export const BEMUSE_CHUNK_BUDGET_BYTES = 1024 * 1024 * 1024;
/** How long the list waits for more uses before it is written, so a song's dozens of chunks write it once. */
const LIST_WRITE_DELAY_MS = 1000;
/** `records` with `url` marked used at `nowMs` (and sized `size` when given), most recently used last. */
export function touchBemuseChunkRecord(
records: readonly BemuseChunkRecord[],
url: string,
size: number | undefined,
nowMs: number,
): BemuseChunkRecord[] {
const previous = records.find((record) => record.url === url);
const rest = records.filter((record) => record.url !== url);
const recordSize = size ?? previous?.size;
if (recordSize === undefined) return rest;
return [...rest, { url, size: recordSize, usedAt: nowMs }];
}
/** The URLs to drop so that the rest fit in `budgetBytes`, used longest ago first. */
export function bemuseChunksOverBudget(records: readonly BemuseChunkRecord[], budgetBytes: number): string[] {
let total = records.reduce((sum, record) => sum + record.size, 0);
const dropped: string[] = [];
for (const record of [...records].sort((a, b) => a.usedAt - b.usedAt)) {
if (total <= budgetBytes) break;
total -= record.size;
dropped.push(record.url);
}
return dropped;
}
/** Reads the stored list, keeping only well-formed records, each URL once. */
export function parseBemuseChunkRecords(raw: string | null | undefined): BemuseChunkRecord[] {
if (!raw) return [];
let parsed: unknown;
try {
parsed = JSON.parse(raw);
} catch {
return [];
}
Iif (!Array.isArray(parsed)) return [];
const records = new Map<string, BemuseChunkRecord>();
for (const value of parsed) {
if (!Array.isArray(value) || value.length !== 3) continue;
const [url, size, usedAt] = value as unknown[];
if (typeof url !== 'string' || !Number.isSafeInteger(size) || (size as number) < 0) continue;
if (typeof usedAt !== 'number' || !Number.isFinite(usedAt)) continue;
records.delete(url);
records.set(url, { url, size: size as number, usedAt });
}
return [...records.values()];
}
function stringifyBemuseChunkRecords(records: readonly BemuseChunkRecord[]): string {
return JSON.stringify(records.map((record) => [record.url, record.size, record.usedAt]));
}
/**
* A chunk store over Cache Storage with its list in `listStorage`, or `undefined` where the page has no Cache Storage.
* Without list storage (a private window) the list lasts for the visit and the budget holds within it.
*/
export function createCacheStorageBemuseChunkStore(
options: {
storage?: CacheStorage | undefined;
listStorage?: Pick<Storage, 'getItem' | 'setItem'> | undefined;
budgetBytes?: number;
now?: () => number;
} = {},
): BemuseChunkStore | undefined {
const storage = 'storage' in options ? options.storage : globalThis.caches;
if (storage === undefined) return undefined;
const listStorage = 'listStorage' in options ? options.listStorage : defaultStorage();
const budgetBytes = options.budgetBytes ?? BEMUSE_CHUNK_BUDGET_BYTES;
const now = options.now ?? Date.now;
const open = () => storage.open(BEMUSE_CHUNK_CACHE_NAME);
let records = readRecords(listStorage);
let writeTimer: ReturnType<typeof setTimeout> | undefined;
const writeList = () => {
writeTimer = undefined;
try {
listStorage?.setItem(BEMUSE_CHUNK_LIST_STORAGE_KEY, stringifyBemuseChunkRecords(records));
} catch {
// Full or blocked storage: the list is rebuilt as chunks are read again.
}
};
const scheduleWrite = () => {
writeTimer ??= setTimeout(writeList, LIST_WRITE_DELAY_MS);
};
return {
async get(url) {
try {
const response = await (await open()).match(url);
if (response === undefined) return undefined;
const bytes = new Uint8Array(await response.arrayBuffer());
records = touchBemuseChunkRecord(records, url, bytes.byteLength, now());
scheduleWrite();
return bytes;
} catch {
return undefined;
}
},
async put(url, bytes) {
if (bytes.byteLength > budgetBytes) return;
try {
const cache = await open();
await cache.put(url, new Response(new Uint8Array(bytes)));
records = touchBemuseChunkRecord(records, url, bytes.byteLength, now());
const dropped = bemuseChunksOverBudget(records, budgetBytes);
if (dropped.length > 0) {
const droppedSet = new Set(dropped);
records = records.filter((record) => !droppedSet.has(record.url));
await Promise.all(dropped.map((droppedURL) => cache.delete(droppedURL).catch(() => false)));
}
scheduleWrite();
} catch {
// A full or blocked store: the chunk is fetched again next time.
}
},
};
}
function readRecords(storage: Pick<Storage, 'getItem'> | undefined): BemuseChunkRecord[] {
try {
return parseBemuseChunkRecords(storage?.getItem(BEMUSE_CHUNK_LIST_STORAGE_KEY));
} catch {
return [];
}
}
function defaultStorage(): Storage | undefined {
try {
return typeof window === 'undefined' ? undefined : window.localStorage;
} catch {
return undefined;
}
}
let pageChunkStore: BemuseChunkStore | null | undefined;
/** The page's chunk store, shared by every server; `undefined` without Cache Storage. */
export function pageBemuseChunkStore(): BemuseChunkStore | undefined {
if (pageChunkStore === undefined) pageChunkStore = createCacheStorageBemuseChunkStore() ?? null;
return pageChunkStore ?? undefined;
}
|