Skip to content

Commit

Permalink
feat: support for multiple sources for mapDir url in case if first …
Browse files Browse the repository at this point in the history
…source can't be fetched
  • Loading branch information
zardoy committed Dec 10, 2024
1 parent b02b250 commit 74cb815
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
38 changes: 28 additions & 10 deletions src/browserfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export const copyFilesAsyncWithProgress = async (pathSrc: string, pathDest: stri
return
}
if (!stat.isDirectory()) {
await fs.promises.writeFile(pathDest, await fs.promises.readFile(pathSrc))
await fs.promises.writeFile(pathDest, await fs.promises.readFile(pathSrc) as any)
console.debug('copied single file', pathSrc, pathDest)
return
}
Expand Down Expand Up @@ -464,7 +464,7 @@ export const copyFilesAsync = async (pathSrc: string, pathDest: string, fileCopi
} else {
// Copy file
try {
await fs.promises.writeFile(curPathDest, await fs.promises.readFile(curPathSrc))
await fs.promises.writeFile(curPathDest, await fs.promises.readFile(curPathSrc) as any)
console.debug('copied file', curPathSrc, curPathDest)
} catch (err) {
console.error('Error copying file', curPathSrc, curPathDest, err)
Expand All @@ -475,17 +475,35 @@ export const copyFilesAsync = async (pathSrc: string, pathDest: string, fileCopi
}))
}

export const openWorldFromHttpDir = async (fileDescriptorUrl: string/* | undefined */, baseUrl = fileDescriptorUrl.split('/').slice(0, -1).join('/')) => {
export const openWorldFromHttpDir = async (fileDescriptorUrls: string[]/* | undefined */, baseUrlParam) => {
// todo try go guess mode
let index
const file = await fetch(fileDescriptorUrl).then(async a => a.json())
if (file.baseUrl) {
baseUrl = new URL(file.baseUrl, baseUrl).toString()
index = file.index
} else {
index = file
let baseUrl
for (const url of fileDescriptorUrls) {
let response: Response | undefined
try {
setLoadingScreenStatus(`Trying to get world descriptor from ${new URL(url).host}`)
const controller = new AbortController()
setTimeout(() => {
controller.abort()
}, 3000)
// eslint-disable-next-line no-await-in-loop
response = await fetch(url, { signal: controller.signal })
} catch (err) {
console.error('Error fetching file descriptor', url, err)
}
if (!response) continue
// eslint-disable-next-line no-await-in-loop
const file = await response.json()
if (file.baseUrl) {
baseUrl = new URL(file.baseUrl, baseUrl).toString()
index = file.index
} else {
index = file
baseUrl = baseUrlParam ?? url.split('/').slice(0, -1).join('/')
}
}
if (!index) throw new Error(`The provided mapDir file is not valid descriptor file! ${fileDescriptorUrl}`)
if (!index) throw new Error(`The provided mapDir file is not valid descriptor file! ${fileDescriptorUrls.join(', ')}`)
await new Promise<void>(async resolve => {
browserfs.configure({
fs: 'MountableFileSystem',
Expand Down
4 changes: 2 additions & 2 deletions src/downloadAndOpenFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export const getFixedFilesize = (bytes: number) => {

const inner = async () => {
const qs = new URLSearchParams(window.location.search)
const mapUrlDir = qs.get('mapDir')
const mapUrlDir = qs.getAll('mapDir')
const mapUrlDirGuess = qs.get('mapDirGuess')
const mapUrlDirBaseUrl = qs.get('mapDirBaseUrl')
if (mapUrlDir) {
if (mapUrlDir.length) {
await openWorldFromHttpDir(mapUrlDir, mapUrlDirBaseUrl ?? undefined)
return true
}
Expand Down

0 comments on commit 74cb815

Please sign in to comment.