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 | 5x 5x 4x 32x 32x 32x 32x 32x 32x 32x 18x 4x 4x 18x 6x 6x 6x 6x 1x 1x 1x 1x 1x 5x 5x 5x 5x 1x 1x 1x 1x 3x 3x 3x 3x 1x 2x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x | import { IFileStorage } from "../types.js";
/**
* Node.js/Bun 環境での OS ファイルシステムを用いた永続ストレージ。
*/
export class NodeFSStorage implements IFileStorage {
private cacheDir: string | null = null;
private customPath?: string | undefined;
constructor(customPath?: string) {
if (customPath !== undefined) {
this.customPath = customPath;
}
}
private async getModules() {
// 2026: Hide from static bundlers like Next.js Turbopack and Vite
const fsName = "node:fs/promises";
const pathName = "node:path";
const osName = "node:os";
const fs = await import(
/* @vite-ignore */ /* webpackIgnore: true */ fsName
);
const path = await import(
/* @vite-ignore */ /* webpackIgnore: true */ pathName
);
const os = await import(
/* @vite-ignore */ /* webpackIgnore: true */ osName
);
return { fs, path, os };
}
private async getDir(): Promise<string> {
if (this.cacheDir) return this.cacheDir;
const { path, os } = await this.getModules();
this.cacheDir =
this.customPath ||
path.join(os.homedir(), ".multi-game-engines", "cache");
return this.cacheDir as string;
}
private async ensureDir(): Promise<void> {
try {
const { fs } = await this.getModules();
const dir = await this.getDir();
await fs.mkdir(dir, { recursive: true });
} catch {
// Ignore
}
}
async get(key: string): Promise<ArrayBuffer | null> {
try {
const { fs } = await this.getModules();
const filePath = await this.getFilePath(key);
const buffer = await fs.readFile(filePath);
// 2026: Node.js Buffer.buffer may return more than just the file content
// if it's using a shared pool. Always slice to be safe.
return buffer.buffer.slice(
buffer.byteOffset,
buffer.byteOffset + buffer.byteLength,
);
} catch {
return null;
}
}
async set(key: string, data: ArrayBuffer): Promise<void> {
await this.ensureDir();
const { fs } = await this.getModules();
const filePath = await this.getFilePath(key);
await fs.writeFile(filePath, Buffer.from(data));
}
async delete(key: string): Promise<void> {
try {
const { fs } = await this.getModules();
const filePath = await this.getFilePath(key);
await fs.unlink(filePath);
} catch {
// Ignore
}
}
async has(key: string): Promise<boolean> {
try {
const { fs } = await this.getModules();
const filePath = await this.getFilePath(key);
await fs.access(filePath);
return true;
} catch {
return false;
}
}
async clear(): Promise<void> {
try {
const { fs } = await this.getModules();
const dir = await this.getDir();
await fs.rm(dir, { recursive: true, force: true });
await this.ensureDir();
} catch {
// Ignore
}
}
async getQuota(): Promise<{ usage: number; quota: number }> {
// 2026 Zenith: Simple stat for disk usage.
// (Note: In Node.js, accurate quota is OS dependent, return safe values)
return { usage: 0, quota: 10 * 1024 * 1024 * 1024 }; // 10GB safe quota
}
async getPhysicalPath(key: string): Promise<string | null> {
return await this.getFilePath(key);
}
private async getFilePath(key: string): Promise<string> {
// 2026: URL-safe filename encoding
const { path } = await this.getModules();
const dir = await this.getDir();
const safeName = encodeURIComponent(key).substring(0, 255);
return path.join(dir, safeName);
}
}
|