fix: ensure typescript routes are hit most to least specific
This commit is contained in:
parent
4f68a64a88
commit
1928bfcb5e
6 changed files with 35 additions and 12 deletions
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "@andyburke/serverus",
|
"name": "@andyburke/serverus",
|
||||||
"description": "A flexible HTTP server for mixed content. Throw static files, markdown, Typescript and (hopefully, eventually) more into a directory and serverus can serve it up a bit more like old-school CGI.",
|
"description": "A flexible HTTP server for mixed content. Throw static files, markdown, Typescript and (hopefully, eventually) more into a directory and serverus can serve it up a bit more like old-school CGI.",
|
||||||
"version": "0.4.0",
|
"version": "0.5.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./serverus.ts",
|
".": "./serverus.ts",
|
||||||
|
@ -14,7 +14,7 @@
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"lint": "deno lint",
|
"lint": "deno lint",
|
||||||
"fmt": "deno fmt",
|
"fmt": "deno fmt",
|
||||||
"test": "DENO_ENV=test DATA_STORAGE_ROOT=./tests/data/$(date --iso-8601=seconds) deno test --allow-env --allow-read --allow-write --allow-net --trace-leaks --fail-fast ./tests/",
|
"test": "DENO_ENV=test DATA_STORAGE_ROOT=./tests/data/$(date --iso-8601=seconds) SERVERUS_TYPESCRIPT_IMPORT_LOGGING=1 deno test --allow-env --allow-read --allow-write --allow-net --trace-leaks --fail-fast ./tests/",
|
||||||
"build": "deno compile --allow-env --allow-read --allow-write --allow-net --include handlers ./serverus.ts -o serverus",
|
"build": "deno compile --allow-env --allow-read --allow-write --allow-net --include handlers ./serverus.ts -o serverus",
|
||||||
"serverus": "deno --allow-env --allow-read --allow-write --allow-net ./serverus.ts"
|
"serverus": "deno --allow-env --allow-read --allow-write --allow-net ./serverus.ts"
|
||||||
},
|
},
|
||||||
|
|
1
deno.lock
generated
1
deno.lock
generated
|
@ -17,6 +17,7 @@
|
||||||
"jsr:@std/fs@^1.0.17": "1.0.18",
|
"jsr:@std/fs@^1.0.17": "1.0.18",
|
||||||
"jsr:@std/html@^1.0.4": "1.0.4",
|
"jsr:@std/html@^1.0.4": "1.0.4",
|
||||||
"jsr:@std/http@^1.0.13": "1.0.17",
|
"jsr:@std/http@^1.0.13": "1.0.17",
|
||||||
|
"jsr:@std/internal@*": "1.0.8",
|
||||||
"jsr:@std/internal@^1.0.6": "1.0.8",
|
"jsr:@std/internal@^1.0.6": "1.0.8",
|
||||||
"jsr:@std/internal@^1.0.8": "1.0.8",
|
"jsr:@std/internal@^1.0.8": "1.0.8",
|
||||||
"jsr:@std/media-types@^1.1.0": "1.1.0",
|
"jsr:@std/media-types@^1.1.0": "1.1.0",
|
||||||
|
|
|
@ -96,7 +96,7 @@ export default async function handle_typescript(request: Request): Promise<Respo
|
||||||
const pattern = new URLPattern({ pathname: import_info.route_path });
|
const pattern = new URLPattern({ pathname: import_info.route_path });
|
||||||
|
|
||||||
if (Deno.env.get('SERVERUS_TYPESCRIPT_IMPORT_LOGGING')) {
|
if (Deno.env.get('SERVERUS_TYPESCRIPT_IMPORT_LOGGING')) {
|
||||||
console.log(`imported: ${import_info.import_path}`);
|
console.log(`${import_info.route_path} : imported: ${import_info.import_path}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
routes.set(pattern, module);
|
routes.set(pattern, module);
|
||||||
|
@ -115,7 +115,7 @@ export default async function handle_typescript(request: Request): Promise<Respo
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const [pattern, handler_module] of routes) {
|
for (const [pattern, handler_module] of routes) {
|
||||||
const match = pattern.exec(request.url);
|
const match = pattern.exec(request.url.replace(/\/$/, ''));
|
||||||
if (match) {
|
if (match) {
|
||||||
const method = request.method as keyof ROUTE_HANDLER;
|
const method = request.method as keyof ROUTE_HANDLER;
|
||||||
const method_handler: ROUTE_HANDLER_METHOD = (handler_module[method] ?? handler_module.default) as ROUTE_HANDLER_METHOD;
|
const method_handler: ROUTE_HANDLER_METHOD = (handler_module[method] ?? handler_module.default) as ROUTE_HANDLER_METHOD;
|
||||||
|
|
|
@ -16,15 +16,28 @@ Deno.test({
|
||||||
Deno.env.set('SERVERUS_ROOT', './tests/www');
|
Deno.env.set('SERVERUS_ROOT', './tests/www');
|
||||||
test_server_info = await get_ephemeral_listen_server();
|
test_server_info = await get_ephemeral_listen_server();
|
||||||
|
|
||||||
const response = await fetch(`http://${test_server_info.hostname}:${test_server_info.port}/echo/hi`, {
|
const echoes: Record<string, string> = {
|
||||||
method: 'GET'
|
'': 'echo! .... echo. ................... echo?',
|
||||||
});
|
hi: 'hello',
|
||||||
|
yo: 'yo',
|
||||||
|
'whoa there': 'whoa there'
|
||||||
|
};
|
||||||
|
|
||||||
const body = await response.text();
|
for await (const key of Object.keys(echoes)) {
|
||||||
|
const response = await fetch(`http://${test_server_info.hostname}:${test_server_info.port}/echo/${key}`, {
|
||||||
|
method: 'GET'
|
||||||
|
});
|
||||||
|
|
||||||
asserts.assert(response.ok);
|
const body = await response.text();
|
||||||
asserts.assert(body);
|
|
||||||
asserts.assertEquals(body, 'hello');
|
asserts.assert(response.ok);
|
||||||
|
asserts.assert(body);
|
||||||
|
console.dir({
|
||||||
|
body,
|
||||||
|
key
|
||||||
|
});
|
||||||
|
asserts.assertEquals(body, echoes[key]);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
Deno.env.delete('SERVERUS_ROOT');
|
Deno.env.delete('SERVERUS_ROOT');
|
||||||
if (test_server_info) {
|
if (test_server_info) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
export function GET(_req: Request, meta: Record<string, any>): Response {
|
export function GET(_req: Request, meta: Record<string, any>): Response {
|
||||||
return new Response(meta.params.input ?? '', {
|
const input = decodeURIComponent(meta.params.input ?? '');
|
||||||
|
return new Response(input, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'text/plain'
|
'Content-Type': 'text/plain'
|
||||||
|
|
8
tests/www/echo/index.ts
Normal file
8
tests/www/echo/index.ts
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
export function GET(_req: Request, _meta: Record<string, any>): Response {
|
||||||
|
return new Response('echo! .... echo. ................... echo?', {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'text/plain'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue