feature: support .spa files for Single Page Apps
This commit is contained in:
parent
604090d8b8
commit
9fc91f4cf4
11 changed files with 329 additions and 8 deletions
|
|
@ -18,7 +18,7 @@ const replaceAsync = async (str: string, regex: RegExp, asyncFn: (match: any, ..
|
|||
};
|
||||
|
||||
const SSI_REGEX: RegExp = /\<\!--\s+#include\s+(?<type>.*?)\s*=\s*["'](?<location>.*?)['"]\s+-->/mg;
|
||||
async function load_html_with_ssi(html_file: string): Promise<string> {
|
||||
export async function load_html_with_ssi(html_file: string): Promise<string> {
|
||||
const html_file_content: string = await Deno.readTextFile(html_file);
|
||||
const processed: string = await replaceAsync(
|
||||
html_file_content,
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@ import * as html from './html.ts';
|
|||
import * as markdown from './markdown.ts';
|
||||
import * as static_files from './static.ts';
|
||||
import * as typescript from './typescript.ts';
|
||||
import * as spa from './spa.ts';
|
||||
|
||||
export default {
|
||||
html,
|
||||
markdown,
|
||||
typescript,
|
||||
static: static_files
|
||||
static: static_files,
|
||||
spa
|
||||
};
|
||||
|
|
|
|||
193
handlers/spa.ts
Normal file
193
handlers/spa.ts
Normal file
|
|
@ -0,0 +1,193 @@
|
|||
/**
|
||||
* Handles requests for route beneath a directory with a .spa file dropped in it.
|
||||
* @module
|
||||
*/
|
||||
|
||||
import * as path from '@std/path';
|
||||
import { PRECHECK, SERVER } from '../server.ts';
|
||||
import { getCookies } from '@std/http/cookie';
|
||||
import { load_html_with_ssi } from './html.ts';
|
||||
|
||||
async function find_spa_file_root(request_path: string): Promise<string | undefined> {
|
||||
let current_path = Deno.cwd();
|
||||
const relative_path = path.relative(current_path, request_path);
|
||||
const path_elements = relative_path.split('/').slice(0, -1);
|
||||
|
||||
let element;
|
||||
|
||||
while ((element = path_elements.shift())) {
|
||||
current_path = path.join(current_path, element);
|
||||
|
||||
try {
|
||||
const spa_file_stat = await Deno.stat(path.join(current_path, '.spa'));
|
||||
|
||||
if (spa_file_stat.isFile) {
|
||||
return current_path;
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof Deno.errors.NotFound) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export type HTTP_METHOD = 'GET' | 'PUT' | 'POST' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
||||
export type HANDLER_METHOD = (
|
||||
request: Request,
|
||||
normalized_path: string,
|
||||
server: SERVER
|
||||
) => Promise<Response | undefined> | Response | undefined;
|
||||
export const PRECHECKS: Partial<Record<HTTP_METHOD, PRECHECK[]>> = {};
|
||||
export const HANDLERS: Partial<Record<HTTP_METHOD, HANDLER_METHOD>> = {
|
||||
HEAD: async (_request: Request, normalized_path: string, _server: SERVER): Promise<Response | undefined> => {
|
||||
const spa_root = await find_spa_file_root(normalized_path);
|
||||
if (!spa_root) {
|
||||
return;
|
||||
}
|
||||
|
||||
for await (const index_filename of ['index.html', 'index.htm']) {
|
||||
try {
|
||||
const index_file_path = path.join(spa_root, index_filename);
|
||||
const index_file_stat = await Deno.stat(index_file_path);
|
||||
|
||||
if (index_file_stat.isFile) {
|
||||
return new Response('', {
|
||||
headers: {
|
||||
'Content-Type': 'text/html',
|
||||
'Content-Length': `${index_file_stat.size}`,
|
||||
'Last-Modified': `${index_file_stat.mtime ?? index_file_stat.ctime}`
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof Deno.errors.NotFound) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
GET: async (request: Request, normalized_path: string, _server: SERVER): Promise<Response | undefined> => {
|
||||
const spa_root = await find_spa_file_root(normalized_path);
|
||||
if (!spa_root) {
|
||||
return;
|
||||
}
|
||||
|
||||
for await (const index_filename of ['index.html', 'index.htm']) {
|
||||
try {
|
||||
const index_file_path = path.join(spa_root, index_filename);
|
||||
const index_file_stat = await Deno.stat(index_file_path);
|
||||
|
||||
if (index_file_stat.isFile) {
|
||||
const processed: string = await load_html_with_ssi(index_file_path);
|
||||
|
||||
const accepts = request.headers.get('accept') ?? 'text/html';
|
||||
if (!['*/*', 'text/html', 'text/plain'].includes(accepts)) {
|
||||
return new Response('unsupported accepts header for SPA: ' + accepts, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
||||
return new Response(processed, {
|
||||
headers: {
|
||||
'Content-Type': accepts === '*/*' ? 'text/html' : accepts
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof Deno.errors.NotFound) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
OPTIONS: async (request: Request, normalized_path: string): Promise<Response | undefined> => {
|
||||
const spa_root = await find_spa_file_root(normalized_path);
|
||||
if (!spa_root) {
|
||||
return;
|
||||
}
|
||||
|
||||
for await (const index_filename of ['index.html', 'index.htm']) {
|
||||
try {
|
||||
const index_file_path = path.join(spa_root, index_filename);
|
||||
const index_file_stat = await Deno.stat(index_file_path);
|
||||
|
||||
if (index_file_stat.isFile) {
|
||||
const accepts = request.headers.get('accept') ?? 'text/html';
|
||||
if (!['*/*', 'text/html', 'text/plain'].includes(accepts)) {
|
||||
return new Response('unsupported accepts header for SPA: ' + accepts, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
||||
const allowed = ['GET', 'HEAD', 'OPTIONS'];
|
||||
|
||||
return new Response('', {
|
||||
headers: {
|
||||
'Allow': allowed.sort().join(','),
|
||||
'Access-Control-Allow-Origin': Deno.env.get('SERVERUS_ACCESS_CONTROL_ALLOW_ORIGIN') ?? '*'
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof Deno.errors.NotFound) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles requests for static files.
|
||||
*
|
||||
* @param request The incoming HTTP request
|
||||
* @returns Either a response (a static file was requested and returned properly) or undefined if unhandled.
|
||||
*/
|
||||
export default async function handle_spa_files_in_path(request: Request, server: SERVER): Promise<Response | undefined> {
|
||||
const method: HTTP_METHOD = request.method.toUpperCase() as HTTP_METHOD;
|
||||
const handler: HANDLER_METHOD | undefined = HANDLERS[method];
|
||||
|
||||
if (!handler) {
|
||||
return;
|
||||
}
|
||||
|
||||
const url = new URL(request.url);
|
||||
const normalized_path = path.resolve(path.normalize(decodeURIComponent(url.pathname)).replace(/^\/+/, ''));
|
||||
|
||||
// if they're requesting something outside the working dir, just bail
|
||||
if (!normalized_path.startsWith(Deno.cwd())) {
|
||||
return;
|
||||
}
|
||||
|
||||
const prechecks: PRECHECK[] = PRECHECKS[method] ?? [];
|
||||
|
||||
const cookies: Record<string, string> = getCookies(request.headers);
|
||||
const query = Object.fromEntries(new URL(request.url).searchParams.entries());
|
||||
|
||||
const metadata = {
|
||||
cookies,
|
||||
query
|
||||
};
|
||||
|
||||
for await (const precheck of prechecks) {
|
||||
const error_response: Response | undefined = await precheck(request, metadata);
|
||||
if (error_response) {
|
||||
return error_response;
|
||||
}
|
||||
}
|
||||
|
||||
return await handler(request, normalized_path, server);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue