1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00
denoland-deno/cli/bench/http/deno_tcp.ts

34 lines
1,003 B
TypeScript
Raw Normal View History

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// Used for benchmarking Deno's networking.
// TODO(bartlomieju): Replace this with a real HTTP server once
2018-10-15 16:44:35 -04:00
// https://github.com/denoland/deno/issues/726 is completed.
// Note: this is a keep-alive server.
const addr = Deno.args[0] || "127.0.0.1:4500";
const [hostname, port] = addr.split(":");
const listener = Deno.listen({ hostname, port: Number(port) });
2018-10-15 16:44:35 -04:00
const response = new TextEncoder().encode(
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n",
2018-10-15 16:44:35 -04:00
);
async function handle(conn: Deno.Conn): Promise<void> {
2018-10-15 16:44:35 -04:00
const buffer = new Uint8Array(1024);
try {
while (true) {
await conn.read(buffer);
2018-10-15 16:44:35 -04:00
await conn.write(response);
}
} catch (e) {
if (
!(e instanceof Deno.errors.BrokenPipe) &&
!(e instanceof Deno.errors.ConnectionReset)
) {
throw e;
}
2018-10-15 16:44:35 -04:00
}
conn.close();
2018-10-15 16:44:35 -04:00
}
console.log("Listening on", addr);
for await (const conn of listener) {
handle(conn);
2018-10-15 16:44:35 -04:00
}