supra-http
Advanced tools
Comparing version 1.0.8 to 1.1.0
@@ -18,9 +18,15 @@ "use strict"; | ||
const requestSender = (url, requestOptions) => { | ||
return this.http | ||
.request(url, requestOptions) | ||
.then(res => { | ||
if (options && options.json && res.body) { | ||
res.json = JSON.parse(res.body); | ||
} | ||
return res; | ||
return new Promise((resolve, reject) => { | ||
this.http | ||
.request(url, requestOptions, (err, res) => { | ||
if (err || !res) { | ||
reject(err); | ||
} | ||
else { | ||
if (options && options.json && res.body) { | ||
res.json = JSON.parse(res.body); | ||
} | ||
resolve(res); | ||
} | ||
}); | ||
}); | ||
@@ -27,0 +33,0 @@ }; |
@@ -5,5 +5,5 @@ /// <reference types="node" /> | ||
private static decompressStream; | ||
static handle(res: http.IncomingMessage): Promise<string>; | ||
static handle(res: http.IncomingMessage, cb: (err: Error | null, body?: string) => void): void; | ||
static getSupportedStreams(): string; | ||
} | ||
export { Compression }; |
@@ -8,27 +8,29 @@ "use strict"; | ||
class Compression { | ||
static decompressStream(res, handlerStream) { | ||
return new Promise((resolve, reject) => { | ||
let buffer = ''; | ||
const stream = handlerStream || res; | ||
stream.on('data', bufferChunk => { | ||
buffer += bufferChunk; | ||
}); | ||
stream.on('error', err => { | ||
reject(err); | ||
}); | ||
stream.on('end', (_) => { | ||
resolve(buffer); | ||
}); | ||
if (handlerStream) { | ||
res.pipe(handlerStream); | ||
} | ||
static decompressStream(res, cb, handlerStream) { | ||
let buffer = ''; | ||
const stream = handlerStream || res; | ||
stream.on('data', bufferChunk => { | ||
buffer += bufferChunk; | ||
}); | ||
stream.on('error', err => { | ||
cb(err); | ||
}); | ||
stream.on('end', (_) => { | ||
cb(null, buffer); | ||
}); | ||
if (handlerStream) { | ||
res.pipe(handlerStream); | ||
} | ||
} | ||
static handle(res) { | ||
static handle(res, cb) { | ||
const encoding = res.headers["content-encoding"]; | ||
if (encoding === 'gzip') | ||
return Compression.decompressStream(res, zlib_1.default.createGunzip()); | ||
if (encoding === 'br') | ||
return Compression.decompressStream(res, zlib_1.default.createBrotliDecompress()); | ||
return Compression.decompressStream(res); | ||
if (encoding === 'gzip') { | ||
Compression.decompressStream(res, cb, zlib_1.default.createGunzip()); | ||
} | ||
else if (encoding === 'br') { | ||
Compression.decompressStream(res, cb, zlib_1.default.createBrotliDecompress()); | ||
} | ||
else { | ||
Compression.decompressStream(res, cb); | ||
} | ||
} | ||
@@ -35,0 +37,0 @@ static getSupportedStreams() { |
@@ -8,5 +8,5 @@ /// <reference types="node" /> | ||
httpsAgent: https.Agent; | ||
request(url: string, requestOptions: HttpRequestOptions): Promise<ClientResponse>; | ||
request(url: string, requestOptions: HttpRequestOptions, cb: (err: null | Error, clientResponse?: ClientResponse) => void): void; | ||
private createRequestOptions; | ||
} | ||
export { Http }; |
@@ -20,3 +20,3 @@ "use strict"; | ||
} | ||
request(url, requestOptions) { | ||
request(url, requestOptions, cb) { | ||
const requestProvider = url.startsWith('https') ? { | ||
@@ -34,18 +34,20 @@ agent: this.httpsAgent, | ||
const options = this.createRequestOptions(url, requestOptions, requestProvider.agent, requestBody); | ||
return new Promise((resolve, reject) => { | ||
const request = requestProvider.client.request(options, response => { | ||
compression_1.Compression | ||
.handle(response) | ||
.then(body => resolve({ | ||
const request = requestProvider.client.request(options, response => { | ||
compression_1.Compression | ||
.handle(response, (err, body) => { | ||
if (err || !body) | ||
return cb(err); | ||
cb(null, { | ||
body, | ||
response | ||
})); | ||
}); | ||
}); | ||
request.on('error', reject); | ||
request.on('timeout', request.abort); | ||
if (requestBody) { | ||
request.write(requestBody); | ||
} | ||
request.end(); | ||
}); | ||
request | ||
.on('error', e => cb(e)) | ||
.on('timeout', request.abort); | ||
if (requestBody) { | ||
request.write(requestBody); | ||
} | ||
request.end(); | ||
} | ||
@@ -57,3 +59,4 @@ createRequestOptions(targetUrl, options, agent, bodyContent) { | ||
agent, | ||
hostname: url.host, | ||
hostname: url.hostname, | ||
port: url.port, | ||
protocol: url._protocol + ':', | ||
@@ -63,4 +66,4 @@ path: url.pathname + (url.search || ''), | ||
}; | ||
if (options.timeout) { | ||
mergedOptions.timeout = options.timeout; | ||
if (options.httpTimeout) { | ||
mergedOptions.timeout = options.httpTimeout; | ||
} | ||
@@ -67,0 +70,0 @@ if (options.json) { |
@@ -9,3 +9,3 @@ /// <reference types="node" /> | ||
headers?: Record<string, string>; | ||
timeout?: number; | ||
httpTimeout?: number; | ||
} | ||
@@ -12,0 +12,0 @@ interface RequestOptions extends CircuitBreaker.Options, HttpRequestOptions { |
{ | ||
"name": "supra-http", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"description": "Circuit breaking http client for NodeJs. And it is fast...", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
17024
243