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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
const addr = Deno.args[1] || "localhost:4555";
function proxyServer() {
const [hostname, p] = addr.split(":");
const port = parseInt(p ?? 4555);
Deno.serve({ hostname, port }, handler);
}
async function handler(req: Request): Promise<Response> {
console.log(`Proxy request to: ${req.url}`);
const headers = new Headers(req.headers);
const proxyAuthorization = headers.get("proxy-authorization");
if (proxyAuthorization) {
console.log(`proxy-authorization: ${proxyAuthorization}`);
headers.delete("proxy-authorization");
}
const resp = await fetch(req.url, {
method: req.method,
headers: headers,
});
return new Response(await resp.bytes(), {
status: resp.status,
headers: resp.headers,
});
}
function assertSuccessOutput(output: Deno.CommandOutput) {
if (output.code !== 0) {
console.error("STDOUT", new TextDecoder().decode(output.stdout));
console.error("STDERR", new TextDecoder().decode(output.stderr));
throw new Error(`Expected exit code 0, was ${output.code}`);
}
}
async function testFetch() {
const output = await new Deno.Command(Deno.execPath(), {
args: [
"run",
"--quiet",
"--reload",
"--allow-net",
"proxy_client.ts",
],
env: {
HTTP_PROXY: `http://${addr}`,
},
}).output();
assertSuccessOutput(output);
}
async function testModuleDownload() {
const output = await new Deno.Command(Deno.execPath(), {
args: [
"cache",
"--reload",
"--quiet",
"http://localhost:4545/run/045_mod.ts",
],
env: {
HTTP_PROXY: `http://${addr}`,
},
}).output();
assertSuccessOutput(output);
}
async function testFetchNoProxy() {
const output = await new Deno.Command(Deno.execPath(), {
args: [
"run",
"--quiet",
"--reload",
"--allow-net",
"proxy_client.ts",
],
env: {
HTTP_PROXY: "http://not.exising.proxy.server",
NO_PROXY: "localhost",
},
}).output();
assertSuccessOutput(output);
}
async function testModuleDownloadNoProxy() {
const output = await new Deno.Command(Deno.execPath(), {
args: [
"cache",
"--reload",
"--quiet",
"http://localhost:4545/run/045_mod.ts",
],
env: {
HTTP_PROXY: "http://not.exising.proxy.server",
NO_PROXY: "localhost",
},
}).output();
assertSuccessOutput(output);
}
async function testFetchProgrammaticProxy() {
const output = await new Deno.Command(Deno.execPath(), {
args: [
"run",
"--quiet",
"--reload",
"--allow-net=localhost:4545,localhost:4555",
"programmatic_proxy_client.ts",
],
}).output();
assertSuccessOutput(output);
}
proxyServer();
await testFetch();
await testModuleDownload();
await testFetchNoProxy();
await testModuleDownloadNoProxy();
await testFetchProgrammaticProxy();
Deno.exit(0);
|