@collight/dns-sd
Version:
A DNS Service Discovery implementation in modern TypeScript
3 lines (2 loc) • 2.21 kB
JavaScript
import{EventEmitter as a}from"events";import i from"os";import"../utils/debug.js";import{encodeTXT as d}from"../utils/dns-utils.js";import"fast-deep-equal";import"multicast-dns";import{ServiceType as p}from"../utils/ServiceType.js";const e=class e extends a{constructor(t){if(super(),this.probe=!0,this.probeAutoResolve=!0,this.started=!1,this.published=!1,this.destroyed=!1,t.port<=0||t.port>65535)throw new Error("Invalid port number");this.protocol=t.protocol??"tcp",this.type=new p(t.type,this.protocol).toString(),this.subtypes=t.subtypes??[],this.name=t.name.split(".").join("-"),this.host=t.host??i.hostname(),this.port=t.port,this.txt=t.txt??{},this.ttl=t.ttl??4500,this.probe=t.probe??!0,this.probeAutoResolve=t.probeAutoResolve??!0,this.disableIPv6=t.disableIPv6??!1,this.fqdn=`${this.name}.${this.type}${e.TLD}`}async start(){this.started||(this.started=!0,await this.registry?.onServiceStart(this))}async stop(){this.started&&(this.started=!1,await this.registry?.onServiceStop(this))}getRecords(){const t=[this.getRecordPTR(),this.getRecordSRV(),this.getRecordTXT(),this.getRecordPTRServiceTypeEnumeration()];for(const r of this.subtypes)t.push(this.getRecordPTRSubtype(r));const o=Object.values(i.networkInterfaces());for(const r of o)for(const s of r??[])if(!(s.internal||s.mac==="00:00:00:00:00:00"))if(s.family==="IPv4")t.push(this.getRecordA(s.address));else{if(this.disableIPv6)continue;t.push(this.getRecordAAAA(s.address))}return t}getRecordPTR(){return{type:"PTR",name:`${this.type}${e.TLD}`,ttl:this.ttl,data:this.fqdn}}getRecordPTRSubtype(t){return{type:"PTR",name:`_${t}._sub.${this.type}${e.TLD}`,ttl:this.ttl,data:`${this.name}.${this.type}${e.TLD}`}}getRecordPTRServiceTypeEnumeration(){return{type:"PTR",name:`_services._dns-sd._udp${e.TLD}`,ttl:this.ttl,data:`${this.type}${e.TLD}`}}getRecordSRV(){return{type:"SRV",name:this.fqdn,ttl:this.ttl,data:{port:this.port,target:this.host}}}getRecordTXT(){return{type:"TXT",name:this.fqdn,ttl:this.ttl,data:d(this.txt)}}getRecordA(t){return{type:"A",name:this.host,ttl:this.ttl,data:t}}getRecordAAAA(t){return{type:"AAAA",name:this.host,ttl:this.ttl,data:t}}};e.TLD=".local";let h=e;export{h as Service};
//# sourceMappingURL=Service.js.map