UNPKG

get-it

Version:

Generic HTTP request library for node, browsers and workers

2 lines (1 loc) 11.8 kB
"use strict";var e=require("tty"),t=require("util"),n=require("through2");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=/* @__PURE__ */r(e),o=/* @__PURE__ */r(t),c=/* @__PURE__ */r(n);const a=!(typeof navigator>"u")&&"ReactNative"===navigator.product,i={timeout:a?6e4:12e4};function u(e){return decodeURIComponent(e.replace(/\+/g," "))}function l(e){if(!1===e||0===e)return!1;if(e.connect||e.socket)return e;const t=Number(e);return isNaN(t)?l(i.timeout):{connect:t,socket:t}}const p=/^https?:\/\//i;var f,d,C,h,m,g={exports:{}},F={exports:{}};function y(){return h?C:(h=1,C=function(e){function t(e){let r,s,o,c=null;function a(...e){if(!a.enabled)return;const n=a,s=Number(/* @__PURE__ */new Date),o=s-(r||s);n.diff=o,n.prev=r,n.curr=s,r=s,e[0]=t.coerce(e[0]),"string"!=typeof e[0]&&e.unshift("%O");let c=0;e[0]=e[0].replace(/%([a-zA-Z%])/g,(r,s)=>{if("%%"===r)return"%";c++;const o=t.formatters[s];if("function"==typeof o){const t=e[c];r=o.call(n,t),e.splice(c,1),c--}return r}),t.formatArgs.call(n,e),(n.log||t.log).apply(n,e)}return a.namespace=e,a.useColors=t.useColors(),a.color=t.selectColor(e),a.extend=n,a.destroy=t.destroy,Object.defineProperty(a,"enabled",{enumerable:!0,configurable:!1,get:()=>null!==c?c:(s!==t.namespaces&&(s=t.namespaces,o=t.enabled(e)),o),set:e=>{c=e}}),"function"==typeof t.init&&t.init(a),a}function n(e,n){const r=t(this.namespace+(typeof n>"u"?":":n)+e);return r.log=this.log,r}function r(e,t){let n=0,r=0,s=-1,o=0;for(;n<e.length;)if(r<t.length&&(t[r]===e[n]||"*"===t[r]))"*"===t[r]?(s=r,o=n,r++):(n++,r++);else{if(-1===s)return!1;r=s+1,o++,n=o}for(;r<t.length&&"*"===t[r];)r++;return r===t.length}return t.debug=t,t.default=t,t.coerce=function(e){return e instanceof Error?e.stack||e.message:e},t.disable=function(){const e=[...t.names,...t.skips.map(e=>"-"+e)].join(",");return t.enable(""),e},t.enable=function(e){t.save(e),t.namespaces=e,t.names=[],t.skips=[];const n=("string"==typeof e?e:"").trim().replace(/\s+/g,",").split(",").filter(Boolean);for(const e of n)"-"===e[0]?t.skips.push(e.slice(1)):t.names.push(e)},t.enabled=function(e){for(const n of t.skips)if(r(e,n))return!1;for(const n of t.names)if(r(e,n))return!0;return!1},t.humanize=function(){if(d)return f;d=1;var e=1e3,t=60*e,n=60*t,r=24*n,s=7*r;function o(e,t,n,r){var s=t>=1.5*n;return Math.round(e/n)+" "+r+(s?"s":"")}return f=function(c,a){a=a||{};var i,u,l=typeof c;if("string"===l&&c.length>0)return function(o){if(!((o=String(o)).length>100)){var c=/^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(o);if(c){var a=parseFloat(c[1]);switch((c[2]||"ms").toLowerCase()){case"years":case"year":case"yrs":case"yr":case"y":return 315576e5*a;case"weeks":case"week":case"w":return a*s;case"days":case"day":case"d":return a*r;case"hours":case"hour":case"hrs":case"hr":case"h":return a*n;case"minutes":case"minute":case"mins":case"min":case"m":return a*t;case"seconds":case"second":case"secs":case"sec":case"s":return a*e;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return a;default:return}}}}(c);if("number"===l&&isFinite(c))return a.long?(i=c,(u=Math.abs(i))>=r?o(i,u,r,"day"):u>=n?o(i,u,n,"hour"):u>=t?o(i,u,t,"minute"):u>=e?o(i,u,e,"second"):i+" ms"):function(s){var o=Math.abs(s);return o>=r?Math.round(s/r)+"d":o>=n?Math.round(s/n)+"h":o>=t?Math.round(s/t)+"m":o>=e?Math.round(s/e)+"s":s+"ms"}(c);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(c))}}(),t.destroy=function(){console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.")},Object.keys(e).forEach(n=>{t[n]=e[n]}),t.names=[],t.skips=[],t.formatters={},t.selectColor=function(e){let n=0;for(let t=0;t<e.length;t++)n=(n<<5)-n+e.charCodeAt(t),n|=0;return t.colors[Math.abs(n)%t.colors.length]},t.enable(t.load()),t})}var w,b,v,x,O={exports:{}};let I=1;const A=65535;let j=null;const k=function(){I=I+1&A};class E extends Error{request;code;constructor(e,t){super(e.message),this.request=t,this.code=e.code}}exports.N=E,exports.a=function(e){let t=e.length||0,n=0,r=Date.now()+e.time,s=0;const o=function(){j||(j=setInterval(k,250),j.unref&&j.unref());const e=[0];let t=1,n=I-1&A;return{getSpeed:function(r){let s=I-n&A;for(s>20&&(s=20),n=I;s--;)20===t&&(t=0),e[t]=e[0===t?19:t-1],t++;r&&(e[t-1]+=r);const o=e[t-1],c=e.length<20?0:e[20===t?0:t];return e.length<4?o:4*(o-c)/e.length},clear:function(){j&&(clearInterval(j),j=null)}}}(),a=Date.now(),i={percentage:0,transferred:n,length:t,remaining:t,eta:0,runtime:0,speed:0,delta:0},u=function(c){i.delta=s,i.percentage=c?100:t?n/t*100:0,i.speed=o.getSpeed(s),i.eta=Math.round(i.remaining/i.speed),i.runtime=Math.floor((Date.now()-a)/1e3),r=Date.now()+e.time,s=0,l.emit("progress",i)},l=c.default({},function(e,o,c){const a=e.length;n+=a,s+=a,i.transferred=n,i.remaining=t>=n?t-n:0,Date.now()>=r&&u(!1),c(null,e)},function(e){u(!0),o.clear(),e()}),p=function(e){t=e,i.length=t,i.remaining=t-i.transferred,l.emit("length",t)};return l.on("pipe",function(e){if(!(t>0)){if(e.readable&&!("writable"in e)&&"headers"in e&&"object"==typeof(n=e.headers)&&null!==n&&!Array.isArray(n)){const t="string"==typeof e.headers["content-length"]?parseInt(e.headers["content-length"],10):0;return p(t)}if("length"in e&&"number"==typeof e.length)return p(e.length);e.on("response",function(e){if(e&&e.headers&&"gzip"!==e.headers["content-encoding"]&&e.headers["content-length"])return p(parseInt(e.headers["content-length"]))})}var n}),l.progress=function(){return i.speed=o.getSpeed(0),i.eta=Math.round(i.remaining/i.speed),i},l},exports.g=function(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e},exports.p=function(e){const t={...i,..."string"==typeof e?{url:e}:e};if(t.timeout=l(t.timeout),t.query){const{url:e,searchParams:n}=function(e){const t=e.indexOf("?");if(-1===t)return{url:e,searchParams:new URLSearchParams};const n=e.slice(0,t),r=e.slice(t+1);if(!a)return{url:n,searchParams:new URLSearchParams(r)};if("function"!=typeof decodeURIComponent)throw new Error("Broken `URLSearchParams` implementation, and `decodeURIComponent` is not defined");const s=new URLSearchParams;for(const e of r.split("&")){const[t,n]=e.split("=");t&&s.append(u(t),u(n||""))}return{url:n,searchParams:s}}(t.url);for(const[r,s]of Object.entries(t.query)){if(void 0!==s)if(Array.isArray(s))for(const e of s)n.append(r,e);else n.append(r,s);const o=n.toString();o&&(t.url=`${e}?${o}`)}}return t.method=t.body&&!t.method?"POST":(t.method||"GET").toUpperCase(),t},exports.r=function(){return x||(x=1,typeof process>"u"||"renderer"===process.type||!0===process.browser||process.__nwjs?g.exports=(m||(m=1,function(e,t){t.formatArgs=function(t){if(t[0]=(this.useColors?"%c":"")+this.namespace+(this.useColors?" %c":" ")+t[0]+(this.useColors?"%c ":" ")+"+"+e.exports.humanize(this.diff),!this.useColors)return;const n="color: "+this.color;t.splice(1,0,n,"color: inherit");let r=0,s=0;t[0].replace(/%[a-zA-Z%]/g,e=>{"%%"!==e&&(r++,"%c"===e&&(s=r))}),t.splice(s,0,n)},t.save=function(e){try{e?t.storage.setItem("debug",e):t.storage.removeItem("debug")}catch{}},t.load=function(){let e;try{e=t.storage.getItem("debug")||t.storage.getItem("DEBUG")}catch{}return!e&&typeof process<"u"&&"env"in process&&(e=process.env.DEBUG),e},t.useColors=function(){if(typeof window<"u"&&window.process&&("renderer"===window.process.type||window.process.__nwjs))return!0;if(typeof navigator<"u"&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/))return!1;let e;return typeof document<"u"&&document.documentElement&&document.documentElement.style&&document.documentElement.style.WebkitAppearance||typeof window<"u"&&window.console&&(window.console.firebug||window.console.exception&&window.console.table)||typeof navigator<"u"&&navigator.userAgent&&(e=navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/))&&parseInt(e[1],10)>=31||typeof navigator<"u"&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/)},t.storage=function(){try{return localStorage}catch{}}(),t.destroy=/* @__PURE__ */(()=>{let e=!1;return()=>{e||(e=!0,console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`."))}})(),t.colors=["#0000CC","#0000FF","#0033CC","#0033FF","#0066CC","#0066FF","#0099CC","#0099FF","#00CC00","#00CC33","#00CC66","#00CC99","#00CCCC","#00CCFF","#3300CC","#3300FF","#3333CC","#3333FF","#3366CC","#3366FF","#3399CC","#3399FF","#33CC00","#33CC33","#33CC66","#33CC99","#33CCCC","#33CCFF","#6600CC","#6600FF","#6633CC","#6633FF","#66CC00","#66CC33","#9900CC","#9900FF","#9933CC","#9933FF","#99CC00","#99CC33","#CC0000","#CC0033","#CC0066","#CC0099","#CC00CC","#CC00FF","#CC3300","#CC3333","#CC3366","#CC3399","#CC33CC","#CC33FF","#CC6600","#CC6633","#CC9900","#CC9933","#CCCC00","#CCCC33","#FF0000","#FF0033","#FF0066","#FF0099","#FF00CC","#FF00FF","#FF3300","#FF3333","#FF3366","#FF3399","#FF33CC","#FF33FF","#FF6600","#FF6633","#FF9900","#FF9933","#FFCC00","#FFCC33"],t.log=console.debug||console.log||(()=>{}),e.exports=y()(t);const{formatters:n}=e.exports;n.j=function(e){try{return JSON.stringify(e)}catch(e){return"[UnexpectedJSONParseError]: "+e.message}}}(F,F.exports)),F.exports):g.exports=(v||(v=1,function(e,t){const n=s.default,r=o.default;t.init=function(e){e.inspectOpts={};const n=Object.keys(t.inspectOpts);for(let r=0;r<n.length;r++)e.inspectOpts[n[r]]=t.inspectOpts[n[r]]},t.log=function(...e){return process.stderr.write(r.formatWithOptions(t.inspectOpts,...e)+"\n")},t.formatArgs=function(n){const{namespace:r,useColors:s}=this;if(s){const t=this.color,s="[3"+(t<8?t:"8;5;"+t),o=` ${s};1m${r} `;n[0]=o+n[0].split("\n").join("\n"+o),n.push(s+"m+"+e.exports.humanize(this.diff)+"")}else n[0]=(t.inspectOpts.hideDate?"":/* @__PURE__ */(new Date).toISOString()+" ")+r+" "+n[0]},t.save=function(e){e?process.env.DEBUG=e:delete process.env.DEBUG},t.load=function(){return process.env.DEBUG},t.useColors=function(){return"colors"in t.inspectOpts?!!t.inspectOpts.colors:n.isatty(process.stderr.fd)},t.destroy=r.deprecate(()=>{},"Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`."),t.colors=[6,2,3,4,5,1];try{const e=function(){if(b)return w;b=1;const e=function(){const e=/(Chrome|Chromium)\/(?<chromeVersion>\d+)\./.exec(navigator.userAgent);if(e)return Number.parseInt(e.groups.chromeVersion,10)}()>=69&&{level:1,hasBasic:!0,has256:!1,has16m:!1};return w={stdout:e,stderr:e}}();e&&(e.stderr||e).level>=2&&(t.colors=[20,21,26,27,32,33,38,39,40,41,42,43,44,45,56,57,62,63,68,69,74,75,76,77,78,79,80,81,92,93,98,99,112,113,128,129,134,135,148,149,160,161,162,163,164,165,166,167,168,169,170,171,172,173,178,179,184,185,196,197,198,199,200,201,202,203,204,205,206,207,208,209,214,215,220,221])}catch{}t.inspectOpts=Object.keys(process.env).filter(e=>/^debug_/i.test(e)).reduce((e,t)=>{const n=t.substring(6).toLowerCase().replace(/_([a-z])/g,(e,t)=>t.toUpperCase());let r=process.env[t];return r=!!/^(yes|on|true|enabled)$/i.test(r)||!/^(no|off|false|disabled)$/i.test(r)&&("null"===r?null:Number(r)),e[n]=r,e},{}),e.exports=y()(t);const{formatters:c}=e.exports;c.o=function(e){return this.inspectOpts.colors=this.useColors,r.inspect(e,this.inspectOpts).split("\n").map(e=>e.trim()).join(" ")},c.O=function(e){return this.inspectOpts.colors=this.useColors,r.inspect(e,this.inspectOpts)}}(O,O.exports)),O.exports)),g.exports},exports.v=function(e){if(!p.test(e.url))throw new Error(`"${e.url}" is not a valid URL`)};//# sourceMappingURL=node-request-error.cjs.map