rsnext/packages/next/lib/file-exists.ts
JJ Kasper 598b1ef11b
Fix long URLs causing 400s with dynamic routes/rewrites (#26221)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2021-06-17 10:59:46 +02:00

24 lines
599 B
TypeScript

import { constants, promises } from 'fs'
export async function fileExists(
fileName: string,
type?: 'file' | 'directory'
): Promise<boolean> {
try {
if (type === 'file') {
const stats = await promises.stat(fileName)
return stats.isFile()
} else if (type === 'directory') {
const stats = await promises.stat(fileName)
return stats.isDirectory()
} else {
await promises.access(fileName, constants.F_OK)
}
return true
} catch (err) {
if (err.code === 'ENOENT' || err.code === 'ENAMETOOLONG') {
return false
}
throw err
}
}