UNPKG

hibp

Version:

An unofficial TypeScript SDK for the 'Have I been pwned?' service.

3 lines (2 loc) 5.72 kB
var w="hibp",P="15.2.1";async function g({baseUrl:e,endpoint:t,headers:r,timeoutMs:s,signal:n,userAgent:o,queryParams:a}){let i={headers:I(o,r),signal:v(s,n)},c=M(e,t,a);return fetch(c,i)}function M(e,t,r){let s=e.replace(/\/$/g,""),n=t.startsWith("/")?t:`/${t}`,o=new URL(`${s}${n}`);if(r)for(let[a,i]of Object.entries(r))o.searchParams.set(a,i);return o.toString()}function I(e,t){let r={...t};return e?r["User-Agent"]=e:typeof navigator>"u"&&(r["User-Agent"]=`${w} ${P}`),r}function v(e,t){let r=[];if(e&&r.push(AbortSignal.timeout(e)),t&&r.push(t),r.length!==0)return r.length===1?r[0]:AbortSignal.any(r)}var b={status:400,statusText:"Bad request \u2014 the account does not comply with an acceptable format."},S={status:401,body:`Your request to the API couldn't be authorised. Check you have the right value in the "hibp-api-key" header, refer to the documentation for more: https://haveibeenpwned.com/API/v3#Authorisation`},f={status:403,statusText:"Forbidden - access denied."};var U={status:404},x={headers:new Map([["retry-after","2"]]),status:429,body:{statusCode:429,message:"Rate limit is exceeded. Try again in 2 seconds."}};var d=class extends Error{retryAfterSeconds;constructor(t,r,s){super(r,s),this.name=this.constructor.name,this.retryAfterSeconds=typeof t=="string"?Number.parseInt(t,10):void 0}};function K(e){return`Request blocked, contact haveibeenpwned.com if this continues (Ray ID: ${e})`}async function m(e,t={}){let{apiKey:r,baseUrl:s="https://haveibeenpwned.com/api/v3",timeoutMs:n,signal:o,userAgent:a}=t,i={};r&&(i["HIBP-API-Key"]=r);let c=await g({baseUrl:s,endpoint:e,headers:i,timeoutMs:n,signal:o,userAgent:a});if(c.ok)return c.json();switch(c.status){case b.status:throw new Error(b.statusText);case S.status:{let p=await c.text();throw new Error(p)}case f.status:{let p=c.headers.get("cf-ray");throw p?new Error(K(p)):new Error(f.statusText)}case U.status:return null;case x.status:{let p=await c.json(),u=c.headers.get("retry-after");throw new d(u,p.message)}default:throw new Error(c.statusText)}}function C(e,t={}){return m(`/breach/${encodeURIComponent(e)}`,t)}function h(e,t={}){let{apiKey:r,domain:s,includeUnverified:n=!0,timeoutMs:o,signal:a,truncate:i=!0,baseUrl:c,userAgent:p}=t,u=`/breachedaccount/${encodeURIComponent(e)}?`,l=[];return s&&l.push(`domain=${encodeURIComponent(s)}`),n||l.push("includeUnverified=false"),i||l.push("truncateResponse=false"),m(`${u}${l.join("&")}`,{apiKey:r,baseUrl:c,timeoutMs:o,signal:a,userAgent:p})}function T(e,t={}){let{apiKey:r,baseUrl:s,timeoutMs:n,signal:o,userAgent:a}=t,i=`/breacheddomain/${encodeURIComponent(e)}`;return m(i,{apiKey:r,baseUrl:s,timeoutMs:n,signal:o,userAgent:a})}function F(e={}){let{domain:t,baseUrl:r,timeoutMs:s,signal:n,userAgent:o}=e,a="/breaches?",i=[];return t&&i.push(`domain=${encodeURIComponent(t)}`),m(`${a}${i.join("&")}`,{baseUrl:r,timeoutMs:s,signal:n,userAgent:o})}function $(e={}){return m("/dataclasses",e)}function j(e={}){return m("/latestbreach",e)}function y(e,t={}){return m(`/pasteaccount/${encodeURIComponent(e)}`,t)}var E={status:400,body:"The hash prefix was not in a valid format"};async function B(e,t={}){let{baseUrl:r="https://api.pwnedpasswords.com",timeoutMs:s,signal:n,userAgent:o,addPadding:a=!1,mode:i="sha1"}=t,c={};a&&(c["Add-Padding"]="true");let p=await g({baseUrl:r,endpoint:e,headers:c,timeoutMs:s,signal:n,userAgent:o,queryParams:{mode:i}});if(p.ok)return p.text();if(p.status===E.status){let u=await p.text();throw new Error(u)}throw new Error(p.statusText)}async function A(e,t={}){let{baseUrl:r,timeoutMs:s,signal:n,userAgent:o,addPadding:a=!1,mode:i="sha1"}=t;return(await B(`/range/${encodeURIComponent(e)}`,{baseUrl:r,timeoutMs:s,signal:n,userAgent:o,addPadding:a,mode:i})).split(` `).filter(Boolean).reduce((u,l)=>{let[R,D]=l.split(":");return u[R]=Number.parseInt(D,10),u},{})}async function L(e,t={}){let[r,s]=await O(e);return(await A(r,t))[s]||0}async function O(e){if(typeof crypto=="object"&&crypto.subtle){let t=new TextEncoder().encode(e),r=await crypto.subtle.digest("SHA-1",t),n=Array.from(new Uint8Array(r)).map(o=>o.toString(16).padStart(2,"0")).join("").toUpperCase();return[n.slice(0,5),n.slice(5)]}throw new Error("The Web Crypto API is not available in this environment.")}async function N(e,t={}){let{apiKey:r,domain:s,truncate:n=!0,baseUrl:o,timeoutMs:a,signal:i,userAgent:c}=t,[p,u]=await Promise.all([h(e,{apiKey:r,domain:s,truncate:n,baseUrl:o,timeoutMs:a,signal:i,userAgent:c}),/^.+@.+$/.test(e)?y(e,{apiKey:r,baseUrl:o,timeoutMs:a,signal:i,userAgent:c}):null]);return{breaches:p,pastes:u}}function _(e,t={}){let{apiKey:r,baseUrl:s,timeoutMs:n,signal:o,userAgent:a}=t,i=`/stealerlogsbyemail/${encodeURIComponent(e)}`;return m(i,{apiKey:r,baseUrl:s,timeoutMs:n,signal:o,userAgent:a})}function H(e,t={}){let{apiKey:r,baseUrl:s,timeoutMs:n,signal:o,userAgent:a}=t,i=`/stealerlogsbyemaildomain/${encodeURIComponent(e)}`;return m(i,{apiKey:r,baseUrl:s,timeoutMs:n,signal:o,userAgent:a})}function k(e,t={}){let{apiKey:r,baseUrl:s,timeoutMs:n,signal:o,userAgent:a}=t,i=`/stealerlogsbywebsitedomain/${encodeURIComponent(e)}`;return m(i,{apiKey:r,baseUrl:s,timeoutMs:n,signal:o,userAgent:a})}function W(e={}){let{apiKey:t,baseUrl:r,timeoutMs:s,signal:n,userAgent:o}=e;return m("/subscribeddomains",{apiKey:t,baseUrl:r,timeoutMs:s,signal:n,userAgent:o})}async function q(e={}){return m("/subscription/status",e)}export{d as RateLimitError,C as breach,h as breachedAccount,T as breachedDomain,F as breaches,$ as dataClasses,j as latestBreach,y as pasteAccount,L as pwnedPassword,A as pwnedPasswordRange,N as search,_ as stealerLogsByEmail,H as stealerLogsByEmailDomain,k as stealerLogsByWebsiteDomain,W as subscribedDomains,q as subscriptionStatus};