From 12e52ec704763387a069fb6814b1a247e035f669 Mon Sep 17 00:00:00 2001 From: Kenny Daniel Date: Thu, 6 Jun 2024 14:05:13 -0700 Subject: [PATCH] Fix serving from npx hyperparam --- src/serve.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/serve.js b/src/serve.js index 6c805f8..f52c4d7 100644 --- a/src/serve.js +++ b/src/serve.js @@ -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 || '' @@ -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} */ -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' }