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AAM,QAAQ,iCAAkC,EAAI,OAAQ,EAAI,KAChE,EAAO,aAAa,EAAK,IAGzB,EAAG,MAAM,EAEX,CAQA,SAAS,qBAAqB,EAAK,GAElC,MAAM,SACL,6CACA,EAAI,OACJ,EAAI,KAIL,IAEI,EAAU,CACb,qBAAsB,uBAEvB,EAAI,UAAU,IAAK,GACnB,EAAI,KACL","file":"/npm/proxy@1.0.2/proxy.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/proxy@1.0.2/proxy.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","/**\n * Module dependencies.\n */\n\nvar net = require('net');\nvar url = require('url');\nvar http = require('http');\nvar assert = require('assert');\nvar debug = require('debug')('proxy');\n\n// log levels\ndebug.request = require('debug')('proxy ← ← ←');\ndebug.response = require('debug')('proxy → → →');\ndebug.proxyRequest = require('debug')('proxy ↑ ↑ ↑');\ndebug.proxyResponse = require('debug')('proxy ↓ ↓ ↓');\n\n// hostname\nvar hostname = require('os').hostname();\n\n// proxy server version\nvar version = require('./package.json').version;\n\n/**\n * Module exports.\n */\n\nmodule.exports = setup;\n\n/**\n * Sets up an `http.Server` or `https.Server` instance with the necessary\n * \"request\" and \"connect\" event listeners in order to make the server act as an\n * HTTP proxy.\n *\n * @param {http.Server|https.Server} server\n * @param {Object} options\n * @api public\n */\n\nfunction setup(server, options) {\n\tif (!server) server = http.createServer();\n\tserver.on('request', onrequest);\n\tserver.on('connect', onconnect);\n\treturn server;\n}\n\n/**\n * 13.5.1 End-to-end and Hop-by-hop Headers\n *\n * Hop-by-hop headers must be removed by the proxy before passing it on to the\n * next endpoint. Per-request basis hop-by-hop headers MUST be listed in a\n * Connection header, (section 14.10) to be introduced into HTTP/1.1 (or later).\n */\n\nvar hopByHopHeaders = [\n\t'Connection',\n\t'Keep-Alive',\n\t'Proxy-Authenticate',\n\t'Proxy-Authorization',\n\t'TE',\n\t'Trailers',\n\t'Transfer-Encoding',\n\t'Upgrade'\n];\n\n// create a case-insensitive RegExp to match \"hop by hop\" headers\nvar isHopByHop = new RegExp('^(' + hopByHopHeaders.join('|') + ')$', 'i');\n\n/**\n * Iterator function for the request/response's \"headers\".\n * Invokes `fn` for \"each\" header entry in the request.\n *\n * @api private\n */\n\nfunction eachHeader(obj, fn) {\n\tif (Array.isArray(obj.rawHeaders)) {\n\t\t// ideal scenario... >= node v0.11.x\n\t\t// every even entry is a \"key\", every odd entry is a \"value\"\n\t\tvar key = null;\n\t\tobj.rawHeaders.forEach(function(v) {\n\t\t\tif (key === null) {\n\t\t\t\tkey = v;\n\t\t\t} else {\n\t\t\t\tfn(key, v);\n\t\t\t\tkey = null;\n\t\t\t}\n\t\t});\n\t} else {\n\t\t// otherwise we can *only* proxy the header names as lowercase'd\n\t\tvar headers = obj.headers;\n\t\tif (!headers) return;\n\t\tObject.keys(headers).forEach(function(key) {\n\t\t\tvar value = headers[key];\n\t\t\tif (Array.isArray(value)) {\n\t\t\t\t// set-cookie\n\t\t\t\tvalue.forEach(function(val) {\n\t\t\t\t\tfn(key, val);\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tfn(key, value);\n\t\t\t}\n\t\t});\n\t}\n}\n\n/**\n * HTTP GET/POST/DELETE/PUT, etc. proxy requests.\n */\n\nfunction onrequest(req, res) {\n\tdebug.request('%s %s HTTP/%s ', req.method, req.url, req.httpVersion);\n\tvar server = this;\n\tvar socket = req.socket;\n\n\t// pause the socket during authentication so no data is lost\n\tsocket.pause();\n\n\tauthenticate(server, req, function(err, auth) {\n\t\tsocket.resume();\n\t\tif (err) {\n\t\t\t// an error occured during login!\n\t\t\tres.writeHead(500);\n\t\t\tres.end((err.stack || err.message || err) + '\\n');\n\t\t\treturn;\n\t\t}\n\t\tif (!auth) return requestAuthorization(req, res);\n\t\tvar parsed = url.parse(req.url);\n\n\t\t// proxy the request HTTP method\n\t\tparsed.method = req.method;\n\n\t\t// setup outbound proxy request HTTP headers\n\t\tvar headers = {};\n\t\tvar hasXForwardedFor = false;\n\t\tvar hasVia = false;\n\t\tvar via = '1.1 ' + hostname + ' (proxy/' + version + ')';\n\n\t\tparsed.headers = headers;\n\t\teachHeader(req, function(key, value) {\n\t\t\tdebug.request('Request Header: \"%s: %s\"', key, value);\n\t\t\tvar keyLower = key.toLowerCase();\n\n\t\t\tif (!hasXForwardedFor && 'x-forwarded-for' === keyLower) {\n\t\t\t\t// append to existing \"X-Forwarded-For\" header\n\t\t\t\t// http://en.wikipedia.org/wiki/X-Forwarded-For\n\t\t\t\thasXForwardedFor = true;\n\t\t\t\tvalue += ', ' + socket.remoteAddress;\n\t\t\t\tdebug.proxyRequest(\n\t\t\t\t\t'appending to existing \"%s\" header: \"%s\"',\n\t\t\t\t\tkey,\n\t\t\t\t\tvalue\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (!hasVia && 'via' === keyLower) {\n\t\t\t\t// append to existing \"Via\" header\n\t\t\t\thasVia = true;\n\t\t\t\tvalue += ', ' + via;\n\t\t\t\tdebug.proxyRequest(\n\t\t\t\t\t'appending to existing \"%s\" header: \"%s\"',\n\t\t\t\t\tkey,\n\t\t\t\t\tvalue\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (isHopByHop.test(key)) {\n\t\t\t\tdebug.proxyRequest('ignoring hop-by-hop header \"%s\"', key);\n\t\t\t} else {\n\t\t\t\tvar v = headers[key];\n\t\t\t\tif (Array.isArray(v)) {\n\t\t\t\t\tv.push(value);\n\t\t\t\t} else if (null != v) {\n\t\t\t\t\theaders[key] = [v, value];\n\t\t\t\t} else {\n\t\t\t\t\theaders[key] = value;\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\t// add \"X-Forwarded-For\" header if it's still not here by now\n\t\t// http://en.wikipedia.org/wiki/X-Forwarded-For\n\t\tif (!hasXForwardedFor) {\n\t\t\theaders['X-Forwarded-For'] = socket.remoteAddress;\n\t\t\tdebug.proxyRequest(\n\t\t\t\t'adding new \"X-Forwarded-For\" header: \"%s\"',\n\t\t\t\theaders['X-Forwarded-For']\n\t\t\t);\n\t\t}\n\n\t\t// add \"Via\" header if still not set by now\n\t\tif (!hasVia) {\n\t\t\theaders.Via = via;\n\t\t\tdebug.proxyRequest('adding new \"Via\" header: \"%s\"', headers.Via);\n\t\t}\n\n\t\t// custom `http.Agent` support, set `server.agent`\n\t\tvar agent = server.agent;\n\t\tif (null != agent) {\n\t\t\tdebug.proxyRequest(\n\t\t\t\t'setting custom `http.Agent` option for proxy request: %s',\n\t\t\t\tagent\n\t\t\t);\n\t\t\tparsed.agent = agent;\n\t\t\tagent = null;\n\t\t}\n\n\t\tif (null == parsed.port) {\n\t\t\t// default the port number if not specified, for >= node v0.11.6...\n\t\t\t// https://github.com/joyent/node/issues/6199\n\t\t\tparsed.port = 80;\n\t\t}\n\n\t\tif ('http:' != parsed.protocol) {\n\t\t\t// only \"http://\" is supported, \"https://\" should use CONNECT method\n\t\t\tres.writeHead(400);\n\t\t\tres.end('Only \"http:\" protocol prefix is supported\\n');\n\t\t\treturn;\n\t\t}\n\n\t\tif (server.localAddress) {\n\t\t\tparsed.localAddress = server.localAddress;\n\t\t}\n\n\t\tvar gotResponse = false;\n\t\tvar proxyReq = http.request(parsed);\n\t\tdebug.proxyRequest('%s %s HTTP/1.1 ', proxyReq.method, proxyReq.path);\n\n\t\tproxyReq.on('response', function(proxyRes) {\n\t\t\tdebug.proxyResponse('HTTP/1.1 %s', proxyRes.statusCode);\n\t\t\tgotResponse = true;\n\n\t\t\tvar headers = {};\n\t\t\teachHeader(proxyRes, function(key, value) {\n\t\t\t\tdebug.proxyResponse(\n\t\t\t\t\t'Proxy Response Header: \"%s: %s\"',\n\t\t\t\t\tkey,\n\t\t\t\t\tvalue\n\t\t\t\t);\n\t\t\t\tif (isHopByHop.test(key)) {\n\t\t\t\t\tdebug.response('ignoring hop-by-hop header \"%s\"', key);\n\t\t\t\t} else {\n\t\t\t\t\tvar v = headers[key];\n\t\t\t\t\tif (Array.isArray(v)) {\n\t\t\t\t\t\tv.push(value);\n\t\t\t\t\t} else if (null != v) {\n\t\t\t\t\t\theaders[key] = [v, value];\n\t\t\t\t\t} else {\n\t\t\t\t\t\theaders[key] = value;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tdebug.response('HTTP/1.1 %s', proxyRes.statusCode);\n\t\t\tres.writeHead(proxyRes.statusCode, headers);\n\t\t\tproxyRes.pipe(res);\n\t\t\tres.on('finish', onfinish);\n\t\t});\n\t\tproxyReq.on('error', function(err) {\n\t\t\tdebug.proxyResponse(\n\t\t\t\t'proxy HTTP request \"error\" event\\n%s',\n\t\t\t\terr.stack || err\n\t\t\t);\n\t\t\tcleanup();\n\t\t\tif (gotResponse) {\n\t\t\t\tdebug.response(\n\t\t\t\t\t'already sent a response, just destroying the socket...'\n\t\t\t\t);\n\t\t\t\tsocket.destroy();\n\t\t\t} else if ('ENOTFOUND' == err.code) {\n\t\t\t\tdebug.response('HTTP/1.1 404 Not Found');\n\t\t\t\tres.writeHead(404);\n\t\t\t\tres.end();\n\t\t\t} else {\n\t\t\t\tdebug.response('HTTP/1.1 500 Internal Server Error');\n\t\t\t\tres.writeHead(500);\n\t\t\t\tres.end();\n\t\t\t}\n\t\t});\n\n\t\t// if the client closes the connection prematurely,\n\t\t// then close the upstream socket\n\t\tfunction onclose() {\n\t\t\tdebug.request(\n\t\t\t\t'client socket \"close\" event, aborting HTTP request to \"%s\"',\n\t\t\t\treq.url\n\t\t\t);\n\t\t\tproxyReq.abort();\n\t\t\tcleanup();\n\t\t}\n\t\tsocket.on('close', onclose);\n\n\t\tfunction onfinish() {\n\t\t\tdebug.response('\"finish\" event');\n\t\t\tcleanup();\n\t\t}\n\n\t\tfunction cleanup() {\n\t\t\tdebug.response('cleanup');\n\t\t\tsocket.removeListener('close', onclose);\n\t\t\tres.removeListener('finish', onfinish);\n\t\t}\n\n\t\treq.pipe(proxyReq);\n\t});\n}\n\n/**\n * HTTP CONNECT proxy requests.\n */\n\nfunction onconnect(req, socket, head) {\n\tdebug.request('%s %s HTTP/%s ', req.method, req.url, req.httpVersion);\n\tassert(\n\t\t!head || 0 == head.length,\n\t\t'\"head\" should be empty for proxy requests'\n\t);\n\n\tvar res;\n\tvar target;\n\tvar gotResponse = false;\n\n\t// define request socket event listeners\n\tsocket.on('close', function onclientclose() {\n\t\tdebug.request('HTTP request %s socket \"close\" event', req.url);\n\t});\n\n\tsocket.on('end', function onclientend() {\n\t\tdebug.request('HTTP request %s socket \"end\" event', req.url);\n\t});\n\n\tsocket.on('error', function onclienterror(err) {\n\t\tdebug.request(\n\t\t\t'HTTP request %s socket \"error\" event:\\n%s',\n\t\t\treq.url,\n\t\t\terr.stack || err\n\t\t);\n\t});\n\n\t// define target socket event listeners\n\tfunction ontargetclose() {\n\t\tdebug.proxyResponse('proxy target %s \"close\" event', req.url);\n\t\tsocket.destroy();\n\t}\n\n\tfunction ontargetend() {\n\t\tdebug.proxyResponse('proxy target %s \"end\" event', req.url);\n\t}\n\n\tfunction ontargeterror(err) {\n\t\tdebug.proxyResponse(\n\t\t\t'proxy target %s \"error\" event:\\n%s',\n\t\t\treq.url,\n\t\t\terr.stack || err\n\t\t);\n\t\tif (gotResponse) {\n\t\t\tdebug.response(\n\t\t\t\t'already sent a response, just destroying the socket...'\n\t\t\t);\n\t\t\tsocket.destroy();\n\t\t} else if ('ENOTFOUND' == err.code) {\n\t\t\tdebug.response('HTTP/1.1 404 Not Found');\n\t\t\tres.writeHead(404);\n\t\t\tres.end();\n\t\t} else {\n\t\t\tdebug.response('HTTP/1.1 500 Internal Server Error');\n\t\t\tres.writeHead(500);\n\t\t\tres.end();\n\t\t}\n\t}\n\n\tfunction ontargetconnect() {\n\t\tdebug.proxyResponse('proxy target %s \"connect\" event', req.url);\n\t\tdebug.response('HTTP/1.1 200 Connection established');\n\t\tgotResponse = true;\n\t\tres.removeListener('finish', onfinish);\n\n\t\tres.writeHead(200, 'Connection established');\n\t\tres.flushHeaders();\n\n\t\t// relinquish control of the `socket` from the ServerResponse instance\n\t\tres.detachSocket(socket);\n\n\t\t// nullify the ServerResponse object, so that it can be cleaned\n\t\t// up before this socket proxying is completed\n\t\tres = null;\n\n\t\tsocket.pipe(target);\n\t\ttarget.pipe(socket);\n\t}\n\n\t// create the `res` instance for this request since Node.js\n\t// doesn't provide us with one :(\n\t// XXX: this is undocumented API, so it will break some day (ノಠ益ಠ)ノ彡┻━┻\n\tres = new http.ServerResponse(req);\n\tres.shouldKeepAlive = false;\n\tres.chunkedEncoding = false;\n\tres.useChunkedEncodingByDefault = false;\n\tres.assignSocket(socket);\n\n\t// called for the ServerResponse's \"finish\" event\n\t// XXX: normally, node's \"http\" module has a \"finish\" event listener that would\n\t// take care of closing the socket once the HTTP response has completed, but\n\t// since we're making this ServerResponse instance manually, that event handler\n\t// never gets hooked up, so we must manually close the socket...\n\tfunction onfinish() {\n\t\tdebug.response('response \"finish\" event');\n\t\tres.detachSocket(socket);\n\t\tsocket.end();\n\t}\n\tres.once('finish', onfinish);\n\n\t// pause the socket during authentication so no data is lost\n\tsocket.pause();\n\n\tauthenticate(this, req, function(err, auth) {\n\t\tsocket.resume();\n\t\tif (err) {\n\t\t\t// an error occured during login!\n\t\t\tres.writeHead(500);\n\t\t\tres.end((err.stack || err.message || err) + '\\n');\n\t\t\treturn;\n\t\t}\n\t\tif (!auth) return requestAuthorization(req, res);\n\n\t\tvar parts = req.url.split(':');\n\t\tvar host = parts[0];\n\t\tvar port = +parts[1];\n\t\tvar opts = { host: host, port: port };\n\n\t\tdebug.proxyRequest('connecting to proxy target %j', opts);\n\t\ttarget = net.connect(opts);\n\t\ttarget.on('connect', ontargetconnect);\n\t\ttarget.on('close', ontargetclose);\n\t\ttarget.on('error', ontargeterror);\n\t\ttarget.on('end', ontargetend);\n\t});\n}\n\n/**\n * Checks `Proxy-Authorization` request headers. Same logic applied to CONNECT\n * requests as well as regular HTTP requests.\n *\n * @param {http.Server} server\n * @param {http.ServerRequest} req\n * @param {Function} fn callback function\n * @api private\n */\n\nfunction authenticate(server, req, fn) {\n\tvar hasAuthenticate = 'function' == typeof server.authenticate;\n\tif (hasAuthenticate) {\n\t\tdebug.request('authenticating request \"%s %s\"', req.method, req.url);\n\t\tserver.authenticate(req, fn);\n\t} else {\n\t\t// no `server.authenticate()` function, so just allow the request\n\t\tfn(null, true);\n\t}\n}\n\n/**\n * Sends a \"407 Proxy Authentication Required\" HTTP response to the `socket`.\n *\n * @api private\n */\n\nfunction requestAuthorization(req, res) {\n\t// request Basic proxy authorization\n\tdebug.response(\n\t\t'requesting proxy authorization for \"%s %s\"',\n\t\treq.method,\n\t\treq.url\n\t);\n\n\t// TODO: make \"realm\" and \"type\" (Basic) be configurable...\n\tvar realm = 'proxy';\n\n\tvar headers = {\n\t\t'Proxy-Authenticate': 'Basic realm=\"' + realm + '\"'\n\t};\n\tres.writeHead(407, headers);\n\tres.end();\n}\n"]}