Comparing version 1.0.1 to 1.0.2
{ | ||
"cSpell.words": [ | ||
"dont", | ||
"Turbify", | ||
"Uniqhtt" | ||
] | ||
} |
@@ -1,5 +0,5 @@ | ||
import { DownloadOptions, DownloadResponse, HttpConfig, fetchApiResponse } from '../types/types'; | ||
import { DownloadOptions, DownloadResponse, HttpConfig, UniqhttResponse } from '../types/types'; | ||
import { YuniqCookies } from './YuniqCookies'; | ||
declare class CustomError extends Error { | ||
response: fetchApiResponse; | ||
declare class UinqhttError extends Error { | ||
response: UniqhttResponse; | ||
constructor(message: string, response: Response, data: any, headers: { | ||
@@ -21,14 +21,14 @@ [key: string]: string; | ||
get<T = any>(input: string | URL, config: DownloadOptions): Promise<DownloadResponse<null>>; | ||
get<T = any>(input: string | URL, config: HttpConfig): Promise<fetchApiResponse<T>>; | ||
get<T = any>(input: string | URL): Promise<fetchApiResponse<T>>; | ||
get<T = any>(input: string | URL, config: HttpConfig): Promise<UniqhttResponse<T>>; | ||
get<T = any>(input: string | URL): Promise<UniqhttResponse<T>>; | ||
post<T = any>(input: string | URL, data: any, config: DownloadOptions): Promise<DownloadResponse<null>>; | ||
post<T = any>(input: string | URL, data: any, config: HttpConfig): Promise<fetchApiResponse<T>>; | ||
post<T = any>(input: string | URL, data: any): Promise<fetchApiResponse<T>>; | ||
post<T = any>(input: string | URL): Promise<fetchApiResponse<T>>; | ||
put<T = any>(input: string | URL, data?: any, config?: HttpConfig): Promise<fetchApiResponse<T>>; | ||
patch<T = any>(input: string | URL, data?: any, config?: HttpConfig): Promise<fetchApiResponse<T>>; | ||
delete<T = any>(input: string | URL, config?: HttpConfig): Promise<fetchApiResponse<T>>; | ||
head<T = any>(input: string | URL, config?: HttpConfig): Promise<fetchApiResponse<T>>; | ||
options<T = any>(input: string | URL, config?: HttpConfig): Promise<fetchApiResponse<T>>; | ||
protected Error(response: Response, message?: string): Promise<CustomError>; | ||
post<T = any>(input: string | URL, data: any, config: HttpConfig): Promise<UniqhttResponse<T>>; | ||
post<T = any>(input: string | URL, data: any): Promise<UniqhttResponse<T>>; | ||
post<T = any>(input: string | URL): Promise<UniqhttResponse<T>>; | ||
put<T = any>(input: string | URL, data?: any, config?: HttpConfig): Promise<UniqhttResponse<T>>; | ||
patch<T = any>(input: string | URL, data?: any, config?: HttpConfig): Promise<UniqhttResponse<T>>; | ||
delete<T = any>(input: string | URL, config?: HttpConfig): Promise<UniqhttResponse<T>>; | ||
head<T = any>(input: string | URL, config?: HttpConfig): Promise<UniqhttResponse<T>>; | ||
options<T = any>(input: string | URL, config?: HttpConfig): Promise<UniqhttResponse<T>>; | ||
protected Error(response: Response, message?: string): Promise<UinqhttError>; | ||
protected formatResponse<T = any>(response: Response, finalUrl: string, downloadConfig?: { | ||
@@ -39,5 +39,5 @@ fileName: string; | ||
size: number; | ||
}): Promise<fetchApiResponse<T>>; | ||
}): Promise<UniqhttResponse<T>>; | ||
protected parseResponseBody<T = any>(response: Response, contentType: string | null): Promise<T>; | ||
} | ||
export {}; |
import { UniqhttAgentBase } from './UniqhttAgentBase'; | ||
import { Cookie, ToughCookie } from './YuniqCookies'; | ||
import { DownloadOptions, HttpConfig } from '../types/types'; | ||
import { DownloadOptions, HttpConfig, Serialized } from '../types/types'; | ||
export declare class UniqhttAgentBrowser extends UniqhttAgentBase { | ||
@@ -8,3 +8,3 @@ constructor(); | ||
getCookies(): { | ||
array: Cookie[]; | ||
array: Serialized[]; | ||
string: string; | ||
@@ -11,0 +11,0 @@ netscape: string; |
import { UniqhttAgentBase } from './UniqhttAgentBase'; | ||
import { Cookie, ToughCookie } from './YuniqCookies'; | ||
import { DownloadOptions, HttpConfig } from '../types/types'; | ||
import { ToughCookie } from './YuniqCookies'; | ||
import { DownloadOptions, HttpConfig, Serialized } from '../types/types'; | ||
export declare class UniqhttAgentEdge extends UniqhttAgentBase { | ||
constructor(); | ||
setCookies(cookies: any[] | Cookie[] | string | ToughCookie[], url?: string, startNew?: boolean): void; | ||
setCookies(cookies: any[] | Serialized[] | string | ToughCookie[], url?: string, startNew?: boolean): void; | ||
getCookies(): { | ||
array: Cookie[]; | ||
array: Serialized[]; | ||
string: string; | ||
@@ -10,0 +10,0 @@ netscape: string; |
import { UniqhttAgentBase } from './UniqhttAgentBase'; | ||
import { Cookie, ToughCookie } from './YuniqCookies'; | ||
import { DownloadOptions, HttpConfig } from '../types/types'; | ||
import { DownloadOptions, HttpConfig, Serialized } from '../types/types'; | ||
export declare class UniqhttAgent extends UniqhttAgentBase { | ||
@@ -8,7 +8,9 @@ constructor(); | ||
getCookies(): { | ||
array: Cookie[]; | ||
array: Serialized[]; | ||
string: string; | ||
netscape: string; | ||
}; | ||
protected request(input: string | URL, method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS', data?: any, config?: HttpConfig | DownloadOptions): Promise<any>; | ||
protected request(input: string | URL, method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS', data?: any, config?: (HttpConfig | DownloadOptions) & { | ||
body?: any; | ||
}): Promise<any>; | ||
private prepareFetchOptions; | ||
@@ -15,0 +17,0 @@ private checkISPermission; |
import { Cookie as ToughCookie, CookieJar, Store } from 'tough-cookie'; | ||
import { Serialized } from '../types/types'; | ||
type GetCookiesOptions = CookieJar.GetCookiesOptions; | ||
@@ -7,3 +8,2 @@ export declare class Cookie extends ToughCookie { | ||
constructor(options: ToughCookie.Properties); | ||
touch(): void; | ||
toSetCookieString(): string; | ||
@@ -26,3 +26,3 @@ toString(): string; | ||
parseCookieString(cookieString: string, url: string, isNew?: boolean): void; | ||
toArray(): Cookie[]; | ||
toArray(): Serialized[]; | ||
fromArray(cookies: (Cookie | any)[], isNew?: boolean): void; | ||
@@ -29,0 +29,0 @@ private parseDate; |
@@ -1,6 +0,6 @@ | ||
"use strict";var J=Object.create;var v=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var M=Object.getPrototypeOf,B=Object.prototype.hasOwnProperty;var z=(h,e)=>{for(var s in e)v(h,s,{get:e[s],enumerable:!0})},H=(h,e,s,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of G(e))!B.call(h,i)&&i!==s&&v(h,i,{get:()=>e[i],enumerable:!(n=_(e,i))||n.enumerable});return h};var R=(h,e,s)=>(s=h!=null?J(M(h)):{},H(e||!h||!h.__esModule?v(s,"default",{value:h,enumerable:!0}):s,h)),W=h=>H(v({},"__esModule",{value:!0}),h);var X={};z(X,{UniqhttBrowser:()=>j,UniqhttEdge:()=>S,UniqhttNode:()=>x,YuniqCookies:()=>g,default:()=>V});module.exports=W(X);var w=R(require("md5")),y=require("tough-cookie"),C=class extends y.Cookie{constructor(e){super(e),this.plainDomain=this.domain?this.domain.startsWith(".")?this.domain.substring(1):this.domain:null,this.hash=this.domain?(0,w.default)(`${this.domain.toString()}_${this.key}`):null}touch(){this.lastAccessed=new Date}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",n=this.expires&&typeof this.expires!="string"?Math.round(this.expires.getTime()/1e3):0;return`${e} TRUE ${this.path} ${s} ${n} ${this.key} ${this.value}`}},g=class extends y.CookieJar{constructor(e,s){super(e,s)}setResponseCookies(e,s){var n;if(e instanceof Response)if((n=e.headers)!=null&&n.getSetCookie())for(let i of e.headers.getSetCookie()){let p=C.parse(i.trim());p&&this.setCookieSync(p,s)}else{let i=e.headers.get("set-cookie");i&&i.split(",").forEach(p=>{let t=C.parse(p);t&&this.setCookieSync(t,s)})}else Array.isArray(e)?e.forEach(i=>this.setCookieSync(i,s)):typeof e=="string"&&e.includes("=")&&e.includes(",")&&e.split(",").forEach(i=>{let p=C.parse(i);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(` | ||
`)}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 n=e.split(` | ||
`),i=this.allCookies().filter(o=>o.hash).map(o=>o.hash),p=n.map(o=>{let[a,d,r,u,c,l,f]=o.split(" "),T=`http${u==="TRUE"?"s":""}://${a.startsWith(".")?a.substring(1):a}${r}`,P=(0,w.default)(`${T}_${l}`);return s?{url:T,cookie:new y.Cookie({domain:a,path:r,secure:u==="TRUE",expires:c?new Date(parseInt(c)*1e3):void 0,key:l,value:f})}:i.includes(P)?null:{url:T,cookie:new y.Cookie({domain:a,path:r,secure:u==="TRUE",expires:c?new Date(parseInt(c)*1e3):void 0,key:l,value:f})}}).filter(o=>o!==null),t={};for(let o of p)t[o.url]||(t[o.url]=[]),t[o.url].push(o.cookie);for(let[o,a]of Object.entries(t))this.setResponseCookies(a,o)}parseCookieString(e,s,n=!1){let i=new URL(s),p=this.allCookies().filter(d=>d.hash).map(d=>d.hash),o=e.split("; ").map(d=>{let[r,...u]=d.split("="),c=u.join("="),l=`http${i.protocol.includes("s")?"s":""}://${i.hostname.startsWith(".")?i.hostname.substring(1):i.hostname}/`,f=(0,w.default)(`${i.hostname}_${r}`);return n?{url:l,cookie:new y.Cookie({key:r,value:c,domain:i.hostname,path:"/"})}:p.includes(f)?null:{url:l,cookie:new y.Cookie({key:r,value:c,domain:i.hostname,path:"/"})}}).filter(d=>d!==null),a={};for(let d of o)a[d.url]||(a[d.url]=[]),a[d.url].push(d.cookie);for(let[d,r]of Object.entries(a))this.setResponseCookies(r,d)}toArray(){return[...this.allCookies()]}fromArray(e,s=!1){let n=this.allCookies().filter(t=>t.hash).map(t=>t.hash),i=e.map(t=>{if(t instanceof y.Cookie){if(t.domain){let o=`http${t.secure?"s":""}://${t.domain.startsWith(".")?t.domain.substring(1):t.domain}${t.path}`,a=(0,w.default)(`${o}_${t.key}`);return s?{url:o,cookie:t}:n.includes(a)?null:{url:o,cookie:t}}return null}else{if(t instanceof C){if(t.domain){let o=`http${t.secure?"s":""}://${t.domain.startsWith(".")?t.domain.substring(1):t.domain}${t.path}`,a=(0,w.default)(`${o}_${t.key}`);return s?{url:o,cookie:new y.Cookie({domain:t.domain,path:t.path||"/",secure:t.secure,expires:this.parseDate(t.expires),key:t.key,value:t.value})}:n.includes(a)?null:{url:o,cookie:new y.Cookie({domain:t.domain,path:t.path||"/",secure:t.secure,expires:this.parseDate(t.expires),key:t.key,value:t.value})}}return null}else{let o=this.parseJson(t);if(o&&o.domain&&o.key){let a=`http${o.secure?"s":""}://${o.domain.startsWith(".")?o.domain.substring(1):o.domain}${o.path}`,d=(0,w.default)(`${a}_${o.key}`);return s?{url:a,cookie:new y.Cookie({domain:o.domain,path:o.path||"/",secure:o.secure,expires:this.parseDate(o.expires),key:o.key,value:o.value})}:n.includes(d)?null:{url:a,cookie:new y.Cookie({domain:o.domain,path:o.path||"/",secure:o.secure,expires:this.parseDate(o.expires),key:o.key,value:o.value})}}}return null}}).filter(t=>t!==null),p={};for(let t of i)p[t.url]||(p[t.url]=[]),p[t.url].push(t.cookie);for(let[t,o]of Object.entries(p))this.setResponseCookies(o,t)}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 n=parseInt(s);return n.toString().length===13?n:n*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(n=>n.key===e&&n.domain===s)}getCookiesSync(e,s){return super.getCookiesSync(e,s).map(n=>new C(n.toJSON()))}async getCookies(e,s,n){if(typeof s=="function")super.getCookies(e,(i,p)=>{s(i,p.map(t=>new C(t.toJSON())))});else{let p=(await super.getCookies(e,s)).map(t=>new C(t.toJSON()));if(n)n(null,p);else return p}}getAllCookies(e){let s=this.getCookiesSync(e);try{let n=this.toJSON().cookies,i=[...s];return n.forEach(p=>{i.some(t=>t.key===p.key)||i.push(new C(p))}),i}catch(n){console.log(n),console.error(`Error parsing cookies: ${n.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 A=class h extends Error{constructor(s,n,i,p){super(s);this.response={};this.response={data:i,status:n.status,statusText:n.statusText,finalUrl:n.url,cookies:void 0,headers:p,contentType:n.headers.get("Content-Type")},Object.setPrototypeOf(this,h.prototype)}},b=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,n){return this.request(e,"POST",s,n)}async put(e,s,n){return this.request(e,"PUT",s,n)}async patch(e,s,n){return this.request(e,"PATCH",s,n)}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)}async Error(e,s){let n=e.headers.get("Content-Type"),i=await this.parseResponseBody(e,n),p={};return e.headers.forEach((t,o)=>{o.toLowerCase()!=="set-cookie"&&o.toLowerCase()!=="set-cookies"&&(p[o]=t)}),new A(s??e.statusText??"Unknown error",e,i,p)}async formatResponse(e,s,n){let i=e.headers.get("Content-Type"),p=n?null:await this.parseResponseBody(e,i),t={};return e.headers.forEach((o,a)=>{a.toLowerCase()!=="set-cookie"&&a.toLowerCase()!=="set-cookies"&&(t[a]=o)}),{data:p,status:e.status,statusText:e.statusText,headers:t,finalUrl:s,cookies:{array:this.jar.toArray(),string:this.jar.toCookieString(),netscape:this.jar.toNetscapeFormat()},contentType:i,...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(i=>s.includes(i))?await e.text():await e.arrayBuffer()}};var m=R(require("fs")),k=R(require("path")),O=R(require("form-data")),x=class extends b{constructor(){super()}setCookies(e,s,n){n&&(this.jar=new g),Array.isArray(e)?this.jar.fromArray(e,n):typeof e=="string"&&(s&&e.includes("; ")?this.jar.parseCookieString(e,s,n):e.includes(` | ||
`)&&e.split(" ").length>3&&this.jar.parseNetscapeCookies(e,n))}getCookies(){return{array:this.jar.toArray(),string:this.jar.toString(),netscape:this.jar.toNetscapeFormat()}}async request(e,s,n=void 0,i={}){let p=process.hrtime(),t,o=i.maxRedirects||10,a=i.debug!==void 0?i.debug:!1,d=null;if(i.saveTo){let c=k.basename(i.saveTo);if(this.checkISPermission(i.saveTo.length?k.dirname(i.saveTo):k.resolve(process.cwd()))){let l=c.length<i.saveTo.length?k.dirname(i.saveTo):k.join(process.cwd(),"download");m.existsSync(l)||m.mkdirSync(l,{recursive:!0}),d=k.join(l,c)}else i.debug&&console.log(`Permission denied to save to ${i.saveTo}, returning the response data instead`)}let r=0,u=e instanceof URL?e.href:e;for(;;){if(i.params&&typeof i.params=="object"){let l=new URLSearchParams(i.params).toString(),f=new URL(u);f.search=f.search?f.search+"&"+l:"?"+l,u=f.toString()}let c=this.prepareFetchOptions(i,u,s);try{let l=await fetch(u,{...c,method:s,redirect:"manual"});if(this.jar.setResponseCookies(l,u),[301,302,303,307,308].includes(l.status)&&!i.dontFollowRedirects){if(r>=o)throw await this.Error(l,`Max redirects (${o}) reached`);let f=l.headers.get("Location");if(!f)throw await this.Error(l,"Redirect location not found");if(l.status<200||l.status>308)throw d?await this.Error(l,`Failed to download: ${l.statusText}`):await this.Error(l);u=new URL(f,u).toString(),r++,a&&console.log(`Redirecting to: ${u}`),l.status===303&&(s="GET",delete i.body);continue}if(d&&l.body){let f=m.createWriteStream(d);await new Promise(async(q,D)=>{let N=l.body.getReader();for(;;){let{done:F,value:I}=await N.read();if(F)break;f.write(Buffer.from(I))}f.end(),f.on("finish",q),f.on("error",D)});let T=m.statSync(d).size,[P,$]=process.hrtime(p),L=P+$/1e9;t={fileName:d,totalTime:L,downloadSpeed:T/L,size:T}}return this.formatResponse(l,u,t)}catch(l){throw l}}}prepareFetchOptions(e,s,n){let p=["post","put","patch"].includes(n.toLowerCase()),t={},o=e.headers||{},a=e.contentType||o["Content-Type"]||(p?"application/json":void 0);if(e.headers?t.headers=o instanceof Headers?o:new Headers(o):t.headers=new Headers,o.Cookie||o.cookie){let r=o.Cookie||o.cookie;this.setCookies(r,s),delete o.Cookie,delete o.cookie,t.headers.delete("Cookie")}e.cookies&&this.setCookies(e.cookies,s);let d;if(e.useAllCookies?d=this.jar.getAllCookies(s):d=this.jar.getCookiesSync(s),e.printHeaders&&console.log("Cookies:",d),d.length>0&&t.headers.set("Cookie",d.map(r=>`${r.key}=${r.value}`).join("; ")),e.body&&(t.body=e.body),e.timeout){let r=new AbortController;setTimeout(()=>r.abort(),e.timeout),t.signal=r.signal}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 r=new O.default;Object.entries(e.multipart).forEach(([u,c])=>{r.append(u,c)}),t.body=r}else if(e.requestType)switch(e.requestType){case"json":a="application/json",typeof t.body=="object"&&(t.body=JSON.stringify(t.body));break;case"form":a="application/x-www-form-urlencoded",typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString());break;case"form-data":case"formData":if(typeof t.body=="object"){let r=new O.default;Object.entries(t.body).forEach(([u,c])=>{r.append(u,c)}),t.body=r}break;case"text":a="text/plain";break}else if(a){let r=a.toLowerCase();if(r.includes("json"))t.headers.set("Content-Type","application/json"),t.body&&typeof t.body=="object"&&(t.body=JSON.stringify(t.body));else if(r.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(r.includes("multipart")){if(t.body&&typeof t.body=="object"){let u=new O.default;Object.entries(t.body).forEach(([c,l])=>{u.append(c,l)}),t.body=u}}else r.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.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 E=R(require("form-data")),S=class extends b{constructor(){super()}setCookies(e,s,n){n&&(this.jar=new g),Array.isArray(e)?this.jar.fromArray(e,n):typeof e=="string"&&(s&&e.includes("; ")?this.jar.parseCookieString(e,s,n):e.includes(` | ||
`)&&e.split(" ").length>3&&this.jar.parseNetscapeCookies(e,n))}getCookies(){return{array:this.jar.toArray(),string:this.jar.toString(),netscape:this.jar.toNetscapeFormat()}}async request(e,s,n=void 0,i={}){let p,t=i.maxRedirects||10,o=i.debug!==void 0?i.debug:!1,a=null;i.saveTo&&console.log("You can only use this feature in Node.js, not in Edge or Browser.");let d=0,r=e instanceof URL?e.href:e;for(;;){if(i.params&&typeof i.params=="object"){let c=new URLSearchParams(i.params).toString(),l=new URL(r);l.search=l.search?l.search+"&"+c:"?"+c,r=l.toString()}let u=this.prepareFetchOptions(i,r,s);try{let c=await fetch(r,{...u,method:s,redirect:"manual"});if(this.jar.setResponseCookies(c,r),[301,302,303,307,308].includes(c.status)&&!i.dontFollowRedirects){if(d>=t)throw await this.Error(c,`Max redirects (${t}) reached`);let l=c.headers.get("Location");if(!l)throw await this.Error(c,"Redirect location not found");if(c.status<200||c.status>308)throw a?await this.Error(c,`Failed to download: ${c.statusText}`):await this.Error(c);r=new URL(l,r).toString(),d++,o&&console.log(`Redirecting to: ${r}`),c.status===303&&(s="GET",delete i.body);continue}return this.formatResponse(c,r,p)}catch(c){throw c}}}prepareFetchOptions(e,s,n){let p=["post","put","patch"].includes(n.toLowerCase()),t={},o=e.headers||{},a=e.contentType||o["Content-Type"]||(p?"application/json":void 0);if(e.headers?t.headers=o instanceof Headers?o:new Headers(o):t.headers=new Headers,o.Cookie||o.cookie){let r=o.Cookie||o.cookie;this.setCookies(r,s),delete o.Cookie,delete o.cookie,t.headers.delete("Cookie")}e.cookies&&this.setCookies(e.cookies,s);let d;if(e.useAllCookies?d=this.jar.getAllCookies(s):d=this.jar.getCookiesSync(s),e.printHeaders&&console.log("Cookies:",d),d.length>0&&t.headers.set("Cookie",d.map(r=>`${r.key}=${r.value}`).join("; ")),e.body&&(t.body=e.body),e.timeout){let r=new AbortController;setTimeout(()=>r.abort(),e.timeout),t.signal=r.signal}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 r=new E.default;Object.entries(e.multipart).forEach(([u,c])=>{r.append(u,c)}),t.body=r}else if(e.requestType)switch(e.requestType){case"json":a="application/json",typeof t.body=="object"&&(t.body=JSON.stringify(t.body));break;case"form":a="application/x-www-form-urlencoded",typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString());break;case"form-data":case"formData":if(typeof t.body=="object"){let r=new E.default;Object.entries(t.body).forEach(([u,c])=>{r.append(u,c)}),t.body=r}break;case"text":a="text/plain";break}else if(a){let r=a.toLowerCase();if(r.includes("json"))t.headers.set("Content-Type","application/json"),t.body&&typeof t.body=="object"&&(t.body=JSON.stringify(t.body));else if(r.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(r.includes("multipart")){if(t.body&&typeof t.body=="object"){let u=new E.default;Object.entries(t.body).forEach(([c,l])=>{u.append(c,l)}),t.body=u}}else r.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.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 U=R(require("form-data")),j=class extends b{constructor(){super()}setCookies(e,s,n){n&&(this.jar=new g),Array.isArray(e)?this.jar.fromArray(e,n):typeof e=="string"&&(s&&e.includes("; ")?this.jar.parseCookieString(e,s,n):e.includes(` | ||
`)&&e.split(" ").length>3&&this.jar.parseNetscapeCookies(e,n))}getCookies(){return{array:this.jar.toArray(),string:this.jar.toString(),netscape:this.jar.toNetscapeFormat()}}async request(e,s,n=void 0,i={}){let p,t=i.maxRedirects||10,o=i.debug!==void 0?i.debug:!1,a=null;i.saveTo&&console.log("You can only use this feature in Node.js, not in Edge or Browser.");let d=0,r=e instanceof URL?e.href:e;for(;;){if(i.params&&typeof i.params=="object"){let c=new URLSearchParams(i.params).toString(),l=new URL(r);l.search=l.search?l.search+"&"+c:"?"+c,r=l.toString()}let u=this.prepareFetchOptions(i,r,s);try{let c=await fetch(r,{...u,method:s,redirect:"manual"});if(this.jar.setResponseCookies(c,r),[301,302,303,307,308].includes(c.status)&&!i.dontFollowRedirects){if(d>=t)throw await this.Error(c,`Max redirects (${t}) reached`);let l=c.headers.get("Location");if(!l)throw await this.Error(c,"Redirect location not found");if(c.status<200||c.status>308)throw a?await this.Error(c,`Failed to download: ${c.statusText}`):await this.Error(c);r=new URL(l,r).toString(),d++,o&&console.log(`Redirecting to: ${r}`),c.status===303&&(s="GET",delete i.body);continue}return this.formatResponse(c,r,p)}catch(c){throw c}}}prepareFetchOptions(e,s,n){let p=["post","put","patch"].includes(n.toLowerCase()),t={},o=e.headers||{},a=e.contentType||o["Content-Type"]||(p?"application/json":void 0);if(e.headers?t.headers=o instanceof Headers?o:new Headers(o):t.headers=new Headers,o.Cookie||o.cookie){let r=o.Cookie||o.cookie;this.setCookies(r,s),delete o.Cookie,delete o.cookie,t.headers.delete("Cookie")}e.cookies&&this.setCookies(e.cookies,s);let d;if(e.useAllCookies?d=this.jar.getAllCookies(s):d=this.jar.getCookiesSync(s),e.printHeaders&&console.log("Cookies:",d),d.length>0&&t.headers.set("Cookie",d.map(r=>`${r.key}=${r.value}`).join("; ")),e.body&&(t.body=e.body),e.timeout){let r=new AbortController;setTimeout(()=>r.abort(),e.timeout),t.signal=r.signal}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 r=new U.default;Object.entries(e.multipart).forEach(([u,c])=>{r.append(u,c)}),t.body=r}else if(e.requestType)switch(e.requestType){case"json":a="application/json",typeof t.body=="object"&&(t.body=JSON.stringify(t.body));break;case"form":a="application/x-www-form-urlencoded",typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString());break;case"form-data":case"formData":if(typeof t.body=="object"){let r=new U.default;Object.entries(t.body).forEach(([u,c])=>{r.append(u,c)}),t.body=r}break;case"text":a="text/plain";break}else if(a){let r=a.toLowerCase();if(r.includes("json"))t.headers.set("Content-Type","application/json"),t.body&&typeof t.body=="object"&&(t.body=JSON.stringify(t.body));else if(r.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(r.includes("multipart")){if(t.body&&typeof t.body=="object"){let u=new U.default;Object.entries(t.body).forEach(([c,l])=>{u.append(c,l)}),t.body=u}}else r.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.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 Y(){return typeof process<"u"&&process.versions&&process.versions.node?"node":typeof window<"u"&&typeof window.document<"u"?"browser":"edge"}function K(){switch(Y()){case"node":return new x;case"browser":return new j;case"edge":return new S;default:throw new Error("Unsupported environment")}}var Q=K(),V=Q;0&&(module.exports={UniqhttBrowser,UniqhttEdge,UniqhttNode,YuniqCookies}); | ||
"use strict";var z=Object.create;var U=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var B=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty;var Y=(u,e)=>{for(var s in e)U(u,s,{get:e[s],enumerable:!0})},A=(u,e,s,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of G(e))!W.call(u,n)&&n!==s&&U(u,n,{get:()=>e[n],enumerable:!(i=M(e,n))||i.enumerable});return u};var S=(u,e,s)=>(s=u!=null?z(B(u)):{},A(e||!u||!u.__esModule?U(s,"default",{value:u,enumerable:!0}):s,u)),K=u=>A(U({},"__esModule",{value:!0}),u);var te={};Y(te,{UniqhttBrowser:()=>j,UniqhttEdge:()=>x,UniqhttNode:()=>R,YuniqCookies:()=>g,default:()=>ee});module.exports=K(te);var w=S(require("md5")),y=require("tough-cookie");function L(u){if(u instanceof Date)return u;if(typeof u=="string"&&u!=="Infinity")return Q(u)?new Date(q(u)):new Date(u);if(typeof u=="number")return new Date(q(u))}function Q(u){return/^[0-9]+$/.test(u)}function q(u){let e=u.toString().trim();try{if(typeof u=="number")return e.length===13?u:u*1e3;let s=parseInt(e);return s.toString().length===13?s:s*1e3}catch{return 0}}var C=class extends y.Cookie{constructor(e){e={...e,expires:L(e.expires)??"Infinity",creation:L(e.creation),lastAccessed:L(e.lastAccessed)},super(e),this.plainDomain=this.domain?this.domain.startsWith(".")?this.domain.substring(1):this.domain:null,this.hash=this.domain?(0,w.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",i=this.expires&&typeof this.expires!="string"?Math.round(this.expires.getTime()/1e3):0;return`${e} TRUE ${this.path} ${s} ${i} ${this.key} ${this.value}`}},g=class extends y.CookieJar{constructor(e,s){super(e,s)}setResponseCookies(e,s){var i;if(e instanceof Response)if((i=e.headers)!=null&&i.getSetCookie())for(let n of e.headers.getSetCookie()){let p=C.parse(n.trim());p&&this.setCookieSync(p,s)}else{let n=e.headers.get("set-cookie");n&&n.split(",").forEach(p=>{let t=C.parse(p);t&&this.setCookieSync(t,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=C.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(` | ||
`)}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 i=e.split(` | ||
`),n=this.allCookies().filter(o=>o.hash).map(o=>o.hash),p=i.map(o=>{let[a,d,r,h,c,l,f]=o.split(" "),T=`http${h==="TRUE"?"s":""}://${a.startsWith(".")?a.substring(1):a}${r}`,P=(0,w.default)(`${T}_${l}`);return s?{url:T,cookie:new y.Cookie({domain:a,path:r,secure:h==="TRUE",expires:c?new Date(parseInt(c)*1e3):void 0,key:l,value:f})}:n.includes(P)?null:{url:T,cookie:new y.Cookie({domain:a,path:r,secure:h==="TRUE",expires:c?new Date(parseInt(c)*1e3):void 0,key:l,value:f})}}).filter(o=>o!==null),t={};for(let o of p)t[o.url]||(t[o.url]=[]),t[o.url].push(o.cookie);for(let[o,a]of Object.entries(t))this.setResponseCookies(a,o)}parseCookieString(e,s,i=!1){let n=new URL(s),p=this.allCookies().filter(d=>d.hash).map(d=>d.hash),o=e.split("; ").map(d=>{let[r,...h]=d.split("="),c=h.join("="),l=`http${n.protocol.includes("s")?"s":""}://${n.hostname.startsWith(".")?n.hostname.substring(1):n.hostname}/`,f=(0,w.default)(`${n.hostname}_${r}`);return i?{url:l,cookie:new y.Cookie({key:r,value:c,domain:n.hostname,path:"/"})}:p.includes(f)?null:{url:l,cookie:new y.Cookie({key:r,value:c,domain:n.hostname,path:"/"})}}).filter(d=>d!==null),a={};for(let d of o)a[d.url]||(a[d.url]=[]),a[d.url].push(d.cookie);for(let[d,r]of Object.entries(a))this.setResponseCookies(r,d)}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 i=this.allCookies().filter(t=>t.hash).map(t=>t.hash),n=e.map(t=>{if(t instanceof y.Cookie){if(t.domain){let o=`http${t.secure?"s":""}://${t.domain.startsWith(".")?t.domain.substring(1):t.domain}${t.path}`,a=(0,w.default)(`${o}_${t.key}`);return s?{url:o,cookie:t}:i.includes(a)?null:{url:o,cookie:t}}return null}else{if(t instanceof C){if(t.domain){let o=`http${t.secure?"s":""}://${t.domain.startsWith(".")?t.domain.substring(1):t.domain}${t.path}`,a=(0,w.default)(`${o}_${t.key}`);return s?{url:o,cookie:new y.Cookie({domain:t.domain,path:t.path||"/",secure:t.secure,expires:this.parseDate(t.expires),key:t.key,value:t.value})}:i.includes(a)?null:{url:o,cookie:new y.Cookie({domain:t.domain,path:t.path||"/",secure:t.secure,expires:this.parseDate(t.expires),key:t.key,value:t.value})}}return null}else{let o=this.parseJson(t);if(o&&o.domain&&o.key){let a=`http${o.secure?"s":""}://${o.domain.startsWith(".")?o.domain.substring(1):o.domain}${o.path}`,d=(0,w.default)(`${a}_${o.key}`);return s?{url:a,cookie:new y.Cookie({domain:o.domain,path:o.path||"/",secure:o.secure,expires:this.parseDate(o.expires),key:o.key,value:o.value})}:i.includes(d)?null:{url:a,cookie:new y.Cookie({domain:o.domain,path:o.path||"/",secure:o.secure,expires:this.parseDate(o.expires),key:o.key,value:o.value})}}}return null}}).filter(t=>t!==null),p={};for(let t of n)p[t.url]||(p[t.url]=[]),p[t.url].push(t.cookie);for(let[t,o]of Object.entries(p))this.setResponseCookies(o,t)}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 i=parseInt(s);return i.toString().length===13?i:i*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(i=>i.key===e&&i.domain===s)}getCookiesSync(e,s){return super.getCookiesSync(e,s).map(i=>new C(i.toJSON()))}async getCookies(e,s,i){if(typeof s=="function")super.getCookies(e,(n,p)=>{s(n,p.map(t=>new C(t.toJSON())))});else{let p=(await super.getCookies(e,s)).map(t=>new C(t.toJSON()));if(i)i(null,p);else return p}}getAllCookies(e){let s=this.getCookiesSync(e);try{let i=this.toJSON().cookies,n=[...s];return i.forEach(p=>{n.some(t=>t.key===p.key)||n.push(new C(p))}),n}catch(i){console.log(i),console.error(`Error parsing cookies: ${i.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 H=class u extends Error{constructor(s,i,n,p){super(s);this.response={};this.response={data:n,status:i.status,statusText:i.statusText,finalUrl:i.url,cookies:void 0,headers:p,contentType:i.headers.get("Content-Type")},Object.setPrototypeOf(this,u.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,i){return this.request(e,"POST",s,i)}async put(e,s,i){return this.request(e,"PUT",s,i)}async patch(e,s,i){return this.request(e,"PATCH",s,i)}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)}async Error(e,s){let i=e.headers.get("Content-Type"),n=await this.parseResponseBody(e,i),p={};return e.headers.forEach((t,o)=>{o.toLowerCase()!=="set-cookie"&&o.toLowerCase()!=="set-cookies"&&(p[o]=t)}),new H(s??e.statusText??"Unknown error",e,n,p)}async formatResponse(e,s,i){let n=e.headers.get("Content-Type"),p=i?null:await this.parseResponseBody(e,n),t={};return e.headers.forEach((o,a)=>{a.toLowerCase()!=="set-cookie"&&a.toLowerCase()!=="set-cookies"&&(t[a]=o)}),{data:p,status:e.status,statusText:e.statusText,headers:t,finalUrl:s,cookies:{array:this.jar.toArray(),string:this.jar.toCookieString(),netscape:this.jar.toNetscapeFormat()},contentType:n,...i}}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=S(require("fs")),b=S(require("path")),O=S(require("form-data")),R=class extends k{constructor(){super()}setCookies(e,s,i){i&&(this.jar=new g),Array.isArray(e)?this.jar.fromArray(e,i):typeof e=="string"&&(s&&e.includes("; ")?this.jar.parseCookieString(e,s,i):e.includes(` | ||
`)&&e.split(" ").length>3&&this.jar.parseNetscapeCookies(e,i))}getCookies(){return{array:this.jar.toArray(),string:this.jar.toCookieString(),netscape:this.jar.toNetscapeFormat()}}async request(e,s,i=void 0,n={}){n.body=i||n.body;let p=process.hrtime(),t,o=n.maxRedirects||10,a=n.debug!==void 0?n.debug:!1,d=null;if(n.saveTo){let c=b.basename(n.saveTo);if(this.checkISPermission(n.saveTo.length?b.dirname(n.saveTo):b.resolve(process.cwd()))){let l=c.length<n.saveTo.length?b.dirname(n.saveTo):b.join(process.cwd(),"download");m.existsSync(l)||m.mkdirSync(l,{recursive:!0}),d=b.join(l,c)}else n.debug&&console.log(`Permission denied to save to ${n.saveTo}, returning the response data instead`)}let r=0,h=e instanceof URL?e.href:e;for(;;){if(n.params&&typeof n.params=="object"){let l=new URLSearchParams(n.params).toString(),f=new URL(h);f.search=f.search?f.search+"&"+l:"?"+l,h=f.toString()}let c=this.prepareFetchOptions(n,h,s);try{let l=await fetch(h,{...c,method:s,redirect:"manual"});if(this.jar.setResponseCookies(l,h),[301,302,303,307,308].includes(l.status)&&!n.dontFollowRedirects){if(r>=o)throw await this.Error(l,`Max redirects (${o}) reached`);let f=l.headers.get("Location");if(!f)throw await this.Error(l,"Redirect location not found");if(l.status<200||l.status>308)throw d?await this.Error(l,`Failed to download: ${l.statusText}`):await this.Error(l);h=new URL(f,h).toString(),r++,a&&console.log(`Redirecting to: ${h}`),l.status===303&&(s="GET",delete n.body);continue}if(d&&l.body){let f=m.createWriteStream(d);await new Promise(async(N,F)=>{let I=l.body.getReader();for(;;){let{done:J,value:_}=await I.read();if(J)break;f.write(Buffer.from(_))}f.end(),f.on("finish",N),f.on("error",F)});let T=m.statSync(d).size,[P,D]=process.hrtime(p),$=P+D/1e9;t={fileName:d,totalTime:$,downloadSpeed:T/$,size:T}}return this.formatResponse(l,h,t)}catch(l){throw l}}}prepareFetchOptions(e,s,i){let p=["post","put","patch"].includes(i.toLowerCase()),t={},o=e.headers||{},a=e.contentType||o["Content-Type"]||(p?"application/json":void 0);if(e.headers?t.headers=o instanceof Headers?o:new Headers(o):t.headers=new Headers,o.Cookie||o.cookie){let r=o.Cookie||o.cookie;this.setCookies(r,s),delete o.Cookie,delete o.cookie,t.headers.delete("Cookie")}e.cookies&&this.setCookies(e.cookies,s);let d;if(e.useAllCookies?d=this.jar.getAllCookies(s):d=this.jar.getCookiesSync(s),e.printHeaders&&console.log("Cookies:",d),d.length>0&&t.headers.set("Cookie",d.map(r=>r.toString()).join("; ")),e.body&&(t.body=e.body),e.timeout){let r=new AbortController;setTimeout(()=>r.abort(),e.timeout),t.signal=r.signal}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 r=new O.default;Object.entries(e.multipart).forEach(([h,c])=>{r.append(h,c)}),t.body=r}else if(e.requestType)switch(e.requestType){case"json":a="application/json",typeof t.body=="object"&&(t.body=JSON.stringify(t.body));break;case"form":a="application/x-www-form-urlencoded",typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString());break;case"form-data":case"formData":if(typeof t.body=="object"){let r=new O.default;Object.entries(t.body).forEach(([h,c])=>{r.append(h,c)}),t.body=r}break;case"text":a="text/plain";break}else if(a){let r=a.toLowerCase();if(r.includes("json"))t.headers.set("Content-Type","application/json"),t.body&&typeof t.body=="object"&&(t.body=JSON.stringify(t.body));else if(r.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(r.includes("multipart")){if(t.body&&typeof t.body=="object"){let h=new O.default;Object.entries(t.body).forEach(([c,l])=>{h.append(c,l)}),t.body=h}}else r.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.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 v=S(require("form-data")),x=class extends k{constructor(){super()}setCookies(e,s,i){i&&(this.jar=new g),Array.isArray(e)?this.jar.fromArray(e,i):typeof e=="string"&&(s&&e.includes("; ")?this.jar.parseCookieString(e,s,i):e.includes(` | ||
`)&&e.split(" ").length>3&&this.jar.parseNetscapeCookies(e,i))}getCookies(){return{array:this.jar.toArray(),string:this.jar.toString(),netscape:this.jar.toNetscapeFormat()}}async request(e,s,i=void 0,n={}){let p,t=n.maxRedirects||10,o=n.debug!==void 0?n.debug:!1,a=null;n.saveTo&&console.log("You can only use this feature in Node.js, not in Edge or Browser.");let d=0,r=e instanceof URL?e.href:e;for(;;){if(n.params&&typeof n.params=="object"){let c=new URLSearchParams(n.params).toString(),l=new URL(r);l.search=l.search?l.search+"&"+c:"?"+c,r=l.toString()}let h=this.prepareFetchOptions(n,r,s);try{let c=await fetch(r,{...h,method:s,redirect:"manual"});if(this.jar.setResponseCookies(c,r),[301,302,303,307,308].includes(c.status)&&!n.dontFollowRedirects){if(d>=t)throw await this.Error(c,`Max redirects (${t}) reached`);let l=c.headers.get("Location");if(!l)throw await this.Error(c,"Redirect location not found");if(c.status<200||c.status>308)throw a?await this.Error(c,`Failed to download: ${c.statusText}`):await this.Error(c);r=new URL(l,r).toString(),d++,o&&console.log(`Redirecting to: ${r}`),c.status===303&&(s="GET",delete n.body);continue}return this.formatResponse(c,r,p)}catch(c){throw c}}}prepareFetchOptions(e,s,i){let p=["post","put","patch"].includes(i.toLowerCase()),t={},o=e.headers||{},a=e.contentType||o["Content-Type"]||(p?"application/json":void 0);if(e.headers?t.headers=o instanceof Headers?o:new Headers(o):t.headers=new Headers,o.Cookie||o.cookie){let r=o.Cookie||o.cookie;this.setCookies(r,s),delete o.Cookie,delete o.cookie,t.headers.delete("Cookie")}e.cookies&&this.setCookies(e.cookies,s);let d;if(e.useAllCookies?d=this.jar.getAllCookies(s):d=this.jar.getCookiesSync(s),e.printHeaders&&console.log("Cookies:",d),d.length>0&&t.headers.set("Cookie",d.map(r=>`${r.key}=${r.value}`).join("; ")),e.body&&(t.body=e.body),e.timeout){let r=new AbortController;setTimeout(()=>r.abort(),e.timeout),t.signal=r.signal}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 r=new v.default;Object.entries(e.multipart).forEach(([h,c])=>{r.append(h,c)}),t.body=r}else if(e.requestType)switch(e.requestType){case"json":a="application/json",typeof t.body=="object"&&(t.body=JSON.stringify(t.body));break;case"form":a="application/x-www-form-urlencoded",typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString());break;case"form-data":case"formData":if(typeof t.body=="object"){let r=new v.default;Object.entries(t.body).forEach(([h,c])=>{r.append(h,c)}),t.body=r}break;case"text":a="text/plain";break}else if(a){let r=a.toLowerCase();if(r.includes("json"))t.headers.set("Content-Type","application/json"),t.body&&typeof t.body=="object"&&(t.body=JSON.stringify(t.body));else if(r.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(r.includes("multipart")){if(t.body&&typeof t.body=="object"){let h=new v.default;Object.entries(t.body).forEach(([c,l])=>{h.append(c,l)}),t.body=h}}else r.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.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 E=S(require("form-data")),j=class extends k{constructor(){super()}setCookies(e,s,i){i&&(this.jar=new g),Array.isArray(e)?this.jar.fromArray(e,i):typeof e=="string"&&(s&&e.includes("; ")?this.jar.parseCookieString(e,s,i):e.includes(` | ||
`)&&e.split(" ").length>3&&this.jar.parseNetscapeCookies(e,i))}getCookies(){return{array:this.jar.toArray(),string:this.jar.toString(),netscape:this.jar.toNetscapeFormat()}}async request(e,s,i=void 0,n={}){let p,t=n.maxRedirects||10,o=n.debug!==void 0?n.debug:!1,a=null;n.saveTo&&console.log("You can only use this feature in Node.js, not in Edge or Browser.");let d=0,r=e instanceof URL?e.href:e;for(;;){if(n.params&&typeof n.params=="object"){let c=new URLSearchParams(n.params).toString(),l=new URL(r);l.search=l.search?l.search+"&"+c:"?"+c,r=l.toString()}let h=this.prepareFetchOptions(n,r,s);try{let c=await fetch(r,{...h,method:s,redirect:"manual"});if(this.jar.setResponseCookies(c,r),[301,302,303,307,308].includes(c.status)&&!n.dontFollowRedirects){if(d>=t)throw await this.Error(c,`Max redirects (${t}) reached`);let l=c.headers.get("Location");if(!l)throw await this.Error(c,"Redirect location not found");if(c.status<200||c.status>308)throw a?await this.Error(c,`Failed to download: ${c.statusText}`):await this.Error(c);r=new URL(l,r).toString(),d++,o&&console.log(`Redirecting to: ${r}`),c.status===303&&(s="GET",delete n.body);continue}return this.formatResponse(c,r,p)}catch(c){throw c}}}prepareFetchOptions(e,s,i){let p=["post","put","patch"].includes(i.toLowerCase()),t={},o=e.headers||{},a=e.contentType||o["Content-Type"]||(p?"application/json":void 0);if(e.headers?t.headers=o instanceof Headers?o:new Headers(o):t.headers=new Headers,o.Cookie||o.cookie){let r=o.Cookie||o.cookie;this.setCookies(r,s),delete o.Cookie,delete o.cookie,t.headers.delete("Cookie")}e.cookies&&this.setCookies(e.cookies,s);let d;if(e.useAllCookies?d=this.jar.getAllCookies(s):d=this.jar.getCookiesSync(s),e.printHeaders&&console.log("Cookies:",d),d.length>0&&t.headers.set("Cookie",d.map(r=>`${r.key}=${r.value}`).join("; ")),e.body&&(t.body=e.body),e.timeout){let r=new AbortController;setTimeout(()=>r.abort(),e.timeout),t.signal=r.signal}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 r=new E.default;Object.entries(e.multipart).forEach(([h,c])=>{r.append(h,c)}),t.body=r}else if(e.requestType)switch(e.requestType){case"json":a="application/json",typeof t.body=="object"&&(t.body=JSON.stringify(t.body));break;case"form":a="application/x-www-form-urlencoded",typeof t.body=="object"&&(t.body=new URLSearchParams(t.body).toString());break;case"form-data":case"formData":if(typeof t.body=="object"){let r=new E.default;Object.entries(t.body).forEach(([h,c])=>{r.append(h,c)}),t.body=r}break;case"text":a="text/plain";break}else if(a){let r=a.toLowerCase();if(r.includes("json"))t.headers.set("Content-Type","application/json"),t.body&&typeof t.body=="object"&&(t.body=JSON.stringify(t.body));else if(r.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(r.includes("multipart")){if(t.body&&typeof t.body=="object"){let h=new E.default;Object.entries(t.body).forEach(([c,l])=>{h.append(c,l)}),t.body=h}}else r.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.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 R;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}); |
@@ -127,3 +127,3 @@ import { Agent as HttpAgent } from 'http'; | ||
} | ||
export interface fetchApiResponse<T = any> { | ||
export interface UniqhttResponse<T = any> { | ||
data: T; | ||
@@ -134,3 +134,3 @@ status: number; | ||
cookies: { | ||
array: any[]; | ||
array: Serialized[]; | ||
string: string; | ||
@@ -294,4 +294,4 @@ netscape: string; | ||
} | ||
export interface uniqhttError extends Error { | ||
response: fetchResponse; | ||
export interface UniqhttError extends Error { | ||
response: UniqhttResponse; | ||
} | ||
@@ -317,20 +317,5 @@ export interface DownloadResponse<T = any> { | ||
} | ||
export interface fetchResponse<T = any> { | ||
data: T; | ||
status: number; | ||
statusText: string; | ||
finalUrl: string; | ||
cookies: { | ||
array: Cookie[]; | ||
string: string; | ||
netscape: string; | ||
}; | ||
headers: { | ||
[p: string]: string; | ||
}; | ||
} | ||
export type HttpConfig = RequestInit & { | ||
cookies?: cookiesArrayType | Cookie[] | string; | ||
cookies?: cookiesArrayType | Serialized[] | string; | ||
withCookies?: boolean; | ||
jar?: YuniqCookies; | ||
params?: { | ||
@@ -346,3 +331,2 @@ [key: string]: string | number | boolean; | ||
withoutContentType?: boolean; | ||
body?: any; | ||
debug?: boolean; | ||
@@ -539,4 +523,21 @@ multipart?: Record<string, any>; | ||
} | ||
export interface IErrorEvent extends uniqhttError { | ||
export interface IErrorEvent extends UniqWgetError { | ||
} | ||
export type Serialized = { | ||
key: string; | ||
value: string; | ||
expires: Date | undefined; | ||
maxAge?: number | "Infinity" | "-Infinity" | undefined; | ||
domain?: string | undefined; | ||
path?: string | undefined; | ||
secure?: boolean | undefined; | ||
httpOnly?: boolean | undefined; | ||
extensions?: string[] | undefined; | ||
creation?: Date | undefined; | ||
creationIndex?: number | undefined; | ||
hostOnly?: boolean | undefined; | ||
pathIsDefault?: boolean | undefined; | ||
lastAccessed?: Date | undefined; | ||
sameSite?: string | undefined; | ||
}; | ||
export {}; |
{ | ||
"name": "uniqhtt", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"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", |
41
t.ts
@@ -1,10 +0,39 @@ | ||
import { UniqhttNode } from './lib/index'; | ||
import { UniqhttNode } from './src/index'; | ||
interface TurbifyTokenData { | ||
success: boolean | ||
result: { | ||
token: string | ||
} | ||
} | ||
const websiteURL = 'https://mail.turbify.com/forgotpassword?.done=https%3A%2F%2Fmail.turbify.com%2Finitiate'; | ||
const tokenURL = 'https://mail.turbify.com/xhr/csrf/getToken'; | ||
const headers = { | ||
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:128.0) Gecko/20100101 Firefox/128.0', | ||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/png,image/svg+xml,*/*;q=0.8', | ||
'Accept-Language': 'en-US,en;q=0.5', | ||
'Accept-Encoding': 'gzip, deflate, br, zstd', | ||
'Connection': 'keep-alive', | ||
'Upgrade-Insecure-Requests': '1', | ||
'Sec-Fetch-Dest': 'document', | ||
'Sec-Fetch-Mode': 'navigate', | ||
'Sec-Fetch-Site': 'same-origin', | ||
'Sec-Fetch-User': '?1', | ||
'Priority': 'u=0, i', | ||
'TE': 'trailers' | ||
}; | ||
(async () => { | ||
const uniqhtt = new UniqhttNode(); | ||
const response = await uniqhtt.get('https://media.post.rvohealth.io/wp-content/uploads/sites/4/2022/01/couple-silhouette-sunset-holding-hands-walking-grass-1296x728-header-1024x575.jpg', { | ||
saveTo: 'image.jpg', | ||
debug: true | ||
}); | ||
console.log(response); | ||
await uniqhtt.get(websiteURL, { headers }) | ||
console.log("Cookies:", uniqhtt.getCookies()) | ||
const { data } = await uniqhtt.get<TurbifyTokenData>(tokenURL, { headers }) | ||
const body = { | ||
email: 'frank@americanbluegrassmarble.com', | ||
_csrf: data.result.token, | ||
'g-recaptcha-response': null, | ||
} | ||
console.log("Cookies:", uniqhtt.getCookies()) | ||
})(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
61169
810
4