UNPKG

get-it

Version:

Generic HTTP request library for node, browsers and workers

2 lines (1 loc) 9.61 kB
import e from"decompress-response";import t from"follow-redirects";import o from"http";import r from"https";import n from"querystring";import{Readable as s}from"stream";import c from"url";import a from"through2";import*as i from"tunnel-agent";function p(e){return Object.keys(e||{}).reduce((t,o)=>(t[o.toLowerCase()]=e[o],t),{})}let u=1;const d=65535;let h=null;const l=function(){u=u+1&d};function f(e){let t=e.length||0,o=0,r=Date.now()+e.time,n=0;const s=function(){h||(h=setInterval(l,250),h.unref&&h.unref());const e=[0];let t=1,o=u-1&d;return{getSpeed:function(r){let n=u-o&d;for(n>20&&(n=20),o=u;n--;)20===t&&(t=0),e[t]=e[0===t?19:t-1],t++;r&&(e[t-1]+=r);const s=e[t-1],c=e.length<20?0:e[20===t?0:t];return e.length<4?s:4*(s-c)/e.length},clear:function(){h&&(clearInterval(h),h=null)}}}(),c=Date.now(),i={percentage:0,transferred:o,length:t,remaining:t,eta:0,runtime:0,speed:0,delta:0},p=function(a){i.delta=n,i.percentage=a?100:t?o/t*100:0,i.speed=s.getSpeed(n),i.eta=Math.round(i.remaining/i.speed),i.runtime=Math.floor((Date.now()-c)/1e3),r=Date.now()+e.time,n=0,f.emit("progress",i)},f=a({},function(e,s,c){const a=e.length;o+=a,n+=a,i.transferred=o,i.remaining=t>=o?t-o:0,Date.now()>=r&&p(!1),c(null,e)},function(e){p(!0),s.clear(),e()}),m=function(e){t=e,i.length=t,i.remaining=t-i.transferred,f.emit("length",t)};return f.on("pipe",function(e){if(!(t>0)){if(e.readable&&!("writable"in e)&&"headers"in e&&"object"==typeof(o=e.headers)&&null!==o&&!Array.isArray(o)){const t="string"==typeof e.headers["content-length"]?parseInt(e.headers["content-length"],10):0;return m(t)}if("length"in e&&"number"==typeof e.length)return m(e.length);e.on("response",function(e){if(e&&e.headers&&"gzip"!==e.headers["content-encoding"]&&e.headers["content-length"])return m(parseInt(e.headers["content-length"]))})}var o}),f.progress=function(){return i.speed=s.getSpeed(0),i.eta=Math.round(i.remaining/i.speed),i},f}function m(e){return e.replace(/^\.*/,".").toLowerCase()}function g(e){const t=e.trim().toLowerCase(),o=t.split(":",2);return{hostname:m(o[0]),port:o[1],hasPort:t.indexOf(":")>-1}}const y=["protocol","slashes","auth","host","port","hostname","hash","search","query","pathname","path","href"],b=["accept","accept-charset","accept-encoding","accept-language","accept-ranges","cache-control","content-encoding","content-language","content-location","content-md5","content-range","content-type","connection","date","expect","max-forwards","pragma","referer","te","user-agent","via"],x=["proxy-authorization"],w=e=>null!==e&&"object"==typeof e&&"function"==typeof e.pipe,O="node";class T extends Error{request;code;constructor(e,t){super(e.message),this.request=t,this.code=e.code}}const v=(e,t,o,r,n)=>({body:n,url:o,method:r,headers:e.headers,statusCode:e.statusCode||0,statusMessage:e.statusMessage||"",remoteAddress:t}),R=(a,u)=>{const{options:d}=a,h=Object.assign({},c.parse(d.url));if("function"==typeof fetch&&d.fetch){const e=new AbortController,t=a.applyMiddleware("finalizeOptions",{...h,method:d.method,headers:{..."object"==typeof d.fetch&&d.fetch.headers?p(d.fetch.headers):{},...p(d.headers)},maxRedirects:d.maxRedirects}),o={credentials:d.withCredentials?"include":"omit",..."object"==typeof d.fetch?d.fetch:{},method:t.method,headers:t.headers,body:d.body,signal:e.signal},r=a.applyMiddleware("interceptRequest",void 0,{adapter:O,context:a});if(r){const e=setTimeout(u,0,null,r);return{abort:()=>clearTimeout(e)}}const n=fetch(d.url,o);return a.applyMiddleware("onRequest",{options:d,adapter:O,request:n,context:a}),n.then(async e=>{const t=d.rawBody?e.body:await e.text(),o={};e.headers.forEach((e,t)=>{o[t]=e}),u(null,{body:t,url:e.url,method:d.method,headers:o,statusCode:e.status,statusMessage:e.statusText})}).catch(e=>{"AbortError"!=e.name&&u(e)}),{abort:()=>e.abort()}}const l=w(d.body)?"stream":typeof d.body;if("undefined"!==l&&"stream"!==l&&"string"!==l&&!Buffer.isBuffer(d.body))throw new Error(`Request body must be a string, buffer or stream, got ${l}`);const R={};d.bodySize?R["content-length"]=d.bodySize:d.body&&"stream"!==l&&(R["content-length"]=Buffer.byteLength(d.body));let j=!1;const M=(e,t)=>!j&&u(e,t);a.channels.abort.subscribe(()=>{j=!0});let $=Object.assign({},h,{method:d.method,headers:Object.assign({},p(d.headers),R),maxRedirects:d.maxRedirects});const q=function(e){const t=typeof e.proxy>"u"?function(e){const t=process.env.NO_PROXY||process.env.no_proxy||"";return"*"===t||""!==t&&function(e,t){const o=e.port||("https:"===e.protocol?"443":"80"),r=m(e.hostname||"");return t.split(",").map(g).some(e=>{const t=r.indexOf(e.hostname),n=t>-1&&t===r.length-e.hostname.length;return e.hasPort?o===e.port&&n:n})}(e,t)?null:"http:"===e.protocol?process.env.HTTP_PROXY||process.env.http_proxy||null:"https:"===e.protocol&&(process.env.HTTPS_PROXY||process.env.https_proxy||process.env.HTTP_PROXY||process.env.http_proxy)||null}(c.parse(e.url)):e.proxy;return"string"==typeof t?c.parse(t):t||null}(d),C=q&&function(e){return typeof e.tunnel<"u"?!!e.tunnel:"https:"===c.parse(e.url).protocol}(d),S=a.applyMiddleware("interceptRequest",void 0,{adapter:O,context:a});if(S){const e=setImmediate(M,null,S);return{abort:()=>clearImmediate(e)}}if(0!==d.maxRedirects&&($.maxRedirects=d.maxRedirects||5),q&&C?$=function(e={},t){const o=Object.assign({},e),r=b.concat(o.proxyHeaderWhiteList||[]).map(e=>e.toLowerCase()),n=x.concat(o.proxyHeaderExclusiveList||[]).map(e=>e.toLowerCase()),s=(c=o.headers,a=r,Object.keys(c).filter(e=>-1!==a.indexOf(e.toLowerCase())).reduce((e,t)=>(e[t]=c[t],e),{}));var c,a;s.host=function(e){const t=e.port,o=e.protocol;let r=`${e.hostname}:`;return r+=t||("https:"===o?"443":"80"),r}(o),o.headers=Object.keys(o.headers||{}).reduce((e,t)=>(-1===n.indexOf(t.toLowerCase())&&(e[t]=o.headers[t]),e),{});const p=function(e,t){const o=function(e){return y.reduce((t,o)=>(t[o]=e[o],t),{})}(e),r=function(e,t){return`${"https:"===e.protocol?"https":"http"}Over${"https:"===t.protocol?"Https":"Http"}`}(o,t);return i[r]}(o,t),u=function(e,t,o){return{proxy:{host:t.hostname,port:+t.port,proxyAuth:t.auth,headers:o},headers:e.headers,ca:e.ca,cert:e.cert,key:e.key,passphrase:e.passphrase,pfx:e.pfx,ciphers:e.ciphers,rejectUnauthorized:e.rejectUnauthorized,secureOptions:e.secureOptions,secureProtocol:e.secureProtocol}}(o,t,s);return o.agent=p(u),o}($,q):q&&!C&&($=function(e,t,o){const r=e.headers||{},n=Object.assign({},e,{headers:r});return r.host=r.host||function(e){const t=e.port||("https:"===e.protocol?"443":"80");return`${e.hostname}:${t}`}(t),n.protocol=o.protocol||n.protocol,n.hostname=(o.host||"hostname"in o&&o.hostname||n.hostname||"").replace(/:\d+/,""),n.port=o.port?`${o.port}`:n.port,n.host=function(e){let t=e.host;return e.port&&("80"===e.port&&"http:"===e.protocol||"443"===e.port&&"https:"===e.protocol)&&(t=e.hostname),t}(Object.assign({},t,o)),n.href=`${n.protocol}//${n.host}${n.path}`,n.path=c.format(t),n}($,h,q)),!C&&q&&q.auth&&!$.headers["proxy-authorization"]){const[e,t]="string"==typeof q.auth?q.auth.split(":").map(e=>n.unescape(e)):[q.auth.username,q.auth.password],o=Buffer.from(`${e}:${t}`,"utf8").toString("base64");$.headers["proxy-authorization"]=`Basic ${o}`}const z=function(e,n,s){const c="https:"===e.protocol,a=0===e.maxRedirects?{http:o,https:r}:{http:t.http,https:t.https};if(!n||s)return c?a.https:a.http;let i=443===n.port;return n.protocol&&(i=/^https:?/.test(n.protocol)),i?a.https:a.http}($,q,C);"function"==typeof d.debug&&q&&d.debug("Proxying using %s",$.agent?"tunnel agent":`${$.host}:${$.port}`);const E="HEAD"!==$.method;let L;E&&!$.headers["accept-encoding"]&&!1!==d.compress&&($.headers["accept-encoding"]=typeof Bun<"u"?"gzip, deflate":"br, gzip, deflate");const k=a.applyMiddleware("finalizeOptions",$),P=z.request(k,t=>{const o=E?e(t):t;L=o;const r=a.applyMiddleware("onHeaders",o,{headers:t.headers,adapter:O,context:a}),n="responseUrl"in t?t.responseUrl:d.url,s=o.socket?.remoteAddress;d.stream?M(null,v(o,s,n,$.method,r)):function(e,t){const o=[];e.on("data",function(e){o.push(e)}),e.once("end",function(){t&&t(null,Buffer.concat(o)),t=null}),e.once("error",function(e){t&&t(e),t=null})}(r,(e,t)=>{if(e)return M(e);const r=d.rawBody?t:t.toString(),c=v(o,s,n,$.method,r);return M(null,c)})});function B(e){L&&L.destroy(e),P.destroy(e)}P.once("socket",e=>{e.once("error",B),P.once("response",t=>{t.once("end",()=>{e.removeListener("error",B)})})}),P.once("error",e=>{L||M(new T(e,P))}),d.timeout&&function(e,t){if(e.timeoutTimer)return e;const o=isNaN(t)?t:{socket:t,connect:t},r=e.getHeader("host"),n=r?" to "+r:"";function s(){e.timeoutTimer&&(clearTimeout(e.timeoutTimer),e.timeoutTimer=null)}function c(t){if(s(),void 0!==o.socket){const r=()=>{const e=new Error("Socket timed out on request"+n);e.code="ESOCKETTIMEDOUT",t.destroy(e)};t.setTimeout(o.socket,r),e.once("response",e=>{e.once("end",()=>{t.removeListener("timeout",r)})})}}void 0!==o.connect&&(e.timeoutTimer=setTimeout(function(){const t=new Error("Connection timed out on request"+n);t.code="ETIMEDOUT",e.destroy(t)},o.connect)),e.on("socket",function(e){e.connecting?e.once("connect",()=>c(e)):c(e)}),e.on("error",s)}(P,d.timeout);const{bodyStream:H,progress:A}=function(e){if(!e.body)return{};const t=w(e.body),o=e.bodySize||(t?null:Buffer.byteLength(e.body));if(!o)return t?{bodyStream:e.body}:{};const r=f({time:32,length:o});return{bodyStream:(t?e.body:s.from(e.body)).pipe(r),progress:r}}(d);return a.applyMiddleware("onRequest",{options:d,adapter:O,request:P,context:a,progress:A}),H?H.pipe(P):P.end(d.body),{abort:()=>P.abort()}};export{T as N,O as a,R as h,f as p};//# sourceMappingURL=node-request.js.map