Add backend jobs system with built-in tasks and TOML-based configuration parsing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9647ac8c50
commit
1f6d41ab06
27 files changed
+1590
-25
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';
|
||||
import type { Duplex } from 'node:stream';
|
||||
import type { Site } from '@homepage/indexer';
|
||||
import { TERMINAL_PATH } from '@homepage/protocol/terminal';
|
||||
import { WebSocketServer, type WebSocket } from 'ws';
|
||||
import type { Asset } from './assets.ts';
|
||||
@@ -11,6 +12,8 @@ export type BackendOptions = {
|
||||
assets?: Map<string, Asset>;
|
||||
/** Resolves to false while the spawner is unreachable. */
|
||||
healthy?: () => Promise<boolean>;
|
||||
/** The static site of the content, rebuilt by the index job. */
|
||||
site?: () => Site | undefined;
|
||||
};
|
||||
|
||||
const MAX_FRAME_BYTES = 64 * 1024;
|
||||
@@ -39,6 +42,13 @@ function send(response: ServerResponse, status: number, type: string, body: stri
|
||||
response.end(head ? undefined : body);
|
||||
}
|
||||
|
||||
/** Decoded path without a trailing slash, or undefined for malformed percent encoding. */
|
||||
function sitePath(path: string): string | undefined
|
||||
{
|
||||
try { return decodeURIComponent(path).replace(/(.)\/+$/, '$1'); }
|
||||
catch { return undefined; }
|
||||
}
|
||||
|
||||
function reject(socket: Duplex, status: number, reason: string): void
|
||||
{
|
||||
socket.end(`HTTP/1.1 ${status} ${reason}\r\nConnection: close\r\nContent-Length: 0\r\n\r\n`);
|
||||
@@ -56,6 +66,7 @@ export function createBackend(config: BackendConfig, hub: SessionHub, options: B
|
||||
{
|
||||
const assets = options.assets ?? new Map<string, Asset>();
|
||||
const healthy = options.healthy ?? (async () => true);
|
||||
const site = options.site ?? (() => undefined);
|
||||
|
||||
const server = createServer(async (request, response) =>
|
||||
{
|
||||
@@ -76,6 +87,22 @@ export function createBackend(config: BackendConfig, hub: SessionHub, options: B
|
||||
return send(response, 200, asset.type, asset.body, { 'Cache-Control': cache }, head);
|
||||
}
|
||||
|
||||
const current = site();
|
||||
const page = current && current.pages.get(sitePath(path) ?? '');
|
||||
if (current && page)
|
||||
{
|
||||
// Every page changes together with the content, so the content hash is a valid ETag for all of them.
|
||||
const etag = `"${current.hash}"`;
|
||||
const headers = { 'Cache-Control': 'no-cache', ETag: etag, ...page.headers };
|
||||
|
||||
if (request.headers['if-none-match'] === etag)
|
||||
{
|
||||
response.writeHead(304, { ...SECURITY_HEADERS, ...headers }).end();
|
||||
return;
|
||||
}
|
||||
return send(response, 200, page.type, page.body, headers, head);
|
||||
}
|
||||
|
||||
send(response, 404, 'text/plain', 'not found', {}, head);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user