(function(d,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(d=typeof globalThis<"u"?globalThis:d||self,s(d["universal-doh"]={}))})(this,function(d){"use strict";function s(e){return e.map(n=>String.fromCharCode(...n)).join(".")}function P(e){const n=e.split(".").filter(i=>i.length>0),t=[];for(const i of n){if(i.length<1||i.length>63)throw new Error(`Label "${i}" must be between 1 and 63 characters long`);if(i.startsWith("-")||i.endsWith("-"))throw new Error(`Label "${i}" cannot start or end with a hyphen`);const a=[];for(const r of i){const c=r.charCodeAt(0);if(c>=48&&c<=57||c>=97&&c<=122||c===45)a.push(c);else throw new Error(`Invalid character '${r}' in label "${i}"`)}t.push(a)}return t}function C(){return{id:0,qr:0,opcode:0,aa:0,tc:0,rd:0,ra:0,z:0,rcode:0,qdcount:0,ancount:0,nscount:0,arcount:0,questions:[],answers:[],authorityRecords:[],additionalRecords:[]}}function N(e){const n=C();return n.rd=1,n.qdcount+=e.length,n.questions.push(...e),n.arcount+=1,n.additionalRecords.push({name:[],type:41,maxPayloadSize:65535,extendedRcode:0,version:0,do:0,z:0,rdlength:0,rdata:[]}),n.arcount+=1,n}function T(e){const n=new ArrayBuffer(1024),t=new DataView(n);let i=0;i=j(t,i,e);for(const a of e.questions)i=$(t,i,a);if(e.answers.length>0||e.authorityRecords.length>0)throw new Error("Cannot serialize answers or authority records for query");for(const a of e.additionalRecords)i=S(t,i,a);return i=M(t,i),n.slice(0,i)}function v(e,n,t){for(const i of t){e.setUint8(n++,i.length);for(const a of i)e.setUint8(n++,a)}return e.setUint8(n++,0),n}function S(e,n,t){n=v(e,n,t.name),e.setUint16(n,t.type),n+=2,t.type===41?e.setUint16(n,t.maxPayloadSize):e.setUint16(n,t.class),n+=2;let i;if(t.type===41?(i=t.extendedRcode<<24,i|=t.version<<16,i|=t.do<<15,i|=t.z):i=t.ttl,e.setUint32(n,i),n+=4,e.setUint16(n,t.rdlength),n+=2,t.type===41)for(const a of t.rdata){e.setUint16(n,a.optionCode),n+=2,e.setUint16(n,a.optionLength),n+=2;for(const r of a.optionData)e.setUint8(n++,r)}else for(const a of t.rdata)e.setUint8(n++,a);return n}function M(e,n){const t=n+4,i=Math.ceil(t/128)*128-t,a=Array.from({length:i},()=>Math.floor(Math.random()*255)),r={name:[],type:41,maxPayloadSize:0,extendedRcode:0,version:0,do:0,z:0,rdlength:a.length+4,rdata:[{optionCode:12,optionLength:a.length,optionData:a}]};return n=S(e,n,r),n}function $(e,n,t){return n=v(e,n,t.qname),e.setUint16(n,t.qtype),n+=2,e.setUint16(n,t.qclass),n+=2,n}function j(e,n,t){return e.setUint16(n,t.id),n+=2,e.setUint16(n,(t.qr&1)<<15|(t.opcode&15)<<11|(t.aa&1)<<10|(t.tc&1)<<9|(t.rd&1)<<8|(t.ra&1)<<7|(t.z&7)<<4|t.rcode&15),n+=2,e.setUint16(4,t.qdcount),n+=2,e.setUint16(6,t.ancount),n+=2,e.setUint16(8,t.nscount),n+=2,e.setUint16(10,t.arcount),n+=2,n}function k(e,n=0){const t=new DataView(e),i=C();if(n=F(t,n,i),n=I(t,n,i.qdcount,i.questions),n=A(t,n,i.ancount,i.answers),n=A(t,n,i.nscount,i.authorityRecords),n=O(t,n,i.arcount,i.additionalRecords),n!==e.byteLength)throw new Error(`Unexpected end of message (offset: ${n}, length: ${e.byteLength})`);return i}function I(e,n,t,i){for(let a=0;a>24,t.version=(i&16711680)>>16,t.do=(i&32768)>>15,t.z=i&32767,t.rdlength=e.getUint16(n),n+=2;const a=n+t.rdlength;for(;n20)throw new Error("Too many nested labels");let a=e.getUint8(n++);for(;a!==0;){const r=a>>6;if(r===0){const c=[];for(let o=0;o>15,t.opcode=(i&30720)>>11,t.aa=(i&1024)>>10,t.tc=(i&512)>>9,t.rd=(i&256)>>8,t.ra=(i&128)>>7,t.z=(i&112)>>4,t.rcode=i&15,t.qdcount=e.getUint16(n),n+=2,t.ancount=e.getUint16(n),n+=2,t.nscount=e.getUint16(n),n+=2,t.arcount=e.getUint16(n),n+=2,n}function w(e){return Q(e).split(".").map(a=>_(a)?a:"xn--"+J(a)).join(".").toLowerCase()}function Q(e){return e.replace(/[.。。]/gu,".").replace(/ẞ/gu,"ß").normalize("NFKC").toLowerCase().replace(/[\uFE00-\uFE0F]/gu,"")}function _(e){return Array.from(e).every(D)}function D(e){return e.charCodeAt(0)<128}const h=36,R=1,z=26,H=38,K=700,W=72,G=128;function J(e){let n=G,t=0,i=W;const a=[],r=Array.from(e),c=r.filter(D),o=c.length;let l=o;a.push(...c),o>0&&a.push("-");const x=r.map(y=>y.codePointAt(0)),f=x.length;for(;l=n&&g=i+z?u=z:u=b-i,mp((h-R)*z,2);)e=p(e,h-R),i+=36;return i+p(36*e,e+H)}const Y="0.0.3";function Z(e,n,t){const i=w(n),a=N([{qname:P(i),qtype:parseInt(t,10),qclass:1}]),r=T(a),c=new URL(e);return c.pathname="/dns-query",fetch(c,{method:"POST",mode:"cors",headers:{Accept:"application/dns-message","Content-Type":"application/dns-message"},body:r}).then(o=>o.ok?o.arrayBuffer():o.text().then(l=>{throw new Error(`HTTP ${o.status}: ${l}`)})).then(o=>{const l=k(o);return V(l)}).catch(o=>{throw console.error(o),o})}function V(e){const n={id:e.id,authoritativeAnswer:e.aa===1,truncated:e.tc===1,recursionAvailable:e.ra===1,responseCode:e.rcode===0?"OK":"ERROR",questions:e.questions.map(t=>({name:s(t.qname),type:t.qtype})),answers:e.answers.map(t=>({name:s(t.name),type:t.type,ttl:t.ttl,value:q(t.type,t.rdata)})),authorityRecords:e.authorityRecords.map(t=>({name:s(t.name),type:t.type,ttl:t.ttl,value:q(t.type,t.rdata)})),additionalRecords:e.additionalRecords.map(t=>t.type===41?{name:s(t.name),type:t.type,maxPayloadSize:t.maxPayloadSize,extendedRcode:t.extendedRcode,version:t.version,do:t.do,z:t.z,value:t.rdata}:{name:s(t.name),type:t.type,class:t.class,ttl:t.ttl,value:q(t.type,t.rdata)})};return JSON.stringify(n,null,2)}function q(e,n){switch(e){case 1:return n.join(".");case 28:return n.map(t=>t.toString(16)).join(":");default:return n}}d.resolve=Z,d.version=Y,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});