appolo-agent
Advanced tools
Comparing version 6.0.25 to 6.0.27
@@ -13,2 +13,5 @@ "use strict"; | ||
}; | ||
const emptyMethods = { | ||
HEAD: true, | ||
}; | ||
let proto = http.ServerResponse.prototype; | ||
@@ -102,3 +105,19 @@ proto.status = function (code) { | ||
proto.gzip = function () { | ||
this.useGzip = true; | ||
let old = this.send, $self = this; | ||
this.send = function (data) { | ||
if (!data) { | ||
old.call($self, data); | ||
return; | ||
} | ||
this.sending = true; | ||
data = checkHeaders.call(this, data); | ||
zlib.gzip(data, (err, gziped) => { | ||
if (err) { | ||
old.call($self, data); | ||
return; | ||
} | ||
$self.setHeader('Content-Encoding', "gzip"); | ||
old.call($self, gziped); | ||
}); | ||
}; | ||
return this; | ||
@@ -121,11 +140,3 @@ }; | ||
}; | ||
function send(data) { | ||
this.sending = true; | ||
let isEmptyStatusCode = statusEmpty[this.statusCode || (this.statusCode = 200)]; | ||
//send empty | ||
if (isEmptyStatusCode || data == undefined) { | ||
this.setHeader('Content-Length', '0'); | ||
this.end(); | ||
return; | ||
} | ||
function checkHeaders(data) { | ||
if (!this.hasHeader("Content-Type")) { | ||
@@ -143,9 +154,16 @@ if (typeof data === 'string' || this.getHeader("Content-Encoding") == "gzip") { | ||
} | ||
//check if need to gzip | ||
if (this.useGzip && data) { | ||
gzipResponse(this, data); | ||
return data; | ||
} | ||
function send(data) { | ||
this.sending = true; | ||
let isEmptyStatusCode = statusEmpty[this.statusCode || (this.statusCode = 200)]; | ||
//send empty | ||
if (isEmptyStatusCode || data == undefined) { | ||
this.setHeader('Content-Length', '0'); | ||
this.end(); | ||
return; | ||
} | ||
data = checkHeaders.call(this, data); | ||
this.setHeader('Content-Length', Buffer.byteLength(data, 'utf8')); | ||
this.req.method[0] == 'H' ? this.end() : this.end(data); | ||
this.end(data); | ||
} | ||
@@ -158,13 +176,2 @@ function sendMiddleware(middlewares, middlewaresError, data) { | ||
proto.send = send; | ||
function gzipResponse(res, data) { | ||
zlib.gzip(data, (err, gziped) => { | ||
res.useGzip = false; | ||
if (err) { | ||
res.send(data); | ||
return; | ||
} | ||
res.setHeader('Content-Encoding', "gzip"); | ||
res.send(gziped); | ||
}); | ||
} | ||
function createResponse(request, response) { | ||
@@ -171,0 +178,0 @@ let res = response; |
@@ -13,2 +13,5 @@ import http = require('http'); | ||
}; | ||
const emptyMethods = { | ||
HEAD: true, | ||
}; | ||
@@ -23,3 +26,2 @@ | ||
req: IRequest | ||
useGzip: boolean; | ||
sending: boolean | ||
@@ -176,3 +178,28 @@ | ||
proto.gzip = function () { | ||
this.useGzip = true; | ||
let old = this.send, $self = this; | ||
this.send = function (data) { | ||
if (!data) { | ||
old.call($self, data); | ||
return; | ||
} | ||
this.sending = true; | ||
data = checkHeaders.call(this, data); | ||
zlib.gzip(data, (err, gziped) => { | ||
if (err) { | ||
old.call($self, data); | ||
return; | ||
} | ||
$self.setHeader('Content-Encoding', "gzip"); | ||
old.call($self, gziped); | ||
}); | ||
}; | ||
return this; | ||
@@ -201,2 +228,16 @@ }; | ||
function checkHeaders(data: string | Buffer | any) { | ||
if (!this.hasHeader("Content-Type")) { | ||
if (typeof data === 'string' || this.getHeader("Content-Encoding") == "gzip") { | ||
this.setHeader("Content-Type", "text/plain;charset=utf-8"); | ||
} else if (Buffer.isBuffer(data)) { | ||
this.setHeader("Content-Type", "application/octet-stream"); | ||
} else { | ||
data = JSON.stringify(data); | ||
this.setHeader("Content-Type", "application/json; charset=utf-8"); | ||
} | ||
} | ||
return data; | ||
} | ||
function send(data?: string | Buffer | any) { | ||
@@ -215,22 +256,7 @@ | ||
if (!this.hasHeader("Content-Type")) { | ||
if (typeof data === 'string' || this.getHeader("Content-Encoding") == "gzip") { | ||
this.setHeader("Content-Type", "text/plain;charset=utf-8"); | ||
} else if (Buffer.isBuffer(data)) { | ||
this.setHeader("Content-Type", "application/octet-stream"); | ||
} else { | ||
data = JSON.stringify(data); | ||
this.setHeader("Content-Type", "application/json; charset=utf-8"); | ||
} | ||
} | ||
data = checkHeaders.call(this, data); | ||
//check if need to gzip | ||
if (this.useGzip && data) { | ||
gzipResponse(this, data); | ||
return; | ||
} | ||
this.setHeader('Content-Length', Buffer.byteLength(data as string, 'utf8')); | ||
this.req.method[0] == 'H' ? this.end() : this.end(data); | ||
this.end(data); | ||
} | ||
@@ -245,17 +271,2 @@ | ||
function gzipResponse(res: IResponse, data: any) { | ||
zlib.gzip(data, (err, gziped) => { | ||
res.useGzip = false; | ||
if (err) { | ||
res.send(data); | ||
return; | ||
} | ||
res.setHeader('Content-Encoding', "gzip"); | ||
res.send(gziped) | ||
}); | ||
} | ||
export function createResponse(request: http.IncomingMessage, response: http.ServerResponse): IResponse { | ||
@@ -262,0 +273,0 @@ let res = response as IResponse; |
@@ -27,3 +27,3 @@ { | ||
"main": "./index.js", | ||
"version": "6.0.25", | ||
"version": "6.0.27", | ||
"license": "MIT", | ||
@@ -30,0 +30,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
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
100750
1699