@ejnshtein/smol-request
Advanced tools
Comparing version 1.0.11 to 1.0.12-b
{ | ||
"name": "@ejnshtein/smol-request", | ||
"version": "1.0.11", | ||
"version": "1.0.12b", | ||
"description": "Tiny http/https request wrapper for Node.js using ESM (13.5 and newer)", | ||
"main": "Request.js", | ||
"type": "module", | ||
"main": "Request.cjs", | ||
"exports": { | ||
"import": "./Request.js", | ||
"require": "./Request.cjs" | ||
}, | ||
"scripts": {}, | ||
@@ -21,3 +26,2 @@ "repository": { | ||
}, | ||
"type": "module", | ||
"typings": "./typings/index.d.ts", | ||
@@ -24,0 +28,0 @@ "author": "ejnshtein", |
141
Request.js
@@ -1,140 +0,3 @@ | ||
// docs: https://nodejs.org/dist/latest-v10.x/docs/api/https.html#https_https_request_url_options_callback | ||
import qs from 'querystring' | ||
import https from 'https' | ||
import http from 'http' | ||
import fs from 'fs' | ||
import { createUnzip } from 'zlib' | ||
import { cleanObject, mergeUrl, deepmerge, isBlob } from './lib/index.js' | ||
const pkg = JSON.parse(fs.readFileSync('./package.json')) | ||
const nativeRequestKeys = ['agent', 'auth', 'createConnection', 'defaultPort', 'family', 'headers', 'host', 'hostname', 'insecureHTTPParser', 'localAddress', 'lookup', 'maxHeaderSize', 'method', 'path', 'port', 'protocol', 'setHost', 'socketPath', 'timeout'] | ||
import request from './Request.cjs' | ||
export default function smolrequest (url, options = {}, formData = null) { | ||
const [body, dataIsObject] = Object.prototype.toString.call(formData) === '[object Object]' ? [qs.stringify(formData), true] : [formData, false] | ||
const mergedOptions = [ | ||
{ | ||
method: 'GET', | ||
responseType: 'text', | ||
headers: { | ||
'User-Agent': `smol-request/${pkg.version}`, | ||
Accept: '*/*' | ||
} | ||
} | ||
] | ||
if (dataIsObject && body) { | ||
mergedOptions.push( | ||
{ headers: { 'Content-Length': Buffer.byteLength(body) } } | ||
) | ||
} | ||
if (dataIsObject && typeof body === 'string') { | ||
mergedOptions.push({ | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' } | ||
}) | ||
} | ||
mergedOptions.push(options) | ||
if (options.responseType && options.responseType === 'buffer') { | ||
mergedOptions.push( | ||
{ headers: { 'Content-Type': 'application/octet-stream' } } | ||
) | ||
} | ||
const requestOptions = deepmerge(mergedOptions) | ||
if (requestOptions.params && typeof requestOptions.params === 'object') { | ||
url = mergeUrl(url, requestOptions.params) | ||
} | ||
return new Promise((resolve, reject) => { | ||
const client = url.startsWith('https') ? https : http | ||
let resolved = false | ||
const result = { | ||
data: null, | ||
headers: null, | ||
status: null, | ||
statusText: null | ||
} | ||
const cleanRequestOptions = cleanObject(requestOptions, nativeRequestKeys) | ||
const req = client.request( | ||
url, | ||
cleanRequestOptions | ||
) | ||
req.on('error', onError) | ||
req.on('response', onResponse) | ||
req.on('close', onClose) | ||
if (requestOptions.headers) { | ||
Object.entries(requestOptions.headers) | ||
.forEach(([name, value]) => req.setHeader(name, value)) | ||
} | ||
function onClose () { | ||
if (resolved) { | ||
return | ||
} | ||
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) | ||
} | ||
function onResponse (res) { | ||
result.headers = res.headers | ||
result.status = res.statusCode | ||
result.statusText = res.statusMessage | ||
if (requestOptions.responseType === 'headers') { | ||
resolved = true | ||
return resolve(result) | ||
} | ||
const stream = ['gzip', 'compress', 'deflate'].includes(res.headers['content-encoding']) && res.statusCode === 204 | ||
? res.pipe(createUnzip()) | ||
: res | ||
if (requestOptions.responseType === 'stream') { | ||
result.data = stream | ||
resolved = true | ||
return resolve(result) | ||
} | ||
// stream.setEncoding('utf8') | ||
const responseData = [] | ||
const onData = chunk => { responseData.push(requestOptions.responseType === 'buffer' ? Buffer.from(chunk) : chunk) } | ||
const onError = err => { | ||
stream.removeListener('error', onError) | ||
stream.removeListener('data', onData) | ||
reject(err) | ||
} | ||
const onRequestEnd = () => { | ||
stream.removeListener('error', onError) | ||
stream.removeListener('data', onData) | ||
stream.removeListener('end', onRequestEnd) | ||
result.data = responseData | ||
} | ||
stream.on('data', onData) | ||
stream.on('error', onError) | ||
stream.on('end', onRequestEnd) | ||
} | ||
if (body === null) { | ||
// body is null | ||
req.end() | ||
} else if (isBlob(body)) { | ||
body.stream().pipe(req) | ||
} else if (Buffer.isBuffer(body) || typeof body === 'string') { | ||
// body is buffer | ||
req.write(body) | ||
req.end() | ||
} else { | ||
// body is stream | ||
body.pipe(req) | ||
} | ||
}) | ||
} | ||
export default request |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
0
12603
227
2
4