opennms
Version:
Client API for the OpenNMS network monitoring platform
1 lines • 24.5 kB
JSON
{"remainingRequest":"/data/node_modules/babel-loader/lib/index.js!/data/node_modules/follow-redirects/index.js","dependencies":[{"path":"/data/node_modules/follow-redirects/index.js","mtime":1553611386512},{"path":"/data/.babelrc","mtime":1553611371556},{"path":"/data/node_modules/cache-loader/dist/cjs.js","mtime":1553611387012},{"path":"/data/node_modules/babel-loader/lib/index.js","mtime":1553611386992}],"contextDependencies":[],"result":["'use strict';\n\nvar url = require('url');\nvar assert = require('assert');\nvar http = require('http');\nvar https = require('https');\nvar Writable = require('stream').Writable;\nvar debug = require('debug')('follow-redirects');\n\nvar nativeProtocols = { 'http:': http, 'https:': https };\nvar schemes = {};\nvar _exports = module.exports = {\n\tmaxRedirects: 21\n};\n// RFC7231§4.2.1: Of the request methods defined by this specification,\n// the GET, HEAD, OPTIONS, and TRACE methods are defined to be safe.\nvar safeMethods = { GET: true, HEAD: true, OPTIONS: true, TRACE: true };\n\n// Create handlers that pass events from native requests\nvar eventHandlers = Object.create(null);\n['abort', 'aborted', 'error', 'socket'].forEach(function (event) {\n\teventHandlers[event] = function (arg) {\n\t\tthis._redirectable.emit(event, arg);\n\t};\n});\n\n// An HTTP(S) request that can be redirected\nfunction RedirectableRequest(options, responseCallback) {\n\t// Initialize the request\n\tWritable.call(this);\n\tthis._options = options;\n\tthis._redirectCount = 0;\n\tthis._bufferedWrites = [];\n\n\t// Attach a callback if passed\n\tif (responseCallback) {\n\t\tthis.on('response', responseCallback);\n\t}\n\n\t// React to responses of native requests\n\tvar self = this;\n\tthis._onNativeResponse = function (response) {\n\t\tself._processResponse(response);\n\t};\n\n\t// Complete the URL object when necessary\n\tif (!options.pathname && options.path) {\n\t\tvar searchPos = options.path.indexOf('?');\n\t\tif (searchPos < 0) {\n\t\t\toptions.pathname = options.path;\n\t\t} else {\n\t\t\toptions.pathname = options.path.substring(0, searchPos);\n\t\t\toptions.search = options.path.substring(searchPos);\n\t\t}\n\t}\n\n\t// Perform the first request\n\tthis._performRequest();\n}\nRedirectableRequest.prototype = Object.create(Writable.prototype);\n\n// Executes the next native request (initial or redirect)\nRedirectableRequest.prototype._performRequest = function () {\n\t// If specified, use the agent corresponding to the protocol\n\t// (HTTP and HTTPS use different types of agents)\n\tvar protocol = this._options.protocol;\n\tif (this._options.agents) {\n\t\tthis._options.agent = this._options.agents[schemes[protocol]];\n\t}\n\n\t// Create the native request\n\tvar nativeProtocol = nativeProtocols[protocol];\n\tvar request = this._currentRequest = nativeProtocol.request(this._options, this._onNativeResponse);\n\tthis._currentUrl = url.format(this._options);\n\n\t// Set up event handlers\n\trequest._redirectable = this;\n\tfor (var event in eventHandlers) {\n\t\t/* istanbul ignore else */\n\t\tif (event) {\n\t\t\trequest.on(event, eventHandlers[event]);\n\t\t}\n\t}\n\n\t// End a redirected request\n\t// (The first request must be ended explicitly with RedirectableRequest#end)\n\tif (this._isRedirect) {\n\t\t// If the request doesn't have en entity, end directly.\n\t\tvar bufferedWrites = this._bufferedWrites;\n\t\tif (bufferedWrites.length === 0) {\n\t\t\trequest.end();\n\t\t\t// Otherwise, write the request entity and end afterwards.\n\t\t} else {\n\t\t\tvar i = 0;\n\t\t\t(function writeNext() {\n\t\t\t\tif (i < bufferedWrites.length) {\n\t\t\t\t\tvar bufferedWrite = bufferedWrites[i++];\n\t\t\t\t\trequest.write(bufferedWrite.data, bufferedWrite.encoding, writeNext);\n\t\t\t\t} else {\n\t\t\t\t\trequest.end();\n\t\t\t\t}\n\t\t\t})();\n\t\t}\n\t}\n};\n\n// Processes a response from the current native request\nRedirectableRequest.prototype._processResponse = function (response) {\n\t// RFC7231§6.4: The 3xx (Redirection) class of status code indicates\n\t// that further action needs to be taken by the user agent in order to\n\t// fulfill the request. If a Location header field is provided,\n\t// the user agent MAY automatically redirect its request to the URI\n\t// referenced by the Location field value,\n\t// even if the specific status code is not understood.\n\tvar location = response.headers.location;\n\tif (location && this._options.followRedirects !== false && response.statusCode >= 300 && response.statusCode < 400) {\n\t\t// RFC7231§6.4: A client SHOULD detect and intervene\n\t\t// in cyclical redirections (i.e., \"infinite\" redirection loops).\n\t\tif (++this._redirectCount > this._options.maxRedirects) {\n\t\t\treturn this.emit('error', new Error('Max redirects exceeded.'));\n\t\t}\n\n\t\t// RFC7231§6.4: Automatic redirection needs to done with\n\t\t// care for methods not known to be safe […],\n\t\t// since the user might not wish to redirect an unsafe request.\n\t\t// RFC7231§6.4.7: The 307 (Temporary Redirect) status code indicates\n\t\t// that the target resource resides temporarily under a different URI\n\t\t// and the user agent MUST NOT change the request method\n\t\t// if it performs an automatic redirection to that URI.\n\t\tvar header;\n\t\tvar headers = this._options.headers;\n\t\tif (response.statusCode !== 307 && !(this._options.method in safeMethods)) {\n\t\t\tthis._options.method = 'GET';\n\t\t\t// Drop a possible entity and headers related to it\n\t\t\tthis._bufferedWrites = [];\n\t\t\tfor (header in headers) {\n\t\t\t\tif (/^content-/i.test(header)) {\n\t\t\t\t\tdelete headers[header];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Drop the Host header, as the redirect might lead to a different host\n\t\tif (!this._isRedirect) {\n\t\t\tfor (header in headers) {\n\t\t\t\tif (/^host$/i.test(header)) {\n\t\t\t\t\tdelete headers[header];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Perform the redirected request\n\t\tvar redirectUrl = url.resolve(this._currentUrl, location);\n\t\tdebug('redirecting to', redirectUrl);\n\t\tObject.assign(this._options, url.parse(redirectUrl));\n\t\tthis._isRedirect = true;\n\t\tthis._performRequest();\n\t} else {\n\t\t// The response is not a redirect; return it as-is\n\t\tresponse.responseUrl = this._currentUrl;\n\t\tthis.emit('response', response);\n\n\t\t// Clean up\n\t\tdelete this._options;\n\t\tdelete this._bufferedWrites;\n\t}\n};\n\n// Aborts the current native request\nRedirectableRequest.prototype.abort = function () {\n\tthis._currentRequest.abort();\n};\n\n// Flushes the headers of the current native request\nRedirectableRequest.prototype.flushHeaders = function () {\n\tthis._currentRequest.flushHeaders();\n};\n\n// Sets the noDelay option of the current native request\nRedirectableRequest.prototype.setNoDelay = function (noDelay) {\n\tthis._currentRequest.setNoDelay(noDelay);\n};\n\n// Sets the socketKeepAlive option of the current native request\nRedirectableRequest.prototype.setSocketKeepAlive = function (enable, initialDelay) {\n\tthis._currentRequest.setSocketKeepAlive(enable, initialDelay);\n};\n\n// Sets the timeout option of the current native request\nRedirectableRequest.prototype.setTimeout = function (timeout, callback) {\n\tthis._currentRequest.setTimeout(timeout, callback);\n};\n\n// Writes buffered data to the current native request\nRedirectableRequest.prototype.write = function (data, encoding, callback) {\n\tthis._currentRequest.write(data, encoding, callback);\n\tthis._bufferedWrites.push({ data: data, encoding: encoding });\n};\n\n// Ends the current native request\nRedirectableRequest.prototype.end = function (data, encoding, callback) {\n\tthis._currentRequest.end(data, encoding, callback);\n\tif (data) {\n\t\tthis._bufferedWrites.push({ data: data, encoding: encoding });\n\t}\n};\n\n// Export a redirecting wrapper for each native protocol\nObject.keys(nativeProtocols).forEach(function (protocol) {\n\tvar scheme = schemes[protocol] = protocol.substr(0, protocol.length - 1);\n\tvar nativeProtocol = nativeProtocols[protocol];\n\tvar wrappedProtocol = _exports[scheme] = Object.create(nativeProtocol);\n\n\t// Executes an HTTP request, following redirects\n\twrappedProtocol.request = function (options, callback) {\n\t\tif (typeof options === 'string') {\n\t\t\toptions = url.parse(options);\n\t\t\toptions.maxRedirects = _exports.maxRedirects;\n\t\t} else {\n\t\t\toptions = Object.assign({\n\t\t\t\tmaxRedirects: _exports.maxRedirects,\n\t\t\t\tprotocol: protocol\n\t\t\t}, options);\n\t\t}\n\t\tassert.equal(options.protocol, protocol, 'protocol mismatch');\n\t\tdebug('options', options);\n\n\t\treturn new RedirectableRequest(options, callback);\n\t};\n\n\t// Executes a GET request, following redirects\n\twrappedProtocol.get = function (options, callback) {\n\t\tvar request = wrappedProtocol.request(options, callback);\n\t\trequest.end();\n\t\treturn request;\n\t};\n});",{"version":3,"sources":["node_modules/follow-redirects/index.js"],"names":["url","require","assert","http","https","Writable","debug","nativeProtocols","schemes","exports","module","maxRedirects","safeMethods","GET","HEAD","OPTIONS","TRACE","eventHandlers","Object","create","forEach","event","arg","_redirectable","emit","RedirectableRequest","options","responseCallback","call","_options","_redirectCount","_bufferedWrites","on","self","_onNativeResponse","response","_processResponse","pathname","path","searchPos","indexOf","substring","search","_performRequest","prototype","protocol","agents","agent","nativeProtocol","request","_currentRequest","_currentUrl","format","_isRedirect","bufferedWrites","length","end","i","writeNext","bufferedWrite","write","data","encoding","location","headers","followRedirects","statusCode","Error","header","method","test","redirectUrl","resolve","assign","parse","responseUrl","abort","flushHeaders","setNoDelay","noDelay","setSocketKeepAlive","enable","initialDelay","setTimeout","timeout","callback","push","keys","scheme","substr","wrappedProtocol","equal","get"],"mappings":"AAAA;;AACA,IAAIA,MAAMC,QAAQ,KAAR,CAAV;AACA,IAAIC,SAASD,QAAQ,QAAR,CAAb;AACA,IAAIE,OAAOF,QAAQ,MAAR,CAAX;AACA,IAAIG,QAAQH,QAAQ,OAAR,CAAZ;AACA,IAAII,WAAWJ,QAAQ,QAAR,EAAkBI,QAAjC;AACA,IAAIC,QAAQL,QAAQ,OAAR,EAAiB,kBAAjB,CAAZ;;AAEA,IAAIM,kBAAkB,EAAC,SAASJ,IAAV,EAAgB,UAAUC,KAA1B,EAAtB;AACA,IAAII,UAAU,EAAd;AACA,IAAIC,WAAUC,OAAOD,OAAP,GAAiB;AAC9BE,eAAc;AADgB,CAA/B;AAGA;AACA;AACA,IAAIC,cAAc,EAACC,KAAK,IAAN,EAAYC,MAAM,IAAlB,EAAwBC,SAAS,IAAjC,EAAuCC,OAAO,IAA9C,EAAlB;;AAEA;AACA,IAAIC,gBAAgBC,OAAOC,MAAP,CAAc,IAAd,CAApB;AACA,CAAC,OAAD,EAAU,SAAV,EAAqB,OAArB,EAA8B,QAA9B,EAAwCC,OAAxC,CAAgD,UAAUC,KAAV,EAAiB;AAChEJ,eAAcI,KAAd,IAAuB,UAA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ACA,EAJD;AAKA,CA5BD","file":"index.js","sourceRoot":"/data","sourcesContent":["'use strict';\nvar url = require('url');\nvar assert = require('assert');\nvar http = require('http');\nvar https = require('https');\nvar Writable = require('stream').Writable;\nvar debug = require('debug')('follow-redirects');\n\nvar nativeProtocols = {'http:': http, 'https:': https};\nvar schemes = {};\nvar exports = module.exports = {\n\tmaxRedirects: 21\n};\n// RFC7231§4.2.1: Of the request methods defined by this specification,\n// the GET, HEAD, OPTIONS, and TRACE methods are defined to be safe.\nvar safeMethods = {GET: true, HEAD: true, OPTIONS: true, TRACE: true};\n\n// Create handlers that pass events from native requests\nvar eventHandlers = Object.create(null);\n['abort', 'aborted', 'error', 'socket'].forEach(function (event) {\n\teventHandlers[event] = function (arg) {\n\t\tthis._redirectable.emit(event, arg);\n\t};\n});\n\n// An HTTP(S) request that can be redirected\nfunction RedirectableRequest(options, responseCallback) {\n\t// Initialize the request\n\tWritable.call(this);\n\tthis._options = options;\n\tthis._redirectCount = 0;\n\tthis._bufferedWrites = [];\n\n\t// Attach a callback if passed\n\tif (responseCallback) {\n\t\tthis.on('response', responseCallback);\n\t}\n\n\t// React to responses of native requests\n\tvar self = this;\n\tthis._onNativeResponse = function (response) {\n\t\tself._processResponse(response);\n\t};\n\n\t// Complete the URL object when necessary\n\tif (!options.pathname && options.path) {\n\t\tvar searchPos = options.path.indexOf('?');\n\t\tif (searchPos < 0) {\n\t\t\toptions.pathname = options.path;\n\t\t} else {\n\t\t\toptions.pathname = options.path.substring(0, searchPos);\n\t\t\toptions.search = options.path.substring(searchPos);\n\t\t}\n\t}\n\n\t// Perform the first request\n\tthis._performRequest();\n}\nRedirectableRequest.prototype = Object.create(Writable.prototype);\n\n// Executes the next native request (initial or redirect)\nRedirectableRequest.prototype._performRequest = function () {\n\t// If specified, use the agent corresponding to the protocol\n\t// (HTTP and HTTPS use different types of agents)\n\tvar protocol = this._options.protocol;\n\tif (this._options.agents) {\n\t\tthis._options.agent = this._options.agents[schemes[protocol]];\n\t}\n\n\t// Create the native request\n\tvar nativeProtocol = nativeProtocols[protocol];\n\tvar request = this._currentRequest =\n\t\t\t\tnativeProtocol.request(this._options, this._onNativeResponse);\n\tthis._currentUrl = url.format(this._options);\n\n\t// Set up event handlers\n\trequest._redirectable = this;\n\tfor (var event in eventHandlers) {\n\t\t/* istanbul ignore else */\n\t\tif (event) {\n\t\t\trequest.on(event, eventHandlers[event]);\n\t\t}\n\t}\n\n\t// End a redirected request\n\t// (The first request must be ended explicitly with RedirectableRequest#end)\n\tif (this._isRedirect) {\n\t\t// If the request doesn't have en entity, end directly.\n\t\tvar bufferedWrites = this._bufferedWrites;\n\t\tif (bufferedWrites.length === 0) {\n\t\t\trequest.end();\n\t\t// Otherwise, write the request entity and end afterwards.\n\t\t} else {\n\t\t\tvar i = 0;\n\t\t\t(function writeNext() {\n\t\t\t\tif (i < bufferedWrites.length) {\n\t\t\t\t\tvar bufferedWrite = bufferedWrites[i++];\n\t\t\t\t\trequest.write(bufferedWrite.data, bufferedWrite.encoding, writeNext);\n\t\t\t\t} else {\n\t\t\t\t\trequest.end();\n\t\t\t\t}\n\t\t\t})();\n\t\t}\n\t}\n};\n\n// Processes a response from the current native request\nRedirectableRequest.prototype._processResponse = function (response) {\n\t// RFC7231§6.4: The 3xx (Redirection) class of status code indicates\n\t// that further action needs to be taken by the user agent in order to\n\t// fulfill the request. If a Location header field is provided,\n\t// the user agent MAY automatically redirect its request to the URI\n\t// referenced by the Location field value,\n\t// even if the specific status code is not understood.\n\tvar location = response.headers.location;\n\tif (location && this._options.followRedirects !== false &&\n\t\t\tresponse.statusCode >= 300 && response.statusCode < 400) {\n\t\t// RFC7231§6.4: A client SHOULD detect and intervene\n\t\t// in cyclical redirections (i.e., \"infinite\" redirection loops).\n\t\tif (++this._redirectCount > this._options.maxRedirects) {\n\t\t\treturn this.emit('error', new Error('Max redirects exceeded.'));\n\t\t}\n\n\t\t// RFC7231§6.4: Automatic redirection needs to done with\n\t\t// care for methods not known to be safe […],\n\t\t// since the user might not wish to redirect an unsafe request.\n\t\t// RFC7231§6.4.7: The 307 (Temporary Redirect) status code indicates\n\t\t// that the target resource resides temporarily under a different URI\n\t\t// and the user agent MUST NOT change the request method\n\t\t// if it performs an automatic redirection to that URI.\n\t\tvar header;\n\t\tvar headers = this._options.headers;\n\t\tif (response.statusCode !== 307 && !(this._options.method in safeMethods)) {\n\t\t\tthis._options.method = 'GET';\n\t\t\t// Drop a possible entity and headers related to it\n\t\t\tthis._bufferedWrites = [];\n\t\t\tfor (header in headers) {\n\t\t\t\tif (/^content-/i.test(header)) {\n\t\t\t\t\tdelete headers[header];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Drop the Host header, as the redirect might lead to a different host\n\t\tif (!this._isRedirect) {\n\t\t\tfor (header in headers) {\n\t\t\t\tif (/^host$/i.test(header)) {\n\t\t\t\t\tdelete headers[header];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Perform the redirected request\n\t\tvar redirectUrl = url.resolve(this._currentUrl, location);\n\t\tdebug('redirecting to', redirectUrl);\n\t\tObject.assign(this._options, url.parse(redirectUrl));\n\t\tthis._isRedirect = true;\n\t\tthis._performRequest();\n\t} else {\n\t\t// The response is not a redirect; return it as-is\n\t\tresponse.responseUrl = this._currentUrl;\n\t\tthis.emit('response', response);\n\n\t\t// Clean up\n\t\tdelete this._options;\n\t\tdelete this._bufferedWrites;\n\t}\n};\n\n// Aborts the current native request\nRedirectableRequest.prototype.abort = function () {\n\tthis._currentRequest.abort();\n};\n\n// Flushes the headers of the current native request\nRedirectableRequest.prototype.flushHeaders = function () {\n\tthis._currentRequest.flushHeaders();\n};\n\n// Sets the noDelay option of the current native request\nRedirectableRequest.prototype.setNoDelay = function (noDelay) {\n\tthis._currentRequest.setNoDelay(noDelay);\n};\n\n// Sets the socketKeepAlive option of the current native request\nRedirectableRequest.prototype.setSocketKeepAlive = function (enable, initialDelay) {\n\tthis._currentRequest.setSocketKeepAlive(enable, initialDelay);\n};\n\n// Sets the timeout option of the current native request\nRedirectableRequest.prototype.setTimeout = function (timeout, callback) {\n\tthis._currentRequest.setTimeout(timeout, callback);\n};\n\n// Writes buffered data to the current native request\nRedirectableRequest.prototype.write = function (data, encoding, callback) {\n\tthis._currentRequest.write(data, encoding, callback);\n\tthis._bufferedWrites.push({data: data, encoding: encoding});\n};\n\n// Ends the current native request\nRedirectableRequest.prototype.end = function (data, encoding, callback) {\n\tthis._currentRequest.end(data, encoding, callback);\n\tif (data) {\n\t\tthis._bufferedWrites.push({data: data, encoding: encoding});\n\t}\n};\n\n// Export a redirecting wrapper for each native protocol\nObject.keys(nativeProtocols).forEach(function (protocol) {\n\tvar scheme = schemes[protocol] = protocol.substr(0, protocol.length - 1);\n\tvar nativeProtocol = nativeProtocols[protocol];\n\tvar wrappedProtocol = exports[scheme] = Object.create(nativeProtocol);\n\n\t// Executes an HTTP request, following redirects\n\twrappedProtocol.request = function (options, callback) {\n\t\tif (typeof options === 'string') {\n\t\t\toptions = url.parse(options);\n\t\t\toptions.maxRedirects = exports.maxRedirects;\n\t\t} else {\n\t\t\toptions = Object.assign({\n\t\t\t\tmaxRedirects: exports.maxRedirects,\n\t\t\t\tprotocol: protocol\n\t\t\t}, options);\n\t\t}\n\t\tassert.equal(options.protocol, protocol, 'protocol mismatch');\n\t\tdebug('options', options);\n\n\t\treturn new RedirectableRequest(options, callback);\n\t};\n\n\t// Executes a GET request, following redirects\n\twrappedProtocol.get = function (options, callback) {\n\t\tvar request = wrappedProtocol.request(options, callback);\n\t\trequest.end();\n\t\treturn request;\n\t};\n});\n"]}]}