{"version":3,"sources":["../../src/classes/Agent.js"],"names":["log","Logger","child","namespace","requestId","Agent","constructor","isProxyConfigured","mustUrlUseProxy","getUrlProxy","fallbackAgent","socketConnectionTimeout","addRequest","request","configuration","requestUrl","path","startsWith","protocol","hostname","host","port","trace","destination","currentRequestId","proxy","authorization","setHeader","Buffer","from","toString","on","error","once","response","headers","statusCode","shouldKeepAlive","connectionConfiguration","createConnection","socket","target","setTimeout","destroy","emit","debug","socketError","onSocket"],"mappings":";;;;;;;AAEA;;AAGA;;;;AASA,MAAMA,GAAG,GAAGC,gBAAOC,KAAP,CAAa;AACvBC,EAAAA,SAAS,EAAE;AADY,CAAb,CAAZ;;AAIA,IAAIC,SAAS,GAAG,CAAhB;;AAEA,MAAMC,KAAN,CAAY;AAeVC,EAAAA,WAAW,CACTC,iBADS,EAETC,eAFS,EAGTC,WAHS,EAITC,aAJS,EAKTC,uBALS,EAMT;AACA,SAAKD,aAAL,GAAqBA,aAArB;AACA,SAAKH,iBAAL,GAAyBA,iBAAzB;AACA,SAAKC,eAAL,GAAuBA,eAAvB;AACA,SAAKC,WAAL,GAAmBA,WAAnB;AACA,SAAKE,uBAAL,GAA+BA,uBAA/B;AACD;;AAEDC,EAAAA,UAAU,CAAEC,OAAF,EAAcC,aAAd,EAAgC;AACxC,QAAIC,UAAJ,CADwC,CAGxC;AACA;AACA;AACA;;AACA,QAAIF,OAAO,CAACG,IAAR,CAAaC,UAAb,CAAwB,SAAxB,KAAsCJ,OAAO,CAACG,IAAR,CAAaC,UAAb,CAAwB,UAAxB,CAA1C,EAA+E;AAC7EF,MAAAA,UAAU,GAAGF,OAAO,CAACG,IAArB;AACD,KAFD,MAEO;AACLD,MAAAA,UAAU,GAAG,KAAKG,QAAL,GAAgB,IAAhB,IAAwBJ,aAAa,CAACK,QAAd,IAA0BL,aAAa,CAACM,IAAhE,KAAyEN,aAAa,CAACO,IAAd,KAAuB,EAAvB,IAA6BP,aAAa,CAACO,IAAd,KAAuB,GAApD,GAA0D,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UAAAA,GAAG,CAACsB,KAAJ,CAAU;AACRmB,YAAAA,MAAM,EAAEH;AADA,WAAV,EAEG,oBAFH;AAIAE,UAAAA,MAAM,CAACE,UAAP,CAAkB,CAAlB;AACD,SAND;AAOD;;AAED,UAAIV,KAAJ,EAAW;AACTnB,QAAAA,OAAO,CAAC+B,IAAR,CAAa,OAAb,EAAsBZ,KAAtB;AACD,OAFD,MAEO;AACLhC,QAAAA,GAAG,CAAC6C,KAAJ,CAAU,gBAAV;AAEAL,QAAAA,MAAM,CAACT,EAAP,CAAU,OAAV,EAAoBe,WAAD,IAAiB;AAClC9C,UAAAA,GAAG,CAACgC,KAAJ,CAAU;AACRA,YAAAA,KAAK,EAAE,oCAAec,WAAf;AADC,WAAV,EAEG,cAFH;AAGD,SAJD;AAMAjC,QAAAA,OAAO,CAACkC,QAAR,CAAiBP,MAAjB;AACD;AACF,KAzCD;AA0CD;;AAnJS;;eAsJGnC,K","sourcesContent":["// @flow\n\nimport {\n serializeError,\n} from 'serialize-error';\nimport Logger from '../Logger';\nimport type {\n AgentType,\n GetUrlProxyMethodType,\n IsProxyConfiguredMethodType,\n MustUrlUseProxyMethodType,\n ProtocolType,\n} from '../types';\n\nconst log = Logger.child({\n namespace: 'Agent',\n});\n\nlet requestId = 0;\n\nclass Agent {\n defaultPort: number;\n\n protocol: ProtocolType;\n\n fallbackAgent: AgentType;\n\n isProxyConfigured: IsProxyConfiguredMethodType;\n\n mustUrlUseProxy: MustUrlUseProxyMethodType;\n\n getUrlProxy: GetUrlProxyMethodType;\n\n socketConnectionTimeout: number;\n\n constructor (\n isProxyConfigured: IsProxyConfiguredMethodType,\n mustUrlUseProxy: MustUrlUseProxyMethodType,\n getUrlProxy: GetUrlProxyMethodType,\n fallbackAgent: AgentType,\n socketConnectionTimeout: number,\n ) {\n this.fallbackAgent = fallbackAgent;\n this.isProxyConfigured = isProxyConfigured;\n this.mustUrlUseProxy = mustUrlUseProxy;\n this.getUrlProxy = getUrlProxy;\n this.socketConnectionTimeout = socketConnectionTimeout;\n }\n\n addRequest (request: *, configuration: *) {\n let requestUrl;\n\n // It is possible that addRequest was constructed for a proxied request already, e.g.\n // \"request\" package does this when it detects that a proxy should be used\n // https://github.com/request/request/blob/212570b6971a732b8dd9f3c73354bcdda158a737/request.js#L402\n // https://gist.github.com/gajus/e2074cd3b747864ffeaabbd530d30218\n if (request.path.startsWith('http://') || request.path.startsWith('https://')) {\n requestUrl = request.path;\n } else {\n requestUrl = this.protocol + '//' + (configuration.hostname || configuration.host) + (configuration.port === 80 || configuration.port === 443 ? '' : ':' + configuration.port) + request.path;\n }\n\n if (!this.isProxyConfigured()) {\n log.trace({\n destination: requestUrl,\n }, 'not proxying request; GLOBAL_AGENT.HTTP_PROXY is not configured');\n\n // $FlowFixMe It appears that Flow is missing the method description.\n this.fallbackAgent.addRequest(request, configuration);\n\n return;\n }\n\n if (!this.mustUrlUseProxy(requestUrl)) {\n log.trace({\n destination: requestUrl,\n }, 'not proxying request; url matches GLOBAL_AGENT.NO_PROXY');\n\n // $FlowFixMe It appears that Flow is missing the method description.\n this.fallbackAgent.addRequest(request, configuration);\n\n return;\n }\n\n const currentRequestId = requestId++;\n\n const proxy = this.getUrlProxy(requestUrl);\n\n if (this.protocol === 'http:') {\n request.path = requestUrl;\n\n if (proxy.authorization) {\n request.setHeader('proxy-authorization', 'Basic ' + Buffer.from(proxy.authorization).toString('base64'));\n }\n }\n\n log.trace({\n destination: requestUrl,\n proxy: 'http://' + proxy.hostname + ':' + proxy.port,\n requestId: currentRequestId,\n }, 'proxying request');\n\n request.on('error', (error) => {\n log.error({\n error: serializeError(error),\n }, 'request error');\n });\n\n request.once('response', (response) => {\n log.trace({\n headers: response.headers,\n requestId: currentRequestId,\n statusCode: response.statusCode,\n }, 'proxying response');\n });\n\n request.shouldKeepAlive = false;\n\n const connectionConfiguration = {\n host: configuration.hostname || configuration.host,\n port: configuration.port || 80,\n proxy,\n };\n\n // $FlowFixMe It appears that Flow is missing the method description.\n this.createConnection(connectionConfiguration, (error, socket) => {\n log.trace({\n target: connectionConfiguration,\n }, 'connecting');\n\n // @see https://github.com/nodejs/node/issues/5757#issuecomment-305969057\n if (socket) {\n socket.setTimeout(this.socketConnectionTimeout, () => {\n socket.destroy();\n });\n\n socket.once('connect', () => {\n log.trace({\n target: connectionConfiguration,\n }, 'connected');\n\n socket.setTimeout(0);\n });\n\n socket.once('secureConnect', () => {\n log.trace({\n target: connectionConfiguration,\n }, 'connected (secure)');\n\n socket.setTimeout(0);\n });\n }\n\n if (error) {\n request.emit('error', error);\n } else {\n log.debug('created socket');\n\n socket.on('error', (socketError) => {\n log.error({\n error: serializeError(socketError),\n }, 'socket error');\n });\n\n request.onSocket(socket);\n }\n });\n }\n}\n\nexport default Agent;\n"],"file":"Agent.js"}