|
| 1 | +import buffer from '../../node/buffer/buffer'; |
| 2 | +import path from '../../node/path/path'; |
| 3 | +import CdnProvider from '../connection/CdnProvider'; |
| 4 | +import FsProvider from '../file/FsProvider'; |
| 5 | +import PathResolver from '../../../r2mm/manager/PathResolver'; |
| 6 | + |
| 7 | +const BUNDLED_PROTOCOL_PREFIX = "public://images/game_selection/"; |
| 8 | +const PLACEHOLDER_URL = `${BUNDLED_PROTOCOL_PREFIX}thunderstore-beta.webp`; |
| 9 | +const LOCALHOST_DEV_BASE = "http://localhost:1337"; |
| 10 | +const LOCALHOST_PROBE_TIMEOUT_MS = 2000; |
| 11 | +const CDN_FETCH_TIMEOUT_MS = 10000; |
| 12 | +const CDN_BREAKER_THRESHOLD = 3; |
| 13 | +const CACHE_SUBDIR = "image-cache"; |
| 14 | + |
| 15 | +export default class GameImageProvider { |
| 16 | + |
| 17 | + private static cacheRoot: string | undefined; |
| 18 | + private static localhostAvailable = false; |
| 19 | + private static cdnConsecutiveFailures = 0; |
| 20 | + private static cdnBreakerTripped = false; |
| 21 | + private static initialised: Promise<void> | undefined; |
| 22 | + |
| 23 | + public static get placeholderUrl(): string { |
| 24 | + return PLACEHOLDER_URL; |
| 25 | + } |
| 26 | + |
| 27 | + public static async resolve(iconUrl: string): Promise<string> { |
| 28 | + await GameImageProvider.ensureInit(); |
| 29 | + |
| 30 | + if (!iconUrl) { |
| 31 | + return PLACEHOLDER_URL; |
| 32 | + } |
| 33 | + |
| 34 | + const bundledUrl = `${BUNDLED_PROTOCOL_PREFIX}${iconUrl}`; |
| 35 | + if (await GameImageProvider.urlReachable(bundledUrl)) { |
| 36 | + return bundledUrl; |
| 37 | + } |
| 38 | + |
| 39 | + if (GameImageProvider.localhostAvailable) { |
| 40 | + const localhostUrl = `${LOCALHOST_DEV_BASE}/assets/${iconUrl}`; |
| 41 | + if (await GameImageProvider.urlReachable(localhostUrl)) { |
| 42 | + return localhostUrl; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + const cachePath = GameImageProvider.cachePathFor(iconUrl); |
| 47 | + if (cachePath && await FsProvider.instance.exists(cachePath)) { |
| 48 | + return GameImageProvider.fileUrlOf(cachePath); |
| 49 | + } |
| 50 | + |
| 51 | + if (cachePath && !GameImageProvider.cdnBreakerTripped) { |
| 52 | + const cached = await GameImageProvider.fetchFromCdnAndCache(iconUrl, cachePath); |
| 53 | + if (cached) { |
| 54 | + return cached; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return PLACEHOLDER_URL; |
| 59 | + } |
| 60 | + |
| 61 | + private static ensureInit(): Promise<void> { |
| 62 | + if (!GameImageProvider.initialised) { |
| 63 | + GameImageProvider.initialised = GameImageProvider.runInit(); |
| 64 | + } |
| 65 | + return GameImageProvider.initialised; |
| 66 | + } |
| 67 | + |
| 68 | + private static async runInit(): Promise<void> { |
| 69 | + if (PathResolver.APPDATA_DIR) { |
| 70 | + const root = path.join(PathResolver.APPDATA_DIR, CACHE_SUBDIR); |
| 71 | + await FsProvider.instance.mkdirs(root); |
| 72 | + GameImageProvider.cacheRoot = root; |
| 73 | + } |
| 74 | + |
| 75 | + if (import.meta.env.MODE === "development") { |
| 76 | + GameImageProvider.localhostAvailable = await GameImageProvider.probeLocalhost(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static async probeLocalhost(): Promise<boolean> { |
| 81 | + const controller = new AbortController(); |
| 82 | + const timer = setTimeout(() => controller.abort(), LOCALHOST_PROBE_TIMEOUT_MS); |
| 83 | + try { |
| 84 | + const res = await fetch(`${LOCALHOST_DEV_BASE}/healthz`, { signal: controller.signal }); |
| 85 | + return res.ok; |
| 86 | + } catch { |
| 87 | + return false; |
| 88 | + } finally { |
| 89 | + clearTimeout(timer); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static async urlReachable(url: string): Promise<boolean> { |
| 94 | + try { |
| 95 | + const res = await fetch(url); |
| 96 | + return res.ok; |
| 97 | + } catch { |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static cachePathFor(iconUrl: string): string | undefined { |
| 103 | + if (!GameImageProvider.cacheRoot) { |
| 104 | + return undefined; |
| 105 | + } |
| 106 | + return path.join(GameImageProvider.cacheRoot, ...iconUrl.split("/")); |
| 107 | + } |
| 108 | + |
| 109 | + private static fileUrlOf(filePath: string): string { |
| 110 | + return `file:///${filePath.replace(/\\/g, "/")}`; |
| 111 | + } |
| 112 | + |
| 113 | + private static async fetchFromCdnAndCache(iconUrl: string, cachePath: string): Promise<string | undefined> { |
| 114 | + const cdnUrl = CdnProvider.cdnUrlFor(`assets/${iconUrl}`); |
| 115 | + if (!cdnUrl) { |
| 116 | + return undefined; |
| 117 | + } |
| 118 | + |
| 119 | + const controller = new AbortController(); |
| 120 | + const timer = setTimeout(() => controller.abort(), CDN_FETCH_TIMEOUT_MS); |
| 121 | + let res: Response; |
| 122 | + try { |
| 123 | + res = await fetch(cdnUrl, { signal: controller.signal }); |
| 124 | + } catch { |
| 125 | + GameImageProvider.recordCdnFailure(); |
| 126 | + return undefined; |
| 127 | + } finally { |
| 128 | + clearTimeout(timer); |
| 129 | + } |
| 130 | + |
| 131 | + if (res.status === 404) { |
| 132 | + GameImageProvider.cdnConsecutiveFailures = 0; |
| 133 | + return undefined; |
| 134 | + } |
| 135 | + if (!res.ok) { |
| 136 | + GameImageProvider.recordCdnFailure(); |
| 137 | + return undefined; |
| 138 | + } |
| 139 | + |
| 140 | + try { |
| 141 | + const arrayBuffer = await res.arrayBuffer(); |
| 142 | + await FsProvider.instance.mkdirs(path.dirname(cachePath)); |
| 143 | + await FsProvider.instance.writeFile(cachePath, buffer.from(arrayBuffer)); |
| 144 | + GameImageProvider.cdnConsecutiveFailures = 0; |
| 145 | + return GameImageProvider.fileUrlOf(cachePath); |
| 146 | + } catch { |
| 147 | + GameImageProvider.recordCdnFailure(); |
| 148 | + return undefined; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private static recordCdnFailure(): void { |
| 153 | + GameImageProvider.cdnConsecutiveFailures += 1; |
| 154 | + if (GameImageProvider.cdnConsecutiveFailures >= CDN_BREAKER_THRESHOLD) { |
| 155 | + GameImageProvider.cdnBreakerTripped = true; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments