Cleaned up problem 2 code
This commit is contained in:
parent
20fca96aa7
commit
0cc8b61df3
64
main.ts
64
main.ts
|
@ -1,4 +1,7 @@
|
||||||
import { copy } from "https://deno.land/std@0.156.0/streams/conversion.ts";
|
import * as bytes 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 PORT = 5588
|
||||||
const listener = Deno.listen({ port: 5588 })
|
const listener = Deno.listen({ port: 5588 })
|
||||||
|
@ -6,13 +9,64 @@ const listener = Deno.listen({ port: 5588 })
|
||||||
console.log(`Listening on 0.0.0.0:${PORT}`)
|
console.log(`Listening on 0.0.0.0:${PORT}`)
|
||||||
|
|
||||||
for await (const conn of listener) {
|
for await (const conn of listener) {
|
||||||
echo(conn)
|
console.log('Connection established:', conn.remoteAddr)
|
||||||
|
try {
|
||||||
|
primeTime(conn)
|
||||||
|
} catch (e) {
|
||||||
|
console.error(conn.remoteAddr, e)
|
||||||
|
}
|
||||||
|
console.log('Connection closed:', conn.remoteAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// problem 2
|
||||||
|
async function meansToAnEnd(conn) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// problem 1
|
||||||
|
async function primeTime(conn) {
|
||||||
|
try {
|
||||||
|
for await (const request of jsons(conn)) {
|
||||||
|
console.log('Request:', request)
|
||||||
|
if (request.method == "isPrime" && typeof request.number == "number") {
|
||||||
|
const response = {
|
||||||
|
method: "isPrime",
|
||||||
|
prime: isPrime(request.number),
|
||||||
|
}
|
||||||
|
console.log('Response:', response)
|
||||||
|
const writeResult = await conn.write(new TextEncoder().encode(JSON.stringify(response) + "\n"))
|
||||||
|
console.log('Wrote:', writeResult)
|
||||||
|
} else {
|
||||||
|
await conn.write("Malformed!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error:", error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isPrime(n) {
|
||||||
|
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 jsons(conn) {
|
||||||
|
const readable = conn.readable
|
||||||
|
.pipeThrough(new TextDecoderStream())
|
||||||
|
.pipeThrough(new TextLineStream())
|
||||||
|
.pipeThrough(new JsonParseStream())
|
||||||
|
|
||||||
|
return {
|
||||||
|
async *[Symbol.asyncIterator]() {
|
||||||
|
for await (const data of readable) {
|
||||||
|
yield data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// problem 0
|
||||||
async function echo(conn) {
|
async function echo(conn) {
|
||||||
await copy(conn, conn)
|
await copy(conn, conn)
|
||||||
conn.close()
|
conn.close()
|
||||||
}
|
|
||||||
|
|
||||||
async function primeTime(conn) {
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue