source: trip-planner-front/node_modules/bonjour/lib/service.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.0 KB
Line 
1'use strict'
2
3var os = require('os')
4var util = require('util')
5var EventEmitter = require('events').EventEmitter
6var serviceName = require('multicast-dns-service-types')
7var txt = require('dns-txt')()
8
9var TLD = '.local'
10
11module.exports = Service
12
13util.inherits(Service, EventEmitter)
14
15function Service (opts) {
16 if (!opts.name) throw new Error('Required name not given')
17 if (!opts.type) throw new Error('Required type not given')
18 if (!opts.port) throw new Error('Required port not given')
19
20 this.name = opts.name
21 this.protocol = opts.protocol || 'tcp'
22 this.type = serviceName.stringify(opts.type, this.protocol)
23 this.host = opts.host || os.hostname()
24 this.port = opts.port
25 this.fqdn = this.name + '.' + this.type + TLD
26 this.subtypes = opts.subtypes || null
27 this.txt = opts.txt || null
28 this.published = false
29
30 this._activated = false // indicates intent - true: starting/started, false: stopping/stopped
31}
32
33Service.prototype._records = function () {
34 var records = [rr_ptr(this), rr_srv(this), rr_txt(this)]
35
36 var self = this
37 var interfaces = os.networkInterfaces()
38 Object.keys(interfaces).forEach(function (name) {
39 interfaces[name].forEach(function (addr) {
40 if (addr.internal) return
41 if (addr.family === 'IPv4') {
42 records.push(rr_a(self, addr.address))
43 } else {
44 records.push(rr_aaaa(self, addr.address))
45 }
46 })
47 })
48
49 return records
50}
51
52function rr_ptr (service) {
53 return {
54 name: service.type + TLD,
55 type: 'PTR',
56 ttl: 28800,
57 data: service.fqdn
58 }
59}
60
61function rr_srv (service) {
62 return {
63 name: service.fqdn,
64 type: 'SRV',
65 ttl: 120,
66 data: {
67 port: service.port,
68 target: service.host
69 }
70 }
71}
72
73function rr_txt (service) {
74 return {
75 name: service.fqdn,
76 type: 'TXT',
77 ttl: 4500,
78 data: txt.encode(service.txt)
79 }
80}
81
82function rr_a (service, ip) {
83 return {
84 name: service.host,
85 type: 'A',
86 ttl: 120,
87 data: ip
88 }
89}
90
91function rr_aaaa (service, ip) {
92 return {
93 name: service.host,
94 type: 'AAAA',
95 ttl: 120,
96 data: ip
97 }
98}
Note: See TracBrowser for help on using the repository browser.