matterbridge 3.1.6 → 3.1.7-dev-20250723-8e073ce

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (241) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README-DOCKER.md +3 -3
  3. package/bin/mb_coap.js +2 -0
  4. package/bin/mb_mdns.js +2 -0
  5. package/dist/cli.js +2 -91
  6. package/dist/cliEmitter.js +0 -30
  7. package/dist/clusters/export.js +0 -2
  8. package/dist/defaultConfigSchema.js +0 -24
  9. package/dist/deviceManager.js +1 -94
  10. package/dist/devices/batteryStorage.js +1 -48
  11. package/dist/devices/dishwasher.js +3 -78
  12. package/dist/devices/evse.js +10 -74
  13. package/dist/devices/export.js +0 -2
  14. package/dist/devices/extractorHood.js +0 -42
  15. package/dist/devices/heatPump.js +2 -50
  16. package/dist/devices/laundryDryer.js +6 -83
  17. package/dist/devices/laundryWasher.js +7 -91
  18. package/dist/devices/roboticVacuumCleaner.js +7 -93
  19. package/dist/devices/solarPower.js +0 -38
  20. package/dist/devices/waterHeater.js +2 -82
  21. package/dist/dgram/coap.js +252 -0
  22. package/dist/dgram/dgram.js +2 -113
  23. package/dist/dgram/mb_coap.js +52 -0
  24. package/dist/dgram/mb_mdns.js +60 -0
  25. package/dist/dgram/mdns.js +135 -380
  26. package/dist/dgram/multicast.js +7 -64
  27. package/dist/dgram/unicast.js +0 -54
  28. package/dist/frontend.js +21 -435
  29. package/dist/globalMatterbridge.js +0 -47
  30. package/dist/helpers.js +0 -53
  31. package/dist/index.js +1 -30
  32. package/dist/logger/export.js +0 -1
  33. package/dist/matter/behaviors.js +0 -2
  34. package/dist/matter/clusters.js +0 -2
  35. package/dist/matter/devices.js +0 -2
  36. package/dist/matter/endpoints.js +0 -2
  37. package/dist/matter/export.js +0 -3
  38. package/dist/matter/types.js +0 -3
  39. package/dist/matterbridge.js +50 -804
  40. package/dist/matterbridgeAccessoryPlatform.js +0 -36
  41. package/dist/matterbridgeBehaviors.js +5 -65
  42. package/dist/matterbridgeDeviceTypes.js +15 -579
  43. package/dist/matterbridgeDynamicPlatform.js +0 -36
  44. package/dist/matterbridgeEndpoint.js +51 -1191
  45. package/dist/matterbridgeEndpointHelpers.js +13 -322
  46. package/dist/matterbridgePlatform.js +0 -233
  47. package/dist/matterbridgeTypes.js +0 -25
  48. package/dist/pluginManager.js +3 -249
  49. package/dist/shelly.js +7 -168
  50. package/dist/storage/export.js +0 -1
  51. package/dist/update.js +0 -54
  52. package/dist/utils/colorUtils.js +2 -263
  53. package/dist/utils/commandLine.js +0 -54
  54. package/dist/utils/copyDirectory.js +1 -38
  55. package/dist/utils/createDirectory.js +0 -33
  56. package/dist/utils/createZip.js +2 -47
  57. package/dist/utils/deepCopy.js +0 -39
  58. package/dist/utils/deepEqual.js +1 -72
  59. package/dist/utils/error.js +0 -41
  60. package/dist/utils/export.js +0 -1
  61. package/dist/utils/hex.js +0 -58
  62. package/dist/utils/isvalid.js +0 -101
  63. package/dist/utils/network.js +5 -81
  64. package/dist/utils/spawn.js +0 -40
  65. package/dist/utils/wait.js +9 -62
  66. package/npm-shrinkwrap.json +5 -3
  67. package/package.json +4 -3
  68. package/dist/cli.d.ts +0 -26
  69. package/dist/cli.d.ts.map +0 -1
  70. package/dist/cli.js.map +0 -1
  71. package/dist/cliEmitter.d.ts +0 -34
  72. package/dist/cliEmitter.d.ts.map +0 -1
  73. package/dist/cliEmitter.js.map +0 -1
  74. package/dist/clusters/export.d.ts +0 -2
  75. package/dist/clusters/export.d.ts.map +0 -1
  76. package/dist/clusters/export.js.map +0 -1
  77. package/dist/defaultConfigSchema.d.ts +0 -28
  78. package/dist/defaultConfigSchema.d.ts.map +0 -1
  79. package/dist/defaultConfigSchema.js.map +0 -1
  80. package/dist/deviceManager.d.ts +0 -112
  81. package/dist/deviceManager.d.ts.map +0 -1
  82. package/dist/deviceManager.js.map +0 -1
  83. package/dist/devices/batteryStorage.d.ts +0 -48
  84. package/dist/devices/batteryStorage.d.ts.map +0 -1
  85. package/dist/devices/batteryStorage.js.map +0 -1
  86. package/dist/devices/dishwasher.d.ts +0 -91
  87. package/dist/devices/dishwasher.d.ts.map +0 -1
  88. package/dist/devices/dishwasher.js.map +0 -1
  89. package/dist/devices/evse.d.ts +0 -75
  90. package/dist/devices/evse.d.ts.map +0 -1
  91. package/dist/devices/evse.js.map +0 -1
  92. package/dist/devices/export.d.ts +0 -11
  93. package/dist/devices/export.d.ts.map +0 -1
  94. package/dist/devices/export.js.map +0 -1
  95. package/dist/devices/extractorHood.d.ts +0 -46
  96. package/dist/devices/extractorHood.d.ts.map +0 -1
  97. package/dist/devices/extractorHood.js.map +0 -1
  98. package/dist/devices/heatPump.d.ts +0 -47
  99. package/dist/devices/heatPump.d.ts.map +0 -1
  100. package/dist/devices/heatPump.js.map +0 -1
  101. package/dist/devices/laundryDryer.d.ts +0 -87
  102. package/dist/devices/laundryDryer.d.ts.map +0 -1
  103. package/dist/devices/laundryDryer.js.map +0 -1
  104. package/dist/devices/laundryWasher.d.ts +0 -242
  105. package/dist/devices/laundryWasher.d.ts.map +0 -1
  106. package/dist/devices/laundryWasher.js.map +0 -1
  107. package/dist/devices/roboticVacuumCleaner.d.ts +0 -112
  108. package/dist/devices/roboticVacuumCleaner.d.ts.map +0 -1
  109. package/dist/devices/roboticVacuumCleaner.js.map +0 -1
  110. package/dist/devices/solarPower.d.ts +0 -40
  111. package/dist/devices/solarPower.d.ts.map +0 -1
  112. package/dist/devices/solarPower.js.map +0 -1
  113. package/dist/devices/waterHeater.d.ts +0 -111
  114. package/dist/devices/waterHeater.d.ts.map +0 -1
  115. package/dist/devices/waterHeater.js.map +0 -1
  116. package/dist/dgram/dgram.d.ts +0 -140
  117. package/dist/dgram/dgram.d.ts.map +0 -1
  118. package/dist/dgram/dgram.js.map +0 -1
  119. package/dist/dgram/mdns.d.ts +0 -288
  120. package/dist/dgram/mdns.d.ts.map +0 -1
  121. package/dist/dgram/mdns.js.map +0 -1
  122. package/dist/dgram/multicast.d.ts +0 -65
  123. package/dist/dgram/multicast.d.ts.map +0 -1
  124. package/dist/dgram/multicast.js.map +0 -1
  125. package/dist/dgram/unicast.d.ts +0 -56
  126. package/dist/dgram/unicast.d.ts.map +0 -1
  127. package/dist/dgram/unicast.js.map +0 -1
  128. package/dist/frontend.d.ts +0 -304
  129. package/dist/frontend.d.ts.map +0 -1
  130. package/dist/frontend.js.map +0 -1
  131. package/dist/globalMatterbridge.d.ts +0 -59
  132. package/dist/globalMatterbridge.d.ts.map +0 -1
  133. package/dist/globalMatterbridge.js.map +0 -1
  134. package/dist/helpers.d.ts +0 -48
  135. package/dist/helpers.d.ts.map +0 -1
  136. package/dist/helpers.js.map +0 -1
  137. package/dist/index.d.ts +0 -33
  138. package/dist/index.d.ts.map +0 -1
  139. package/dist/index.js.map +0 -1
  140. package/dist/logger/export.d.ts +0 -2
  141. package/dist/logger/export.d.ts.map +0 -1
  142. package/dist/logger/export.js.map +0 -1
  143. package/dist/matter/behaviors.d.ts +0 -2
  144. package/dist/matter/behaviors.d.ts.map +0 -1
  145. package/dist/matter/behaviors.js.map +0 -1
  146. package/dist/matter/clusters.d.ts +0 -2
  147. package/dist/matter/clusters.d.ts.map +0 -1
  148. package/dist/matter/clusters.js.map +0 -1
  149. package/dist/matter/devices.d.ts +0 -2
  150. package/dist/matter/devices.d.ts.map +0 -1
  151. package/dist/matter/devices.js.map +0 -1
  152. package/dist/matter/endpoints.d.ts +0 -2
  153. package/dist/matter/endpoints.d.ts.map +0 -1
  154. package/dist/matter/endpoints.js.map +0 -1
  155. package/dist/matter/export.d.ts +0 -5
  156. package/dist/matter/export.d.ts.map +0 -1
  157. package/dist/matter/export.js.map +0 -1
  158. package/dist/matter/types.d.ts +0 -3
  159. package/dist/matter/types.d.ts.map +0 -1
  160. package/dist/matter/types.js.map +0 -1
  161. package/dist/matterbridge.d.ts +0 -460
  162. package/dist/matterbridge.d.ts.map +0 -1
  163. package/dist/matterbridge.js.map +0 -1
  164. package/dist/matterbridgeAccessoryPlatform.d.ts +0 -42
  165. package/dist/matterbridgeAccessoryPlatform.d.ts.map +0 -1
  166. package/dist/matterbridgeAccessoryPlatform.js.map +0 -1
  167. package/dist/matterbridgeBehaviors.d.ts +0 -1351
  168. package/dist/matterbridgeBehaviors.d.ts.map +0 -1
  169. package/dist/matterbridgeBehaviors.js.map +0 -1
  170. package/dist/matterbridgeDeviceTypes.d.ts +0 -709
  171. package/dist/matterbridgeDeviceTypes.d.ts.map +0 -1
  172. package/dist/matterbridgeDeviceTypes.js.map +0 -1
  173. package/dist/matterbridgeDynamicPlatform.d.ts +0 -42
  174. package/dist/matterbridgeDynamicPlatform.d.ts.map +0 -1
  175. package/dist/matterbridgeDynamicPlatform.js.map +0 -1
  176. package/dist/matterbridgeEndpoint.d.ts +0 -1328
  177. package/dist/matterbridgeEndpoint.d.ts.map +0 -1
  178. package/dist/matterbridgeEndpoint.js.map +0 -1
  179. package/dist/matterbridgeEndpointHelpers.d.ts +0 -3198
  180. package/dist/matterbridgeEndpointHelpers.d.ts.map +0 -1
  181. package/dist/matterbridgeEndpointHelpers.js.map +0 -1
  182. package/dist/matterbridgePlatform.d.ts +0 -310
  183. package/dist/matterbridgePlatform.d.ts.map +0 -1
  184. package/dist/matterbridgePlatform.js.map +0 -1
  185. package/dist/matterbridgeTypes.d.ts +0 -195
  186. package/dist/matterbridgeTypes.d.ts.map +0 -1
  187. package/dist/matterbridgeTypes.js.map +0 -1
  188. package/dist/pluginManager.d.ts +0 -270
  189. package/dist/pluginManager.d.ts.map +0 -1
  190. package/dist/pluginManager.js.map +0 -1
  191. package/dist/shelly.d.ts +0 -174
  192. package/dist/shelly.d.ts.map +0 -1
  193. package/dist/shelly.js.map +0 -1
  194. package/dist/storage/export.d.ts +0 -2
  195. package/dist/storage/export.d.ts.map +0 -1
  196. package/dist/storage/export.js.map +0 -1
  197. package/dist/update.d.ts +0 -59
  198. package/dist/update.d.ts.map +0 -1
  199. package/dist/update.js.map +0 -1
  200. package/dist/utils/colorUtils.d.ts +0 -117
  201. package/dist/utils/colorUtils.d.ts.map +0 -1
  202. package/dist/utils/colorUtils.js.map +0 -1
  203. package/dist/utils/commandLine.d.ts +0 -59
  204. package/dist/utils/commandLine.d.ts.map +0 -1
  205. package/dist/utils/commandLine.js.map +0 -1
  206. package/dist/utils/copyDirectory.d.ts +0 -33
  207. package/dist/utils/copyDirectory.d.ts.map +0 -1
  208. package/dist/utils/copyDirectory.js.map +0 -1
  209. package/dist/utils/createDirectory.d.ts +0 -34
  210. package/dist/utils/createDirectory.d.ts.map +0 -1
  211. package/dist/utils/createDirectory.js.map +0 -1
  212. package/dist/utils/createZip.d.ts +0 -39
  213. package/dist/utils/createZip.d.ts.map +0 -1
  214. package/dist/utils/createZip.js.map +0 -1
  215. package/dist/utils/deepCopy.d.ts +0 -32
  216. package/dist/utils/deepCopy.d.ts.map +0 -1
  217. package/dist/utils/deepCopy.js.map +0 -1
  218. package/dist/utils/deepEqual.d.ts +0 -54
  219. package/dist/utils/deepEqual.d.ts.map +0 -1
  220. package/dist/utils/deepEqual.js.map +0 -1
  221. package/dist/utils/error.d.ts +0 -44
  222. package/dist/utils/error.d.ts.map +0 -1
  223. package/dist/utils/error.js.map +0 -1
  224. package/dist/utils/export.d.ts +0 -12
  225. package/dist/utils/export.d.ts.map +0 -1
  226. package/dist/utils/export.js.map +0 -1
  227. package/dist/utils/hex.d.ts +0 -49
  228. package/dist/utils/hex.d.ts.map +0 -1
  229. package/dist/utils/hex.js.map +0 -1
  230. package/dist/utils/isvalid.d.ts +0 -103
  231. package/dist/utils/isvalid.d.ts.map +0 -1
  232. package/dist/utils/isvalid.js.map +0 -1
  233. package/dist/utils/network.d.ts +0 -74
  234. package/dist/utils/network.d.ts.map +0 -1
  235. package/dist/utils/network.js.map +0 -1
  236. package/dist/utils/spawn.d.ts +0 -33
  237. package/dist/utils/spawn.d.ts.map +0 -1
  238. package/dist/utils/spawn.js.map +0 -1
  239. package/dist/utils/wait.d.ts +0 -56
  240. package/dist/utils/wait.d.ts.map +0 -1
  241. package/dist/utils/wait.js.map +0 -1
@@ -1,29 +1,5 @@
1
- /**
2
- * @description This file contains the class Multicast.
3
- * @file multicast.ts
4
- * @author Luca Liguori
5
- * @created 2025-03-22
6
- * @version 1.0.0
7
- * @license Apache-2.0
8
- *
9
- * Copyright 2025, 2026, 2027 Luca Liguori.
10
- *
11
- * Licensed under the Apache License, Version 2.0 (the "License");
12
- * you may not use this file except in compliance with the License.
13
- * You may obtain a copy of the License at
14
- *
15
- * http://www.apache.org/licenses/LICENSE-2.0
16
- *
17
- * Unless required by applicable law or agreed to in writing, software
18
- * distributed under the License is distributed on an "AS IS" BASIS,
19
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
- * See the License for the specific language governing permissions and
21
- * limitations under the License.
22
- */
23
1
  import os from 'node:os';
24
- // AnsiLogger imports
25
2
  import { BLUE, CYAN, db, RED, YELLOW } from 'node-ansi-logger';
26
- // Net imports
27
3
  import { Dgram } from './dgram.js';
28
4
  export const MDNS_MULTICAST_IPV4_ADDRESS = '224.0.0.251';
29
5
  export const MDNS_MULTICAST_IPV6_ADDRESS = 'ff02::fb';
