UNPKG

sync-request-curl

Version:

Fast way to send synchronous web requests in NodeJS. API is a subset of sync-request. Leverages node-libcurl for high performance. Cannot be used in a browser.

36 lines (29 loc) 3.32 kB
const e=require(`./errors-BS3se1ZO.cjs`);let t=require(`node-libcurl`);const n=(e,t)=>{let n=new URL(e);for(let[e,r]of Object.entries(t))Array.isArray(r)?(n.searchParams.delete(e),r.forEach((t,r)=>n.searchParams.append(`${e}[${r}]`,String(t)))):r===null?n.searchParams.set(e,``):r!==void 0&&n.searchParams.set(e,String(r));return n.search=n.searchParams.toString(),n.href},r=e=>e?Object.entries(e).filter(([e,t])=>t!==void 0).map(([e,t])=>t===``?`${e};`:`${e}: ${t}`):[],i=e=>e.reduce((e,t)=>{let[n,...r]=t.split(`:`);return n&&r.length>0&&(e[n.trim().toLowerCase()]=r.join(`:`).trim()),e},{}),a=(n,r)=>{if(n!==t.CurlCode.CURLE_OK)throw new e.t(n,` Curl request failed with code ${n}: - ${t.Easy.strError(n)} You can also look up the Libcurl Error (code ${n}) here: - https://curl.se/libcurl/c/libcurl-errors.html DEBUG: { method: "${r.method}", url: "${r.url}", options: ${JSON.stringify(r.options)} } `)},o=(e,t)=>{if(e>=300)throw Error(` Server responded with status code ${e} Body: ${t.toString()} Use 'res.body' instead of 'res.getBody()' to not have any errors thrown. The status code (in this case, ${e}) can be checked manually with res.statusCode. `)},s=(e,n)=>{let r=new t.Easy;return r.setOpt(t.Curl.option.CUSTOMREQUEST,e),r.setOpt(t.Curl.option.TIMEOUT_MS,n.timeout??0),r.setOpt(t.Curl.option.FOLLOWLOCATION,n.followRedirects===void 0||n.followRedirects),r.setOpt(t.Curl.option.MAXREDIRS,n.maxRedirects??-1),r.setOpt(t.Curl.option.SSL_VERIFYPEER,!n.insecure),r.setOpt(t.Curl.option.NOBODY,e===`HEAD`),r},c=(e,r,i)=>{r=i&&Object.keys(i).length?n(r,i):r,e.setOpt(t.Curl.option.URL,r)},l=(e,n)=>{e.setOpt(t.Curl.option.HEADERFUNCTION,e=>(n.push(e.toString(`utf-8`).trim()),e.length))},u=(e,n,r)=>{r.push(`Content-Type: application/json`);let i=JSON.stringify(n);r.push(`Content-Length: ${Buffer.byteLength(i,`utf-8`)}`),e.setOpt(t.Curl.option.POSTFIELDS,i)},d=(e,n,r)=>{if(Buffer.isBuffer(n)){let r=0;e.setOpt(t.Curl.option.POST,!0),e.setOpt(t.Curl.option.POSTFIELDSIZE,-1),e.setOpt(t.Curl.option.READFUNCTION,(e,t,i)=>{let a=t*i;if(r===n.length)return 0;let o=n.copy(e,0,r,Math.min(a,n.length));return r+=o,o})}else e.setOpt(t.Curl.option.POSTFIELDS,n),r.push(`Content-Length: ${Buffer.byteLength(n,`utf-8`)}`)},f=(e,n)=>{e.setOpt(t.Curl.option.HTTPPOST,n)},p=(e,n,r,i)=>{n.json?u(e,n.json,i):n.body?d(e,n.body,i):n.formData?f(e,n.formData):i.push(`Content-Length: 0`),e.setOpt(t.Curl.option.WRITEFUNCTION,(e,t,n)=>(r.body=Buffer.concat([r.body,e.subarray(0,t*n)]),t*n)),e.setOpt(t.Curl.option.HTTPHEADER,i)},m=(e,n,u={})=>{let d=s(e,u);c(d,n,u.qs);let f={body:Buffer.alloc(0)};p(d,u,f,r(u.headers));let m=[];l(d,m),u.setEasyOptions&&u.setEasyOptions(d,t.Curl.option),a(d.perform(),{method:e,url:n,options:u});let h=d.getInfo(`RESPONSE_CODE`).data,g=i(m),{body:_}=f;function v(e){return o(h,_),e?_.toString(e):_}return n=d.getInfo(`EFFECTIVE_URL`).data,d.close(),{statusCode:h,headers:g,url:n,body:_,getBody:v,getJSON:t=>{try{return JSON.parse(_.toString(t))}catch(r){throw Error(` The server body response for - ${e} - ${n} cannot be parsed as JSON. Body: ${_.toString(t)} JSON-Parsing Error Message: ${r instanceof Error?r.message:String(r)} `)}}}};var h=m,g=h;module.exports=g; //# sourceMappingURL=index.cjs.map