Skip to content

Commit

Permalink
Fix serving from npx hyperparam
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Jun 6, 2024
1 parent fbbfe8a commit 12e52ec
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,21 @@ function handleRequest(req) {
const parsedUrl = url.parse(req.url, true)
const pathname = parsedUrl.pathname || ''

// get location of hyperparam assets
const hyperparamPath = import.meta.url
.replace('file://', '')
.replace('/src/serve.js', '')

if (pathname === '/' || pathname === '/files') {
// redirect to /files
return { status: 301, content: '/files/' }
} else if (pathname.startsWith('/files/')) {
// serve index.html
return handleStatic('/public/index.html')
console.log('serving index.html', `${hyperparamPath}/public/index.html`)
return handleStatic(`${hyperparamPath}/public/index.html`)
} else if (pathname.startsWith('/public/')) {
// serve static files
return handleStatic(pathname)
return handleStatic(`${hyperparamPath}${pathname}`)
} else if (pathname === '/api/store/list') {
// serve file list
const prefix = parsedUrl.query.prefix || ''
Expand All @@ -75,12 +81,11 @@ function handleRequest(req) {

/**
* Serve static file from the serve directory
* @param {string} pathname
* @param {string} filePath
* @param {string} [range]
* @returns {Promise<ServeResult>}
*/
async function handleStatic(pathname, range) {
const filePath = path.join(process.cwd(), pathname)
async function handleStatic(filePath, range) {
const stats = await fs.stat(filePath).catch(() => undefined)
if (!stats || !stats.isFile()) {
return { status: 404, content: 'not found' }
Expand Down

0 comments on commit 12e52ec

Please sign in to comment.