Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(file_server): hoistStatic and simplify operations #3

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 13 additions & 8 deletions benchmarks/file_server/deno_serve.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { FILE_SERVER_PATH, HTTP_PORT, HTTP_URL } from "../SERVER_DATA.ts";
import { getMimeType } from "https://deno.land/x/hono@v3.2.7/utils/mime.ts";
import * as path from "https://deno.land/std@0.192.0/path/mod.ts";
import { join } from "https://deno.land/std@0.192.0/path/mod.ts";

export const NAME = "Deno.serve";
export const DESCRIPTION = "";
export const VERSION = "std 0.192.0";

const body = {
status: 404,
statusText: "Not Found",
};

const base = join(Deno.cwd(), FILE_SERVER_PATH);

if (import.meta.main) {
await Deno.serve({ hostname: HTTP_URL, port: HTTP_PORT }, (request: Request) => {
await Deno.serve({ hostname: HTTP_URL, port: HTTP_PORT }, async (request: Request) => {
try {
const filePath = path.join(Deno.cwd(), FILE_SERVER_PATH, new URL(request.url).pathname);
const file = Deno.openSync(filePath);
const pathname = `/${request.url.split("/").pop()}`;
const filePath = join(base, pathname);
const file = await Deno.open(filePath);
return new Response(file.readable, {
headers: { "Content-Type": getMimeType(request.url)! },
});
} catch {
return new Response("Not Found", {
status: 404,
statusText: "Not Found",
});
return new Response(body.statusText, body);
}
}).finished;
}
19 changes: 12 additions & 7 deletions benchmarks/file_server/deno_serve_http.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { FILE_SERVER_PATH, HTTP_PORT, HTTP_URL } from "../SERVER_DATA.ts";
import { getMimeType } from "https://deno.land/x/hono@v3.2.7/utils/mime.ts";
import * as path from "https://deno.land/std@0.192.0/path/mod.ts";
import { join } from "https://deno.land/std@0.192.0/path/mod.ts";

export const NAME = "Deno.serveHttp";
export const DESCRIPTION = "";
export const VERSION = `Deno ${Deno.version.deno}`;

const body = {
status: 404,
statusText: "Not Found",
};

const base = join(Deno.cwd(), FILE_SERVER_PATH);

if (import.meta.main) {
for await (const conn of Deno.listen({ port: HTTP_PORT, hostname: HTTP_URL })) {
serveHttp(conn);
Expand All @@ -15,18 +22,16 @@ if (import.meta.main) {
async function serveHttp(conn: Deno.Conn) {
for await (const req of Deno.serveHttp(conn)) {
try {
const filePath = path.join(Deno.cwd(), FILE_SERVER_PATH, new URL(req.request.url).pathname);
const file = Deno.openSync(filePath);
const pathname = `/${req.request.url.split("/").pop()}`;
const filePath = join(base, pathname)
const file = await Deno.open(filePath);
const response = new Response(file.readable, {
headers: { "Content-Type": getMimeType(req.request.url)! },
});
req.respondWith(response);
} catch {
req.respondWith(
new Response("Not Found", {
status: 404,
statusText: "Not Found",
}),
new Response(body.statusText, body),
);
}
}
Expand Down
9 changes: 5 additions & 4 deletions benchmarks/file_server/deno_serve_serveDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ export const NAME = "Deno.serve + serveDir from std";
export const DESCRIPTION = "";
export const VERSION = "std 0.192.0";

const opts = {
fsRoot: FILE_SERVER_PATH,
};

if (import.meta.main) {
await Deno.serve({ hostname: HTTP_URL, port: HTTP_PORT }, (request) =>
serveDir(request, {
fsRoot: FILE_SERVER_PATH,
})).finished;
await Deno.serve({ hostname: HTTP_URL, port: HTTP_PORT }, (request) => serveDir(request, opts)).finished;
}
9 changes: 5 additions & 4 deletions benchmarks/file_server/deno_std_serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export const NAME = "std serve + serveDir";
export const DESCRIPTION = "";
export const VERSION = "0.192.0";

const opts = {
fsRoot: FILE_SERVER_PATH,
};

if (import.meta.main) {
await serve((request) =>
serveDir(request, {
fsRoot: FILE_SERVER_PATH,
}), { hostname: HTTP_URL, port: HTTP_PORT });
await serve((request) => serveDir(request, opts), { hostname: HTTP_URL, port: HTTP_PORT });
}
8 changes: 5 additions & 3 deletions benchmarks/file_server/oak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ export const NAME = "Oak";
export const DESCRIPTION = "";
export const VERSION = "12.5.0";

const opts = {
root: FILE_SERVER_PATH,
};

if (import.meta.main) {
const app = new Application();

app.use(async (context, next) => {
try {
await context.send({
root: FILE_SERVER_PATH,
});
await context.send(opts);
} catch {
await next();
}
Expand Down