request-micro
Advanced tools
Comparing version 2.0.0 to 2.1.0-0
@@ -1,156 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isErrorStatus = exports.isSuccessStatus = exports.options = exports.delete = exports.head = exports.patch = exports.put = exports.post = exports.get = exports.request = exports.raw = void 0; | ||
const tslib_1 = require("tslib"); | ||
const node_url_1 = tslib_1.__importDefault(require("node:url")); | ||
let _http; | ||
let _https; | ||
const net = { | ||
http: function () { | ||
if (!_http) | ||
_http = require('node:http'); | ||
return _http; | ||
}, | ||
https: function () { | ||
if (!_https) | ||
_https = require('node:https'); | ||
return _https; | ||
}, | ||
}; | ||
function once(fn) { | ||
let f = function () { | ||
if (f.called) | ||
return f.value; | ||
f.called = true; | ||
f.value = fn.apply(this, arguments); | ||
return f.value; | ||
}; | ||
f.called = false; | ||
return f; | ||
} | ||
function rawRequest(optionsOrUrl, cb) { | ||
const options = typeof optionsOrUrl === 'string' | ||
? { url: optionsOrUrl } | ||
: { ...optionsOrUrl }; | ||
const callback = once(cb); | ||
if (options.url) | ||
parseOptsUrl(options); | ||
if (options.headers == null) | ||
options.headers = {}; | ||
if (options.maxRedirects == null) | ||
options.maxRedirects = 10; | ||
const body = options.json ? JSON.stringify(options.body) : options.body; | ||
options.body = undefined; | ||
if (body && !options.method) | ||
options.method = 'POST'; | ||
if (options.method) | ||
options.method = options.method.toUpperCase(); | ||
if (options.json) | ||
options.headers['accept'] = 'application/json'; | ||
if (options.json && body) | ||
options.headers['content-type'] = 'application/json'; | ||
const protocol = options.protocol === 'https:' ? net.https() : net.http(); | ||
const req = protocol.request(options, (res) => { | ||
if (res.statusCode >= 300 && | ||
res.statusCode < 400 && | ||
'location' in res.headers) { | ||
options.url = res.headers.location; | ||
parseOptsUrl(options); | ||
res.resume(); | ||
options.maxRedirects -= 1; | ||
if (options.maxRedirects > 0) { | ||
options.body = body; | ||
rawRequest(options, callback); | ||
} | ||
else { | ||
callback(new Error('too many redirects')); | ||
} | ||
return; | ||
} | ||
callback(null, res); | ||
}); | ||
req.on('timeout', () => { | ||
req.abort(); | ||
callback(new Error('Request timed out')); | ||
}); | ||
req.on('error', callback); | ||
if (isStream(body)) | ||
body.on('error', callback).pipe(req); | ||
else | ||
req.end(body); | ||
return req; | ||
} | ||
exports.raw = rawRequest; | ||
async function request(optionsOrUrl) { | ||
return new Promise((resolve, reject) => { | ||
rawRequest(optionsOrUrl, function (err, res) { | ||
if (err) | ||
return reject(err); | ||
const chunks = []; | ||
res.on('data', function (chunk) { | ||
chunks.push(chunk); | ||
}); | ||
res.on('end', function () { | ||
var data = Buffer.concat(chunks); | ||
if (typeof optionsOrUrl === 'object' && optionsOrUrl.json) { | ||
if (data.length === 0) | ||
return resolve(res); | ||
try { | ||
data = JSON.parse(data.toString()); | ||
res.data = data; | ||
} | ||
catch (err) { | ||
return reject(err); | ||
} | ||
} | ||
else { | ||
res.data = data; | ||
} | ||
resolve(res); | ||
}); | ||
}); | ||
}); | ||
} | ||
exports.default = request; | ||
exports.request = request; | ||
const mainRequest = request; | ||
function asMethod(method) { | ||
return function request(options) { | ||
if (typeof options === 'string') | ||
options = { url: options }; | ||
options.method = method.toUpperCase(); | ||
return mainRequest(options); | ||
}; | ||
} | ||
exports.get = asMethod('get'); | ||
exports.post = asMethod('post'); | ||
exports.put = asMethod('put'); | ||
exports.patch = asMethod('patch'); | ||
exports.head = asMethod('head'); | ||
const _delete = asMethod('delete'); | ||
exports.delete = _delete; | ||
exports.options = asMethod('options'); | ||
function parseOptsUrl(opts) { | ||
let loc = node_url_1.default.parse(opts.url); | ||
if (loc.hostname) | ||
opts.hostname = loc.hostname; | ||
if (loc.port) | ||
opts.port = loc.port; | ||
if (loc.protocol) | ||
opts.protocol = loc.protocol; | ||
if (loc.auth) | ||
opts.auth = loc.auth; | ||
opts.path = loc.path; | ||
delete opts.url; | ||
} | ||
function isSuccessStatus(response) { | ||
return response.statusCode?.toString()[0] === '2'; | ||
} | ||
exports.isSuccessStatus = isSuccessStatus; | ||
function isErrorStatus(response) { | ||
return !isSuccessStatus(response); | ||
} | ||
exports.isErrorStatus = isErrorStatus; | ||
function isStream(o) { | ||
return o !== null && typeof o === 'object' && typeof o.pipe === 'function'; | ||
} | ||
var H=Object.create;var i=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames,h=Object.getOwnPropertySymbols,M=Object.getPrototypeOf,m=Object.prototype.hasOwnProperty,O=Object.prototype.propertyIsEnumerable;var y=(e,n,t)=>n in e?i(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t,g=(e,n)=>{for(var t in n||(n={}))m.call(n,t)&&y(e,t,n[t]);if(h)for(var t of h(n))O.call(n,t)&&y(e,t,n[t]);return e};var I=(e,n)=>{for(var t in n)i(e,t,{get:n[t],enumerable:!0})},R=(e,n,t,s)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of F(n))!m.call(e,o)&&o!==t&&i(e,o,{get:()=>n[o],enumerable:!(s=j(n,o))||s.enumerable});return e};var v=(e,n,t)=>(t=e!=null?H(M(e)):{},R(n||!e||!e.__esModule?i(t,"default",{value:e,enumerable:!0}):t,e)),w=e=>R(i({},"__esModule",{value:!0}),e);var D={};I(D,{default:()=>d,delete:()=>N,get:()=>P,head:()=>J,isErrorStatus:()=>B,isSuccessStatus:()=>S,options:()=>A,patch:()=>k,post:()=>T,put:()=>_,raw:()=>f,request:()=>d});module.exports=w(D);var b=v(require("url")),c,l,q={http:function(){return c||(c=require("http")),c},https:function(){return l||(l=require("https")),l}};function C(e){let n=function(){return n.called||(n.called=!0,n.value=e.apply(this,arguments)),n.value};return n.called=!1,n}function f(e,n){let t=typeof e=="string"?{url:e}:g({},e),s=C(n);t.url&&x(t),t.headers==null&&(t.headers={}),t.maxRedirects==null&&(t.maxRedirects=10);let o=t.json?JSON.stringify(t.body):t.body;t.body=void 0,o&&!t.method&&(t.method="POST"),t.method&&(t.method=t.method.toUpperCase()),t.json&&(t.headers.accept="application/json"),t.json&&o&&(t.headers["content-type"]="application/json");let r=(t.protocol==="https:"?q.https():q.http()).request(t,a=>{if(a.statusCode>=300&&a.statusCode<400&&"location"in a.headers){t.url=a.headers.location,x(t),a.resume(),t.maxRedirects-=1,t.maxRedirects>0?(t.body=o,f(t,s)):s(new Error("too many redirects"));return}s(null,a)});return r.on("timeout",()=>{r.abort(),s(new Error("Request timed out"))}),r.on("error",s),z(o)?o.on("error",s).pipe(r):r.end(o),r}async function d(e){return new Promise((n,t)=>{f(e,function(s,o){if(s)return t(s);let p=[];o.on("data",function(r){p.push(r)}),o.on("end",function(){var r=Buffer.concat(p);if(typeof e=="object"&&e.json){if(r.length===0)return n(o);try{r=JSON.parse(r.toString()),o.data=r}catch(a){return t(a)}}else o.data=r;n(o)})})})}var E=d;function u(e){return function(t){return typeof t=="string"&&(t={url:t}),t.method=e.toUpperCase(),E(t)}}var P=u("get"),T=u("post"),_=u("put"),k=u("patch"),J=u("head"),N=u("delete");var A=u("options");function x(e){let n=b.default.parse(e.url);n.hostname&&(e.hostname=n.hostname),n.port&&(e.port=n.port),n.protocol&&(e.protocol=n.protocol),n.auth&&(e.auth=n.auth),e.path=n.path,delete e.url}function S(e){var n;return((n=e.statusCode)==null?void 0:n.toString()[0])==="2"}function B(e){return!S(e)}function z(e){return e!==null&&typeof e=="object"&&typeof e.pipe=="function"}0&&(module.exports={delete:null,get,head,isErrorStatus,isSuccessStatus,options,patch,post,put,raw,request}); |
{ | ||
"name": "request-micro", | ||
"description": "A tiny request library with no dependencies. Supports Http(s) and follows redirects", | ||
"version": "2.0.0", | ||
"version": "2.1.0-0", | ||
"main": "./dist/request.js", | ||
@@ -11,4 +11,6 @@ "types": "./dist/request.d.ts", | ||
"scripts": { | ||
"build": "run-s build:tsc", | ||
"build:tsc": "del dist/ && tsc --project tsconfig.build.json", | ||
"build": "run-s build:clean build:tsc build:js", | ||
"build:clean": "del dist/", | ||
"build:tsc": "tsc --project tsconfig.build.json", | ||
"build:js": "node esbuild.js", | ||
"check": "run-s style lint", | ||
@@ -37,2 +39,3 @@ "check:ci": "run-s style:ci lint", | ||
"del-cli": "^4.0.1", | ||
"esbuild": "^0.14.42", | ||
"eslint": "^8.16.0", | ||
@@ -54,8 +57,8 @@ "eslint-config-prettier": "^8.5.0", | ||
{ | ||
"limit": "4.5kb", | ||
"limit": "2kb", | ||
"path": "dist/request.js", | ||
"ignore": [ | ||
"node:http", | ||
"node:https", | ||
"node:url" | ||
"http", | ||
"https", | ||
"url" | ||
] | ||
@@ -62,0 +65,0 @@ } |
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
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
8095
22
38
1
2
2