Skip to content

Commit

Permalink
fix: fix the error when launching with a malformed link
Browse files Browse the repository at this point in the history
  • Loading branch information
arianrhodsandlot committed Mar 10, 2024
1 parent 531c959 commit c6a5703
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/nostalgist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@ import type {
NostalgistResolveFileFunction,
} from './types/nostalgist-options'
import type { RetroArchCommand } from './types/retroarch-command'
import { merge } from './utils'
import { merge, urlBaseName } from './utils'
import { vendors } from './vendors'

function baseName(url: string) {
let name = url.split('/').pop() || ''
name = decodeURIComponent(name)
return name
}

export class Nostalgist {
static Nostalgist = Nostalgist
static vendors = vendors
Expand Down Expand Up @@ -579,14 +573,14 @@ export class Nostalgist {
} else if (file instanceof Blob) {
fileContent = file
} else if (typeof file === 'string') {
fileName = baseName(file)
fileName = urlBaseName(file)
const resolvedRom = resolveFunction ? await resolveFunction(file, this.options) : file
if (!resolvedRom) {
throw new Error('file is invalid')
} else if (resolvedRom instanceof Blob) {
fileContent = resolvedRom
} else if (typeof resolvedRom === 'string') {
fileName = baseName(resolvedRom)
fileName = urlBaseName(resolvedRom)
const response = await this.fetch(resolvedRom)
fileContent = await response.blob()
}
Expand All @@ -603,6 +597,7 @@ export class Nostalgist {
throw new TypeError('file is invalid')
}

fileName = fileName.replaceAll(/["%*/:<>?\\|]/g, '-')
fileName ||= 'rom.bin'

return { fileName, fileContent }
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const { Buffer } = BFSRequire('buffer')
export const path = BFSRequire('path')
export const { basename, extname, dirname, join, relative } = path

export function urlBaseName(url: string) {
let pathname = url
try {
pathname = new URL(url).pathname
} catch {}
return basename(pathname)
}

export function isAbsoluteUrl(string: string) {
if (!string) {
return false
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/nostalgist.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,11 @@ describe.concurrent('nostalgist', () => {
const options = nostalgist.getOptions()
expect(options.core).toStrictEqual('nestopia')
})

test('Nostalgist.launch with a malformed url', async () => {
const nostalgist = await Nostalgist.nes({ rom: 'http://example.com/a%b%c.nes?xxx=1' })

const emulatorOptions = nostalgist.getEmulatorOptions()
expect(emulatorOptions.rom[0].fileName).toStrictEqual('a-b-c.nes')
})
})

0 comments on commit c6a5703

Please sign in to comment.