yuniq-cookie
Advanced tools
Comparing version 1.0.1 to 1.0.2
"use strict";var p=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var F=(h,e)=>{for(var i in e)p(h,i,{get:e[i],enumerable:!0})},A=(h,e,i,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of b(e))!D.call(h,n)&&n!==i&&p(h,n,{get:()=>e[n],enumerable:!(t=k(e,n))||t.enumerable});return h};var I=h=>A(p({},"__esModule",{value:!0}),h);var v={};F(v,{Cookie:()=>u,CookieJar:()=>E,YuniqCookie:()=>g});module.exports=I(v);var m=class{static rotateLeft(e,i){return e<<i|e>>>32-i}static addUnsigned(e,i){let t=e&1073741824,n=i&1073741824,s=e&2147483648,r=i&2147483648,o=(e&1073741823)+(i&1073741823);return t&n?o^2147483648^s^r:t|n?o&1073741824?o^3221225472^s^r:o^1073741824^s^r:o^s^r}static F(e,i,t){return e&i|~e&t}static G(e,i,t){return e&t|i&~t}static H(e,i,t){return e^i^t}static I(e,i,t){return i^(e|~t)}static FF(e,i,t,n,s,r,o){return e=this.addUnsigned(e,this.addUnsigned(this.addUnsigned(this.F(i,t,n),s),o)),this.addUnsigned(this.rotateLeft(e,r),i)}static GG(e,i,t,n,s,r,o){return e=this.addUnsigned(e,this.addUnsigned(this.addUnsigned(this.G(i,t,n),s),o)),this.addUnsigned(this.rotateLeft(e,r),i)}static HH(e,i,t,n,s,r,o){return e=this.addUnsigned(e,this.addUnsigned(this.addUnsigned(this.H(i,t,n),s),o)),this.addUnsigned(this.rotateLeft(e,r),i)}static II(e,i,t,n,s,r,o){return e=this.addUnsigned(e,this.addUnsigned(this.addUnsigned(this.I(i,t,n),s),o)),this.addUnsigned(this.rotateLeft(e,r),i)}static convertToWordArray(e){let i=e.length,t=((i+8>>6)+1)*16,n=Array(t-1),s=0,r=0;for(;r<i;){let a=(r-r%4)/4;s=r%4*8,n[a]=n[a]|e.charCodeAt(r)<<s,r++}let o=(r-r%4)/4;return s=r%4*8,n[o]=n[o]|128<<s,n[t-2]=i<<3,n[t-1]=i>>>29,n}static wordToHex(e){let i="";for(let t=0;t<=3;t++)i+=("0"+(e>>>t*8&255).toString(16)).slice(-2);return i}static md5(e){let i=this.convertToWordArray(e),t=1732584193,n=4023233417,s=2562383102,r=271733878,o=[7,12,17,22,5,9,14,20,4,11,16,23,6,10,15,21],a=[3614090360,3905402710,606105819,3250441966,4118548399,1200080426,2821735955,4249261313,1770035416,2336552879,4294925233,2304563134,1804603682,4254626195,2792965006,1236535329,4129170786,3225465664,643717713,3921069994,3593408605,38016083,3634488961,3889429448,568446438,3275163606,4107603335,1163531501,2850285829,4243563512,1735328473,2368359562,4294588738,2272392833,1839030562,4259657740,2763975236,1272893353,4139469664,3200236656,681279174,3936430074,3572445317,76029189,3654602809,3873151461,530742520,3299628645,4096336452,1126891415,2878612391,4237533241,1700485571,2399980690,4293915773,2240044497,1873313359,4264355552,2734768916,1309151649,4149444226,3174756917,718787259,3951481745];for(let l=0;l<i.length;l+=16){let c=t,d=n,f=s,x=r;t=this.FF(t,n,s,r,i[l],o[0],a[0]),r=this.FF(r,t,n,s,i[l+1],o[1],a[1]),s=this.FF(s,r,t,n,i[l+2],o[2],a[2]),n=this.FF(n,s,r,t,i[l+3],o[3],a[3]),t=this.FF(t,n,s,r,i[l+4],o[4],a[4]),r=this.FF(r,t,n,s,i[l+5],o[5],a[5]),s=this.FF(s,r,t,n,i[l+6],o[6],a[6]),n=this.FF(n,s,r,t,i[l+7],o[7],a[7]),t=this.FF(t,n,s,r,i[l+8],o[8],a[8]),r=this.FF(r,t,n,s,i[l+9],o[9],a[9]),s=this.FF(s,r,t,n,i[l+10],o[10],a[10]),n=this.FF(n,s,r,t,i[l+11],o[11],a[11]),t=this.FF(t,n,s,r,i[l+12],o[12],a[12]),r=this.FF(r,t,n,s,i[l+13],o[13],a[13]),s=this.FF(s,r,t,n,i[l+14],o[14],a[14]),n=this.FF(n,s,r,t,i[l+15],o[15],a[15]),t=this.GG(t,n,s,r,i[l+1],o[16],a[16]),r=this.GG(r,t,n,s,i[l+6],o[17],a[17]),s=this.GG(s,r,t,n,i[l+11],o[18],a[18]),n=this.GG(n,s,r,t,i[l],o[19],a[19]),t=this.GG(t,n,s,r,i[l+5],o[20],a[20]),r=this.GG(r,t,n,s,i[l+10],o[21],a[21]),s=this.GG(s,r,t,n,i[l+15],o[22],a[22]),n=this.GG(n,s,r,t,i[l+4],o[23],a[23]),t=this.GG(t,n,s,r,i[l+9],o[24],a[24]),r=this.GG(r,t,n,s,i[l+14],o[25],a[25]),s=this.GG(s,r,t,n,i[l+3],o[26],a[26]),n=this.GG(n,s,r,t,i[l+8],o[27],a[27]),t=this.GG(t,n,s,r,i[l+13],o[28],a[28]),r=this.GG(r,t,n,s,i[l+2],o[29],a[29]),s=this.GG(s,r,t,n,i[l+7],o[30],a[30]),n=this.GG(n,s,r,t,i[l+12],o[31],a[31]),t=this.HH(t,n,s,r,i[l+5],o[32],a[32]),r=this.HH(r,t,n,s,i[l+8],o[33],a[33]),s=this.HH(s,r,t,n,i[l+11],o[34],a[34]),n=this.HH(n,s,r,t,i[l+14],o[35],a[35]),t=this.HH(t,n,s,r,i[l+1],o[36],a[36]),r=this.HH(r,t,n,s,i[l+4],o[37],a[37]),s=this.HH(s,r,t,n,i[l+7],o[38],a[38]),n=this.HH(n,s,r,t,i[l+10],o[39],a[39]),t=this.HH(t,n,s,r,i[l+13],o[40],a[40]),r=this.HH(r,t,n,s,i[l],o[41],a[41]),s=this.HH(s,r,t,n,i[l+3],o[42],a[42]),n=this.HH(n,s,r,t,i[l+6],o[43],a[43]),t=this.HH(t,n,s,r,i[l+9],o[44],a[44]),r=this.HH(r,t,n,s,i[l+12],o[45],a[45]),s=this.HH(s,r,t,n,i[l+15],o[46],a[46]),n=this.HH(n,s,r,t,i[l+2],o[47],a[47]),t=this.II(t,n,s,r,i[l],o[48],a[48]),r=this.II(r,t,n,s,i[l+7],o[49],a[49]),s=this.II(s,r,t,n,i[l+14],o[50],a[50]),n=this.II(n,s,r,t,i[l+5],o[51],a[51]),t=this.II(t,n,s,r,i[l+12],o[52],a[52]),r=this.II(r,t,n,s,i[l+3],o[53],a[53]),s=this.II(s,r,t,n,i[l+10],o[54],a[54]),n=this.II(n,s,r,t,i[l+1],o[55],a[55]),t=this.II(t,n,s,r,i[l+8],o[56],a[56]),r=this.II(r,t,n,s,i[l+15],o[57],a[57]),s=this.II(s,r,t,n,i[l+6],o[58],a[58]),n=this.II(n,s,r,t,i[l+13],o[59],a[59]),t=this.II(t,n,s,r,i[l+4],o[60],a[60]),r=this.II(r,t,n,s,i[l+11],o[61],a[61]),s=this.II(s,r,t,n,i[l+2],o[62],a[62]),n=this.II(n,s,r,t,i[l+9],o[63],a[63]),t=this.addUnsigned(t,c),n=this.addUnsigned(n,d),s=this.addUnsigned(s,f),r=this.addUnsigned(r,x)}return this.wordToHex(t)+this.wordToHex(n)+this.wordToHex(s)+this.wordToHex(r)}};function y(h){if(h instanceof Date)return h;if(typeof h=="string"&&h!=="Infinity")return S(h)?new Date(C(h)):new Date(h);if(typeof h=="number")return new Date(C(h))}function S(h){return/^[0-9]+$/.test(h)}Date.prototype.addDay=function(h){let e=new Date(this);return e.setDate(e.getDate()+h),e};function C(h){let e=h.toString().trim();try{if(typeof h=="number")return e.length===13?h:h*1e3;let i=parseInt(e);return i.toString().length===13?i:i*1e3}catch{return 0}}var u=class h{key;value;expires;maxAge;domain;path;secure;httpOnly;extensions;creation;creationIndex;hostOnly;pathIsDefault;lastAccessed;sameSite;plainDomain;hash;constructor(e){e={...e,expires:y(e.expires)??"Infinity",creation:y(e.creation),lastAccessed:y(e.lastAccessed)},this.key=e.key||"",this.value=e.value||"",this.expires=e.expires||null,this.maxAge=e.maxAge||null,this.domain=e.domain||null,this.path=e.path||null,this.secure=e.secure||!1,this.httpOnly=e.httpOnly||!1,this.extensions=e.extensions||null,this.creation=e.creation||new Date,this.creationIndex=h.cookiesCreated++,this.hostOnly=e.hostOnly||null,this.pathIsDefault=e.pathIsDefault||null,this.lastAccessed=e.lastAccessed||new Date,this.sameSite=e.sameSite,this.plainDomain=this.domain?this.domain.startsWith(".")?this.domain.substring(1):this.domain:null,this.hash=this.domain?m.md5(`${this.domain.toString()}_${this.key}`):null}toJSON(){let e={};return h.serializableProperties.forEach(i=>{this[i]!==void 0&&(this[i]instanceof Date?e[i]=this[i].toISOString():e[i]=this[i])}),e}clone(){return h.fromJSON(this.toJSON())}validate(){return!(!this.key||this.key.trim()===""||this.value===void 0||this.expires&&!(this.expires instanceof Date)&&this.expires!=="Infinity"||this.maxAge!==null&&typeof this.maxAge!="number"&&this.maxAge!=="Infinity"&&this.maxAge!=="-Infinity"||this.domain&&typeof this.domain!="string"||this.path&&typeof this.path!="string"||typeof this.secure!="boolean"||typeof this.httpOnly!="boolean"||this.sameSite&&!["Strict","Lax","None"].includes(this.sameSite))}setExpires(e){this.expires=e instanceof Date?e:h.parseDate(e)}setMaxAge(e){this.maxAge=isFinite(e)?e:e===1/0?"Infinity":"-Infinity"}cookieString(){return`${this.key}=${this.value}`}toSetCookieString(){let e=this.cookieString();return this.expires instanceof Date&&(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}`),this.extensions&&(e+=`; ${this.extensions.join("; ")}`),e}toArray(){let e={key:this.key,value:this.value};return this.domain&&(e.domain=this.domain),this.path&&(e.path=this.path),this.secure&&(e.secure=this.secure),this.httpOnly&&(e.httpOnly=this.httpOnly),this.sameSite&&(e.sameSite=this.sameSite),this.extensions&&(e.extensions=this.extensions),this.expires&&(e.expires=this.expires),this.creation&&(e.creation=this.creation),this.lastAccessed&&(e.lastAccessed=this.lastAccessed),this.hostOnly&&(e.hostOnly=this.hostOnly),this.pathIsDefault&&(e.pathIsDefault=this.pathIsDefault),this.hash&&(e.hash=this.hash),this.plainDomain&&(e.plainDomain=this.plainDomain),this.maxAge&&(e.maxAge=this.maxAge),e}toString(){return`${this.key}=${this.value}`}toNetscapeFormat(){let e=this.hostOnly?this.domain:this.domain.startsWith(".")?this.domain:"."+this.domain,i=this.secure?"TRUE":"FALSE",t=this.expires&&typeof this.expires!="string"?Math.round(this.expires.getTime()/1e3):0;return`${e} TRUE ${this.path} ${i} ${t} ${this.key} ${this.value}`}TTL(e=Date.now()){return this.expires==="Infinity"||!this.expires?1/0:Math.max(0,this.expires.getTime()-e)}expiryTime(e=new Date){if(this.maxAge!=null){let i=this.maxAge==="Infinity"?1/0:Number(this.maxAge)*1e3;return(this.lastAccessed instanceof Date?this.lastAccessed:e).getTime()+i}return this.expires instanceof Date?this.expires.getTime():void 0}isPersistent(){return this.expires!=null||this.maxAge!=null}canonicalizedDomain(){return this.domain?h.canonicalDomain(this.domain):void 0}cdomain(){return this.canonicalizedDomain()}static parse(e,i={}){try{let t=e.split(";").map(c=>c.trim()),[n,...s]=t,[r,...o]=n.split("=").map(c=>c.trim()),a=o.join("=");if(!r&&!i.loose)return;let l=new h({key:r,value:a});return s.forEach(c=>{let[d,f]=c.split("=").map(x=>x.trim());switch(d.toLowerCase()){case"expires":l.expires=h.parseDate(f);break;case"max-age":l.setMaxAge(parseInt(f,10));break;case"domain":l.domain=f;break;case"path":l.path=f;break;case"secure":l.secure=!0;break;case"httponly":l.httpOnly=!0;break;case"samesite":l.sameSite=f;break;default:l.extensions||(l.extensions=[]),l.extensions.push(c)}}),l}catch{return}}static fromJSONString(e){if(typeof e=="string")try{e=JSON.parse(e)}catch{return}if(typeof e!="object"||e===null)return;let i={};return h.serializableProperties.forEach(t=>{e[t]!==void 0&&(t==="expires"||t==="creation"||t==="lastAccessed"?i[t]=new Date(e[t]):i[t]=e[t])}),new h(i)}static fromJSON(e){if(typeof e=="object"&&e!==null)try{return new h(e)}catch{return}}static parseDate(e){return new Date(e)}static canonicalDomain(e){return e.toLowerCase()}static cookiesCreated=0;static defaultPath="/";static sameSiteLevel={strict:3,lax:2,none:1};static sameSiteCanonical={strict:"Strict",lax:"Lax"};static serializableProperties=["key","value","expires","maxAge","domain","path","secure","httpOnly","extensions","hostOnly","pathIsDefault","creation","lastAccessed","sameSite"];isDomainMatch(e,i){return e=this.isValidUrl(e)?new URL(e).hostname:e,i===e||i.startsWith(".")&&(e===i.substring(1)||e.endsWith(i))||e.endsWith(i)}inDomain(e,i=!0){return this.domain?this.isDomainMatch(e,this.domain):i}isValidUrl(e){try{return new URL(e),e.startsWith("http")||e.startsWith("https")}catch{return!1}}};var g=class{constructor(e=!1){this.debug=e}cookies=[];setCookieSync(e,i,t=!0){let n=this.getDomain(i);if(!n)return;e.domain=e.domain?e.domain:n;let s=e.domain?m.md5(`${e.domain.toString()}_${e.key}`):null;e.lastAccessed=new Date;let r=!1;if(s&&this.cookies.find(o=>o.hash===s)){if(t){let o=this.cookies.find(a=>a.hash===s);o&&(e.creationIndex=o.creationIndex,e.creation=o.creation),this.cookies=this.cookies.filter(a=>a.hash!==s)}r=!0}r||(e.creationIndex=this.cookies.length),s&&(e.hash=s),(!r||t)&&this.cookies.push(e),this.removeExpiredCookies()}setResponseCookies(e,i){var t,n;if(e instanceof Response)if((e==null?void 0:e.headers)&&typeof((t=e.headers)==null?void 0:t.getSetCookie)<"u")for(let r of e.headers.getSetCookie()){let o=u.parse(r.trim());o&&this.setCookieSync(o,i)}else{let r=(n=e==null?void 0:e.headers)==null?void 0:n.get("set-cookie");r&&r.split(",").forEach(o=>{let a=u.parse(o);a&&this.setCookieSync(a,i)})}else Array.isArray(e)?typeof e[0]=="string"?e.forEach(s=>{let r=u.parse(s.trim());r&&this.setCookieSync(r,i)}):e[0]instanceof u?e.forEach(s=>this.setCookieSync(s,i)):e.forEach(s=>{let r=u.fromJSON(s);r&&this.setCookieSync(r,i)}):typeof e=="string"&&e.includes("=")&&e.includes(",")&&e.split(",").forEach(s=>{let r=u.parse(s);r&&this.setCookieSync(r,i)})}toNetscapeFormat(e){this.removeExpiredCookies();let i=`# Netscape HTTP Cookie File | ||
`;return i+=`# This file was generated by YuniqCookies npm package | ||
`,i+=`# https://www.npmjs.com/package/yuniq-cookies | ||
`;return i+=`# This file was generated by YuniqCookie npm package | ||
`,i+=`# https://www.npmjs.com/package/yuniq-cookie | ||
`,i+this.cookies.filter(t=>!e||t.inDomain(e)).map(t=>t.toNetscapeFormat()).join(` | ||
`)||""}getDomain(e){if(!e)return null;try{return new URL(e).hostname}catch{return null}}toArray(){return this.removeExpiredCookies(),this.cookies.map(e=>e.toArray())||[]}toCookieString(e){var i;return this.removeExpiredCookies(),((i=this.allCookies())==null?void 0:i.filter(t=>!e||t.inDomain(e,!1)).map(t=>t.toString()).join("; "))||""}toSetCookiesString(e){var i;return this.removeExpiredCookies(),((i=this.allCookies())==null?void 0:i.filter(t=>!e||t.inDomain(e,!1)).map(t=>t.toSetCookieString()).join("; "))||""}domainToCookieString(e){var i;return this.removeExpiredCookies(),((i=this.allCookies())==null?void 0:i.filter(t=>t.domain===e).map(t=>`${t.key}=${t.value}`).join("; "))||""}removeExpiredCookies(){let e=new Date,i=this.cookies.length;return this.cookies=this.cookies.filter(n=>{if(n.expires instanceof Date&&n.expires.getTime()<=e.getTime())return!1;if(n.expires===null&&typeof n.maxAge=="number"){let s=n.lastAccessed instanceof Date?n.lastAccessed:n.creation instanceof Date?n.creation:new Date(0);if(new Date(s.getTime()+this.isMillisecondsLength(n.maxAge)).getTime()<=e.getTime())return!1}return!0}),i-this.cookies.length}getCookieByKey(e,i,t){if(this.removeExpiredCookies(),i){let s=this.getCookies(i).map(r=>{let o=t?r==null?void 0:r.key.trim():r==null?void 0:r.key.trim().toLowerCase(),a=t?e.trim():e.trim().toLowerCase();return o===a?r:null}).filter(r=>r!==null);if(s&&s.length>0)return s[0]}else{let n=this.cookies.map(s=>{let r=s,o=t?r==null?void 0:r.key.trim():r==null?void 0:r.key.trim().toLowerCase(),a=t?e.trim():e.trim().toLowerCase();return o===a?s:null}).filter(s=>s!==null);if(n&&n.length>0)return n[0]}return null}parseNetscapeCookies(e,i=!0){let t=e.split(` | ||
`);for(let n of t)try{let[s,r,o,a,l,c,d]=n.split(" "),f=`http${a==="TRUE"?"s":""}://${s.startsWith(".")?s.substring(1):s}${o}`;this.setCookieSync(new u({domain:s,path:o,secure:a==="TRUE",expires:l?new Date(parseInt(l)*1e3):void 0,key:c,value:d}),f,i)}catch(s){this.debug&&console.error("Error parsing netscape cookies:",s)}}parseCookieString(e,i,t=!0,n=!0){this.removeExpiredCookies();try{let s=new URL(i),r=e.replaceAll(";","_____").replaceAll("; ","_____").split("_____");for(let o of r){let[a,...l]=o.split("="),c=l.join("=");this.setCookieSync(new u({key:a,value:c,domain:s.hostname,path:"/",...this.setDates(n)}),s.toString(),t)}}catch(s){console.error(s)}}setDates(e){return{expires:e?new Date().addDay(7):"Infinity",creation:e?new Date:null,lastAccessed:e?new Date:null}}fromArray(e,i=!0){var t;for(let n of e)if(n instanceof u)n.domain&&this.setCookieSync(n,`http${n.secure?"s":""}://${n.domain.startsWith(".")?n.domain.substring(1):n.domain}${n.path}`,i);else{let s=u.fromJSON(n);s&&(n.plainDomain&&(s.plainDomain=n.plainDomain),n.hash&&(s.hash=n.hash),n.secure&&(s.secure=n.secure),this.setCookieSync(s,`http${s.secure?"s":""}://${(t=s==null?void 0:s.domain)!=null&&t.startsWith(".")?s.domain.substring(1):s.domain}${s.path}`,i))}this.removeExpiredCookies()}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)}isMillisecondsLength(e){let i=e.toString().trim();try{if(typeof e=="number")return i.length===13?e:e*1e3;let t=parseInt(i);return t.toString().length===13?t:t*1e3}catch{return 0}}clear(){this.removeAllCookiesSync()}removeAllCookiesSync(){this.cookies.length=0}getCookiesForDomain(e){return this.removeExpiredCookies(),this.allCookies().filter(i=>i.domain&&e.includes(i.domain))}allCookies(){return this.cookies}findCookie(e,i){return this.removeExpiredCookies(),this.allCookies().find(t=>t.key===e&&t.inDomain(i,!1))}getCookies(e,i){this.removeExpiredCookies();try{if(i&&typeof i=="function")i(null,this.cookies.map(t=>t&&t.inDomain(e,!1)?t:null).filter(t=>t!==null));else return this.cookies.map(t=>t&&t.inDomain(e,!1)?t:null).filter(t=>t!==null)}catch(t){if(i&&typeof i=="function")i(t,void 0);else return this.debug&&console.error("Error getting cookies:",t.message),[]}}getAllCookies(e){this.removeExpiredCookies();let i=this.getCookies(e);try{let t=[...i];return this.cookies.forEach(n=>{t.some(s=>s.hash===n.hash)||t.push(new u(n))}),t}catch(t){this.debug&&console.error(`Error parsing cookies: ${t.message}`)}return i}get size(){return this.removeExpiredCookies(),this.allCookies().length}isEmpty(){return this.removeExpiredCookies(),this.allCookies().length===0}*[Symbol.iterator](){for(let e of this.allCookies())yield e}},E=g;0&&(module.exports={Cookie,CookieJar,YuniqCookie}); |
{ | ||
"name": "yuniq-cookie", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "YuniqCookie is a powerful and versatile cookie management library for Node.js, optimized for web scraping, browser automation, and API interactions. It offers advanced features including automatic expiration handling, domain-specific filtering, HTTP request serialization, and Netscape format import/export. Building upon the foundation of tough-cookie, YuniqCookie provides enhanced functionality and detailed cookie representations, making it an ideal choice for modern Node.js applications requiring robust cookie management.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is too big to display
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
142331