Files
splatoon3.ink/app/data/LocalizationProcessor.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

108 lines
2.6 KiB
JavaScript

import { storage } from '../common/storage/index.mjs';
import jsonpath from '../common/jsonpath.mjs';
import get from 'lodash/get.js';
import set from 'lodash/set.js';
import pLimit from 'p-limit';
const limit = pLimit(1);
function makeArray(value) {
return Array.isArray(value) ? value : [value];
}
export class LocalizationProcessor {
// Public storage key prefix
outputDirectory = 'data/locale';
constructor(locale, rulesets) {
this.locale = locale;
this.rulesets = rulesets;
}
get filename() {
return `${this.outputDirectory}/${this.locale.code}.json`;
}
*rulesetIterations() {
for (let ruleset of this.rulesets) {
for (let node of makeArray(ruleset.nodes)) {
for (let value of makeArray(ruleset.values)) {
yield {
key: ruleset.key,
node,
id: ruleset.id,
value,
};
}
}
}
}
*dataIterations(data) {
for (let ruleset of this.rulesetIterations()) {
for (let node of jsonpath.query(data, ruleset.node)) {
if (!node) continue;
let id = get(node, ruleset.id);
yield {
ruleset,
node,
id,
value: get(node, ruleset.value),
path: `${ruleset.key}.${id}.${ruleset.value}`,
};
}
}
}
async updateLocalizations(data) {
// We're reading, modifying, and writing back to the same file,
// so we have to make sure the whole operation is atomic.
return limit(() => this._updateLocalizations(data));
}
async _updateLocalizations(data) {
let localizations = await this.readData();
for (let { path, value } of this.dataIterations(data)) {
set(localizations, path, value);
}
await this.writeData(localizations);
}
async hasMissingLocalizations(data) {
let localizations = await this.readData();
for (let { path } of this.dataIterations(data)) {
if (get(localizations, path) === undefined) {
return true;
}
}
return false;
}
async writeData(data) {
// If we're running in debug mode, format the JSON output so it's easier to read
let debug = !!process.env.DEBUG;
let space = debug ? 2 : undefined;
data = JSON.stringify(data, undefined, space);
await storage().public.writeText(this.filename, data);
}
async readData() {
try {
// Missing files (null) start out empty
return await storage().public.readJson(this.filename) || {};
} catch (e) {
console.warn(`Failed to read localization file ${this.filename}:`, e.message);
}
return {};
}
}