UNPKG

hibp

Version:

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

3 lines (2 loc) 4.04 kB
var w="hibp",y="15.0.1";var h={status:400,statusText:"Bad request \u2014 the account does not comply with an acceptable format."},P={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`},g={status:403,statusText:"Forbidden - access denied."};var x={status:404},U={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,n){super(r,n),this.name=this.constructor.name,this.retryAfterSeconds=typeof t=="string"?Number.parseInt(t,10):void 0}};function I(e){return`Request blocked, contact haveibeenpwned.com if this continues (Ray ID: ${e})`}async function p(e,t={}){let{apiKey:r,baseUrl:n="https://haveibeenpwned.com/api/v3",timeoutMs:o,userAgent:a}=t,i={};r&&(i["HIBP-API-Key"]=r),a&&(i["User-Agent"]=a),!a&&typeof navigator>"u"&&(i["User-Agent"]=`${w} ${y}`);let u={headers:i,...o?{signal:AbortSignal.timeout(o)}:{}},m=`${n.replace(/\/$/g,"")}${e}`,s=await fetch(m,u);if(s.ok)return s.json();switch(s.status){case h.status:throw new Error(h.statusText);case P.status:{let c=await s.text();throw new Error(c)}case g.status:{let c=s.headers.get("cf-ray");throw c?new Error(I(c)):new Error(g.statusText)}case x.status:return null;case U.status:{let c=await s.json(),f=s.headers.get("retry-after");throw new d(f,c.message)}default:throw new Error(s.statusText)}}function B(e,t={}){return p(`/breach/${encodeURIComponent(e)}`,t)}function l(e,t={}){let{apiKey:r,domain:n,includeUnverified:o=!0,timeoutMs:a,truncate:i=!0,baseUrl:u,userAgent:m}=t,s=`/breachedaccount/${encodeURIComponent(e)}?`,c=[];return n&&c.push(`domain=${encodeURIComponent(n)}`),o||c.push("includeUnverified=false"),i||c.push("truncateResponse=false"),p(`${s}${c.join("&")}`,{apiKey:r,baseUrl:u,timeoutMs:a,userAgent:m})}function M(e={}){let{domain:t,baseUrl:r,timeoutMs:n,userAgent:o}=e,a="/breaches?",i=[];return t&&i.push(`domain=${encodeURIComponent(t)}`),p(`${a}${i.join("&")}`,{baseUrl:r,timeoutMs:n,userAgent:o})}function T(e={}){return p("/dataclasses",e)}function b(e,t={}){return p(`/pasteaccount/${encodeURIComponent(e)}`,t)}var E={status:400,body:"The hash prefix was not in a valid format"};async function R(e,t={}){let{baseUrl:r="https://api.pwnedpasswords.com",timeoutMs:n,userAgent:o,addPadding:a=!1,mode:i="sha1"}=t,u={headers:{...o?{"User-Agent":o}:{},...a?{"Add-Padding":"true"}:{}},...n?{signal:AbortSignal.timeout(n)}:{}},m=`${r.replace(/\/$/g,"")}${e}?mode=${i}`,s=await fetch(m,u);if(s.ok)return s.text();if(s.status===E.status){let c=await s.text();throw new Error(c)}throw new Error(s.statusText)}async function A(e,t={}){let{baseUrl:r,timeoutMs:n,userAgent:o,addPadding:a=!1,mode:i="sha1"}=t;return(await R(`/range/${encodeURIComponent(e)}`,{baseUrl:r,timeoutMs:n,userAgent:o,addPadding:a,mode:i})).split(` `).filter(Boolean).reduce((s,c)=>{let[f,S]=c.split(":");return s[f]=Number.parseInt(S,10),s},{})}async function v(e,t={}){let[r,n]=await D(e);return(await A(r,t))[n]||0}async function D(e){if(typeof crypto=="object"&&crypto.subtle){let t=new TextEncoder().encode(e),r=await crypto.subtle.digest("SHA-1",t),o=Array.from(new Uint8Array(r)).map(a=>a.toString(16).padStart(2,"0")).join("").toUpperCase();return[o.slice(0,5),o.slice(5)]}throw new Error("The Web Crypto API is not available in this environment.")}async function $(e,t={}){let{apiKey:r,domain:n,truncate:o=!0,baseUrl:a,timeoutMs:i,userAgent:u}=t,[m,s]=await Promise.all([l(e,{apiKey:r,domain:n,truncate:o,baseUrl:a,timeoutMs:i,userAgent:u}),/^.+@.+$/.test(e)?b(e,{apiKey:r,baseUrl:a,timeoutMs:i,userAgent:u}):null]);return{breaches:m,pastes:s}}async function C(e={}){return p("/subscription/status",e)}export{d as RateLimitError,B as breach,l as breachedAccount,M as breaches,T as dataClasses,b as pasteAccount,v as pwnedPassword,A as pwnedPasswordRange,$ as search,C as subscriptionStatus};