Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

uniqhtt

Package Overview
Dependencies
Maintainers
0
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uniqhtt - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

58

lib/index.js

@@ -1,52 +0,6 @@

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UniqhttEdge = exports.UniqhttBrowser = exports.UniqhttNode = exports.YuniqCookies = void 0;
var YuniqCookies_1 = require("./classes/YuniqCookies");
Object.defineProperty(exports, "YuniqCookies", { enumerable: true, get: function () { return YuniqCookies_1.YuniqCookies; } });
const UniqhttAgentNode_1 = require("./classes/UniqhttAgentNode");
Object.defineProperty(exports, "UniqhttNode", { enumerable: true, get: function () { return UniqhttAgentNode_1.UniqhttAgent; } });
const UniqhttAgentEdge_1 = require("./classes/UniqhttAgentEdge");
Object.defineProperty(exports, "UniqhttEdge", { enumerable: true, get: function () { return UniqhttAgentEdge_1.UniqhttAgentEdge; } });
const UniqhttAgentBrowser_1 = require("./classes/UniqhttAgentBrowser");
Object.defineProperty(exports, "UniqhttBrowser", { enumerable: true, get: function () { return UniqhttAgentBrowser_1.UniqhttAgentBrowser; } });
function detectEnvironment() {
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
return 'node';
}
else if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
return 'browser';
}
else {
return 'edge';
}
}
function createUniqhtt() {
const environment = detectEnvironment();
switch (environment) {
case 'node':
return new UniqhttAgentNode_1.UniqhttAgent();
case 'browser':
return new UniqhttAgentBrowser_1.UniqhttAgentBrowser();
case 'edge':
return new UniqhttAgentEdge_1.UniqhttAgentEdge();
default:
throw new Error('Unsupported environment');
}
}
const uniqhtt = createUniqhtt();
exports.default = uniqhtt;
__exportStar(require("./types/types"), exports);
"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});

@@ -292,6 +292,2 @@ import { Agent as HttpAgent } from 'http';

}
export interface UniqWgetStatic extends UniqWgetInstance {
create(config?: RequestOptions): UniqWgetInstance;
createWithR2(r2Bucket: UniqWgetR2Bucket, config?: RequestOptions): UniqWgetInstance;
}
export interface uniqhttError extends Error {

@@ -541,3 +537,2 @@ response: fetchResponse;

}
declare const UniqWget: UniqWgetStatic;
export default UniqWget;
export {};

4

package.json
{
"name": "uniqhtt",
"version": "1.0.0",
"version": "1.0.1",
"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.",

@@ -35,2 +35,4 @@ "main": "lib/index.js",

"@types/tough-cookie": "^4.0.5",
"esbuild": "^0.23.1",
"esbuild-plugin-d.ts": "^1.3.0",
"javascript-obfuscator": "^4.1.1",

@@ -37,0 +39,0 @@ "typescript": "^5.5.4"

@@ -1,2 +0,2 @@

import { UniqhttNode } from './src/index';
import { UniqhttNode } from './lib/index';

@@ -3,0 +3,0 @@ (async () => {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc