Conditions | 17 |
Paths | 12 |
Total Lines | 73 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like common-util.js ➔ ??? often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** |
||
16 | exports.getClientIp = (request) => { |
||
17 | // the ipAddress we return |
||
18 | let ipAddress; |
||
19 | |||
20 | // workaround to get real client IP |
||
21 | // most likely because our app will be behind a [reverse] proxy or load balancer |
||
22 | const clientIp = request.headers['x-client-ip']; |
||
23 | const forwardedForAlt = request.headers['x-forwarded-for']; |
||
24 | const realIp = request.headers['x-real-ip']; |
||
25 | |||
26 | // more obsure ones below |
||
27 | const clusterClientIp = request.headers['x-cluster-client-ip']; |
||
28 | const forwardedAlt = request.headers['x-forwarded']; |
||
29 | const forwardedFor = request.headers['forwarded-for']; |
||
30 | const forwarded = request.headers['forwarded']; |
||
31 | |||
32 | // remote address check |
||
33 | const reqConnectionRemoteAddress = request.connection ? request.connection.remoteAddress : null; |
||
34 | const reqSocketRemoteAddress = request.socket ? request.socket.remoteAddress : null; |
||
35 | const reqConnectionSocketRemoteAddress = (request.connection && request.connection.socket) |
||
36 | ? request.connection.socket.remoteAddress : null; |
||
37 | const reqInfoRemoteAddress = request.info ? request.info.remoteAddress : null; |
||
38 | |||
39 | if (clientIp) { |
||
40 | // x-client-ip |
||
41 | ipAddress = clientIp; |
||
42 | } else if (forwardedForAlt) { |
||
43 | // x-forwarded-for |
||
44 | // (typically when your node app is behind a load-balancer (eg. AWS ELB) or proxy) |
||
45 | // |
||
46 | // x-forwarded-for may return multiple IP addresses in the format: |
||
47 | // "client IP, proxy 1 IP, proxy 2 IP" |
||
48 | // Therefore, the right-most IP address is the IP address of the most recent proxy |
||
49 | // and the left-most IP address is the IP address of the originating client. |
||
50 | // source: http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html |
||
51 | const forwardedIps = forwardedForAlt.split(','); |
||
52 | ipAddress = forwardedIps[0]; |
||
53 | } else if (realIp) { |
||
54 | // x-real-ip |
||
55 | // (default nginx proxy/fcgi) |
||
56 | // alternative to x-forwarded-for, used by some proxies |
||
57 | ipAddress = realIp; |
||
58 | } else if (clusterClientIp) { |
||
59 | // x-cluster-client-ip |
||
60 | // (Rackspace LB and Riverbed's Stingray) |
||
61 | // http://www.rackspace.com/knowledge_center/article/controlling-access-to-linux-cloud-sites-based-on-the-client-ip-address |
||
62 | // https://splash.riverbed.com/docs/DOC-1926 |
||
63 | ipAddress = clusterClientIp; |
||
64 | } else if (forwardedAlt) { |
||
65 | // x-forwarded |
||
66 | ipAddress = forwardedAlt; |
||
67 | } else if (forwardedFor) { |
||
68 | // forwarded-for |
||
69 | ipAddress = forwardedFor; |
||
70 | } else if (forwarded) { |
||
71 | // forwarded |
||
72 | ipAddress = forwarded; |
||
73 | } else if (reqConnectionRemoteAddress) { |
||
74 | // remote address checks |
||
75 | ipAddress = reqConnectionRemoteAddress; |
||
76 | } else if (reqSocketRemoteAddress) { |
||
77 | ipAddress = reqSocketRemoteAddress; |
||
78 | } else if (reqConnectionSocketRemoteAddress) { |
||
79 | ipAddress = reqConnectionSocketRemoteAddress; |
||
80 | } else if (reqInfoRemoteAddress) { |
||
81 | ipAddress = reqInfoRemoteAddress; |
||
82 | } else { |
||
83 | // return null if we cannot find an address |
||
84 | ipAddress = null; |
||
85 | } |
||
86 | |||
87 | return ipAddress; |
||
88 | }; |
||
89 | |||
230 |