diff --git a/client/sw/register.spec.js b/client/sw/register.spec.js index 5cb98049..47420ba7 100644 --- a/client/sw/register.spec.js +++ b/client/sw/register.spec.js @@ -84,7 +84,7 @@ test('sw: register: registerSW', async (t, {location, navigator}) => { const {register} = navigator.serviceWorker; await registerSW('/hello'); - t.calledWith(register, ['/hello/sw.mjs'], 'should call register'); + t.calledWith(register, ['/hello/sw.js'], 'should call register'); t.end(); }); @@ -98,6 +98,6 @@ test('sw: register: unregisterSW', async (t, {location, navigator}) => { await unregisterSW('/hello'); - t.calledWith(register, ['/hello/sw.mjs'], 'should call register'); + t.calledWith(register, ['/hello/sw.js'], 'should call register'); t.end(); }); diff --git a/server/cloudcmd.js b/server/cloudcmd.js index 996ac5d4..a135b14e 100644 --- a/server/cloudcmd.js +++ b/server/cloudcmd.js @@ -18,7 +18,7 @@ import * as cloudfunc from '#common/cloudfunc'; import authentication from './auth.js'; import {createConfig, configPath} from './config.js'; import modulas from './modulas.js'; -import userMenu from './user-menu.js'; +import {userMenu} from './user-menu.js'; import rest from './rest/index.js'; import route from './route.js'; import * as validate from './validate.js'; diff --git a/server/user-menu.js b/server/user-menu.js index 4eaa3a8c..4c56ad9f 100644 --- a/server/user-menu.js +++ b/server/user-menu.js @@ -1,6 +1,8 @@ import {homedir} from 'node:os'; import {readFile as _readFile} from 'node:fs/promises'; import {join} from 'node:path'; +import {readFileSync} from 'node:fs'; +// warm up worker cache import montag from 'montag'; import {tryToCatch} from 'try-to-catch'; import currify from 'currify'; @@ -10,9 +12,8 @@ import {putout, codeframe} from 'putout'; transpile(''); const PREFIX = '/api/v1/user-menu'; -const DEFAULT_MENU_PATH = new URL('../static/user-menu.js', import.meta.url).pathname; -export default currify(async ({menuName, readFile = _readFile}, req, res, next) => { +export const userMenu = currify(async ({menuName, readFile = _readFile}, req, res, next) => { if (!req.url.startsWith(PREFIX)) return next(); @@ -34,7 +35,7 @@ async function onGET({req, res, menuName, readFile}) { const url = req.url.replace(PREFIX, ''); if (url === '/default') - return sendDefaultMenu(res); + return await sendDefaultMenu(res); const {findUp} = await import('find-up'); @@ -57,7 +58,7 @@ async function onGET({req, res, menuName, readFile}) { .send(e.message); if (e) - return sendDefaultMenu(res); + return await sendDefaultMenu(res); const [parseError, result] = await transpile(source); @@ -85,10 +86,12 @@ function getError(error, source) { `; } -function sendDefaultMenu(res) { - res.sendFile(DEFAULT_MENU_PATH, { - cacheControl: false, - }); +async function sendDefaultMenu(res) { + const menu = await getDefaultMenu(); + + res + .type('js') + .send(menu); } async function transpile(source) { @@ -102,3 +105,11 @@ async function transpile(source) { ], }); } + +async function getDefaultMenu() { + const DEFAULT_MENU_PATH = new URL('../static/user-menu.js', import.meta.url).pathname; + const menu = readFileSync(DEFAULT_MENU_PATH, 'utf8'); + const [, result] = await transpile(menu); + + return result.code; +} diff --git a/server/user-menu.spec.js b/server/user-menu.spec.js index 976eebd9..b88cb07a 100644 --- a/server/user-menu.spec.js +++ b/server/user-menu.spec.js @@ -4,7 +4,7 @@ import {readFileSync} from 'node:fs'; import {test, stub} from 'supertape'; import serveOnce from 'serve-once'; import {putout} from 'putout'; -import userMenu from './user-menu.js'; +import {userMenu} from './user-menu.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); @@ -54,6 +54,21 @@ test('cloudcmd: user menu: io.mv', async (t) => { t.end(); }); +test('cloudcmd: user menu: default menu', async (t) => { + const options = { + menuName: '111.cloudcmd.menu.js', + }; + + const {request} = serveOnce(userMenu); + + const {body} = await request.get(`/api/v1/user-menu?dir=abcd`, { + options, + }); + + t.match(body, 'module.exports'); + t.end(); +}); + test('cloudcmd: user menu: io.cp', async (t) => { const readFile = stub().returns(fixtureCopy); const options = {