Skip to content

Commit

Permalink
feat: edge: v2 exchange endpoint
Browse files Browse the repository at this point in the history
v2 endpoint exchange info with JSON, so that we can ensure client's
forward compatibility easily by ignoring unknown fields.
  • Loading branch information
Contextualist committed Feb 13, 2023
1 parent 53fcb8d commit 4c2bff3
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions edge/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { serve, type ConnInfo } from "https://deno.land/[email protected]/http/server.ts";


interface ClientInfo {
priAddr: string,
chanName: string,
}

async function handleExchangeV2(req: Request, connInfo: ConnInfo): Promise<Response> {
if (req.method != "POST")
return new Response("Invalid method", { status: 405 })
if (req.headers.get("Content-Type") != "application/octet-stream")
return new Response("Invalid content type", { status: 415 })

const pubAddr = joinHostPort(connInfo.remoteAddr)
const conn = req.body!.getReader({ mode: "byob" })
const { priAddr, chanName }: ClientInfo = JSON.parse(
new TextDecoder().decode(await receivePacket(conn))
)
const x0 = JSON.stringify({ pubAddr, priAddr })
//console.log(`accepted from ${x0}`)

const x1 = await exchange(chanName, x0, conn)
if (x1 == "")
return new Response("")
//console.log(`exchanged, got ${x1}`)

const msg = marshallPacket(new TextEncoder().encode(x1))
return new Response(msg)
}


// (priAddr0|chanName) -> pubAddr1|priAddr1
async function handleExchange(req: Request, connInfo: ConnInfo): Promise<Response> {
async function handleExchangeV1(req: Request, connInfo: ConnInfo): Promise<Response> {
if (req.method != "POST")
return new Response("Invalid method", { status: 405 })
if (req.headers.get("Content-Type") != "application/octet-stream")
Expand Down Expand Up @@ -117,8 +146,10 @@ async function handler(req: Request, connInfo: ConnInfo): Promise<Response> {
`,
{ headers: { "Content-Type": "text/plain; charset=utf-8" } }
)
case "/v2/exchange":
return await handleExchangeV2(req, connInfo)
case "/exchange":
return await handleExchange(req, connInfo)
return await handleExchangeV1(req, connInfo)
default:
return new Response("Not found", { status: 404 })
}
Expand Down

0 comments on commit 4c2bff3

Please sign in to comment.