Files
splatoon3.ink/app/common/storage/FilesystemStorage.mjs
Matt Isenhower 21a4f5d85c Route backend file access through storage and auth seams
Prepare the shared updater and social code to run against R2 in a Worker
while keeping the local Node runner working:

- Add app/common/storage with public, private, and archive areas keyed like
  the R2 objects. FilesystemStorage maps them onto dist/ and storage/ for
  Node; BucketStorage wraps R2 bindings. Storage is configured once per
  process; only image existence is cached, so JSON reads are always fresh.
- Move ValueCache, DataUpdater (data, calendars, archive snapshots),
  ImageProcessor, LocalizationProcessor, festival updaters, status
  generators, social translations, ImageWriter, and Buffer/Threads image
  hosting onto storage. Query hashes are a JSON import.
- Add app/splatnet/auth.mjs: the existing bullet-token code behind auth(),
  so the Worker can route token requests to per-region Durable Objects.
  warmCaches uses auth().refresh().
- Remove the S3/R2 sync, vfs, the dual upload, the sync commands, and
  s3-sync-client. Local runs no longer read from or write to remote buckets.
- Give every test fresh in-memory storage via app/test/setup.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 16:03:33 -08:00

101 lines
2.4 KiB
JavaScript

import { promises as fs } from 'node:fs';
import path from 'node:path';
/**
* Storage over a local directory, used by the Node runner. Keys map to paths under the root;
* `relocations` maps key prefixes to different directories (for example, R2's `splatnet/`
* images live under dist/assets/splatnet/ locally). HTTP metadata is accepted and ignored.
*/
export default class FilesystemStorage {
constructor(root, { relocations = {} } = {}) {
this.root = path.resolve(root);
this.relocations = Object.entries(relocations);
}
pathFor(key) {
for (let [prefix, replacement] of this.relocations) {
if (key.startsWith(prefix)) {
key = replacement + key.slice(prefix.length);
break;
}
}
return path.join(this.root, ...key.split('/'));
}
async exists(key) {
return (await this.#stat(key))?.isFile() ?? false;
}
/**
* @param {string} key
* @returns {Promise<Date|null>} When the file was last written, or null if missing
*/
async lastModified(key) {
return (await this.#stat(key))?.mtime ?? null;
}
/**
* @param {string} key
* @returns {Promise<object|null>} The parsed document, or null if it does not exist
*/
async readJson(key) {
let buffer = await this.#read(key);
return buffer === null ? null : JSON.parse(buffer.toString('utf8'));
}
/**
* @param {string} key
* @returns {Promise<Uint8Array|null>} The file's bytes, or null if it does not exist
*/
async readBytes(key) {
let buffer = await this.#read(key);
return buffer === null ? null : new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
}
async writeText(key, text) {
await this.#write(key, text);
}
async writeBytes(key, bytes) {
await this.#write(key, bytes);
}
async delete(key) {
await fs.rm(this.pathFor(key), { force: true });
}
async #stat(key) {
try {
return await fs.stat(this.pathFor(key));
} catch (error) {
if (error.code === 'ENOENT') {
return null;
}
throw error;
}
}
async #read(key) {
try {
return await fs.readFile(this.pathFor(key));
} catch (error) {
if (error.code === 'ENOENT' || error.code === 'EISDIR') {
return null;
}
throw error;
}
}
async #write(key, body) {
let filename = this.pathFor(key);
await fs.mkdir(path.dirname(filename), { recursive: true });
await fs.writeFile(filename, body);
}
}