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.24 kB
JavaScript
import{t as e}from"./errors-DAhwmOTl.mjs";import{Curl as t,CurlCode as n,Easy as r}from"node-libcurl";const i=(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},a=e=>e?Object.entries(e).filter(([e,t])=>t!==void 0).map(([e,t])=>t===``?`${e};`:`${e}: ${t}`):[],o=e=>e.reduce((e,t)=>{let[n,...r]=t.split(`:`);return n&&r.length>0&&(e[n.trim().toLowerCase()]=r.join(`:`).trim()),e},{}),s=(t,i)=>{if(t!==n.CURLE_OK)throw new e(t,`
Curl request failed with code ${t}:
- ${r.strError(t)}
You can also look up the Libcurl Error (code ${t}) here:
- https://curl.se/libcurl/c/libcurl-errors.html
DEBUG: {
method: "${i.method}",
url: "${i.url}",
options: ${JSON.stringify(i.options)}
}
`)},c=(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.
`)},l=(e,n)=>{let i=new r;return i.setOpt(t.option.CUSTOMREQUEST,e),i.setOpt(t.option.TIMEOUT_MS,n.timeout??0),i.setOpt(t.option.FOLLOWLOCATION,n.followRedirects===void 0||n.followRedirects),i.setOpt(t.option.MAXREDIRS,n.maxRedirects??-1),i.setOpt(t.option.SSL_VERIFYPEER,!n.insecure),i.setOpt(t.option.NOBODY,e===`HEAD`),i},u=(e,n,r)=>{n=r&&Object.keys(r).length?i(n,r):n,e.setOpt(t.option.URL,n)},d=(e,n)=>{e.setOpt(t.option.HEADERFUNCTION,e=>(n.push(e.toString(`utf-8`).trim()),e.length))},f=(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.option.POSTFIELDS,i)},p=(e,n,r)=>{if(Buffer.isBuffer(n)){let r=0;e.setOpt(t.option.POST,!0),e.setOpt(t.option.POSTFIELDSIZE,-1),e.setOpt(t.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.option.POSTFIELDS,n),r.push(`Content-Length: ${Buffer.byteLength(n,`utf-8`)}`)},m=(e,n)=>{e.setOpt(t.option.HTTPPOST,n)},h=(e,n,r,i)=>{n.json?f(e,n.json,i):n.body?p(e,n.body,i):n.formData?m(e,n.formData):i.push(`Content-Length: 0`),e.setOpt(t.option.WRITEFUNCTION,(e,t,n)=>(r.body=Buffer.concat([r.body,e.subarray(0,t*n)]),t*n)),e.setOpt(t.option.HTTPHEADER,i)};var g=(e,n,r={})=>{let i=l(e,r);u(i,n,r.qs);let f={body:Buffer.alloc(0)};h(i,r,f,a(r.headers));let p=[];d(i,p),r.setEasyOptions&&r.setEasyOptions(i,t.option),s(i.perform(),{method:e,url:n,options:r});let m=i.getInfo(`RESPONSE_CODE`).data,g=o(p),{body:_}=f;function v(e){return c(m,_),e?_.toString(e):_}return n=i.getInfo(`EFFECTIVE_URL`).data,i.close(),{statusCode:m,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)}
`)}}}};export{g as default};
//# sourceMappingURL=index.mjs.map