@algolia/requester-node-http
Advanced tools
@@ -8,2 +8,3 @@ 'use strict'; | ||
| var URL = require('url'); | ||
| var zlib = require('zlib'); | ||
@@ -22,2 +23,8 @@ /* eslint functional/prefer-readonly-type: 0 */ | ||
| const path = url.query === null ? url.pathname : `${url.pathname}?${url.query}`; | ||
| const COMPRESSION_THRESHOLD = 750; | ||
| const acceptEncoding = request.headers['accept-encoding']; | ||
| const shouldCompress = request.data !== undefined && | ||
| Buffer.byteLength(request.data) >= COMPRESSION_THRESHOLD && | ||
| acceptEncoding !== undefined && | ||
| acceptEncoding.toLowerCase().includes('gzip'); | ||
| const options = { | ||
@@ -32,16 +39,33 @@ ...requesterOptions, | ||
| ...request.headers, | ||
| ...(shouldCompress ? { 'content-encoding': 'gzip' } : {}), | ||
| }, | ||
| ...(url.port !== undefined ? { port: url.port || '' } : {}), | ||
| }; | ||
| // eslint-disable-next-line functional/no-let, prefer-const | ||
| let connectTimeout; | ||
| // eslint-disable-next-line functional/no-let | ||
| let responseTimeout; | ||
| // eslint-disable-next-line functional/no-let | ||
| let gunzip; | ||
| const cleanup = () => { | ||
| clearTimeout(connectTimeout); | ||
| clearTimeout(responseTimeout); | ||
| if (gunzip) { | ||
| gunzip.destroy(); | ||
| } | ||
| }; | ||
| const onError = (error) => { | ||
| cleanup(); | ||
| resolve({ status: 0, content: error.message, isTimedOut: false }); | ||
| }; | ||
| const req = (url.protocol === 'https:' ? https : http).request(options, response => { | ||
| const contentEncoding = response.headers['content-encoding']; | ||
| const isGzipResponse = contentEncoding !== undefined && contentEncoding.toLowerCase().includes('gzip'); | ||
| // eslint-disable-next-line functional/no-let | ||
| let contentBuffers = []; | ||
| response.on('data', chunk => { | ||
| const onData = (chunk) => { | ||
| contentBuffers = contentBuffers.concat(chunk); | ||
| }); | ||
| response.on('end', () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
| clearTimeout(connectTimeout); | ||
| // eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
| clearTimeout(responseTimeout); | ||
| }; | ||
| const onEnd = () => { | ||
| cleanup(); | ||
| resolve({ | ||
@@ -52,3 +76,15 @@ status: response.statusCode || 0, | ||
| }); | ||
| }); | ||
| }; | ||
| response.on('error', onError); | ||
| if (isGzipResponse) { | ||
| gunzip = zlib.createGunzip(); | ||
| response.pipe(gunzip); | ||
| gunzip.on('data', onData); | ||
| gunzip.on('end', onEnd); | ||
| gunzip.on('error', onError); | ||
| } | ||
| else { | ||
| response.on('data', onData); | ||
| response.on('end', onEnd); | ||
| } | ||
| }); | ||
@@ -58,2 +94,5 @@ const createTimeout = (timeout, content) => { | ||
| req.abort(); | ||
| if (gunzip) { | ||
| gunzip.destroy(); | ||
| } | ||
| resolve({ | ||
@@ -66,10 +105,4 @@ status: 0, | ||
| }; | ||
| const connectTimeout = createTimeout(request.connectTimeout, 'Connection timeout'); | ||
| // eslint-disable-next-line functional/no-let | ||
| let responseTimeout; | ||
| req.on('error', error => { | ||
| clearTimeout(connectTimeout); | ||
| clearTimeout(responseTimeout); | ||
| resolve({ status: 0, content: error.message, isTimedOut: false }); | ||
| }); | ||
| connectTimeout = createTimeout(request.connectTimeout, 'Connection timeout'); | ||
| req.on('error', onError); | ||
| req.once('response', () => { | ||
@@ -79,6 +112,20 @@ clearTimeout(connectTimeout); | ||
| }); | ||
| if (request.data !== undefined) { | ||
| req.write(request.data); | ||
| if (request.data !== undefined && shouldCompress) { | ||
| zlib.gzip(request.data, (error, compressedBody) => { | ||
| if (error) { | ||
| onError(error); | ||
| return; | ||
| } | ||
| req.setHeader('content-length', compressedBody.byteLength); | ||
| req.write(compressedBody); | ||
| req.end(); | ||
| }); | ||
| } | ||
| req.end(); | ||
| else { | ||
| if (request.data !== undefined) { | ||
| req.setHeader('content-length', Buffer.byteLength(request.data)); | ||
| req.write(request.data); | ||
| } | ||
| req.end(); | ||
| } | ||
| }); | ||
@@ -85,0 +132,0 @@ }, |
@@ -6,2 +6,3 @@ import * as http from 'http'; | ||
| import { parse } from 'url'; | ||
| import { createGunzip, gzip } from 'zlib'; | ||
@@ -20,2 +21,8 @@ /* eslint functional/prefer-readonly-type: 0 */ | ||
| const path = url.query === null ? url.pathname : `${url.pathname}?${url.query}`; | ||
| const COMPRESSION_THRESHOLD = 750; | ||
| const acceptEncoding = request.headers['accept-encoding']; | ||
| const shouldCompress = request.data !== undefined && | ||
| Buffer.byteLength(request.data) >= COMPRESSION_THRESHOLD && | ||
| acceptEncoding !== undefined && | ||
| acceptEncoding.toLowerCase().includes('gzip'); | ||
| const options = { | ||
@@ -30,16 +37,33 @@ ...requesterOptions, | ||
| ...request.headers, | ||
| ...(shouldCompress ? { 'content-encoding': 'gzip' } : {}), | ||
| }, | ||
| ...(url.port !== undefined ? { port: url.port || '' } : {}), | ||
| }; | ||
| // eslint-disable-next-line functional/no-let, prefer-const | ||
| let connectTimeout; | ||
| // eslint-disable-next-line functional/no-let | ||
| let responseTimeout; | ||
| // eslint-disable-next-line functional/no-let | ||
| let gunzip; | ||
| const cleanup = () => { | ||
| clearTimeout(connectTimeout); | ||
| clearTimeout(responseTimeout); | ||
| if (gunzip) { | ||
| gunzip.destroy(); | ||
| } | ||
| }; | ||
| const onError = (error) => { | ||
| cleanup(); | ||
| resolve({ status: 0, content: error.message, isTimedOut: false }); | ||
| }; | ||
| const req = (url.protocol === 'https:' ? https : http).request(options, response => { | ||
| const contentEncoding = response.headers['content-encoding']; | ||
| const isGzipResponse = contentEncoding !== undefined && contentEncoding.toLowerCase().includes('gzip'); | ||
| // eslint-disable-next-line functional/no-let | ||
| let contentBuffers = []; | ||
| response.on('data', chunk => { | ||
| const onData = (chunk) => { | ||
| contentBuffers = contentBuffers.concat(chunk); | ||
| }); | ||
| response.on('end', () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
| clearTimeout(connectTimeout); | ||
| // eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
| clearTimeout(responseTimeout); | ||
| }; | ||
| const onEnd = () => { | ||
| cleanup(); | ||
| resolve({ | ||
@@ -50,3 +74,15 @@ status: response.statusCode || 0, | ||
| }); | ||
| }); | ||
| }; | ||
| response.on('error', onError); | ||
| if (isGzipResponse) { | ||
| gunzip = createGunzip(); | ||
| response.pipe(gunzip); | ||
| gunzip.on('data', onData); | ||
| gunzip.on('end', onEnd); | ||
| gunzip.on('error', onError); | ||
| } | ||
| else { | ||
| response.on('data', onData); | ||
| response.on('end', onEnd); | ||
| } | ||
| }); | ||
@@ -56,2 +92,5 @@ const createTimeout = (timeout, content) => { | ||
| req.abort(); | ||
| if (gunzip) { | ||
| gunzip.destroy(); | ||
| } | ||
| resolve({ | ||
@@ -64,10 +103,4 @@ status: 0, | ||
| }; | ||
| const connectTimeout = createTimeout(request.connectTimeout, 'Connection timeout'); | ||
| // eslint-disable-next-line functional/no-let | ||
| let responseTimeout; | ||
| req.on('error', error => { | ||
| clearTimeout(connectTimeout); | ||
| clearTimeout(responseTimeout); | ||
| resolve({ status: 0, content: error.message, isTimedOut: false }); | ||
| }); | ||
| connectTimeout = createTimeout(request.connectTimeout, 'Connection timeout'); | ||
| req.on('error', onError); | ||
| req.once('response', () => { | ||
@@ -77,6 +110,20 @@ clearTimeout(connectTimeout); | ||
| }); | ||
| if (request.data !== undefined) { | ||
| req.write(request.data); | ||
| if (request.data !== undefined && shouldCompress) { | ||
| gzip(request.data, (error, compressedBody) => { | ||
| if (error) { | ||
| onError(error); | ||
| return; | ||
| } | ||
| req.setHeader('content-length', compressedBody.byteLength); | ||
| req.write(compressedBody); | ||
| req.end(); | ||
| }); | ||
| } | ||
| req.end(); | ||
| else { | ||
| if (request.data !== undefined) { | ||
| req.setHeader('content-length', Buffer.byteLength(request.data)); | ||
| req.write(request.data); | ||
| } | ||
| req.end(); | ||
| } | ||
| }); | ||
@@ -83,0 +130,0 @@ }, |
+2
-2
| { | ||
| "name": "@algolia/requester-node-http", | ||
| "version": "4.25.3", | ||
| "version": "4.26.0", | ||
| "private": false, | ||
@@ -20,4 +20,4 @@ "description": "Promise-based request library for node using the native http module.", | ||
| "dependencies": { | ||
| "@algolia/requester-common": "4.25.3" | ||
| "@algolia/requester-common": "4.26.0" | ||
| } | ||
| } |
Unstable ownership
Supply chain riskA new collaborator has begun publishing package versions. Package stability and security risk may be elevated.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
13602
47.61%275
51.93%1
Infinity%9
12.5%+ Added
- Removed