Skip to content

Commit dbca557

Browse files
committed
added html inference
1 parent 3883a54 commit dbca557

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

srvd.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const http = require("http");
1+
const fs = require("node:fs");
2+
const http = require("node:http");
3+
const path = require("node:path");
24

35
const serveStatic = require("serve-static");
46
const finalhandler = require("finalhandler");
@@ -30,7 +32,7 @@ function serve(
3032
if (debug) log(`[srvd] waiting ${wait} seconds for requests`);
3133
const wait_ms = wait * 1000;
3234

33-
const serve = serveStatic(root, { acceptRanges });
35+
const _serve = serveStatic(root, { acceptRanges });
3436

3537
let last = Date.now();
3638
let server;
@@ -73,7 +75,14 @@ function serve(
7375
count++;
7476
last = Date.now();
7577
if (debug) log(`[srvd] received a "${req.method}" request for "${req.url}"`);
76-
serve(req, res, finalhandler(req, res));
78+
79+
let filepath = path.join(root, req.url);
80+
if (!fs.existsSync(filepath) && fs.existsSync(filepath + ".html")) {
81+
if (debug) console.log(`[srvd] inferred ${req.url}.html`);
82+
req.url += ".html";
83+
}
84+
85+
_serve(req, res, finalhandler(req, res));
7786
if (count >= max) {
7887
if (debug) log("[srvd] reached maximum number of requests " + max);
7988
server.close();
@@ -86,6 +95,7 @@ function serve(
8695

8796
server.listen(port);
8897
if (debug) log("[srvd] serving on port " + port);
98+
if (debug) log("[srvd] visit at http://localhost:" + port);
8999

90100
checkWaitTimeout = setInterval(checkWait, 500);
91101
checkForCloseTimeout = setInterval(checkForCloseRequest, 500);
@@ -110,7 +120,7 @@ if (require.main === module) {
110120
const str = args.join(" ");
111121

112122
let wait = Array.prototype.slice.call(str.match(/-?-wait(?:=|== )(inf(inity)?|\d+)/i) || [], 1)[0];
113-
if (wait?.startsWith("inf")) wait = Infinity;
123+
if (wait?.toLowerCase().startsWith("inf")) wait = Infinity;
114124

115125
serve({
116126
debug: !!str.match(/-?-debug((=|== )(true|True|TRUE))?/),

test/cjs/test.infer.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const test = require("flug");
2+
const utils = require("./utils.js");
3+
const srvd = require("../../srvd");
4+
5+
test("testing inference", async ({ eq }) => {
6+
const { port } = srvd.serve({ debug: true, max: 1 });
7+
const result = await utils.get({
8+
hostname: "localhost",
9+
port,
10+
path: "/test/data/sample",
11+
method: "GET"
12+
});
13+
eq(result, "<!DOCTYPE html>\n<html>\n <body>Hello, World!</body>\n</html>");
14+
});

test/cjs/test.max.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
const http = require("http");
21
const test = require("flug");
2+
const utils = require("./utils.js");
33
const srvd = require("../../srvd");
44

5-
const get = options =>
6-
new Promise((resolve, reject) => {
7-
let data = "";
8-
const req = http.request(options, res => {
9-
res.on("data", chunk => (data += chunk));
10-
res.on("end", () => resolve(data));
11-
});
12-
req.on("error", reject);
13-
req.end();
14-
});
15-
165
test("max requests", async ({ eq }) => {
176
const max_requests = 5;
187
const { port } = srvd.serve({
@@ -23,7 +12,7 @@ test("max requests", async ({ eq }) => {
2312
});
2413

2514
for (let i = 0; i < max_requests; i++) {
26-
const data = await get({
15+
const data = await utils.get({
2716
hostname: "localhost",
2817
port,
2918
path: "/package.json",
@@ -36,7 +25,7 @@ test("max requests", async ({ eq }) => {
3625
for (let i = 0; i < max_requests; i++) {
3726
let message;
3827
try {
39-
await get({
28+
await utils.get({
4029
hostname: "localhost",
4130
port,
4231
path: "/package.json",

test/cjs/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const http = require("http");
2+
3+
const get = options =>
4+
new Promise((resolve, reject) => {
5+
let data = "";
6+
const req = http.request(options, res => {
7+
res.on("data", chunk => (data += chunk));
8+
res.on("end", () => resolve(data));
9+
});
10+
req.on("error", reject);
11+
req.end();
12+
});
13+
14+
module.exports = { get };

test/data/sample.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>Hello, World!</body>
4+
</html>

0 commit comments

Comments
 (0)