http2-proxy
Advanced tools
Comparing version 0.2.13 to 0.2.14
75
index.js
@@ -268,3 +268,3 @@ const http2 = require('http2') | ||
handler.callback = callback | ||
handler.req.on('close', handler._release) | ||
OnFinished.create(req, handler._release) | ||
return handler._handle | ||
@@ -342,3 +342,3 @@ } | ||
handler.errorHandler = errorHandler | ||
handler.req.on('close', handler._release) | ||
OnFinished.create(req, handler._release) | ||
return handler._handle | ||
@@ -372,26 +372,30 @@ } | ||
if (this.resOrSocket instanceof net.Socket) { | ||
const socket = this.resOrSocket | ||
if (this.onRes) { | ||
this.onRes(this.req, this.resOrSocket) | ||
this.onRes(this.req, socket) | ||
} | ||
if (!proxyRes.upgrade) { | ||
this.resOrSocket.end() | ||
socket.end() | ||
} | ||
} else { | ||
const res = this.resOrSocket | ||
setupHeaders(proxyRes.headers) | ||
this.resOrSocket.statusCode = proxyRes.statusCode | ||
res.statusCode = proxyRes.statusCode | ||
for (const key of Object.keys(proxyRes.headers)) { | ||
this.resOrSocket.setHeader(key, proxyRes.headers[key]) | ||
res.setHeader(key, proxyRes.headers[key]) | ||
} | ||
if (this.onRes) { | ||
this.onRes(this.req, this.resOrSocket) | ||
this.onRes(this.req, res) | ||
} | ||
this.resOrSocket.writeHead(this.resOrSocket.statusCode) | ||
proxyRes.on('end', this._addTrailers) | ||
res.writeHead(res.statusCode) | ||
proxyRes | ||
.on('end', this._addTrailers) | ||
.on('error', this.proxyErrorHandler) | ||
.pipe(this.resOrSocket) | ||
.pipe(res) | ||
} | ||
@@ -421,3 +425,3 @@ } | ||
handler.proxyRes = null | ||
handler.req.on('close', handler._release) | ||
OnFinished.create(req, handler._release) | ||
return handler._handle | ||
@@ -431,3 +435,3 @@ } | ||
this.req = null | ||
this.resOrSocket = null | ||
this.socket = null | ||
this.proxyErrorHandler = null | ||
@@ -467,3 +471,3 @@ this.proxyRes = null | ||
this.resOrSocket.write(head) | ||
this.socket.write(head) | ||
@@ -474,3 +478,3 @@ proxyRes.on('error', this.proxyErrorHandler) | ||
.on('error', this.proxyErrorHandler) | ||
.pipe(this.resOrSocket) | ||
.pipe(this.socket) | ||
.pipe(proxySocket) | ||
@@ -488,3 +492,3 @@ } | ||
this.req = null | ||
this.resOrSocket = null | ||
this.socket = null | ||
this.proxyErrorHandler = null | ||
@@ -497,8 +501,8 @@ this.proxyRes = null | ||
static create (req, resOrSocket, proxyErrorHandler) { | ||
static create (req, socket, proxyErrorHandler) { | ||
const handler = ProxyUpgradeHandler.pool.pop() || new ProxyUpgradeHandler() | ||
handler.req = req | ||
handler.resOrSocket = resOrSocket | ||
handler.socket = socket | ||
handler.proxyErrorHandler = proxyErrorHandler | ||
handler.req.on('close', handler._release) | ||
OnFinished.create(req, handler._release) | ||
return handler._handle | ||
@@ -508,1 +512,36 @@ } | ||
ProxyUpgradeHandler.pool = [] | ||
class OnFinished { | ||
constructor () { | ||
this.req = null | ||
this.callback = null | ||
this._handle = this._handle.bind(this) | ||
} | ||
_handle () { | ||
if (this.req.stream) { | ||
this.req.stream.removeListener('streamClosed', this._handle) | ||
} else { | ||
this.req.removeListener('close', this._handle) | ||
} | ||
this.req.removeListener('error', this._handle) | ||
this.callback = null | ||
OnFinished.pool.push(this) | ||
} | ||
static create (req, callback) { | ||
const onFinished = OnFinished.pool.pop() || new OnFinished() | ||
onFinished.req = req | ||
onFinished.callback = callback | ||
if (req.stream) { | ||
req.stream.on('streamClosed', onFinished._handle) | ||
} else { | ||
req.on('close', onFinished._handle) | ||
} | ||
req.on('error', onFinished._handle) | ||
} | ||
} | ||
OnFinished.pool = [] |
{ | ||
"name": "http2-proxy", | ||
"version": "0.2.13", | ||
"version": "0.2.14", | ||
"scripts": { | ||
@@ -5,0 +5,0 @@ "dev": "nodemon --inspect=9308 --expose-http2 src", |
20016
437