This commit is contained in:
Daniel Flanagan 2022-09-26 20:57:53 -05:00
parent 4264ab6dc3
commit 1c7efe5a9e
Signed by untrusted user: lytedev-divvy
GPG key ID: 6D69CEEE4ABBCD82

83
main.ts
View file

@ -1,73 +1,50 @@
import { copy } from "https://deno.land/std@0.157.0/streams/conversion.ts?s=copy";
import { Buffer } from "https://deno.land/std@0.157.0/io/buffer.ts";
import { ByteSet } from "https://deno.land/x/bytes@1.0.3/mod.ts"; import { ByteSet } from "https://deno.land/x/bytes@1.0.3/mod.ts";
import { JsonParseStream } from "https://deno.land/std@0.157.0/encoding/json/stream.ts";
import { TextLineStream } from "https://deno.land/std@0.156.0/streams/delimiter.ts";
import { writeAll, copy } from "https://deno.land/std@0.156.0/streams/conversion.ts";
const PORT = 5588 const QUERY_SIZE = 9;
const listener = Deno.listen({ port: 5588 }) const PORT = 5588;
console.log(`Listening on 0.0.0.0:${PORT}`) const listener = Deno.listen({ port: 5588 });
console.log(`Listening on 0.0.0.0:${PORT}`);
for await (const conn of listener) { for await (const conn of listener) {
console.log('Connection established:', conn.remoteAddr) console.log("Connection established:", conn.remoteAddr);
try { try {
meansToAnEnd(conn) meansToAnEnd(conn);
} catch (e) { } catch (e) {
console.error(conn.remoteAddr, e) console.error(conn.remoteAddr, e);
} }
console.log('Connection closed:', conn.remoteAddr) console.log("Connection closed:", conn.remoteAddr);
} }
// problem 2 // problem 2
async function meansToAnEnd(conn) { async function meansToAnEnd(conn: Deno.Conn) {
new ByteSet(
}
// problem 1
async function primeTime(conn) {
try { try {
for await (const request of jsonLines(conn)) { for await (const query of queries(conn)) {
console.log('Request:', request) console.log("Query:", query);
if (request.method == "isPrime" && typeof request.number == "number") { conn.write(ByteSet.from(Uint8Array.from([0]), "big").buffer);
const response = {
method: "isPrime",
prime: isPrime(request.number),
} }
console.log('Response:', response) console.log("Done!");
const writeResult = await conn.write(new TextEncoder().encode(JSON.stringify(response) + "\n")) } catch (err) {
console.log('Wrote:', writeResult) console.error(err);
} else {
await conn.write("Malformed!")
}
}
} catch (error) {
console.error("Error:", error)
} }
} }
function isPrime(n) { function queries(conn: Deno.Conn) {
if (!Number.isInteger(n)) return false
for (let i = 2, s = Math.sqrt(n); i <= s; i++) if (n % i === 0) return false
return n > 1
}
function jsonLines(conn) {
const readable = conn.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream())
.pipeThrough(new JsonParseStream())
return { return {
async *[Symbol.asyncIterator]() { async *[Symbol.asyncIterator]() {
for await (const data of readable) { const b = new Uint8Array(1024);
yield data const q = new Uint8Array();
let bytesRead = 0;
console.debug(b.length);
while (bytesRead < QUERY_SIZE) {
const n = await conn.read(b);
bytesRead += n;
console.debug(n, b.length);
} }
} yield q;
} },
} };
// problem 0
async function echo(conn) {
await copy(conn, conn)
conn.close()
} }