Files
splatoon3.ink/app/common/storage/MemoryBucket.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

97 lines
2.4 KiB
JavaScript

// In-memory bucket with the subset of the R2 binding API the app uses. For tests.
function toBytes(value) {
if (typeof value === 'string') {
return new TextEncoder().encode(value);
}
if (value instanceof ArrayBuffer) {
return new Uint8Array(value);
}
if (ArrayBuffer.isView(value)) {
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength).slice();
}
throw new TypeError('MemoryBucket.put: unsupported value type');
}
export default class MemoryBucket {
objects = new Map();
async head(key) {
let object = this.objects.get(key);
return object
? { key, size: object.bytes.byteLength, uploaded: object.uploaded, httpMetadata: object.httpMetadata }
: null;
}
async get(key) {
let object = this.objects.get(key);
if (!object) {
return null;
}
let bytes = object.bytes;
return {
key,
size: bytes.byteLength,
uploaded: object.uploaded,
httpMetadata: object.httpMetadata,
json: async () => JSON.parse(new TextDecoder().decode(bytes)),
text: async () => new TextDecoder().decode(bytes),
arrayBuffer: async () => bytes.slice().buffer,
};
}
async put(key, value, options = {}) {
let bytes = value instanceof ReadableStream
? new Uint8Array(await new Response(value).arrayBuffer())
: toBytes(value);
this.objects.set(key, { bytes, uploaded: new Date(), httpMetadata: options.httpMetadata ?? {} });
return { key };
}
async delete(keys) {
for (let key of [].concat(keys)) {
this.objects.delete(key);
}
}
async list({ prefix = '', cursor, limit = 1000 } = {}) {
let keys = [...this.objects.keys()].filter(key => key.startsWith(prefix)).sort();
let start = cursor ? Number(cursor) : 0;
let page = keys.slice(start, start + limit);
let truncated = start + limit < keys.length;
return {
objects: page.map(key => ({
key,
size: this.objects.get(key).bytes.byteLength,
uploaded: this.objects.get(key).uploaded,
})),
truncated,
cursor: truncated ? String(start + limit) : undefined,
};
}
// Test helpers
text(key) {
let object = this.objects.get(key);
return object ? new TextDecoder().decode(object.bytes) : undefined;
}
json(key) {
let text = this.text(key);
return text === undefined ? undefined : JSON.parse(text);
}
}