浏览代码

@uppy/companion: refactor `getProtectedHttpAgent` to make TS happy (#4654)

Antoine du Hamel 1 年之前
父节点
当前提交
06eef39a4e
共有 1 个文件被更改,包括 2 次插入17 次删除
  1. 2 17
      packages/@uppy/companion/src/server/helpers/request.js

+ 2 - 17
packages/@uppy/companion/src/server/helpers/request.js

@@ -94,11 +94,9 @@ const getProtectedHttpAgent = ({ protocol, blockLocalIPs }) => {
     })
   }
 
-  const isBlocked = (options) => ipaddr.isValid(options.host) && blockLocalIPs && isDisallowedIP(options.host)
-
-  class HttpAgent extends http.Agent {
+  return class HttpAgent extends (protocol.startsWith('https') ? https : http).Agent {
     createConnection (options, callback) {
-      if (isBlocked(options)) {
+      if (ipaddr.isValid(options.host) && blockLocalIPs && isDisallowedIP(options.host)) {
         callback(new Error(FORBIDDEN_IP_ADDRESS))
         return undefined
       }
@@ -106,19 +104,6 @@ const getProtectedHttpAgent = ({ protocol, blockLocalIPs }) => {
       return super.createConnection({ ...options, lookup: dnsLookup }, callback)
     }
   }
-
-  class HttpsAgent extends https.Agent {
-    createConnection (options, callback) {
-      if (isBlocked(options)) {
-        callback(new Error(FORBIDDEN_IP_ADDRESS))
-        return undefined
-      }
-      // @ts-ignore
-      return super.createConnection({ ...options, lookup: dnsLookup }, callback)
-    }
-  }
-
-  return protocol.startsWith('https') ? HttpsAgent : HttpAgent
 }
 
 module.exports.getProtectedHttpAgent = getProtectedHttpAgent