@@ -31,34 +7,16 @@ export const MDNS_MULTICAST_PORT = 5353;
31
7
  export const COAP_MULTICAST_IPV4_ADDRESS = '224.0.1.187';
32
8
  export const COAP_MULTICAST_IPV6_ADDRESS = 'ff02::fd';
33
9
  export const COAP_MULTICAST_PORT = 5683;
34
- /**
35
- * This class implements a multicast dgram socket.
36
- */
37
10
  export class Multicast extends Dgram {
38
11
  multicastAddress;
39
12
  multicastPort;
40
13
  joinedInterfaces = [];
41
- /**
42
- * Creates an instance of Multicast.
43
- *
44
- * @param {string} name - The name of the socket.
45
- * @param {string} multicastAddress - The multicast address to join.
46
- * @param {number} multicastPort - The port number to bind to.
47
- * @param {'udp4' | 'udp6'} socketType - The type of the socket (IPv4 or IPv6).
48
- * @param {boolean | undefined} reuseAddr - Whether to allow address reuse.
49
- * @param {string} [interfaceName] - The name of the network interface to bind to.
50
- * @param {string} [interfaceAddress] - The address of the network interface to bind to.
51
- */
52
14
  constructor(name, multicastAddress, multicastPort, socketType, reuseAddr = true, interfaceName, interfaceAddress) {
53
15
  super(name, socketType, reuseAddr, interfaceName, interfaceAddress);
54
16
  this.multicastAddress = multicastAddress;
55
17
  this.multicastPort = multicastPort;
56
18
  }
57
- /**
58
- * Starts the dgram multicast socket.
59
- */
60
19
  start() {
61
- // Get the local ipv4 or ipv6 address to bind to.
62
20
  if (this.socketType === 'udp4') {
63
21
  this.log.debug(`Starting ipv4 dgram multicast socket...`);
64
22
  this.interfaceAddress = this.interfaceAddress ?? this.getIpv4InterfaceAddress(this.interfaceName);
@@ -74,59 +32,49 @@ export class Multicast extends Dgram {
74
32
  this.emit('bound', address);
75
33
  });
76
34
  }
77
- /**
78
- * Event handler for the 'listening' event. This event is emitted when the socket starts listening for datagram messages.
79
- *
80
- * @param {AddressInfo} [address] - The address info.
81
- */
82
35
  onListening(address) {
83
36
  this.log.debug(`Dgram multicast socket listening on ${BLUE}${address.family}${db} ${BLUE}${address.address}${db}:${BLUE}${address.port}${db}`);
84
37
  this.socket.setBroadcast(true);
85
38
  this.log.debug(`Dgram multicast socket broadcast enabled`);
39
+ this.socket.setTTL(255);
40
+ this.log.debug(`Dgram multicast socket TTL set to 255`);
86
41
  this.socket.setMulticastTTL(255);
87
42
  this.log.debug(`Dgram multicast socket multicast TTL set to 255`);
88
43
  this.socket.setMulticastLoopback(true);
89
44
  this.log.debug(`Dgram multicast socket multicast loopback enabled`);
90
- // Find the correct network interfaces and join the multicast group on each interface. For IPv6, we will use the scope ID if available. If not available, we will use the Unique Local Address.
91
45
  Object.entries(os.networkInterfaces()).forEach(([name, interfaces]) => {
92
46
  this.log.debug(`Dgram multicast socket processing interface ${CYAN}${name}${db}`);
93
47
  if (!interfaces)
94
48
  return;
95
49
  if (this.interfaceName && name !== this.interfaceName)
96
- return; // Only process the specified interface
50
+ return;
97
51
  let iface;
98
52
  let membershipInterface;
99
- // Find the first ipv4 interface
100
53
  const ifaceIpv4 = interfaces.find((iface) => iface.family === 'IPv4' && this.socketType === 'udp4');
101
54
  if (ifaceIpv4) {
102
55
  iface = ifaceIpv4;
103
56
  membershipInterface = ifaceIpv4.address;
104
57
  }
105
- // Find the first IPv6 Address
106
58
  const ifaceIpv6 = interfaces.find((iface) => iface.family === 'IPv6' && this.socketType === 'udp6');
107
59
  if (ifaceIpv6) {
108
60
  iface = ifaceIpv6;
109
- membershipInterface = ifaceIpv6.address;
61
+ membershipInterface = ifaceIpv6.address + (ifaceIpv6.scopeid !== undefined ? (process.platform === 'win32' ? '%' + String(ifaceIpv6.scopeid) : '%' + name) : '');
110
62
  }
111
- // Find the first Unique Local IPv6 Address (ULA)
112
63
  const ifaceUla = interfaces.find((iface) => iface.family === 'IPv6' && this.socketType === 'udp6' && iface.address.startsWith('fd'));
113
64
  if (ifaceUla) {
114
65
  iface = ifaceUla;
115
- membershipInterface = ifaceUla.address + (this.socketType === 'udp6' && ifaceUla.scopeid ? (process.platform === 'win32' ? '%' + String(ifaceUla.scopeid) : '%' + name) : '');
66
+ membershipInterface = ifaceUla.address + (ifaceUla.scopeid !== undefined ? (process.platform === 'win32' ? '%' + String(ifaceUla.scopeid) : '%' + name) : '');
116
67
  }
117
- // Find the first Unique Local IPv6 Address (ULA) with prefix length /64 (netmask ffff:ffff:ffff:ffff::)
118
68
  const ifaceUla64 = interfaces.find((iface) => iface.family === 'IPv6' && this.socketType === 'udp6' && iface.address.startsWith('fd') && iface.netmask === 'ffff:ffff:ffff:ffff::');
119
69
  if (ifaceUla64) {
120
70
  iface = ifaceUla64;
121
- membershipInterface = ifaceUla64.address + (this.socketType === 'udp6' && ifaceUla64.scopeid ? (process.platform === 'win32' ? '%' + String(ifaceUla64.scopeid) : '%' + name) : '');
71
+ membershipInterface = ifaceUla64.address + (ifaceUla64.scopeid !== undefined ? (process.platform === 'win32' ? '%' + String(ifaceUla64.scopeid) : '%' + name) : '');
122
72
  }
123
- // Find the first Link-local IPv6 Address
124
73
  const ifaceLinkLocal = interfaces.find((iface) => iface.family === 'IPv6' && this.socketType === 'udp6' && iface.address.startsWith('fe80'));
125
74
  if (ifaceLinkLocal) {
126
75
  iface = ifaceLinkLocal;
127
- membershipInterface = ifaceLinkLocal.address + (this.socketType === 'udp6' && ifaceLinkLocal.scopeid ? (process.platform === 'win32' ? '%' + String(ifaceLinkLocal.scopeid) : '%' + name) : '');
76
+ membershipInterface = ifaceLinkLocal.address + (ifaceLinkLocal.scopeid !== undefined ? (process.platform === 'win32' ? '%' + String(ifaceLinkLocal.scopeid) : '%' + name) : '');
128
77
  }
129
- // Add the interface address to the multicast group
130
78
  if (iface && membershipInterface) {
131
79
  try {
132
80
  this.socket.addMembership(this.multicastAddress, membershipInterface);
@@ -138,7 +86,6 @@ export class Multicast extends Dgram {
138
86
  }
139
87
  }
140
88
  });
141
- // If the interfaceAddress is '::', we need to set the default outgoing multicast interface to '::' + the scope ID for all interfaces address.
142
89
  let interfaceAddress = this.interfaceAddress;
143
90
  if (this.socketType === 'udp6' && this.interfaceAddress === '::') {
144
91
  interfaceAddress = '::' + this.getIpv6ScopeIdForAllInterfacesAddress();
@@ -148,9 +95,6 @@ export class Multicast extends Dgram {
148
95
  this.log.debug(`Dgram multicast socket multicastInterface set to ${BLUE}${interfaceAddress}${db}`);
149
96
  this.emit('ready', address);
150
97
  }
151
- /**
152
- * Stops the dgram multicast socket.
153
- */
154
98
  stop() {
155
99
  this.log.debug('Stopping dgram multicast socket...');
156
100
  this.joinedInterfaces.forEach((membershipInterface) => {
@@ -167,4 +111,3 @@ export class Multicast extends Dgram {
167
111
  this.log.debug('Stopped dgram multicast socket.');
168
112
  }
169
113
  }
170
- //# sourceMappingURL=multicast.js.map
@@ -1,54 +1,12 @@
1
- /**
2
- * @description This file contains the class Unicast.
3
- * @file unicast.ts
4
- * @author Luca Liguori
5
- * @created 2025-03-22
6
- * @version 1.0.0
7
- * @license Apache-2.0
8
- *
9
- * Copyright 2025, 2026, 2027 Luca Liguori.
10
- *
11
- * Licensed under the Apache License, Version 2.0 (the "License");
12
- * you may not use this file except in compliance with the License.
13
- * You may obtain a copy of the License at
14
- *
15
- * http://www.apache.org/licenses/LICENSE-2.0
16
- *
17
- * Unless required by applicable law or agreed to in writing, software
18
- * distributed under the License is distributed on an "AS IS" BASIS,
19
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
- * See the License for the specific language governing permissions and
21
- * limitations under the License.
22
- */
23
- // AnsiLogger imports
24
1
  import { BLUE, db } from 'node-ansi-logger';
25
- // Net imports
26
2
  import { Dgram } from './dgram.js';
27
- /**
28
- * This class implements a unicast dgram socket.
29
- */
30
3
  export class Unicast extends Dgram {
31
4
  port;
32
- /**
33
- * Creates an instance of Unicast.
34
- *
35
- * @param {string} name - The name of the socket.
36
- * @param {'udp4' | 'udp6'} socketType - The type of the socket (IPv4 or IPv6).
37
- * @param {boolean | undefined} reuseAddr - Whether to allow address reuse.
38
- * @param {string} [interfaceName] - The name of the network interface to bind to.
39
- * @param {string} [interfaceAddress] - The address of the network interface to bind to.
40
- * @param {number} [port] - The port number to bind to. If not provided, it will bind to any available port.
41
- */
42
5
  constructor(name, socketType, reuseAddr = true, interfaceName, interfaceAddress, port) {
43
6
  super(name, socketType, reuseAddr, interfaceName, interfaceAddress);
44
7
  this.port = port;
45
8
  }
46
- /**
47
- * Starts the dgram unicast socket.
48
- */
49
9
  start() {
50
- // Get the local ipv4 or ipv6 interfaceAddress to bind to. If not provided and interfaceName has been provided, use the first one found.
51
- // If neither interfaceAddress nor interfaceName is provided, use undefined to bind to any available address. In this case broadcast will not work.
52
10
  if (this.socketType === 'udp4') {
53
11
  this.log.debug(`Starting ipv4 dgram unicast socket...`);
54
12
  this.interfaceAddress = this.interfaceAddress ?? (this.interfaceName ? this.getIpv4InterfaceAddress(this.interfaceName) : undefined);
@@ -58,20 +16,12 @@ export class Unicast extends Dgram {
58
16
  this.interfaceAddress = this.interfaceAddress ?? (this.interfaceName ? this.getIpv6InterfaceAddress(this.interfaceName) : undefined);
59
17
  }
60
18
  this.interfaceNetmask = this.interfaceAddress ? this.getNetmask(this.interfaceAddress) : undefined;
61
- // Bind to the local address and port:
62
- // port 0 or undefined means "assign any available port"
63
- // address 0.0.0.0 or :: means "bind to all available addresses"
64
19
  this.socket.bind(this.port, this.interfaceAddress, () => {
65
20
  const address = this.socket.address();
66
21
  this.log.debug(`Dgram unicast socket bound to ${BLUE}${address.family}${db} ${BLUE}${address.address}${db}:${BLUE}${address.port}${db}`);
67
22
  this.emit('bound', address);
68
23
  });
69
24
  }
70
- /**
71
- * Event handler for the 'listening' event. This event is emitted when the socket starts listening for datagram messages.
72
- *
73
- * @param {AddressInfo} [address] - The address info.
74
- */
75
25
  onListening(address) {
76
26
  this.log.debug(`Dgram unicast socket listening on ${BLUE}${address.family}${db} ${BLUE}${address.address}${db}:${BLUE}${address.port}${db}`);
77
27
  this.socket.setBroadcast(true);
@@ -79,13 +29,9 @@ export class Unicast extends Dgram {
79
29
  this.emit('ready', address);
80
30
  this.onReady(address);
81
31
  }
82
- /**
83
- * Stops the dgram unicast socket.
84
- */
85
32
  stop() {
86
33
  this.log.debug('Stopping dgram unicast socket...');
87
34
  this.socket.close();
88
35
  this.log.debug('Stopped dgram unicast socket.');
89
36
  }
90
37
  }
91
- //# sourceMappingURL=unicast.js.map