-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnot-working-server.deno.ts
64 lines (55 loc) · 1.48 KB
/
not-working-server.deno.ts
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
58
59
60
61
62
63
64
// @ts-ignore
import { serve } from "https://deno.land/std/http/server.ts"
const PORT = 8000
class FetchEventDeno extends CustomEvent<FetchEvent> implements FetchEvent {
constructor(type: string, init: FetchEventInit, responder: Responder) {
super(type, init)
this.request = init.request
this.clientId = init.clientId!
this.responder = responder
}
responder: Responder
request: Request
clientId: string
preloadResponse!: Promise<any>
replacesClientId!: string
resultingClientId!: string
passThroughOnException!: () => void
waitUntil(_: any): void {
throw new Error("Method not implemented.")
}
async respondWith(responsePromise: Promise<Response>) {
const response = await responsePromise
console.log(response.headers)
console.log(await response.text())
this.responder(response)
}
}
function buildResponse(req: any) {
return async function responder(response: Response) {
req.respond({
body: await response.text(),
status: response.status,
headers: response.headers,
})
}
}
async function run() {
const server = serve({ port: PORT })
const url = `http://localhost:${PORT}/`
console.log(url)
// @ts-ignore
for await (const req of server) {
const request = new Request(url + req.url, {
headers: req.headers,
})
const initEvent = { request }
const fetchEvent = new FetchEventDeno(
"fetch",
initEvent,
buildResponse(req),
)
dispatchEvent(fetchEvent)
}
}
run()