forked from denoland/deno
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add http proxy benchmark (denoland#2462)
- Loading branch information
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
import { | ||
serve, | ||
ServerRequest | ||
} from "../js/deps/https/deno.land/std/http/server.ts"; | ||
|
||
const addr = Deno.args[1] || "127.0.0.1:4500"; | ||
const originAddr = Deno.args[2] || "127.0.0.1:4501"; | ||
const server = serve(addr); | ||
|
||
async function main(): Promise<void> { | ||
console.log(`http://${addr}/`); | ||
for await (const req of server) { | ||
proxyRequest(req); | ||
} | ||
} | ||
|
||
async function proxyRequest(req: ServerRequest) { | ||
const url = `http://${originAddr}${req.url}`; | ||
const resp = await fetch(url, { | ||
method: req.method, | ||
headers: req.headers | ||
}); | ||
req.respond(resp); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
const http = require("http"); | ||
const port = process.argv[2] || "4544"; | ||
const originPort = process.argv[3] || "4545"; | ||
console.log("port", port); | ||
http | ||
.Server((req, res) => { | ||
const options = { | ||
port: originPort, | ||
path: req.url, | ||
method: req.method, | ||
headers: req.headers | ||
}; | ||
|
||
const proxy = http.request(options, proxyRes => { | ||
res.writeHead(proxyRes.statusCode, proxyRes.headers); | ||
proxyRes.pipe( | ||
res, | ||
{ end: true } | ||
); | ||
}); | ||
|
||
req.pipe( | ||
proxy, | ||
{ end: true } | ||
); | ||
}) | ||
.listen(port); |