Comparing version 0.4.11 to 0.4.12
@@ -22,2 +22,5 @@ const zlib = require('zlib') | ||
// Cache http.ServerResponse prototype chain for future use | ||
const resProto = Object.getPrototypeOf(res) | ||
var buf = [] | ||
@@ -99,3 +102,3 @@ var length = 0 | ||
function finisher (cb, err, body, encoding) { | ||
if (isEnded()) return cb && cb() | ||
if (isEnded() || res.headersSent) return cb && cb() | ||
@@ -109,24 +112,19 @@ if (err) { | ||
const finalEncoding = encoding || res._originalEncoding | ||
var finalBody = body || res.body | ||
res.body = body == null ? res.body : body | ||
if (isGzip(res, headArgs)) { | ||
finalBody = deflate(finalBody) | ||
res.body = deflate(res.body) | ||
} | ||
// Set the proper new content length | ||
if (finalBody) { | ||
res.setHeader('content-length', Buffer.byteLength(finalBody)) | ||
} | ||
// Set content length response header | ||
setContentLength(res) | ||
// Write the response head | ||
if (headArgs && !res.headersSent) { | ||
res.writeHead(headArgs) | ||
res.writeHead.apply(res, headArgs) | ||
} | ||
// Write body | ||
res.write(finalBody, finalEncoding) | ||
resProto.write.call(res, res.body, finalEncoding) | ||
// Be sure we expose the final body in the response | ||
if (finalBody) res.body = finalBody | ||
// Clean references to prevent leaks | ||
@@ -136,3 +134,3 @@ cleanup() | ||
// Send EOF | ||
res.end(cb) | ||
resProto.end.call(res, cb) | ||
} | ||
@@ -161,2 +159,12 @@ | ||
function setContentLength (res) { | ||
if (typeof res.body === 'string') { | ||
return res.setHeader('content-length', Buffer.byteLength(res.body)) | ||
} | ||
if (Buffer.isBuffer(res.body)) { | ||
return res.setHeader('content-length', res.body.length) | ||
} | ||
res.setHeader('content-length', 0) | ||
} | ||
function parseJSON (res) { | ||
@@ -173,3 +181,3 @@ if (res.json) return | ||
function isGzip (res, head) { | ||
return res.getHeader('content-encoding') === 'gzip' || | ||
return (res.body && res.getHeader('content-encoding') === 'gzip') || | ||
(head && head[1] && head[1]['content-encoding'] === 'gzip') || | ||
@@ -176,0 +184,0 @@ false |
{ | ||
"name": "rocky", | ||
"version": "0.4.11", | ||
"version": "0.4.12", | ||
"description": "Full-featured, middleware-oriented, hackable HTTP and WebSocket proxy", | ||
@@ -5,0 +5,0 @@ "repository": "h2non/rocky", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
187668
4841