Comparing version 1.0.5 to 1.0.6
@@ -1,6 +0,6 @@ | ||
"use strict";var _=Object.create;var O=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var G=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty;var Y=(h,e)=>{for(var s in e)O(h,s,{get:e[s],enumerable:!0})},$=(h,e,s,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of M(e))!W.call(h,n)&&n!==s&&O(h,n,{get:()=>e[n],enumerable:!(o=z(e,n))||o.enumerable});return h};var R=(h,e,s)=>(s=h!=null?_(G(h)):{},$(e||!h||!h.__esModule?O(s,"default",{value:h,enumerable:!0}):s,h)),K=h=>$(O({},"__esModule",{value:!0}),h);var te={};Y(te,{UniqhttBrowser:()=>j,UniqhttEdge:()=>x,UniqhttNode:()=>S,YuniqCookies:()=>g,default:()=>ee});module.exports=K(te);var T=R(require("md5")),f=require("tough-cookie");function E(h){if(h instanceof Date)return h;if(typeof h=="string"&&h!=="Infinity")return Q(h)?new Date(q(h)):new Date(h);if(typeof h=="number")return new Date(q(h))}function Q(h){return/^[0-9]+$/.test(h)}function q(h){let e=h.toString().trim();try{if(typeof h=="number")return e.length===13?h:h*1e3;let s=parseInt(e);return s.toString().length===13?s:s*1e3}catch{return 0}}var b=class extends f.Cookie{constructor(e){e={...e,expires:E(e.expires)??"Infinity",creation:E(e.creation),lastAccessed:E(e.lastAccessed)},super(e),this.plainDomain=this.domain?this.domain.startsWith(".")?this.domain.substring(1):this.domain:null,this.hash=this.domain?(0,T.default)(`${this.domain.toString()}_${this.key}`):null}toSetCookieString(){let e=`${this.key}=${this.value}`;return this.expires!=="Infinity"&&(e+=`; Expires=${this.expires.toUTCString()}`),this.maxAge!==null&&(e+=`; Max-Age=${this.maxAge}`),this.domain&&(e+=`; Domain=${this.domain}`),this.path&&(e+=`; Path=${this.path}`),this.secure&&(e+="; Secure"),this.httpOnly&&(e+="; HttpOnly"),this.sameSite&&(e+=`; SameSite=${this.sameSite}`),e}toString(){return`${encodeURIComponent(this.key)}=${encodeURIComponent(this.value)}`}toNetscapeFormat(){let e=this.hostOnly?this.domain:this.domain.startsWith(".")?this.domain:"."+this.domain,s=this.secure?"TRUE":"FALSE",o=this.expires&&typeof this.expires!="string"?Math.round(this.expires.getTime()/1e3):0;return`${e} TRUE ${this.path} ${s} ${o} ${this.key} ${this.value}`}},g=class extends f.CookieJar{constructor(e,s){super(e,s)}setResponseCookies(e,s){var o;if(e instanceof Response)if((o=e.headers)!=null&&o.getSetCookie())for(let n of e.headers.getSetCookie()){let p=b.parse(n.trim());p&&this.setCookieSync(p,s)}else{let n=e.headers.get("set-cookie");n&&n.split(",").forEach(p=>{let i=b.parse(p);i&&this.setCookieSync(i,s)})}else Array.isArray(e)?e.forEach(n=>this.setCookieSync(n,s)):typeof e=="string"&&e.includes("=")&&e.includes(",")&&e.split(",").forEach(n=>{let p=b.parse(n);p&&this.setCookieSync(p,s)})}toNetscapeFormat(e){return e=this.getDomain(e),this.allCookies().filter(s=>s.domain===e||!e).map(s=>s.toNetscapeFormat()).join(` | ||
"use strict";var _=Object.create;var U=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var W=Object.getPrototypeOf,Y=Object.prototype.hasOwnProperty;var K=(y,e)=>{for(var s in e)U(y,s,{get:e[s],enumerable:!0})},$=(y,e,s,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of M(e))!Y.call(y,n)&&n!==s&&U(y,n,{get:()=>e[n],enumerable:!(o=z(e,n))||o.enumerable});return y};var S=(y,e,s)=>(s=y!=null?_(W(y)):{},$(e||!y||!y.__esModule?U(s,"default",{value:y,enumerable:!0}):s,y)),Q=y=>$(U({},"__esModule",{value:!0}),y);var se={};K(se,{UniqhttBrowser:()=>O,UniqhttEdge:()=>j,UniqhttNode:()=>x,YuniqCookies:()=>b,default:()=>te});module.exports=Q(se);var R=S(require("md5")),m=require("tough-cookie");function v(y){if(y instanceof Date)return y;if(typeof y=="string"&&y!=="Infinity")return V(y)?new Date(q(y)):new Date(y);if(typeof y=="number")return new Date(q(y))}function V(y){return/^[0-9]+$/.test(y)}function q(y){let e=y.toString().trim();try{if(typeof y=="number")return e.length===13?y:y*1e3;let s=parseInt(e);return s.toString().length===13?s:s*1e3}catch{return 0}}var C=class extends m.Cookie{constructor(e){e={...e,expires:v(e.expires)??"Infinity",creation:v(e.creation),lastAccessed:v(e.lastAccessed)},super(e),this.plainDomain=this.domain?this.domain.startsWith(".")?this.domain.substring(1):this.domain:null,this.hash=this.domain?(0,R.default)(`${this.domain.toString()}_${this.key}`):null}toSetCookieString(){let e=`${this.key}=${this.value}`;return this.expires!=="Infinity"&&(e+=`; Expires=${this.expires.toUTCString()}`),this.maxAge!==null&&(e+=`; Max-Age=${this.maxAge}`),this.domain&&(e+=`; Domain=${this.domain}`),this.path&&(e+=`; Path=${this.path}`),this.secure&&(e+="; Secure"),this.httpOnly&&(e+="; HttpOnly"),this.sameSite&&(e+=`; SameSite=${this.sameSite}`),e}toString(){return`${encodeURIComponent(this.key)}=${encodeURIComponent(this.value)}`}toNetscapeFormat(){let e=this.hostOnly?this.domain:this.domain.startsWith(".")?this.domain:"."+this.domain,s=this.secure?"TRUE":"FALSE",o=this.expires&&typeof this.expires!="string"?Math.round(this.expires.getTime()/1e3):0;return`${e} TRUE ${this.path} ${s} ${o} ${this.key} ${this.value}`}},b=class extends m.CookieJar{constructor(e,s){super(e,s)}setResponseCookies(e,s){var o;if(e instanceof Response)if((o=e.headers)!=null&&o.getSetCookie())for(let n of e.headers.getSetCookie()){let u=C.parse(n.trim());u&&this.setCookieSync(u,s)}else{let n=e.headers.get("set-cookie");n&&n.split(",").forEach(u=>{let r=C.parse(u);r&&this.setCookieSync(r,s)})}else Array.isArray(e)?e.forEach(n=>this.setCookieSync(n,s)):typeof e=="string"&&e.includes("=")&&e.includes(",")&&e.split(",").forEach(n=>{let u=C.parse(n);u&&this.setCookieSync(u,s)})}toNetscapeFormat(e){return e=this.getDomain(e),this.allCookies().filter(s=>s.domain===e||!e).map(s=>s.toNetscapeFormat()).join(` | ||
`)}getDomain(e){if(!e)return null;try{return new URL(e).hostname}catch{return null}}getURL(e){if(!e)return null;try{return new URL(e)}catch{return null}}toCookieString(e){return e=this.getDomain(e),this.allCookies().filter(s=>s.domain===e||!e).map(s=>s.toString()).join("; ")}toSetCookiesString(e){return e=this.getDomain(e),this.allCookies().filter(s=>s.domain===e||!e).map(s=>s.toSetCookieString()).join("; ")}domainToCookieString(e){return this.allCookies().filter(s=>s.domain===e).map(s=>`${s.key}=${s.value}`).join("; ")}parseNetscapeCookies(e,s=!1){let o=e.split(` | ||
`),n=this.allCookies().filter(t=>t.hash).map(t=>t.hash),p=o.map(t=>{let[l,r,u,a,d,c,y]=t.split(" "),w=`http${a==="TRUE"?"s":""}://${l.startsWith(".")?l.substring(1):l}${u}`,v=(0,T.default)(`${w}_${c}`);return s?{url:w,cookie:new f.Cookie({domain:l,path:u,secure:a==="TRUE",expires:d?new Date(parseInt(d)*1e3):void 0,key:c,value:y})}:n.includes(v)?null:{url:w,cookie:new f.Cookie({domain:l,path:u,secure:a==="TRUE",expires:d?new Date(parseInt(d)*1e3):void 0,key:c,value:y})}}).filter(t=>t!==null),i={};for(let t of p)i[t.url]||(i[t.url]=[]),i[t.url].push(t.cookie);for(let[t,l]of Object.entries(i))this.setResponseCookies(l,t)}parseCookieString(e,s,o=!1){let n=new URL(s),p=this.allCookies().filter(r=>r.hash).map(r=>r.hash),t=e.split("; ").map(r=>{let[u,...a]=r.split("="),d=a.join("="),c=`http${n.protocol.includes("s")?"s":""}://${n.hostname.startsWith(".")?n.hostname.substring(1):n.hostname}/`,y=(0,T.default)(`${n.hostname}_${u}`);return o?{url:c,cookie:new f.Cookie({key:u,value:d,domain:n.hostname,path:"/"})}:p.includes(y)?null:{url:c,cookie:new f.Cookie({key:u,value:d,domain:n.hostname,path:"/"})}}).filter(r=>r!==null),l={};for(let r of t)l[r.url]||(l[r.url]=[]),l[r.url].push(r.cookie);for(let[r,u]of Object.entries(l))this.setResponseCookies(u,r)}toArray(){return super.toJSON().cookies.map(e=>{let s=e;return{...s,expires:this.parseDate(s.expires),creation:this.parseDate(s.creation),lastAccessed:this.parseDate(s.lastAccessed)}})}fromArray(e,s=!1){let o=this.allCookies().filter(i=>i.hash).map(i=>i.hash),n=e.map(i=>{if(i instanceof f.Cookie){if(i.domain){let t=`http${i.secure?"s":""}://${i.domain.startsWith(".")?i.domain.substring(1):i.domain}${i.path}`,l=(0,T.default)(`${t}_${i.key}`);return s?{url:t,cookie:i}:o.includes(l)?null:{url:t,cookie:i}}return null}else{if(i instanceof b){if(i.domain){let t=`http${i.secure?"s":""}://${i.domain.startsWith(".")?i.domain.substring(1):i.domain}${i.path}`,l=(0,T.default)(`${t}_${i.key}`);return s?{url:t,cookie:new f.Cookie({domain:i.domain,path:i.path||"/",secure:i.secure,expires:this.parseDate(i.expires),key:i.key,value:i.value})}:o.includes(l)?null:{url:t,cookie:new f.Cookie({domain:i.domain,path:i.path||"/",secure:i.secure,expires:this.parseDate(i.expires),key:i.key,value:i.value})}}return null}else{let t=this.parseJson(i);if(t&&t.domain&&t.key){let l=`http${t.secure?"s":""}://${t.domain.startsWith(".")?t.domain.substring(1):t.domain}${t.path}`,r=(0,T.default)(`${l}_${t.key}`);return s?{url:l,cookie:new f.Cookie({domain:t.domain,path:t.path||"/",secure:t.secure,expires:this.parseDate(t.expires),key:t.key,value:t.value})}:o.includes(r)?null:{url:l,cookie:new f.Cookie({domain:t.domain,path:t.path||"/",secure:t.secure,expires:this.parseDate(t.expires),key:t.key,value:t.value})}}}return null}}).filter(i=>i!==null),p={};for(let i of n)p[i.url]||(p[i.url]=[]),p[i.url].push(i.cookie);for(let[i,t]of Object.entries(p))this.setResponseCookies(t,i)}parseDate(e){if(e instanceof Date)return e;if(typeof e=="string"&&e!=="Infinity")return this.isNumberOnly(e)?new Date(this.isMillisecondsLength(e)):new Date(e);if(typeof e=="number")return new Date(this.isMillisecondsLength(e))}isNumberOnly(e){return/^[0-9]+$/.test(e)}parseJson(e){try{return typeof e=="object"?e:JSON.parse(e)}catch{return null}}isMillisecondsLength(e){let s=e.toString().trim();try{if(typeof e=="number")return s.length===13?e:e*1e3;let o=parseInt(s);return o.toString().length===13?o:o*1e3}catch{return 0}}clear(){super.removeAllCookiesSync()}removeAllCookiesSync(){super.removeAllCookiesSync()}async removeAllCookies(){await super.removeAllCookies()}getCookiesForDomain(e){return this.allCookies().filter(s=>s.domain&&e.includes(s.domain))}allCookies(){return super.toJSON().cookies.map(e=>new b(e))}findCookie(e,s){return this.allCookies().find(o=>o.key===e&&o.domain===s)}getCookiesSync(e,s){return super.getCookiesSync(e,s).map(o=>new b(o.toJSON()))}async getCookies(e,s,o){if(typeof s=="function")super.getCookies(e,(n,p)=>{s(n,p.map(i=>new b(i.toJSON())))});else{let p=(await super.getCookies(e,s)).map(i=>new b(i.toJSON()));if(o)o(null,p);else return p}}getAllCookies(e){let s=this.getCookiesSync(e);try{let o=this.toJSON().cookies,n=[...s];return o.forEach(p=>{n.some(i=>i.key===p.key)||n.push(new b(p))}),n}catch(o){console.log(o),console.error(`Error parsing cookies: ${o.message}`)}return s}get size(){return this.allCookies().length}isEmpty(){return this.allCookies().length===0}*[Symbol.iterator](){for(let e of this.allCookies())yield e}};var U=class h extends Error{constructor(s,o,n,p,i){super(s);this.response={};o instanceof Response?this.response={data:n,status:o.status,statusText:o.statusText,finalUrl:o.url,cookies:void 0,headers:p,contentType:o.headers.get("Content-Type"),config:i}:this.response={data:n,status:o==null?void 0:o.status,statusText:o==null?void 0:o.statusText,finalUrl:o==null?void 0:o.url,cookies:void 0,headers:p,contentType:void 0,config:i},Object.setPrototypeOf(this,h.prototype)}},k=class{constructor(){this.fetch=fetch,this.jar=new g}async get(e,s){return this.request(e,"GET",void 0,s)}async post(e,s,o){return this.request(e,"POST",s,o)}async postForm(e,s,o){let n="";if(s instanceof URLSearchParams)n=s.toString();else if(s instanceof FormData){let i=[];for(let[t,l]of s.entries())i.push(`${t}=${l}`);n=i.join("&")}else if(typeof s=="string")n=s;else if(typeof s=="object"){let i=new URLSearchParams;for(let[t,l]of Object.entries(s))i.append(t,l?typeof l=="object"?JSON.stringify(l):l.toString():"");n=i.toString()}let p=o!=null&&o.headers?o.headers instanceof Headers?o.headers:new Headers(o.headers):new Headers;return p.set("Content-Type","application/x-www-form-urlencoded"),this.request(e,"POST",n,{...o,headers:p,isFormData:!0})}async postJson(e,s,o){let n={};if(typeof s=="string"){if(n=this.parseJson(s),!n)throw this.Error({status:400,statusText:"Bad Request"},"Invalid JSON string")}else n=s;let p=o!=null&&o.headers?o.headers instanceof Headers?o.headers:new Headers(o.headers):new Headers;return p.set("Content-Type","application/json"),this.request(e,"POST",JSON.stringify(n),{...o,headers:p,isJson:!0})}async put(e,s,o){return this.request(e,"PUT",s,o)}async patch(e,s,o){return this.request(e,"PATCH",s,o)}async delete(e,s){return this.request(e,"DELETE",void 0,s)}async head(e,s){return this.request(e,"HEAD",void 0,s)}async options(e,s){return this.request(e,"OPTIONS",void 0,s)}parseJson(e){try{return JSON.parse(e)}catch{return null}}async Error(e,s,o){if(e instanceof Response){let n=e.headers.get("Content-Type"),p=await this.parseResponseBody(e,n),i={};return e.headers.forEach((t,l)=>{l.toLowerCase()!=="set-cookie"&&l.toLowerCase()!=="set-cookies"&&(i[l]=t)}),new U(s??e.statusText??"Unknown error",e,p,i,o)}return new U(s??"Unknown error",e??{},null,{})}async formatResponse(e,s,o,n){let p=e.headers.get("Content-Type"),i=n?null:await this.parseResponseBody(e,p),t={};return e.headers.forEach((l,r)=>{r.toLowerCase()!=="set-cookie"&&r.toLowerCase()!=="set-cookies"&&(t[r]=l)}),{data:i,status:e.status,statusText:e.statusText,headers:t,finalUrl:s,cookies:{array:this.jar.toArray(),string:this.jar.toCookieString(),netscape:this.jar.toNetscapeFormat()},config:o,contentType:p,...n}}async parseResponseBody(e,s){return s&&s.includes("application/json")?await e.json():s&&["text","xml","xhtml+xml","html","php","javascript","ecmascript","x-javascript","typescript","x-httpd-php","x-php","x-python","x-python","x-ruby","x-ruby","x-sh","x-bash","x-java","x-java-source","x-c","x-c++","x-csrc","x-chdr","x-csharp","x-csh","x-go","x-go","x-scala","x-scala","x-rust","rust","x-swift","x-swift","x-kotlin","x-kotlin","x-perl","x-perl","x-lua","x-lua","x-haskell","x-haskell","x-sql","sql","css","csv","plain","markdown","x-markdown","x-latex","x-tex"].some(n=>s.includes(n))?await e.text():await e.arrayBuffer()}};var m=R(require("fs")),C=R(require("path")),H=R(require("form-data")),S=class extends k{constructor(){super()}setCookies(e,s,o){o&&(this.jar=new g),Array.isArray(e)?this.jar.fromArray(e,o):typeof e=="string"&&(s&&e.includes("; ")?this.jar.parseCookieString(e,s,o):e.includes(` | ||
`)&&e.split(" ").length>3&&this.jar.parseNetscapeCookies(e,o))}getCookies(){return{array:this.jar.toArray(),string:this.jar.toCookieString(),netscape:this.jar.toNetscapeFormat()}}async request(e,s,o=void 0,n={}){n.body=o||n.body;let p=process.hrtime(),i,t=n.maxRedirects||10,l=n.debug!==void 0?n.debug:!1,r=null;if(n.saveTo){let d=C.basename(n.saveTo);if(this.checkISPermission(n.saveTo.length?C.dirname(n.saveTo):C.resolve(process.cwd()))){let c=d.length<n.saveTo.length?C.dirname(n.saveTo):C.join(process.cwd(),"download");m.existsSync(c)||m.mkdirSync(c,{recursive:!0}),r=C.join(c,d)}else n.debug&&console.log(`Permission denied to save to ${n.saveTo}, returning the response data instead`)}let u=0,a=e instanceof URL?e.href:e;for(;;){if(n.params&&typeof n.params=="object"){let c=new URLSearchParams(n.params).toString(),y=new URL(a);y.search=y.search?y.search+"&"+c:"?"+c,a=y.toString()}let d=this.prepareFetchOptions(n,a,s);try{let c=await fetch(a,{...d,method:s,redirect:"manual"});if(this.jar.setResponseCookies(c,a),[301,302,303,307,308].includes(c.status)&&!n.dontFollowRedirects){if(u>=t)throw await this.Error(c,`Max redirects (${t}) reached`,d);let y=c.headers.get("Location");if(!y)throw await this.Error(c,"Redirect location not found",d);a=new URL(y,a).toString(),u++,l&&console.log(`Redirecting to: ${a}`),c.status===303&&(s="GET",delete n.body);continue}if(c.status<200||c.status>308||!c.ok)throw r?await this.Error(c,`Failed to download: ${c.statusText}`,d):await this.Error(c,void 0,d);if(r&&c.body){let y=m.createWriteStream(r);await new Promise(async(F,J)=>{let N=c.body.getReader();for(;;){let{done:B,value:I}=await N.read();if(B)break;y.write(Buffer.from(I))}y.end(),y.on("finish",F),y.on("error",J)});let w=m.statSync(r).size,[v,A]=process.hrtime(p),D=v+A/1e9;i={fileName:r,totalTime:D,downloadSpeed:w/D,size:w}}return this.formatResponse(c,a,d,i)}catch(c){throw c}}}prepareFetchOptions(e,s,o,n){let i=["post","put","patch"].includes(o.toLowerCase()),t={},l=e.headers instanceof Headers?e.headers:new Headers(e.headers||{}),r=e.contentType||l.get("Content-Type")||(i?"application/json":void 0);if(e.headers?t.headers=l instanceof Headers?l:new Headers(l):t.headers=new Headers,l.has("Cookie")){let a=l.get("Cookie");this.setCookies(a,s),l.delete("Cookie"),t.headers.delete("Cookie")}e.cookies&&this.setCookies(e.cookies,s);let u;if(e.useAllCookies?u=this.jar.getAllCookies(s):u=this.jar.getCookiesSync(s),e.printHeaders&&console.log("Cookies:",u),u.length>0&&t.headers.set("Cookie",u.map(a=>`${a.key}=${a.value}`).join("; ")),e.body&&(t.body=e.body),e.timeout){let a=new AbortController;setTimeout(()=>a.abort(),e.timeout),t.signal=a.signal}if(e.isFormData||e.isJson)e.isFormData?(t.body=e.body,r="application/x-www-form-urlencoded",t.headers.set("Content-Type",r)):(t.body=e.body,r="application/json",t.headers.set("Content-Type",r));else if(e.json)t.body=JSON.stringify(e.json),r="application/json",t.headers.set("Content-Type",r);else if(e.form_params)t.body=new URLSearchParams(e.form_params).toString(),r="application/x-www-form-urlencoded",t.headers.set("Content-Type",r);else if(e.multipart){let a=new H.default;Object.entries(e.multipart).forEach(([d,c])=>{a.append(d,c)}),t.body=a}else if(e.requestType)switch(e.requestType){case"json":r="application/json",typeof t.body=="object"&&(t.body=JSON.stringify(t.body)),t.headers.set("Content-Type",r);break;case"form":r="application/x-www-form-urlencoded",typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString()),t.headers.set("Content-Type",r);break;case"formData":if(typeof t.body=="object"){let a=new H.default;Object.entries(t.body).forEach(([d,c])=>{a.append(d,c)}),t.body=a}break;case"text":r="text/plain",t.headers.set("Content-Type",r);break}else if(r){let a=r.toLowerCase();if(a.includes("json"))t.headers.set("Content-Type","application/json"),t.body&&typeof t.body=="object"&&(t.body=JSON.stringify(t.body));else if(a.includes("x-www-form-urlencoded"))t.headers.set("Content-Type","application/x-www-form-urlencoded"),t.body&&typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString());else if(a.includes("multipart")){if(t.body&&typeof t.body=="object"){let d=new H.default;Object.entries(t.body).forEach(([c,y])=>{d.append(c,y)}),t.body=d}}else a.includes("text/plain")&&t.headers.set("Content-Type","text/plain")}else r&&!e.withoutContentType&&t.headers.set("Content-Type",r);return e.withoutContentType&&t.headers.delete("Content-Type"),e.withoutBodyOnRedirect&&n&&(t.body=void 0),e.printHeaders&&console.log("Fetch headers:",t.headers),t}checkISPermission(e){try{return m.accessSync(e,m.constants.R_OK|m.constants.W_OK),!0}catch{return!1}}async getHeaders(e){let s=await this.fetch(e,{method:"HEAD"});return Object.fromEntries(s.headers.entries())}};var P=R(require("form-data")),x=class extends k{constructor(){super()}setCookies(e,s,o){o&&(this.jar=new g),Array.isArray(e)?this.jar.fromArray(e,o):typeof e=="string"&&(s&&e.includes("; ")?this.jar.parseCookieString(e,s,o):e.includes(` | ||
`)&&e.split(" ").length>3&&this.jar.parseNetscapeCookies(e,o))}getCookies(){return{array:this.jar.toArray(),string:this.jar.toString(),netscape:this.jar.toNetscapeFormat()}}async request(e,s,o=void 0,n={}){let p,i=n.maxRedirects||10,t=n.debug!==void 0?n.debug:!1,l=null;if(n.saveTo)throw this.Error(void 0,"You can only use this feature in Node.js, not in Edge or Browser.");let r=0,u=e instanceof URL?e.href:e;for(;;){if(n.params&&typeof n.params=="object"){let d=new URLSearchParams(n.params),c=new URL(u);d.forEach((y,w)=>{c.searchParams.set(w,y)}),u=c.href,delete n.params}let a=this.prepareFetchOptions(n,u,s,r>0);try{let d=await fetch(u,{...a,method:s,redirect:"manual"});if(this.jar.setResponseCookies(d,u),[301,302,303,307,308].includes(d.status)&&!n.dontFollowRedirects){if(r>=i)throw await this.Error(d,`Max redirects (${i}) reached`,a);let c=d.headers.get("Location");if(!c)throw await this.Error(d,"Redirect location not found",a);u=new URL(c,u).toString(),r++,t&&console.log(`Redirecting to: ${u}`),d.status===303&&(s="GET",delete n.body);continue}if(d.status<200||d.status>308||!d.ok)throw l?await this.Error(d,`Failed to download: ${d.statusText}`,a):await this.Error(d,void 0,a);return this.formatResponse(d,u,a,p)}catch(d){throw d}}}prepareFetchOptions(e,s,o,n){let i=["post","put","patch"].includes(o.toLowerCase()),t={},l=e.headers instanceof Headers?e.headers:new Headers(e.headers||{}),r=e.contentType||l.get("Content-Type")||(i?"application/json":void 0);if(e.headers?t.headers=l instanceof Headers?l:new Headers(l):t.headers=new Headers,l.has("Cookie")){let a=l.get("Cookie");this.setCookies(a,s),l.delete("Cookie"),t.headers.delete("Cookie")}e.cookies&&this.setCookies(e.cookies,s);let u;if(e.useAllCookies?u=this.jar.getAllCookies(s):u=this.jar.getCookiesSync(s),e.printHeaders&&console.log("Cookies:",u),u.length>0&&t.headers.set("Cookie",u.map(a=>`${a.key}=${a.value}`).join("; ")),e.body&&(t.body=e.body),e.timeout){let a=new AbortController;setTimeout(()=>a.abort(),e.timeout),t.signal=a.signal}if(e.isFormData||e.isJson)e.isFormData?(t.body=e.body,r="application/x-www-form-urlencoded",t.headers.set("Content-Type",r)):(t.body=e.body,r="application/json",t.headers.set("Content-Type",r));else if(e.json)t.body=JSON.stringify(e.json),r="application/json",t.headers.set("Content-Type",r);else if(e.form_params)t.body=new URLSearchParams(e.form_params).toString(),r="application/x-www-form-urlencoded",t.headers.set("Content-Type",r);else if(e.multipart){let a=new P.default;Object.entries(e.multipart).forEach(([d,c])=>{a.append(d,c)}),t.body=a}else if(e.requestType)switch(e.requestType){case"json":r="application/json",typeof t.body=="object"&&(t.body=JSON.stringify(t.body)),t.headers.set("Content-Type",r);break;case"form":r="application/x-www-form-urlencoded",typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString()),t.headers.set("Content-Type",r);break;case"formData":if(typeof t.body=="object"){let a=new P.default;Object.entries(t.body).forEach(([d,c])=>{a.append(d,c)}),t.body=a}break;case"text":r="text/plain",t.headers.set("Content-Type",r);break}else if(r){let a=r.toLowerCase();if(a.includes("json"))t.headers.set("Content-Type","application/json"),t.body&&typeof t.body=="object"&&(t.body=JSON.stringify(t.body));else if(a.includes("x-www-form-urlencoded"))t.headers.set("Content-Type","application/x-www-form-urlencoded"),t.body&&typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString());else if(a.includes("multipart")){if(t.body&&typeof t.body=="object"){let d=new P.default;Object.entries(t.body).forEach(([c,y])=>{d.append(c,y)}),t.body=d}}else a.includes("text/plain")&&t.headers.set("Content-Type","text/plain")}else r&&!e.withoutContentType&&t.headers.set("Content-Type",r);return e.withoutContentType&&t.headers.delete("Content-Type"),e.withoutBodyOnRedirect&&n&&(t.body=void 0),e.printHeaders&&console.log("Fetch headers:",t.headers),t}async getHeaders(e){let s=await this.fetch(e,{method:"HEAD"});return Object.fromEntries(s.headers.entries())}};var L=R(require("form-data")),j=class extends k{constructor(){super()}setCookies(e,s,o){o&&(this.jar=new g),Array.isArray(e)?this.jar.fromArray(e,o):typeof e=="string"&&(s&&e.includes("; ")?this.jar.parseCookieString(e,s,o):e.includes(` | ||
`)&&e.split(" ").length>3&&this.jar.parseNetscapeCookies(e,o))}getCookies(){return{array:this.jar.toArray(),string:this.jar.toString(),netscape:this.jar.toNetscapeFormat()}}async request(e,s,o=void 0,n={}){let p,i=n.maxRedirects||10,t=n.debug!==void 0?n.debug:!1,l=null;if(n.saveTo)throw this.Error(void 0,"You can only use this feature in Node.js, not in Edge or Browser.");let r=0,u=e instanceof URL?e.href:e;for(;;){if(n.params&&typeof n.params=="object"){let d=new URLSearchParams(n.params),c=new URL(u);d.forEach((y,w)=>{c.searchParams.set(w,y)}),u=c.href,delete n.params}let a=this.prepareFetchOptions(n,u,s,r>0);try{let d=await fetch(u,{...a,method:s,redirect:"manual"});if(this.jar.setResponseCookies(d,u),[301,302,303,307,308].includes(d.status)&&!n.dontFollowRedirects){if(r>=i)throw await this.Error(d,`Max redirects (${i}) reached`,a);let c=d.headers.get("Location");if(!c)throw await this.Error(d,"Redirect location not found",a);u=new URL(c,u).toString(),r++,t&&console.log(`Redirecting to: ${u}`),d.status===303&&(s="GET",delete n.body);continue}if(d.status<200||d.status>308||!d.ok)throw l?await this.Error(d,`Failed to download: ${d.statusText}`,a):await this.Error(d,void 0,a);return this.formatResponse(d,u,a,p)}catch(d){throw d}}}prepareFetchOptions(e,s,o,n){let i=["post","put","patch"].includes(o.toLowerCase()),t={},l=e.headers instanceof Headers?e.headers:new Headers(e.headers||{}),r=e.contentType||l.get("Content-Type")||(i?"application/json":void 0);if(e.headers?t.headers=l instanceof Headers?l:new Headers(l):t.headers=new Headers,l.has("Cookie")){let a=l.get("Cookie");this.setCookies(a,s),l.delete("Cookie"),t.headers.delete("Cookie")}e.cookies&&this.setCookies(e.cookies,s);let u;if(e.useAllCookies?u=this.jar.getAllCookies(s):u=this.jar.getCookiesSync(s),e.printHeaders&&console.log("Cookies:",u),u.length>0&&t.headers.set("Cookie",u.map(a=>`${a.key}=${a.value}`).join("; ")),e.body&&(t.body=e.body),e.timeout){let a=new AbortController;setTimeout(()=>a.abort(),e.timeout),t.signal=a.signal}if(e.isFormData||e.isJson)e.isFormData?(t.body=e.body,r="application/x-www-form-urlencoded",t.headers.set("Content-Type",r)):(t.body=e.body,r="application/json",t.headers.set("Content-Type",r));else if(e.json)t.body=JSON.stringify(e.json),r="application/json",t.headers.set("Content-Type",r);else if(e.form_params)t.body=new URLSearchParams(e.form_params).toString(),r="application/x-www-form-urlencoded",t.headers.set("Content-Type",r);else if(e.multipart){let a=new L.default;Object.entries(e.multipart).forEach(([d,c])=>{a.append(d,c)}),t.body=a}else if(e.requestType)switch(e.requestType){case"json":r="application/json",typeof t.body=="object"&&(t.body=JSON.stringify(t.body)),t.headers.set("Content-Type",r);break;case"form":r="application/x-www-form-urlencoded",typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString()),t.headers.set("Content-Type",r);break;case"formData":if(typeof t.body=="object"){let a=new L.default;Object.entries(t.body).forEach(([d,c])=>{a.append(d,c)}),t.body=a}break;case"text":r="text/plain",t.headers.set("Content-Type",r);break}else if(r){let a=r.toLowerCase();if(a.includes("json"))t.headers.set("Content-Type","application/json"),t.body&&typeof t.body=="object"&&(t.body=JSON.stringify(t.body));else if(a.includes("x-www-form-urlencoded"))t.headers.set("Content-Type","application/x-www-form-urlencoded"),t.body&&typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString());else if(a.includes("multipart")){if(t.body&&typeof t.body=="object"){let d=new L.default;Object.entries(t.body).forEach(([c,y])=>{d.append(c,y)}),t.body=d}}else a.includes("text/plain")&&t.headers.set("Content-Type","text/plain")}else r&&!e.withoutContentType&&t.headers.set("Content-Type",r);return e.withoutContentType&&t.headers.delete("Content-Type"),e.withoutBodyOnRedirect&&n&&(t.body=void 0),e.printHeaders&&console.log("Fetch headers:",t.headers),t}async getHeaders(e){let s=await this.fetch(e,{method:"HEAD"});return Object.fromEntries(s.headers.entries())}};function V(){return typeof process<"u"&&process.versions&&process.versions.node?"node":typeof window<"u"&&typeof window.document<"u"?"browser":"edge"}function X(){switch(V()){case"node":return new S;case"browser":return new j;case"edge":return new x;default:throw new Error("Unsupported environment")}}var Z=X(),ee=Z;0&&(module.exports={UniqhttBrowser,UniqhttEdge,UniqhttNode,YuniqCookies}); | ||
`),n=this.allCookies().filter(t=>t.hash).map(t=>t.hash),u=o.map(t=>{let[d,a,h,i,c,l,p]=t.split(" "),f=`http${i==="TRUE"?"s":""}://${d.startsWith(".")?d.substring(1):d}${h}`,T=(0,R.default)(`${f}_${l}`);return s?{url:f,cookie:new m.Cookie({domain:d,path:h,secure:i==="TRUE",expires:c?new Date(parseInt(c)*1e3):void 0,key:l,value:p})}:n.includes(T)?null:{url:f,cookie:new m.Cookie({domain:d,path:h,secure:i==="TRUE",expires:c?new Date(parseInt(c)*1e3):void 0,key:l,value:p})}}).filter(t=>t!==null),r={};for(let t of u)r[t.url]||(r[t.url]=[]),r[t.url].push(t.cookie);for(let[t,d]of Object.entries(r))this.setResponseCookies(d,t)}parseCookieString(e,s,o=!1){let n=new URL(s),u=this.allCookies().filter(a=>a.hash).map(a=>a.hash),t=e.split("; ").map(a=>{let[h,...i]=a.split("="),c=i.join("="),l=`http${n.protocol.includes("s")?"s":""}://${n.hostname.startsWith(".")?n.hostname.substring(1):n.hostname}/`,p=(0,R.default)(`${n.hostname}_${h}`);return o?{url:l,cookie:new m.Cookie({key:h,value:c,domain:n.hostname,path:"/"})}:u.includes(p)?null:{url:l,cookie:new m.Cookie({key:h,value:c,domain:n.hostname,path:"/"})}}).filter(a=>a!==null),d={};for(let a of t)d[a.url]||(d[a.url]=[]),d[a.url].push(a.cookie);for(let[a,h]of Object.entries(d))this.setResponseCookies(h,a)}toArray(){return super.toJSON().cookies.map(e=>{let s=e;return{...s,expires:this.parseDate(s.expires),creation:this.parseDate(s.creation),lastAccessed:this.parseDate(s.lastAccessed)}})}fromArray(e,s=!1){let o=this.allCookies().filter(r=>r.hash).map(r=>r.hash),n=e.map(r=>{if(r instanceof m.Cookie){if(r.domain){let t=`http${r.secure?"s":""}://${r.domain.startsWith(".")?r.domain.substring(1):r.domain}${r.path}`,d=(0,R.default)(`${t}_${r.key}`);return s?{url:t,cookie:r}:o.includes(d)?null:{url:t,cookie:r}}return null}else{if(r instanceof C){if(r.domain){let t=`http${r.secure?"s":""}://${r.domain.startsWith(".")?r.domain.substring(1):r.domain}${r.path}`,d=(0,R.default)(`${t}_${r.key}`);return s?{url:t,cookie:new m.Cookie({domain:r.domain,path:r.path||"/",secure:r.secure,expires:this.parseDate(r.expires),key:r.key,value:r.value})}:o.includes(d)?null:{url:t,cookie:new m.Cookie({domain:r.domain,path:r.path||"/",secure:r.secure,expires:this.parseDate(r.expires),key:r.key,value:r.value})}}return null}else{let t=this.parseJson(r);if(t&&t.domain&&t.key){let d=`http${t.secure?"s":""}://${t.domain.startsWith(".")?t.domain.substring(1):t.domain}${t.path}`,a=(0,R.default)(`${d}_${t.key}`);return s?{url:d,cookie:new m.Cookie({domain:t.domain,path:t.path||"/",secure:t.secure,expires:this.parseDate(t.expires),key:t.key,value:t.value})}:o.includes(a)?null:{url:d,cookie:new m.Cookie({domain:t.domain,path:t.path||"/",secure:t.secure,expires:this.parseDate(t.expires),key:t.key,value:t.value})}}}return null}}).filter(r=>r!==null),u={};for(let r of n)u[r.url]||(u[r.url]=[]),u[r.url].push(r.cookie);for(let[r,t]of Object.entries(u))this.setResponseCookies(t,r)}parseDate(e){if(e instanceof Date)return e;if(typeof e=="string"&&e!=="Infinity")return this.isNumberOnly(e)?new Date(this.isMillisecondsLength(e)):new Date(e);if(typeof e=="number")return new Date(this.isMillisecondsLength(e))}isNumberOnly(e){return/^[0-9]+$/.test(e)}parseJson(e){try{return typeof e=="object"?e:JSON.parse(e)}catch{return null}}isMillisecondsLength(e){let s=e.toString().trim();try{if(typeof e=="number")return s.length===13?e:e*1e3;let o=parseInt(s);return o.toString().length===13?o:o*1e3}catch{return 0}}clear(){super.removeAllCookiesSync()}removeAllCookiesSync(){super.removeAllCookiesSync()}async removeAllCookies(){await super.removeAllCookies()}getCookiesForDomain(e){return this.allCookies().filter(s=>s.domain&&e.includes(s.domain))}allCookies(){return super.toJSON().cookies.map(e=>new C(e))}findCookie(e,s){return this.allCookies().find(o=>o.key===e&&o.domain===s)}getCookiesSync(e,s){return super.getCookiesSync(e,s).map(o=>new C(o.toJSON()))}async getCookies(e,s,o){if(typeof s=="function")super.getCookies(e,(n,u)=>{s(n,u.map(r=>new C(r.toJSON())))});else{let u=(await super.getCookies(e,s)).map(r=>new C(r.toJSON()));if(o)o(null,u);else return u}}getAllCookies(e){let s=this.getCookiesSync(e);try{let o=this.toJSON().cookies,n=[...s];return o.forEach(u=>{n.some(r=>r.key===u.key)||n.push(new C(u))}),n}catch(o){console.log(o),console.error(`Error parsing cookies: ${o.message}`)}return s}get size(){return this.allCookies().length}isEmpty(){return this.allCookies().length===0}*[Symbol.iterator](){for(let e of this.allCookies())yield e}};var P=class y extends Error{constructor(s,o,n,u,r){super(s);this.response={};o instanceof Response?this.response={data:n,status:o.status,statusText:o.statusText,finalUrl:o.url,cookies:void 0,headers:u,contentType:o.headers.get("Content-Type"),config:r}:this.response={data:n,status:o==null?void 0:o.status,statusText:o==null?void 0:o.statusText,finalUrl:o==null?void 0:o.url,cookies:void 0,headers:u,contentType:void 0,config:r},Object.setPrototypeOf(this,y.prototype)}},k=class{constructor(){this.fetch=fetch,this.jar=new b}async get(e,s){return this.request(e,"GET",void 0,s)}async post(e,s,o){return this.request(e,"POST",s,o)}async postForm(e,s,o){let n="";if(s instanceof URLSearchParams)n=s.toString();else if(s instanceof FormData){let r=[];for(let[t,d]of s.entries())r.push(`${t}=${d}`);n=r.join("&")}else if(typeof s=="string")n=s;else if(typeof s=="object"){let r=new URLSearchParams;for(let[t,d]of Object.entries(s))r.append(t,d?typeof d=="object"?JSON.stringify(d):d.toString():"");n=r.toString()}let u=o!=null&&o.headers?o.headers instanceof Headers?o.headers:new Headers(o.headers):new Headers;return u.set("Content-Type","application/x-www-form-urlencoded"),this.request(e,"POST",n,{...o,headers:u,isFormData:!0})}async postJson(e,s,o){let n={};if(typeof s=="string"){if(n=this.parseJson(s),!n)throw this.Error({status:400,statusText:"Bad Request"},"Invalid JSON string")}else n=s;let u=o!=null&&o.headers?o.headers instanceof Headers?o.headers:new Headers(o.headers):new Headers;return u.set("Content-Type","application/json"),this.request(e,"POST",JSON.stringify(n),{...o,headers:u,isJson:!0})}async put(e,s,o){return this.request(e,"PUT",s,o)}async patch(e,s,o){return this.request(e,"PATCH",s,o)}async delete(e,s){return this.request(e,"DELETE",void 0,s)}async head(e,s){return this.request(e,"HEAD",void 0,s)}async options(e,s){return this.request(e,"OPTIONS",void 0,s)}parseJson(e){try{return JSON.parse(e)}catch{return null}}async Error(e,s,o){if(e instanceof Response){let n=e.headers.get("Content-Type"),u=await this.parseResponseBody(e,n),r={};return e.headers.forEach((t,d)=>{d.toLowerCase()!=="set-cookie"&&d.toLowerCase()!=="set-cookies"&&(r[d]=t)}),new P(s??e.statusText??"Unknown error",e,u,r,o)}return new P(s??"Unknown error",e??{},null,{})}async formatResponse(e,s,o,n){let u=e.headers.get("Content-Type"),r=n?null:await this.parseResponseBody(e,u),t={};return e.headers.forEach((d,a)=>{a.toLowerCase()!=="set-cookie"&&a.toLowerCase()!=="set-cookies"&&(t[a]=d)}),{data:r,status:e.status,statusText:e.statusText,headers:t,finalUrl:s,cookies:{array:this.jar.toArray(),string:this.jar.toCookieString(),netscape:this.jar.toNetscapeFormat()},config:o,contentType:u,...n}}async parseResponseBody(e,s){return s&&s.includes("application/json")?await e.json():s&&["text","xml","xhtml+xml","html","php","javascript","ecmascript","x-javascript","typescript","x-httpd-php","x-php","x-python","x-python","x-ruby","x-ruby","x-sh","x-bash","x-java","x-java-source","x-c","x-c++","x-csrc","x-chdr","x-csharp","x-csh","x-go","x-go","x-scala","x-scala","x-rust","rust","x-swift","x-swift","x-kotlin","x-kotlin","x-perl","x-perl","x-lua","x-lua","x-haskell","x-haskell","x-sql","sql","css","csv","plain","markdown","x-markdown","x-latex","x-tex"].some(n=>s.includes(n))?await e.text():await e.arrayBuffer()}};var g=S(require("fs")),w=S(require("path")),E=S(require("form-data")),x=class extends k{constructor(){super()}setCookies(e,s,o){o&&(this.jar=new b),Array.isArray(e)?this.jar.fromArray(e,o):typeof e=="string"&&(s&&e.includes("; ")?this.jar.parseCookieString(e,s,o):e.includes(` | ||
`)&&e.split(" ").length>3&&this.jar.parseNetscapeCookies(e,o))}getCookies(){return{array:this.jar.toArray(),string:this.jar.toCookieString(),netscape:this.jar.toNetscapeFormat()}}async request(e,s,o=void 0,n={}){let u=["GET","POST","PUT","PATCH","DELETE","HEAD","OPTIONS"];s=u&&u.includes(s.toUpperCase())?s.toUpperCase():"GET",n.body=o||n.body;let r=process.hrtime(),t,d=n.maxRedirects||10,a=n.debug!==void 0?n.debug:!1,h=null;if(n.saveTo){let l=w.basename(n.saveTo);if(this.checkISPermission(n.saveTo.length?w.dirname(n.saveTo):w.resolve(process.cwd()))){let p=l.length<n.saveTo.length?w.dirname(n.saveTo):w.join(process.cwd(),"download");g.existsSync(p)||g.mkdirSync(p,{recursive:!0}),h=w.join(p,l)}else if(n.debug)throw this.Error(void 0,`Permission denied to save to ${n.saveTo}, returning the response data instead`)}let i=0,c=e instanceof URL?e.href:e;for(;;){if(n.params&&typeof n.params=="object"){let p=new URLSearchParams(n.params).toString(),f=new URL(c);f.search=f.search?f.search+"&"+p:"?"+p,c=f.toString()}let l=this.prepareFetchOptions(n,c,s);try{let p=await fetch(c,{...l,method:s,redirect:"manual"});if(this.jar.setResponseCookies(p,c),[301,302,303,307,308].includes(p.status)&&!n.dontFollowRedirects){if(i>=d)throw await this.Error(p,`Max redirects (${d}) reached`,l);let f=p.headers.get("Location");if(!f)throw await this.Error(p,"Redirect location not found",l);c=new URL(f,c).toString(),i++,a&&console.log(`Redirecting to: ${c}`),p.status===303&&(s="GET",delete n.body);continue}if(p.status<200||p.status>308||!p.ok)throw h?await this.Error(p,`Failed to download: ${p.statusText}`,l):await this.Error(p,void 0,l);if(h&&p.body){let f=g.createWriteStream(h);await new Promise(async(J,N)=>{let B=p.body.getReader();for(;;){let{done:I,value:G}=await B.read();if(I)break;f.write(Buffer.from(G))}f.end(),f.on("finish",J),f.on("error",N)});let T=g.statSync(h).size,[A,F]=process.hrtime(r),D=A+F/1e9;t={fileName:h,totalTime:D,downloadSpeed:T/D,size:T}}return this.formatResponse(p,c,{...l,method:s},t)}catch(p){throw p}}}prepareFetchOptions(e,s,o,n){let r=["post","put","patch"].includes(o.toLowerCase()),t={},d=e.headers instanceof Headers?e.headers:new Headers(e.headers||{}),a=e.contentType||d.get("Content-Type")||(r?"application/json":void 0);if(e.headers?t.headers=d instanceof Headers?d:new Headers(d):t.headers=new Headers,d.has("Cookie")){let i=d.get("Cookie");this.setCookies(i,s),d.delete("Cookie"),t.headers.delete("Cookie")}e.cookies&&this.setCookies(e.cookies,s);let h;if(e.useAllCookies?h=this.jar.getAllCookies(s):h=this.jar.getCookiesSync(s),e.printHeaders&&console.log("Cookies:",h),h.length>0&&t.headers.set("Cookie",h.map(i=>`${i.key}=${i.value}`).join("; ")),e.body&&(t.body=e.body),e.timeout){let i=new AbortController;setTimeout(()=>i.abort(),e.timeout),t.signal=i.signal}if(e.isFormData||e.isJson)e.isFormData?(t.body=e.body,a="application/x-www-form-urlencoded",t.headers.set("Content-Type",a)):(t.body=e.body,a="application/json",t.headers.set("Content-Type",a));else if(e.json)t.body=JSON.stringify(e.json),a="application/json",t.headers.set("Content-Type",a);else if(e.form_params)t.body=new URLSearchParams(e.form_params).toString(),a="application/x-www-form-urlencoded",t.headers.set("Content-Type",a);else if(e.multipart){let i=new E.default;Object.entries(e.multipart).forEach(([c,l])=>{i.append(c,l)}),t.body=i}else if(e.requestType)switch(e.requestType){case"json":a="application/json",typeof t.body=="object"&&(t.body=JSON.stringify(t.body)),t.headers.set("Content-Type",a);break;case"form":a="application/x-www-form-urlencoded",typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString()),t.headers.set("Content-Type",a);break;case"formData":if(typeof t.body=="object"){let i=new E.default;Object.entries(t.body).forEach(([c,l])=>{i.append(c,l)}),t.body=i}break;case"text":a="text/plain",t.headers.set("Content-Type",a);break}else if(a){let i=a.toLowerCase();if(i.includes("json"))t.headers.set("Content-Type","application/json"),t.body&&typeof t.body=="object"&&(t.body=JSON.stringify(t.body));else if(i.includes("x-www-form-urlencoded"))t.headers.set("Content-Type","application/x-www-form-urlencoded"),t.body&&typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString());else if(i.includes("multipart")){if(t.body&&typeof t.body=="object"){let c=new E.default;Object.entries(t.body).forEach(([l,p])=>{c.append(l,p)}),t.body=c}}else i.includes("text/plain")&&t.headers.set("Content-Type","text/plain")}else a&&!e.withoutContentType&&t.headers.set("Content-Type",a);return e.withoutContentType&&t.headers.delete("Content-Type"),e.withoutBodyOnRedirect&&n&&(t.body=void 0),e.printHeaders&&console.log("Fetch headers:",t.headers),t}checkISPermission(e){try{return g.accessSync(e,g.constants.R_OK|g.constants.W_OK),!0}catch{return!1}}async getHeaders(e){let s=await this.fetch(e,{method:"HEAD"});return Object.fromEntries(s.headers.entries())}};var H=S(require("form-data")),j=class extends k{constructor(){super()}setCookies(e,s,o){o&&(this.jar=new b),Array.isArray(e)?this.jar.fromArray(e,o):typeof e=="string"&&(s&&e.includes("; ")?this.jar.parseCookieString(e,s,o):e.includes(` | ||
`)&&e.split(" ").length>3&&this.jar.parseNetscapeCookies(e,o))}getCookies(){return{array:this.jar.toArray(),string:this.jar.toString(),netscape:this.jar.toNetscapeFormat()}}async request(e,s,o=void 0,n={}){let u=["GET","POST","PUT","PATCH","DELETE","HEAD","OPTIONS"];s=u&&u.includes(s.toUpperCase())?s.toUpperCase():"GET";let r,t=n.maxRedirects||10,d=n.debug!==void 0?n.debug:!1,a=null;if(n.saveTo)throw this.Error(void 0,"You can only use this feature in Node.js, not in Edge or Browser.");let h=0,i=e instanceof URL?e.href:e;for(;;){if(n.params&&typeof n.params=="object"){let l=new URLSearchParams(n.params),p=new URL(i);l.forEach((f,T)=>{p.searchParams.set(T,f)}),i=p.href,delete n.params}let c=this.prepareFetchOptions(n,i,s,h>0);try{let l=await fetch(i,{...c,method:s,redirect:"manual"});if(this.jar.setResponseCookies(l,i),[301,302,303,307,308].includes(l.status)&&!n.dontFollowRedirects){if(h>=t)throw await this.Error(l,`Max redirects (${t}) reached`,c);let p=l.headers.get("Location");if(!p)throw await this.Error(l,"Redirect location not found",c);i=new URL(p,i).toString(),h++,d&&console.log(`Redirecting to: ${i}`),l.status===303&&(s="GET",delete n.body);continue}if(l.status<200||l.status>308||!l.ok)throw a?await this.Error(l,`Failed to download: ${l.statusText}`,c):await this.Error(l,void 0,c);return this.formatResponse(l,i,{...c,method:s},r)}catch(l){throw l}}}prepareFetchOptions(e,s,o,n){let r=["post","put","patch"].includes(o.toLowerCase()),t={},d=e.headers instanceof Headers?e.headers:new Headers(e.headers||{}),a=e.contentType||d.get("Content-Type")||(r?"application/json":void 0);if(e.headers?t.headers=d instanceof Headers?d:new Headers(d):t.headers=new Headers,d.has("Cookie")){let i=d.get("Cookie");this.setCookies(i,s),d.delete("Cookie"),t.headers.delete("Cookie")}e.cookies&&this.setCookies(e.cookies,s);let h;if(e.useAllCookies?h=this.jar.getAllCookies(s):h=this.jar.getCookiesSync(s),e.printHeaders&&console.log("Cookies:",h),h.length>0&&t.headers.set("Cookie",h.map(i=>`${i.key}=${i.value}`).join("; ")),e.body&&(t.body=e.body),e.timeout){let i=new AbortController;setTimeout(()=>i.abort(),e.timeout),t.signal=i.signal}if(e.isFormData||e.isJson)e.isFormData?(t.body=e.body,a="application/x-www-form-urlencoded",t.headers.set("Content-Type",a)):(t.body=e.body,a="application/json",t.headers.set("Content-Type",a));else if(e.json)t.body=JSON.stringify(e.json),a="application/json",t.headers.set("Content-Type",a);else if(e.form_params)t.body=new URLSearchParams(e.form_params).toString(),a="application/x-www-form-urlencoded",t.headers.set("Content-Type",a);else if(e.multipart){let i=new H.default;Object.entries(e.multipart).forEach(([c,l])=>{i.append(c,l)}),t.body=i}else if(e.requestType)switch(e.requestType){case"json":a="application/json",typeof t.body=="object"&&(t.body=JSON.stringify(t.body)),t.headers.set("Content-Type",a);break;case"form":a="application/x-www-form-urlencoded",typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString()),t.headers.set("Content-Type",a);break;case"formData":if(typeof t.body=="object"){let i=new H.default;Object.entries(t.body).forEach(([c,l])=>{i.append(c,l)}),t.body=i}break;case"text":a="text/plain",t.headers.set("Content-Type",a);break}else if(a){let i=a.toLowerCase();if(i.includes("json"))t.headers.set("Content-Type","application/json"),t.body&&typeof t.body=="object"&&(t.body=JSON.stringify(t.body));else if(i.includes("x-www-form-urlencoded"))t.headers.set("Content-Type","application/x-www-form-urlencoded"),t.body&&typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString());else if(i.includes("multipart")){if(t.body&&typeof t.body=="object"){let c=new H.default;Object.entries(t.body).forEach(([l,p])=>{c.append(l,p)}),t.body=c}}else i.includes("text/plain")&&t.headers.set("Content-Type","text/plain")}else a&&!e.withoutContentType&&t.headers.set("Content-Type",a);return e.withoutContentType&&t.headers.delete("Content-Type"),e.withoutBodyOnRedirect&&n&&(t.body=void 0),e.printHeaders&&console.log("Fetch headers:",t.headers),t}async getHeaders(e){let s=await this.fetch(e,{method:"HEAD"});return Object.fromEntries(s.headers.entries())}};var L=S(require("form-data")),O=class extends k{constructor(){super()}setCookies(e,s,o){o&&(this.jar=new b),Array.isArray(e)?this.jar.fromArray(e,o):typeof e=="string"&&(s&&e.includes("; ")?this.jar.parseCookieString(e,s,o):e.includes(` | ||
`)&&e.split(" ").length>3&&this.jar.parseNetscapeCookies(e,o))}getCookies(){return{array:this.jar.toArray(),string:this.jar.toString(),netscape:this.jar.toNetscapeFormat()}}async request(e,s,o=void 0,n={}){let u=["GET","POST","PUT","PATCH","DELETE","HEAD","OPTIONS"];s=u&&u.includes(s.toUpperCase())?s.toUpperCase():"GET";let r,t=n.maxRedirects||10,d=n.debug!==void 0?n.debug:!1,a=null;if(n.saveTo)throw this.Error(void 0,"You can only use this feature in Node.js, not in Edge or Browser.");let h=0,i=e instanceof URL?e.href:e;for(;;){if(n.params&&typeof n.params=="object"){let l=new URLSearchParams(n.params),p=new URL(i);l.forEach((f,T)=>{p.searchParams.set(T,f)}),i=p.href,delete n.params}let c=this.prepareFetchOptions(n,i,s,h>0);try{let l=await fetch(i,{...c,method:s,redirect:"manual"});if(this.jar.setResponseCookies(l,i),[301,302,303,307,308].includes(l.status)&&!n.dontFollowRedirects){if(h>=t)throw await this.Error(l,`Max redirects (${t}) reached`,c);let p=l.headers.get("Location");if(!p)throw await this.Error(l,"Redirect location not found",c);i=new URL(p,i).toString(),h++,d&&console.log(`Redirecting to: ${i}`),l.status===303&&(s="GET",delete n.body);continue}if(l.status<200||l.status>308||!l.ok)throw a?await this.Error(l,`Failed to download: ${l.statusText}`,c):await this.Error(l,void 0,c);return this.formatResponse(l,i,{...c,method:s},r)}catch(l){throw l}}}prepareFetchOptions(e,s,o,n){let r=["post","put","patch"].includes(o.toLowerCase()),t={},d=e.headers instanceof Headers?e.headers:new Headers(e.headers||{}),a=e.contentType||d.get("Content-Type")||(r?"application/json":void 0);if(e.headers?t.headers=d instanceof Headers?d:new Headers(d):t.headers=new Headers,d.has("Cookie")){let i=d.get("Cookie");this.setCookies(i,s),d.delete("Cookie"),t.headers.delete("Cookie")}e.cookies&&this.setCookies(e.cookies,s);let h;if(e.useAllCookies?h=this.jar.getAllCookies(s):h=this.jar.getCookiesSync(s),e.printHeaders&&console.log("Cookies:",h),h.length>0&&t.headers.set("Cookie",h.map(i=>`${i.key}=${i.value}`).join("; ")),e.body&&(t.body=e.body),e.timeout){let i=new AbortController;setTimeout(()=>i.abort(),e.timeout),t.signal=i.signal}if(e.isFormData||e.isJson)e.isFormData?(t.body=e.body,a="application/x-www-form-urlencoded",t.headers.set("Content-Type",a)):(t.body=e.body,a="application/json",t.headers.set("Content-Type",a));else if(e.json)t.body=JSON.stringify(e.json),a="application/json",t.headers.set("Content-Type",a);else if(e.form_params)t.body=new URLSearchParams(e.form_params).toString(),a="application/x-www-form-urlencoded",t.headers.set("Content-Type",a);else if(e.multipart){let i=new L.default;Object.entries(e.multipart).forEach(([c,l])=>{i.append(c,l)}),t.body=i}else if(e.requestType)switch(e.requestType){case"json":a="application/json",typeof t.body=="object"&&(t.body=JSON.stringify(t.body)),t.headers.set("Content-Type",a);break;case"form":a="application/x-www-form-urlencoded",typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString()),t.headers.set("Content-Type",a);break;case"formData":if(typeof t.body=="object"){let i=new L.default;Object.entries(t.body).forEach(([c,l])=>{i.append(c,l)}),t.body=i}break;case"text":a="text/plain",t.headers.set("Content-Type",a);break}else if(a){let i=a.toLowerCase();if(i.includes("json"))t.headers.set("Content-Type","application/json"),t.body&&typeof t.body=="object"&&(t.body=JSON.stringify(t.body));else if(i.includes("x-www-form-urlencoded"))t.headers.set("Content-Type","application/x-www-form-urlencoded"),t.body&&typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString());else if(i.includes("multipart")){if(t.body&&typeof t.body=="object"){let c=new L.default;Object.entries(t.body).forEach(([l,p])=>{c.append(l,p)}),t.body=c}}else i.includes("text/plain")&&t.headers.set("Content-Type","text/plain")}else a&&!e.withoutContentType&&t.headers.set("Content-Type",a);return e.withoutContentType&&t.headers.delete("Content-Type"),e.withoutBodyOnRedirect&&n&&(t.body=void 0),e.printHeaders&&console.log("Fetch headers:",t.headers),t}async getHeaders(e){let s=await this.fetch(e,{method:"HEAD"});return Object.fromEntries(s.headers.entries())}};function X(){return typeof process<"u"&&process.versions&&process.versions.node?"node":typeof window<"u"&&typeof window.document<"u"?"browser":"edge"}function Z(){switch(X()){case"node":return new x;case"browser":return new O;case"edge":return new j;default:throw new Error("Unsupported environment")}}var ee=Z(),te=ee;0&&(module.exports={UniqhttBrowser,UniqhttEdge,UniqhttNode,YuniqCookies}); |
{ | ||
"name": "uniqhtt", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Uniqhtt is a versatile Node.js library designed for handling HTTP requests and responses with advanced cookie management, HTML parsing, and CSS processing capabilities. It leverages powerful tools like jsdom, fetch-cookie, and postcss to provide a robust solution for web scraping, data extraction, and web automation tasks.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
67137
877