UNPKG

dns-over-http-resolver

Version:
5 lines (4 loc) 12.7 kB
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.DnsOverHttpResolver = factory()}(typeof self !== 'undefined' ? self : this, function () { "use strict";var DnsOverHttpResolver=(()=>{var _=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var R=Object.prototype.hasOwnProperty;var K=(r,e)=>{for(var t in e)_(r,t,{get:e[t],enumerable:!0})},X=(r,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of k(e))!R.call(r,o)&&o!==t&&_(r,o,{get:()=>e[o],enumerable:!(s=O(e,o))||s.enumerable});return r};var P=r=>X(_({},"__esModule",{value:!0}),r);var oe={};K(oe,{default:()=>ne});var p=class extends Map{#s=0;#e=new Map;#t=new Map;#n;#i;#o;constructor(e={}){if(super(),!(e.maxSize&&e.maxSize>0))throw new TypeError("`maxSize` must be a number greater than 0");if(typeof e.maxAge=="number"&&e.maxAge===0)throw new TypeError("`maxAge` must be a number greater than 0");this.#n=e.maxSize,this.#i=e.maxAge||Number.POSITIVE_INFINITY,this.#o=e.onEviction}get __oldCache(){return this.#t}#c(e){if(typeof this.#o=="function")for(let[t,s]of e)this.#o(t,s.value)}#r(e,t){return typeof t.expiry=="number"&&t.expiry<=Date.now()?(typeof this.#o=="function"&&this.#o(e,t.value),this.delete(e)):!1}#f(e,t){if(this.#r(e,t)===!1)return t.value}#a(e,t){return t.expiry?this.#f(e,t):t.value}#l(e,t){let s=t.get(e);return this.#a(e,s)}#u(e,t){this.#e.set(e,t),this.#s++,this.#s>=this.#n&&(this.#s=0,this.#c(this.#t),this.#t=this.#e,this.#e=new Map)}#d(e,t){this.#t.delete(e),this.#u(e,t)}*#h(){for(let e of this.#t){let[t,s]=e;this.#e.has(t)||this.#r(t,s)===!1&&(yield e)}for(let e of this.#e){let[t,s]=e;this.#r(t,s)===!1&&(yield e)}}get(e){if(this.#e.has(e)){let t=this.#e.get(e);return this.#a(e,t)}if(this.#t.has(e)){let t=this.#t.get(e);if(this.#r(e,t)===!1)return this.#d(e,t),t.value}}set(e,t,{maxAge:s=this.#i}={}){let o=typeof s=="number"&&s!==Number.POSITIVE_INFINITY?Date.now()+s:void 0;return this.#e.has(e)?this.#e.set(e,{value:t,expiry:o}):this.#u(e,{value:t,expiry:o}),this}has(e){return this.#e.has(e)?!this.#r(e,this.#e.get(e)):this.#t.has(e)?!this.#r(e,this.#t.get(e)):!1}peek(e){if(this.#e.has(e))return this.#l(e,this.#e);if(this.#t.has(e))return this.#l(e,this.#t)}delete(e){let t=this.#e.delete(e);return t&&this.#s--,this.#t.delete(e)||t}clear(){this.#e.clear(),this.#t.clear(),this.#s=0}resize(e){if(!(e&&e>0))throw new TypeError("`maxSize` must be a number greater than 0");let t=[...this.#h()],s=t.length-e;s<0?(this.#e=new Map(t),this.#t=new Map,this.#s=t.length):(s>0&&this.#c(t.slice(0,s)),this.#t=new Map(t.slice(s)),this.#e=new Map,this.#s=0),this.#n=e}*keys(){for(let[e]of this)yield e}*values(){for(let[,e]of this)yield e}*[Symbol.iterator](){for(let e of this.#e){let[t,s]=e;this.#r(t,s)===!1&&(yield[t,s.value])}for(let e of this.#t){let[t,s]=e;this.#e.has(t)||this.#r(t,s)===!1&&(yield[t,s.value])}}*entriesDescending(){let e=[...this.#e];for(let t=e.length-1;t>=0;--t){let s=e[t],[o,u]=s;this.#r(o,u)===!1&&(yield[o,u.value])}e=[...this.#t];for(let t=e.length-1;t>=0;--t){let s=e[t],[o,u]=s;this.#e.has(o)||this.#r(o,u)===!1&&(yield[o,u.value])}}*entriesAscending(){for(let[e,t]of this.#h())yield[e,t.value]}get size(){if(!this.#s)return this.#t.size;let e=0;for(let t of this.#t.keys())this.#e.has(t)||e++;return Math.min(this.#s+e,this.#n)}get maxSize(){return this.#n}entries(){return this.entriesAscending()}forEach(e,t=this){for(let[s,o]of this.entriesAscending())e.call(t,o,s,this)}get[Symbol.toStringTag](){return"QuickLRU"}toString(){return`QuickLRU(${this.size}/${this.maxSize})`}[Symbol.for("nodejs.util.inspect.custom")](){return this.toString()}};function V(r,e){try{if(typeof r=="string"&&r.length>0)return U(r);if(typeof r=="number"&&isFinite(r))return e?.long?Q(r):J(r);throw new Error("Value is not a string or number.")}catch(t){let s=Y(t)?`${t.message}. value=${JSON.stringify(r)}`:"An unknown error has occured.";throw new Error(s)}}function U(r){if(r=String(r),r.length>100)throw new Error("Value exceeds the maximum length of 100 characters.");let e=/^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(r);if(!e)return NaN;let t=parseFloat(e[1]),s=(e[2]||"ms").toLowerCase();switch(s){case"years":case"year":case"yrs":case"yr":case"y":return t*315576e5;case"weeks":case"week":case"w":return t*6048e5;case"days":case"day":case"d":return t*864e5;case"hours":case"hour":case"hrs":case"hr":case"h":return t*36e5;case"minutes":case"minute":case"mins":case"min":case"m":return t*6e4;case"seconds":case"second":case"secs":case"sec":case"s":return t*1e3;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return t;default:throw new Error(`The unit ${s} was matched, but no matching case exists.`)}}var v=V;function J(r){let e=Math.abs(r);return e>=864e5?`${Math.round(r/864e5)}d`:e>=36e5?`${Math.round(r/36e5)}h`:e>=6e4?`${Math.round(r/6e4)}m`:e>=1e3?`${Math.round(r/1e3)}s`:`${r}ms`}function Q(r){let e=Math.abs(r);return e>=864e5?F(r,e,864e5,"day"):e>=36e5?F(r,e,36e5,"hour"):e>=6e4?F(r,e,6e4,"minute"):e>=1e3?F(r,e,1e3,"second"):`${r} ms`}function F(r,e,t,s){let o=e>=t*1.5;return`${Math.round(r/t)} ${s}${o?"s":""}`}function Y(r){return typeof r=="object"&&r!==null&&"message"in r}function $(r){t.debug=t,t.default=t,t.coerce=f,t.disable=u,t.enable=o,t.enabled=l,t.humanize=v,t.destroy=C,Object.keys(r).forEach(n=>{t[n]=r[n]}),t.names=[],t.skips=[],t.formatters={};function e(n){let i=0;for(let a=0;a<n.length;a++)i=(i<<5)-i+n.charCodeAt(a),i|=0;return t.colors[Math.abs(i)%t.colors.length]}t.selectColor=e;function t(n){let i,a=null,w,M;function d(...h){if(!d.enabled)return;let g=d,b=Number(new Date),j=b-(i||b);g.diff=j,g.prev=i,g.curr=b,i=b,h[0]=t.coerce(h[0]),typeof h[0]!="string"&&h.unshift("%O");let y=0;h[0]=h[0].replace(/%([a-zA-Z%])/g,(E,q)=>{if(E==="%%")return"%";y++;let I=t.formatters[q];if(typeof I=="function"){let D=h[y];E=I.call(g,D),h.splice(y,1),y--}return E}),t.formatArgs.call(g,h),(g.log||t.log).apply(g,h)}return d.namespace=n,d.useColors=t.useColors(),d.color=t.selectColor(n),d.extend=s,d.destroy=t.destroy,Object.defineProperty(d,"enabled",{enumerable:!0,configurable:!1,get:()=>a!==null?a:(w!==t.namespaces&&(w=t.namespaces,M=t.enabled(n)),M),set:h=>{a=h}}),typeof t.init=="function"&&t.init(d),d}function s(n,i){let a=t(this.namespace+(typeof i>"u"?":":i)+n);return a.log=this.log,a}function o(n){t.save(n),t.namespaces=n,t.names=[],t.skips=[];let i,a=(typeof n=="string"?n:"").split(/[\s,]+/),w=a.length;for(i=0;i<w;i++)a[i]&&(n=a[i].replace(/\*/g,".*?"),n[0]==="-"?t.skips.push(new RegExp("^"+n.substr(1)+"$")):t.names.push(new RegExp("^"+n+"$")))}function u(){let n=[...t.names.map(c),...t.skips.map(c).map(i=>"-"+i)].join(",");return t.enable(""),n}function l(n){if(n[n.length-1]==="*")return!0;let i,a;for(i=0,a=t.skips.length;i<a;i++)if(t.skips[i].test(n))return!1;for(i=0,a=t.names.length;i<a;i++)if(t.names[i].test(n))return!0;return!1}function c(n){return n.toString().substring(2,n.toString().length-2).replace(/\.\*\?$/,"*")}function f(n){return n instanceof Error?n.stack??n.message:n}function C(){console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.")}return t.setupFormatters(t.formatters),t.enable(t.load()),t}var x=te(),Z=["#0000CC","#0000FF","#0033CC","#0033FF","#0066CC","#0066FF","#0099CC","#0099FF","#00CC00","#00CC33","#00CC66","#00CC99","#00CCCC","#00CCFF","#3300CC","#3300FF","#3333CC","#3333FF","#3366CC","#3366FF","#3399CC","#3399FF","#33CC00","#33CC33","#33CC66","#33CC99","#33CCCC","#33CCFF","#6600CC","#6600FF","#6633CC","#6633FF","#66CC00","#66CC33","#9900CC","#9900FF","#9933CC","#9933FF","#99CC00","#99CC33","#CC0000","#CC0033","#CC0066","#CC0099","#CC00CC","#CC00FF","#CC3300","#CC3333","#CC3366","#CC3399","#CC33CC","#CC33FF","#CC6600","#CC6633","#CC9900","#CC9933","#CCCC00","#CCCC33","#FF0000","#FF0033","#FF0066","#FF0099","#FF00CC","#FF00FF","#FF3300","#FF3333","#FF3366","#FF3399","#FF33CC","#FF33FF","#FF6600","#FF6633","#FF9900","#FF9933","#FFCC00","#FFCC33"];function B(){return typeof window<"u"&&window.process&&(window.process.type==="renderer"||window.process.__nwjs)?!0:typeof navigator<"u"&&navigator.userAgent?.toLowerCase().match(/(edge|trident)\/(\d+)/)!=null?!1:typeof document<"u"&&document.documentElement?.style?.WebkitAppearance||typeof window<"u"&&window.console&&(window.console.firebug||window.console.exception&&window.console.table)||typeof navigator<"u"&&navigator.userAgent?.toLowerCase().match(/firefox\/(\d+)/)!=null&&parseInt(RegExp.$1,10)>=31||typeof navigator<"u"&&navigator.userAgent?.toLowerCase().match(/applewebkit\/(\d+)/)}function G(r){if(r[0]=(this.useColors?"%c":"")+this.namespace+(this.useColors?" %c":" ")+r[0]+(this.useColors?"%c ":" ")+"+"+v(this.diff),!this.useColors)return;let e="color: "+this.color;r.splice(1,0,e,"color: inherit");let t=0,s=0;r[0].replace(/%[a-zA-Z%]/g,o=>{o!=="%%"&&(t++,o==="%c"&&(s=t))}),r.splice(s,0,e)}var H=console.debug??console.log??(()=>{});function W(r){try{r?x?.setItem("debug",r):x?.removeItem("debug")}catch{}}function ee(){let r;try{r=x?.getItem("debug")}catch{}return!r&&typeof globalThis.process<"u"&&"env"in globalThis.process&&(r=globalThis.process.env.DEBUG),r}function te(){try{return localStorage}catch{}}function re(r){r.j=function(e){try{return JSON.stringify(e)}catch(t){return"[UnexpectedJSONParseError]: "+t.message}}}var N=$({formatArgs:G,save:W,load:ee,useColors:B,setupFormatters:re,colors:Z,storage:x,log:H});var T=N;function A(r,e,t){return`${r}?name=${e}&type=${t}`}async function z(r,e){return await(await fetch(r,{headers:new Headers({accept:"application/dns-json"}),signal:e})).json()}function m(r,e){return`${e}_${r}`}var S=Object.assign(T("dns-over-http-resolver"),{error:T("dns-over-http-resolver:error")}),L=class{_cache;_TXTcache;_servers;_request;_abortControllers;constructor(e={}){this._cache=new p({maxSize:e?.maxCache??100}),this._TXTcache=new p({maxSize:e?.maxCache??100}),this._servers=["https://cloudflare-dns.com/dns-query","https://dns.google/resolve"],this._request=e.request??z,this._abortControllers=[]}cancel(){this._abortControllers.forEach(e=>{e.abort()})}getServers(){return this._servers}_getShuffledServers(){let e=[...this._servers];for(let t=e.length-1;t>0;t--){let s=Math.floor(Math.random()*t),o=e[t];e[t]=e[s],e[s]=o}return e}setServers(e){this._servers=e}async resolve(e,t="A"){switch(t){case"A":return this.resolve4(e);case"AAAA":return this.resolve6(e);case"TXT":return this.resolveTxt(e);default:throw new Error(`${t} is not supported`)}}async resolve4(e){let t="A",s=this._cache.get(m(e,t));if(s!=null)return s;let o=!1;for(let u of this._getShuffledServers()){let l=new AbortController;this._abortControllers.push(l);try{let c=await this._request(A(u,e,t),l.signal),f=c.Answer.map(n=>n.data),C=Math.min(...c.Answer.map(n=>n.TTL));return this._cache.set(m(e,t),f,{maxAge:C}),f}catch{l.signal.aborted&&(o=!0),S.error(`${u} could not resolve ${e} record ${t}`)}finally{this._abortControllers=this._abortControllers.filter(c=>c!==l)}}throw o?Object.assign(new Error("queryA ECANCELLED"),{code:"ECANCELLED"}):new Error(`Could not resolve ${e} record ${t}`)}async resolve6(e){let t="AAAA",s=this._cache.get(m(e,t));if(s!=null)return s;let o=!1;for(let u of this._getShuffledServers()){let l=new AbortController;this._abortControllers.push(l);try{let c=await this._request(A(u,e,t),l.signal),f=c.Answer.map(n=>n.data),C=Math.min(...c.Answer.map(n=>n.TTL));return this._cache.set(m(e,t),f,{maxAge:C}),f}catch{l.signal.aborted&&(o=!0),S.error(`${u} could not resolve ${e} record ${t}`)}finally{this._abortControllers=this._abortControllers.filter(c=>c!==l)}}throw o?Object.assign(new Error("queryAaaa ECANCELLED"),{code:"ECANCELLED"}):new Error(`Could not resolve ${e} record ${t}`)}async resolveTxt(e){let t="TXT",s=this._TXTcache.get(m(e,t));if(s!=null)return s;let o=!1;for(let u of this._getShuffledServers()){let l=new AbortController;this._abortControllers.push(l);try{let c=await this._request(A(u,e,t),l.signal),f=c.Answer.map(n=>[n.data.replace(/['"]+/g,"")]),C=Math.min(...c.Answer.map(n=>n.TTL));return this._TXTcache.set(m(e,t),f,{maxAge:C}),f}catch{l.signal.aborted&&(o=!0),S.error(`${u} could not resolve ${e} record ${t}`)}finally{this._abortControllers=this._abortControllers.filter(c=>c!==l)}}throw o?Object.assign(new Error("queryTxt ECANCELLED"),{code:"ECANCELLED"}):new Error(`Could not resolve ${e} record ${t}`)}clearCache(){this._cache.clear(),this._TXTcache.clear()}},ne=L;return P(oe);})(); return DnsOverHttpResolver})); //# sourceMappingURL=index.min.js.map