fix: more data for puter site serve event (#3251)
Maintain Release Merge PR / update-release-pr (push) Has been cancelled
Notify HeyPuter / notify (push) Has been cancelled
release-please / release-please (push) Has been cancelled

This commit is contained in:
Daniel Salazar
2026-06-10 23:27:13 -07:00
committed by GitHub
parent 40d1c998bb
commit a155464d46
2 changed files with 52 additions and 2 deletions
@@ -534,6 +534,51 @@ describe('createPuterSiteMiddleware — file serving', () => {
expect(piped!.equals(body)).toBe(true);
});
it('emits site.htmlServed with the original URL target including query string', async () => {
const owner = await makeUserWithHome();
const homePath = `/${owner.username}`;
const homeEntry = await server.stores.fsEntry.getEntryByPath(homePath);
const sub = `serveq-${Math.random().toString(36).slice(2, 8)}`;
await server.stores.subdomain.create({
userId: owner.id,
subdomain: sub,
rootDirId: homeEntry!.id,
});
await writeFile(
owner.id,
`${homePath}/index.html`,
Buffer.from('<html>hi</html>'),
'text/html',
);
const emitSpy = vi.spyOn(server.clients.event, 'emit');
try {
const mw = buildMiddleware();
const { res } = makeRes();
await mw(
makeReq({
hostname: `${sub}.site.puter.localhost`,
path: '/index.html',
originalUrl: '/index.html?brand=paypal',
}),
res,
vi.fn(),
);
const htmlServedCall = emitSpy.mock.calls.find(
(call) => call[0] === 'site.htmlServed',
);
expect(htmlServedCall?.[1]).toMatchObject({
subdomain: sub,
requestPath: '/index.html',
requestUrl: '/index.html?brand=paypal',
mime: 'text/html',
});
} finally {
emitSpy.mockRestore();
}
});
it('returns 206 status when a Range header is supplied', async () => {
const owner = await makeUserWithHome();
const homePath = `/${owner.username}`;
@@ -555,8 +555,12 @@ export const createPuterSiteMiddleware = (
res.setHeader('Content-Type', mime);
// Fire-and-forget signal for downstream extensions
if (mime === 'text/html' || mime === 'application/xhtml+xml') {
const mimeBase = mime.split(';', 1)[0].trim().toLowerCase();
if (mimeBase === 'text/html' || mimeBase === 'application/xhtml+xml') {
try {
const requestUrl = (req.originalUrl || '/').startsWith('/')
? req.originalUrl || '/'
: `/${req.originalUrl}`;
layers.clients.event.emit(
'site.htmlServed',
{
@@ -564,7 +568,8 @@ export const createPuterSiteMiddleware = (
entry,
host,
requestPath: req.path,
mime,
requestUrl,
mime: mimeBase,
},
{},
);