Skip to content

Commit

Permalink
added html inference
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Nov 26, 2022
1 parent 3883a54 commit dbca557
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
18 changes: 14 additions & 4 deletions srvd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const http = require("http");
const fs = require("node:fs");
const http = require("node:http");
const path = require("node:path");

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

const serve = serveStatic(root, { acceptRanges });
const _serve = serveStatic(root, { acceptRanges });

let last = Date.now();
let server;
Expand Down Expand Up @@ -73,7 +75,14 @@ function serve(
count++;
last = Date.now();
if (debug) log(`[srvd] received a "${req.method}" request for "${req.url}"`);
serve(req, res, finalhandler(req, res));

let filepath = path.join(root, req.url);
if (!fs.existsSync(filepath) && fs.existsSync(filepath + ".html")) {
if (debug) console.log(`[srvd] inferred ${req.url}.html`);
req.url += ".html";
}

_serve(req, res, finalhandler(req, res));
if (count >= max) {
if (debug) log("[srvd] reached maximum number of requests " + max);
server.close();
Expand All @@ -86,6 +95,7 @@ function serve(

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

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

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

serve({
debug: !!str.match(/-?-debug((=|== )(true|True|TRUE))?/),
Expand Down
14 changes: 14 additions & 0 deletions test/cjs/test.infer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const test = require("flug");
const utils = require("./utils.js");
const srvd = require("../../srvd");

test("testing inference", async ({ eq }) => {
const { port } = srvd.serve({ debug: true, max: 1 });
const result = await utils.get({
hostname: "localhost",
port,
path: "/test/data/sample",
method: "GET"
});
eq(result, "<!DOCTYPE html>\n<html>\n <body>Hello, World!</body>\n</html>");
});
17 changes: 3 additions & 14 deletions test/cjs/test.max.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
const http = require("http");
const test = require("flug");
const utils = require("./utils.js");
const srvd = require("../../srvd");

const get = options =>
new Promise((resolve, reject) => {
let data = "";
const req = http.request(options, res => {
res.on("data", chunk => (data += chunk));
res.on("end", () => resolve(data));
});
req.on("error", reject);
req.end();
});

test("max requests", async ({ eq }) => {
const max_requests = 5;
const { port } = srvd.serve({
Expand All @@ -23,7 +12,7 @@ test("max requests", async ({ eq }) => {
});

for (let i = 0; i < max_requests; i++) {
const data = await get({
const data = await utils.get({
hostname: "localhost",
port,
path: "/package.json",
Expand All @@ -36,7 +25,7 @@ test("max requests", async ({ eq }) => {
for (let i = 0; i < max_requests; i++) {
let message;
try {
await get({
await utils.get({
hostname: "localhost",
port,
path: "/package.json",
Expand Down
14 changes: 14 additions & 0 deletions test/cjs/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const http = require("http");

const get = options =>
new Promise((resolve, reject) => {
let data = "";
const req = http.request(options, res => {
res.on("data", chunk => (data += chunk));
res.on("end", () => resolve(data));
});
req.on("error", reject);
req.end();
});

module.exports = { get };
4 changes: 4 additions & 0 deletions test/data/sample.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!DOCTYPE html>
<html>
<body>Hello, World!</body>
</html>

0 comments on commit dbca557

Please sign in to comment.