Skip to content

Commit

Permalink
feat: add multiple cdn
Browse files Browse the repository at this point in the history
  • Loading branch information
arianrhodsandlot committed Feb 27, 2024
1 parent 399ab74 commit 078dc81
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/core/classes/emulator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import delay from 'delay'
import { Nostalgist } from 'nostalgist'
import { cdnHost, vendorsInfo } from '../constants/dependencies'
import { getCDNHost, vendorsInfo } from '../constants/dependencies'
import { platformCoreMap } from '../constants/platforms'
import { defaultRetroarchCoresConfig, getRetroarchConfig } from '../helpers/retroarch'
import { type Rom } from './rom'
Expand Down Expand Up @@ -113,11 +113,11 @@ export class Emulator {
Object.assign(launchOptions, {
resolveCoreJs(core: string) {
const corePath = `dist/cores/${core}_libretro.js`
return `${cdnHost}/npm/${vendorsInfo.name}@${vendorsInfo.version}/${corePath}`
return `${getCDNHost()}/npm/${vendorsInfo.name}@${vendorsInfo.version}/${corePath}`
},
resolveCoreWasm(core: string) {
const corePath = `dist/cores/${core}_libretro.wasm`
return `${cdnHost}/npm/${vendorsInfo.name}@${vendorsInfo.version}/${corePath}`
return `${getCDNHost()}/npm/${vendorsInfo.name}@${vendorsInfo.version}/${corePath}`
},
})
}
Expand Down
5 changes: 2 additions & 3 deletions src/core/classes/file-system-providers/demo-provider.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { noop } from 'lodash-es'
import { cdnHost } from '../../constants/dependencies'
import { getCDNHost } from '../../constants/dependencies'
import { retrobrewsGames } from '../../constants/retrobrews'
import { http } from '../../helpers/http'
import { FileAccessor } from './file-accessor'
import { type FileSystemProvider } from './file-system-provider'

const cdnBaseUrl = `${cdnHost}/gh`

export class DemoProvider implements FileSystemProvider {
static getSingleton() {
return new DemoProvider()
Expand Down Expand Up @@ -36,6 +34,7 @@ export class DemoProvider implements FileSystemProvider {
}

const encodedFile = encodeURIComponent(file)
const cdnBaseUrl = `${getCDNHost()}/gh`
const url = `${cdnBaseUrl}/${romRepo}@master/${encodedFile}`
return await http(url).blob()
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/classes/games-database.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { camelCase, isEqual, pick, sortBy } from 'lodash-es'
import { parse } from 'path-browserify'
import { cdnHost, fbneoInfo, libretroDatabaseInfo } from '../constants/dependencies'
import { fbneoInfo, getCDNHost, libretroDatabaseInfo } from '../constants/dependencies'
import { platformFullNameMap } from '../constants/platforms'
import { blobToBuffer } from '../helpers/file'
import { http } from '../helpers/http'
import { parseGoodCode } from '../helpers/misc'
import { Libretrodb } from './libretrodb/libretrodb'
import { type Entry } from './libretrodb/types'

const arcadeGameListUrl = `${cdnHost}/gh/${fbneoInfo.name}@${fbneoInfo.version}/gamelist.txt`
const arcadeGameListUrl = `${getCDNHost()}/gh/${fbneoInfo.name}@${fbneoInfo.version}/gamelist.txt`

function getDbUrl(platformFullName: string) {
const dbPath = `rdb/${platformFullName}.rdb`
return `${cdnHost}/gh/${libretroDatabaseInfo.name}@${libretroDatabaseInfo.version}/${dbPath}`
return `${getCDNHost()}/gh/${libretroDatabaseInfo.name}@${libretroDatabaseInfo.version}/${dbPath}`
}

function normalizeGameName(originalName: string) {
Expand Down
35 changes: 33 additions & 2 deletions src/core/constants/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const cdnHost = 'https://cdn.jsdelivr.net'

export const vendorsInfo = {
name: 'retro-assembly-vendors',
version: '1.17.0-20240225183742',
Expand All @@ -14,3 +12,36 @@ export const fbneoInfo = {
name: 'libretro/FBNeo',
version: '4a555e0',
}

const cdnHosts = [
'https://cdn.jsdelivr.net',
'https://gcore.jsdelivr.net',
'https://fastly.jsdelivr.net',
'https://testingcf.jsdelivr.net',
'https://jsd.cdn.zzko.cn',
'https://jsd.lihaoyu.cn',
]

let [selectedCDNHost] = cdnHosts
async function testCDNPerformance() {
const testFile = 'Nintendo - Nintendo Entertainment System.rdb'
const path = `/gh/${libretroDatabaseInfo.name}@${libretroDatabaseInfo.version}/rdb/${encodeURIComponent(testFile)}`
const controller = new AbortController()
const init = { method: 'HEAD', signal: controller.signal }
const requests = cdnHosts.map(async (host) => {
const input = `${host}${path}`
try {
await fetch(input, init)
selectedCDNHost = host
} catch {}
controller.abort()
})

await Promise.all(requests)
}

testCDNPerformance()

export function getCDNHost() {
return selectedCDNHost
}
4 changes: 2 additions & 2 deletions src/core/helpers/misc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isThisYear, isToday, lightFormat } from 'date-fns'
import { parse } from 'goodcodes-parser'
import { capitalize } from 'lodash-es'
import { cdnHost } from '../constants/dependencies'
import { getCDNHost } from '../constants/dependencies'
import { platformFullNameMap } from '../constants/platforms'

function encodeRFC3986URIComponent(str) {
Expand All @@ -22,7 +22,7 @@ export function getCover({ platform, name, type = 'boxart' }) {
const pathPrefix = `gh/libretro-thumbnails/${normalizedPlatformFullName}@master`
const normalizedFileName = name.replaceAll(/&|\*|\/|:|`|<|>|\?|\\|\|"/g, '_')
const encode = encodeRFC3986URIComponent
return `${cdnHost}/${pathPrefix}/${encode(typeUrlPart)}/${encode(normalizedFileName)}.png`
return `${getCDNHost()}/${pathPrefix}/${encode(typeUrlPart)}/${encode(normalizedFileName)}.png`
}

export function parseGoodCode(name: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useMemo, useRef } from 'react'
import { useAsync } from 'react-use'
import { type Rom, isUsingDemo } from '../../../../../../../core'
import { cdnHost } from '../../../../../../../core/constants/dependencies'
import { getCDNHost } from '../../../../../../../core/constants/dependencies'
import { platformContentImageMap } from '../../../../../../lib/constants'
import { GameEntryImage } from './game-entry-image'
import { loadImageWithLimit } from './utils'
Expand All @@ -27,7 +27,7 @@ export function GameEntryContent({ rom }: { rom: Rom }) {
platform = 'md'
}
const repo = `${platform}-games`
const cover = `${cdnHost}/gh/retrobrews/${repo}@master/${fileAccessor.basename}.png`
const cover = `${getCDNHost()}/gh/retrobrews/${repo}@master/${fileAccessor.basename}.png`
await loadImageWithLimit(cover, abortController.signal)
return cover
}
Expand Down

0 comments on commit 078dc81

Please sign in to comment.