@everymundo/promise-data-to
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -32,2 +32,3 @@ const EMUrl = require('./EMUrl.class') | ||
this.method = undefined | ||
this.timeout = undefined | ||
@@ -54,3 +55,3 @@ if (this.auth) { | ||
if (headers != null && !(headers instanceof Object)) { | ||
throw new Error(`headers must be an instance of Object! Got a ${headers.constructor.name}`) | ||
throw new Error(`headers must be an instance of Object! Got a ${headers.constructor.name} [${headers}]`) | ||
} | ||
@@ -57,0 +58,0 @@ |
@@ -12,5 +12,11 @@ const zlib = require('zlib') | ||
const getDataFromXData = (xData, compressMethod) => { | ||
const data = (xData && [Object, Array].indexOf(xData.constructor) > -1) ? JSON.stringify(xData) : xData | ||
const jsonTypesSet = new WeakSet([Object, Array]) | ||
const getProperDataFromInputData = (inputData, compressMethod) => { | ||
if (inputData == null) return inputData | ||
// const data = ([Object, Array].indexOf(inputData.constructor) > -1) ? JSON.stringify(inputData) : inputData | ||
const data = jsonTypesSet.has(inputData.constructor) | ||
? JSON.stringify(inputData) | ||
: inputData | ||
if (compressMethod && compression[compressMethod]) { | ||
@@ -24,4 +30,5 @@ return compression[compressMethod](data) | ||
module.exports = { | ||
getDataFromXData, | ||
getDataFromXData: getProperDataFromInputData, | ||
getProperDataFromInputData, | ||
compression | ||
} |
@@ -14,6 +14,7 @@ 'use strict' | ||
const MAX_RETRY_ATTEMPTS = Math.abs(process.env.MAX_RETRY_ATTEMPTS) || 3 | ||
const RETRY_TIMEOUT_MS = Math.abs(process.env.RETRY_TIMEOUT_MS) || 500 | ||
const REQUEST_TIMEOUT_MS = Math.abs(process.env.RETRY_TIMEOUT_MS) || 10000 | ||
const writeMethods = ['PUT', 'PATCH', 'POST'] | ||
const RETRY_TIMEOUT_MS = process.env.RETRY_TIMEOUT_MS && Math.abs(process.env.RETRY_TIMEOUT_MS) | ||
const REQUEST_TIMEOUT_MS = process.env.REQUEST_TIMEOUT_MS && Math.abs(process.env.REQUEST_TIMEOUT_MS) | ||
const writeMethods = new Set(['PUT', 'PATCH', 'POST']) | ||
const getEndpoint = (endpoint) => { | ||
@@ -69,4 +70,3 @@ if (!endpoint) { | ||
headers, | ||
agent: endpoint.agent, | ||
timeout | ||
agent: endpoint.agent | ||
} | ||
@@ -148,6 +148,20 @@ | ||
if (timeout != null) { | ||
if (Number.isNaN(+timeout)) { | ||
throw new Error(`timeout param is not a number [${timeout}]`) | ||
} | ||
request.on('socket', (socket) => { | ||
socket.on('timeout', () => { | ||
request.abort() | ||
}) | ||
socket.setTimeout(timeout) | ||
}) | ||
} | ||
// post the data | ||
if (writeMethods.indexOf(method.toUpperCase()) > -1) { | ||
if (writeMethods.has(method.toUpperCase())) { | ||
request.write(data) | ||
} | ||
request.end() | ||
@@ -154,0 +168,0 @@ } |
const setHeaders = (headers, data, compress) => { | ||
if (headers['content-type'] === undefined) { | ||
headers['content-type'] = 'application/json' | ||
} | ||
if (data) { | ||
@@ -10,3 +6,2 @@ headers['content-length'] = Buffer.byteLength(data) | ||
// if (compress && ['deflate', 'gzip'].includes(compress)) { | ||
if (compress && (compress === 'deflate' || compress === 'gzip')) { | ||
@@ -13,0 +8,0 @@ headers['content-encoding'] = compress |
'use strict' | ||
/* eslint-disable no-console */ | ||
/* | ||
const protos = { 'http:': require('http'), 'https:': require('https') } | ||
@@ -42,3 +43,6 @@ const { parseUrl } = require('../lib/parse-url') | ||
} | ||
*/ | ||
const Endpoint = require('../classes/Endpoint.class') | ||
const urlToEndpoint = (url, headers, agent) => new Endpoint(url, headers || null, agent) | ||
module.exports = { urlToEndpoint } |
{ | ||
"name": "@everymundo/promise-data-to", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "This is a helper to perform POST requests using promises and no external dependencies", | ||
@@ -12,4 +12,4 @@ "main": "index.js", | ||
"check-coverage": "nyc check-coverage --statements 100 --branches 100 --functions 100 --lines 100", | ||
"check-lint": "standard --env=mocha *.js lib/*.js test/*.js test/lib/*.js", | ||
"fix-lint": "standard --env=mocha --fix *.js lib/*.js test/*.js", | ||
"check-lint": "standard --env=mocha *.js lib/*.js test/*.js test/*/*.js", | ||
"fix-lint": "standard --env=mocha --fix *.js lib/*.js test/*.js test/*/*.js", | ||
"test": "env RETRY_TIMEOUT_MS= MAX_RETRY_ATTEMPTS= SIMULATE= LOG_LEVEL=silent mocha test --recursive" | ||
@@ -44,5 +44,5 @@ }, | ||
"ghooks": "^2.0.4", | ||
"mocha": "^7.0.1", | ||
"mocha": "^7.1.0", | ||
"nyc": "^15.0.0", | ||
"sinon": "^8.1.1", | ||
"sinon": "^9.0.0", | ||
"standard": "^14.3.1" | ||
@@ -49,0 +49,0 @@ }, |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
24224
625
4
11