@ejnshtein/smol-request
Advanced tools
Comparing version 1.0.9 to 1.0.10
{ | ||
"name": "@ejnshtein/smol-request", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "Tiny http/https request wrapper for Node.js using ESM (13.5 and newer)", | ||
@@ -27,5 +27,9 @@ "main": "Request.js", | ||
"form-data": "^3.0.0", | ||
"https-proxy-agent": "^5.0.0", | ||
"node-fetch": "^2.6.0", | ||
"proxy-agent": "^3.1.1", | ||
"proxying-agent": "^2.4.0", | ||
"socks-proxy-agent": "^5.0.0", | ||
"standard": "^14.3.3" | ||
} | ||
} |
@@ -9,3 +9,3 @@ // docs: https://nodejs.org/dist/latest-v10.x/docs/api/https.html#https_https_request_url_options_callback | ||
const pkg = JSON.parse(fs.readFileSync('./package.json')) | ||
const nativeRequestKeys = ['protocol', 'host', 'hostname', 'family', 'port', 'localAddres', 'socketPath', 'method', 'path', 'auth', 'agent', 'createConnection', 'timeout'] | ||
const nativeRequestKeys = ['agent', 'auth', 'createConnection', 'defaultPort', 'family', 'headers', 'host', 'hostname', 'insecureHTTPParser', 'localAddress', 'lookup', 'maxHeaderSize', 'method', 'path', 'port', 'protocol', 'setHost', 'socketPath', 'timeout'] | ||
@@ -46,6 +46,16 @@ export default function smolrequest (url, options = {}, formData = null) { | ||
const client = url.startsWith('https') ? https : http | ||
const result = { | ||
data: null, | ||
headers: null, | ||
status: null, | ||
statusText: null | ||
} | ||
const cleanRequestOptions = cleanObject(requestOptions, nativeRequestKeys) | ||
const req = client.request( | ||
url, | ||
cleanObject(requestOptions, nativeRequestKeys) | ||
cleanRequestOptions | ||
) | ||
req.on('error', onError) | ||
req.on('response', onResponse) | ||
req.on('close', onClose) | ||
if (requestOptions.headers) { | ||
@@ -55,14 +65,31 @@ Object.entries(requestOptions.headers) | ||
} | ||
const onError = err => { | ||
function onClose () { | ||
switch (requestOptions.responseType) { | ||
case 'buffer': { | ||
result.data = Buffer.concat(result.data) | ||
break | ||
} | ||
case 'json': { | ||
try { | ||
result.data = JSON.parse(result.data.join('')) | ||
} catch (e) { | ||
return reject(new Error(`JSON parsing error: ${e.message}: ${result.data}`)) | ||
} | ||
break | ||
} | ||
default: { | ||
result.data = result.data.join('') | ||
break | ||
} | ||
} | ||
resolve(result) | ||
} | ||
function onError (err) { | ||
req.removeListener('error', onError) | ||
reject(err) | ||
} | ||
req.on('error', onError) | ||
req.on('response', (res) => { | ||
const result = { | ||
data: null, | ||
headers: res.headers, | ||
status: res.statusCode, | ||
statusText: res.statusMessage | ||
} | ||
function onResponse (res) { | ||
result.headers = res.headers | ||
result.status = res.statusCode | ||
result.statusText = res.statusMessage | ||
if (requestOptions.responseType === 'headers') { | ||
@@ -86,23 +113,12 @@ return resolve(result) | ||
} | ||
const onClose = () => { | ||
const onRequestEnd = () => { | ||
stream.removeListener('error', onError) | ||
stream.removeListener('data', onData) | ||
stream.removeListener('end', onClose) | ||
if (requestOptions.responseType === 'buffer') { | ||
result.data = Buffer.concat(responseData) | ||
} else if (requestOptions.responseType === 'json') { | ||
try { | ||
result.data = JSON.parse(responseData.join('')) | ||
} catch (e) { | ||
return reject(new Error(`JSON parsing error: ${e.message}: ${responseData}`)) | ||
} | ||
} else { | ||
result.data = responseData.join('') | ||
} | ||
resolve(result) | ||
stream.removeListener('end', onRequestEnd) | ||
result.data = responseData | ||
} | ||
stream.on('data', onData) | ||
stream.on('error', onError) | ||
stream.on('end', onClose) | ||
}) | ||
stream.on('end', onRequestEnd) | ||
} | ||
if (body === null) { | ||
@@ -109,0 +125,0 @@ // body is null |
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
14898
278
7