Files
WT-Offline/packages/local-service/src/web-ui/web-helpers.ts
T
N0VAN0M4LY 64b11d56bf Serve stock artwork and link official store media
Show the installed game artwork in the local store and marketplace. Use the official cover and gallery for each mapped product, with on-demand video playback. Include store cover images and screenshots in optional offline asset packs.

Verified TypeScript, browser script syntax, local service startup, game artwork endpoints, a sample image pack, and BMD-4M WebM byte-range delivery. In-game verification is pending.
2026-11-26 17:18:32 +08:00

90 lines
3.7 KiB
TypeScript

/**
* Shared static-asset server, JSON helpers, and explicit account resolution for the local web
* pages (`/store`, `/trade.gaijin.net` and the shared `/_wto/ui/` files). The game opens them in
* its built-in browser (Chromium 100; implementation notes).
*/
import { readFileSync } from 'node:fs';
import { extname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { Account, AccountStore } from '../accounts/account-store.ts';
import type { ServiceResponse } from '../server/exchange.ts';
const CONTENT_TYPES: Readonly<Record<string, string>> = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.svg': 'image/svg+xml',
'.avif': 'image/avif',
'.json': 'application/json; charset=utf-8',
};
const PAGE_FILES = ['index.html', 'styles.css', 'app.js'];
export interface StaticWebSite {
serve(subPath: string, summaryPrefix: string): ServiceResponse | undefined;
}
/** Serve the listed files of one folder; only names on the list are ever read from disk. */
export function createStaticWebSite(folderUrl: URL, files: readonly string[] = PAGE_FILES): StaticWebSite {
const rootDir = fileURLToPath(folderUrl);
const allowed = new Set(files);
return {
serve(subPath: string, summaryPrefix: string): ServiceResponse | undefined {
const normalized = subPath === '' || subPath === '/' ? 'index.html' : subPath.replace(/^\/+/, '');
if (!allowed.has(normalized)) return undefined;
return contentResponse(normalized, readFileSync(join(rootDir, normalized)), `${summaryPrefix}: ${normalized}`);
},
};
}
/** A 200 answer whose content type follows the file name's extension. */
export function contentResponse(fileName: string, body: Uint8Array | string, summary: string): ServiceResponse {
const contentType = CONTENT_TYPES[extname(fileName)] ?? 'application/octet-stream';
return { status: 200, headers: { 'content-type': contentType, 'cache-control': 'no-cache' }, body, logNotes: { summary } };
}
export function notFound(summary: string): ServiceResponse {
return { status: 404, headers: { 'content-type': 'text/plain; charset=utf-8' }, body: 'Not found', logNotes: { summary } };
}
export function parseJsonBody(raw: Buffer): Record<string, unknown> {
try {
const parsed = JSON.parse(raw.toString('utf8')) as unknown;
return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
? (parsed as Record<string, unknown>)
: {};
} catch {
return {};
}
}
export function jsonResponse(payload: unknown, summary: string): ServiceResponse {
const body = JSON.stringify(payload);
return {
status: 200,
headers: { 'content-type': 'application/json; charset=utf-8' },
body,
logNotes: { summary, responseText: body.slice(0, 1200) },
};
}
/**
* Select the account displayed in a read-only web state query:
* - when `requestedUserId` is provided, look up that exact account (never falling back to another);
* - when `requestedUserId` is omitted on initial page load, default the UI selector to the account
* currently signed into the game in this session, or the first registered account otherwise.
*/
export function selectWebViewAccount(accounts: AccountStore, requestedUserId: string | null): Account | undefined {
const trimmed = requestedUserId?.trim() ?? '';
return trimmed ? accounts.byUserId(trimmed) : (accounts.lastSignedIn() ?? accounts.all()[0]);
}
/**
* Require an explicit, valid `userId` on mutating web actions (buy, redeem); never fall back.
*/
export function requireWebActionAccount(accounts: AccountStore, rawUserId: unknown): Account | undefined {
if (typeof rawUserId !== 'string') return undefined;
const trimmed = rawUserId.trim();
return trimmed ? accounts.byUserId(trimmed) : undefined;
}