-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocal-test1.ts
More file actions
57 lines (51 loc) · 1.7 KB
/
local-test1.ts
File metadata and controls
57 lines (51 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as engine from "./engine/mod.ts";
engine.on("connect",async function({socket,client}: HttpSocket): Promise<void>{
console.log("connect",client);
if(!client.isValid){
socket.status=400;
socket.statusMessage="Bad Request";
socket.close("Cannot read client request");
} else if(client.headers.upgrade?.includes("h2c")){
console.log("h2c");
const h2c=await socket.http2();
console.log(h2c,socket);
if(!h2c)return socket.deny();
h2cHandler(h2c);
} else if(client.headers.upgrade?.includes("websocket")){
console.log("ws");
const ws=await socket.websocket();
console.log(ws,socket);
if(!ws)return socket.close();
ws.on("frame",async(f:WsFrame)=>{ws.sendText(`${f.opcode}: ${new TextDecoder().decode(f.payload)}`)});
} else {
console.log("plain");
handler(socket);
}
});
engine.on("http2",h2cHandler);
async function handler(sock:HttpSocket|PseudoHttpSocket){
console.log("http1");
sock.setHeader("Content-Type","text/plain");
sock.close(JSON.stringify(sock.client,null,2));
};
async function h2cHandler(socket: Http2Socket){
socket.on("error",console.error);
await socket.ready;
console.log("http2");
socket.on("stream",async function(stream:Http2Stream){
console.log("http2 stream");
const ps=stream.pseudo();
handler(ps);
})
};
engine.on("error",console.error);
engine.start(8080);
console.log("listening 8080");
engine.setTls({
key:Deno.readTextFileSync("localhost.key"),
cert:Deno.readTextFileSync("localhost.crt"),
alpn:["h2","http1"]
});
engine.start(1443,true);
console.log("listening 1443");
globalThis.engine=engine;