@collight/dns-sd
Version:
A DNS Service Discovery implementation in modern TypeScript
1 lines • 4.28 kB
Source Map (JSON)
{"version":3,"file":"ServiceType.cjs","sources":["../../../src/utils/ServiceType.ts"],"sourcesContent":["// MARK: ServiceType\n/**\n * Represents a network service type with optional protocol and subtype.\n */\nexport class ServiceType {\n readonly name?: string;\n readonly protocol?: string;\n readonly subtype?: string;\n\n constructor(name?: string, protocol?: string, subtype?: string) {\n this.name = name;\n this.protocol = protocol;\n this.subtype = subtype;\n }\n\n /**\n * Parses a service type string into a ServiceType instance.\n *\n * Expected formats include:\n * - \"_http._tcp\"\n * - \"_printer._sub._http._tcp\" (a subtype \"printer\" of \"_http._tcp\")\n *\n * @param {string} input - The service type string to parse.\n * @returns {ServiceType} The parsed ServiceType instance.\n *\n * @example\n * ServiceType.fromString('_http._tcp');\n * // { name: \"http\", protocol: \"tcp\", subtype: undefined }\n *\n * ServiceType.fromString('_printer._sub._http._tcp');\n * // { name: \"http\", protocol: \"tcp\", subtype: \"printer\" }\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc6763#section-7.1 | RFC 6763 §7.1 Selective Instance Enumeration (Subtypes)}\n */\n static fromString(input: string): ServiceType {\n if (!input) throw new Error('Service type string is empty');\n\n let parts = input.split('.').map(p => p.trim());\n\n // Remove leading underscores from each part\n parts = parts.map(part => (part.startsWith('_') ? part.slice(1) : part));\n\n // Look for 'sub' marker and extract subtype accordingly\n let subtype: string | undefined;\n const subIndex = parts.indexOf('sub');\n\n let name: string | undefined;\n let protocol: string | undefined;\n\n if (subIndex !== -1) {\n // subtype is the part before 'sub'\n if (subIndex === 0) {\n throw new Error('Invalid service type: \"sub\" cannot be first element');\n }\n subtype = parts[subIndex - 1];\n // name and protocol are after 'sub'\n name = parts[subIndex + 1];\n protocol = parts[subIndex + 2];\n } else {\n // no subtype: assume first two parts are name and protocol\n name = parts[0];\n protocol = parts[1];\n }\n\n if (name === undefined || protocol === undefined) {\n throw new Error('Invalid service type format: missing name or protocol');\n }\n\n return new ServiceType(name, protocol, subtype);\n }\n\n /**\n * Serializes the ServiceType instance into a string following DNS-SD format.\n *\n * @returns {string} The serialized service type string.\n *\n * @example\n * const svc = new ServiceType('http', 'tcp');\n * console.log(svc.toString()); // _http._tcp\n *\n * const svcWithSubtype = new ServiceType('http', 'tcp', 'printer');\n * console.log(svcWithSubtype.toString()); // _printer._sub._http._tcp\n */\n toString(): string {\n const parts: string[] = [];\n\n if (this.subtype !== undefined) {\n parts.push(`_${this.subtype}`);\n parts.push('_sub');\n }\n\n if (this.name !== undefined) parts.push(`_${this.name}`);\n if (this.protocol !== undefined) parts.push(`_${this.protocol}`);\n\n return parts.join('.');\n }\n}\n"],"names":["ServiceType","name","protocol","subtype","input","parts","p","part","subIndex"],"mappings":"gFAIO,MAAMA,CAAY,CAKvB,YAAYC,EAAeC,EAAmBC,EAAkB,CAC9D,KAAK,KAAOF,EACZ,KAAK,SAAWC,EAChB,KAAK,QAAUC,CACjB,CAqBA,OAAO,WAAWC,EAA4B,CAC5C,GAAI,CAACA,EAAO,MAAM,IAAI,MAAM,8BAA8B,EAE1D,IAAIC,EAAQD,EAAM,MAAM,GAAG,EAAE,IAAIE,GAAKA,EAAE,MAAM,EAG9CD,EAAQA,EAAM,IAAIE,GAASA,EAAK,WAAW,GAAG,EAAIA,EAAK,MAAM,CAAC,EAAIA,CAAK,EAGvE,IAAIJ,EACJ,MAAMK,EAAWH,EAAM,QAAQ,KAAK,EAEpC,IAAIJ,EACAC,EAEJ,GAAIM,IAAa,GAAI,CAEnB,GAAIA,IAAa,EACf,MAAM,IAAI,MAAM,qDAAqD,EAEvEL,EAAUE,EAAMG,EAAW,CAAC,EAE5BP,EAAOI,EAAMG,EAAW,CAAC,EACzBN,EAAWG,EAAMG,EAAW,CAAC,CAC/B,MAEEP,EAAOI,EAAM,CAAC,EACdH,EAAWG,EAAM,CAAC,EAGpB,GAAIJ,IAAS,QAAaC,IAAa,OACrC,MAAM,IAAI,MAAM,uDAAuD,EAGzE,OAAO,IAAIF,EAAYC,EAAMC,EAAUC,CAAO,CAChD,CAcA,UAAmB,CACjB,MAAME,EAAkB,CAAA,EAExB,OAAI,KAAK,UAAY,SACnBA,EAAM,KAAK,IAAI,KAAK,OAAO,EAAE,EAC7BA,EAAM,KAAK,MAAM,GAGf,KAAK,OAAS,QAAWA,EAAM,KAAK,IAAI,KAAK,IAAI,EAAE,EACnD,KAAK,WAAa,QAAWA,EAAM,KAAK,IAAI,KAAK,QAAQ,EAAE,EAExDA,EAAM,KAAK,GAAG,CACvB,CACF"}