Socket
Socket
Sign inDemoInstall

http2-proxy

Package Overview
Dependencies
192
Maintainers
1
Versions
193
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 5.0.34 to 5.0.35

benchmarks/hello.js

152

index.js
const net = require('net')
const http = require('http')
const https = require('https')
const compat = require('./compat')

@@ -29,99 +28,9 @@ const CONNECTION = 'connection'

module.exports = proxy
module.exports = compat(proxy)
proxy.ws = function ws (req, socket, head, options, callback) {
const promise = compat({ req, socket, head }, options)
if (!callback) {
return promise
}
// Legacy compat...
promise
.then(() => callback(null, req, socket, head))
.catch(err => callback(err, req, socket, head))
}
proxy.web = function web (req, res, options, callback) {
const promise = compat({ req, res }, options)
if (!callback) {
return promise
}
// Legacy compat...
promise
.then(() => callback(null, req, res))
.catch(err => callback(err, req, res))
}
async function compat (ctx, options) {
const { req, res } = ctx
const {
hostname,
port,
protocol,
timeout,
proxyTimeout,
proxyName,
onReq,
onRes
} = options
// Legacy compat...
if (timeout != null) {
req.setTimeout(timeout)
}
await proxy(
{ ...ctx, proxyName },
async ureq => {
ureq.hostname = hostname
ureq.port = port
ureq.timeout = proxyTimeout
if (onReq) {
if (onReq.length <= 2) {
return onReq(req, ureq)
} else {
// Legacy compat...
return new Promise((resolve, reject) => {
const promiseOrReq = onReq(req, ureq, (err, val) => err ? reject(err) : resolve(val))
if (promiseOrReq) {
if (promiseOrReq.then) {
promiseOrReq.then(resolve).catch(reject)
} else if (promiseOrReq.abort) {
resolve(promiseOrReq)
} else {
throw new Error('onReq must return a promise or a request object')
}
}
})
}
} else {
let agent
if (protocol == null || /^(http|ws):?$/.test(protocol)) {
agent = http
} else if (/^(http|ws)s:?$/.test(protocol)) {
agent = https
} else {
throw new HttpError(`invalid protocol`, null, 500)
}
return agent.request(ureq)
}
},
onRes ? async (proxyRes, headers) => {
proxyRes.headers = headers
if (onRes.length <= 3) {
return onRes(req, res, proxyRes)
} else {
// Legacy compat...
return new Promise((resolve, reject) => {
const promise = onRes(req, res, proxyRes, (err, val) => err ? reject(err) : resolve(val))
if (promise && promise.then) {
promise.then(resolve).catch(reject)
}
})
}
} : null
)
}
async function proxy ({ req, socket, res = socket, head, proxyName }, onReq, onRes) {
async function proxy (
{ req, socket, res = socket, head, proxyName },
onReq,
onRes
) {
if (req.aborted) {

@@ -169,3 +78,3 @@ return

const promise = new Promise((resolve, reject) => {
callback = err => err ? reject(err) : resolve()
callback = err => (err ? reject(err) : resolve())
})

@@ -253,4 +162,2 @@

.off('error', onComplete)
.off('data', onRequestData)
.off('end', onRequestEnd)

@@ -266,2 +173,4 @@ if (proxySocket) {

if (proxyReq) {
req.unpipe(proxyReq)
if (proxyReq.abort) {

@@ -279,25 +188,5 @@ proxyReq.abort()

this[kConnected] = true
this[kReq]
.on('data', onRequestData)
.on('end', onRequestEnd)
this
.on('drain', onProxyRequestDrain)
this[kReq].pipe(this)
}
function onRequestData (buf) {
if (!this[kProxyReq].write(buf)) {
this.pause()
}
}
function onRequestEnd () {
this[kProxyReq].end()
}
function onProxyRequestDrain () {
this[kReq].resume()
}
function onProxyReqError (err) {

@@ -320,5 +209,3 @@ err.statusCode = this[kConnected] ? 502 : 503

proxyRes
.on('aborted', onProxyResAborted)
.on('error', onProxyResError)
proxyRes.on('aborted', onProxyResAborted).on('error', onProxyResError)

@@ -333,3 +220,8 @@ if (this[kOnRes]) {

if (!proxyRes.upgrade) {
res.write(createHttpHeader(`HTTP/${proxyRes.httpVersion} ${proxyRes.statusCode} ${proxyRes.statusMessage}`, proxyRes.headers))
res.write(
createHttpHeader(
`HTTP/${proxyRes.httpVersion} ${proxyRes.statusCode} ${proxyRes.statusMessage}`,
proxyRes.headers
)
)
proxyRes.pipe(res)

@@ -342,5 +234,3 @@ }

}
proxyRes
.on('end', onProxyResEnd)
.pipe(res)
proxyRes.on('end', onProxyResEnd).pipe(res)
}

@@ -361,3 +251,5 @@ }

res.write(createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers))
res.write(
createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers)
)

@@ -364,0 +256,0 @@ proxySocket

{
"name": "http2-proxy",
"version": "5.0.34",
"version": "5.0.35",
"scripts": {
"dev": "nodemon --inspect=9308 src"
"test": "jest",
"lint": "eslint .",
"format": "prettier --write *.js && eslint --fix *.js"
},

@@ -16,6 +18,32 @@ "main": "index.js",

],
"eslintConfig": {
"extends": [
"standard"
],
"overrides": [
{
"files": [
"*.test.js"
],
"env": {
"jest": true
},
"plugins": [
"jest"
]
}
],
"parser": "babel-eslint"
},
"devDependencies": {
"babel-eslint": "^10.0.2",
"eslint": "^6.1.0",
"eslint-config-standard": "^13.0.1",
"eslint-plugin-jest": "^22.14.1",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"husky": "^3.0.0",
"nodemon": "^1.17.3",
"standard": "^13.1.0",
"jest": "^24.9.0",
"prettier": "^1.18.2",
"validate-commit-msg": "^2.14.0"

@@ -26,5 +54,8 @@ },

"commit-msg": "validate-commit-msg",
"pre-commit": "standard"
"pre-commit": "files=`git diff --cached --diff-filter=d --name-only | grep '\\.js$' || true` && ( [ -z \"$files\" ] || eslint --format=unix $files ) && yarn test"
}
},
"dependencies": {
"eslint-plugin-import": "^2.18.2"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc