hap-nodejs 1.0.0-beta.2 → 1.0.0-beta.3

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 (265) hide show
  1. package/@types/bonjour-hap.d.ts +4 -3
  2. package/@types/simple-plist.d.ts +2 -1
  3. package/README.md +2 -1
  4. package/dist/BridgedCore.js +7 -5
  5. package/dist/BridgedCore.js.map +1 -1
  6. package/dist/Core.js +14 -8
  7. package/dist/Core.js.map +1 -1
  8. package/dist/accessories/AirConditioner_accessory.d.ts +2 -0
  9. package/dist/accessories/AirConditioner_accessory.d.ts.map +1 -0
  10. package/dist/accessories/AirConditioner_accessory.js +128 -0
  11. package/dist/accessories/AirConditioner_accessory.js.map +1 -0
  12. package/dist/accessories/AppleTVRemote_accessory.d.ts +2 -0
  13. package/dist/accessories/AppleTVRemote_accessory.d.ts.map +1 -0
  14. package/dist/accessories/AppleTVRemote_accessory.js +159 -0
  15. package/dist/accessories/AppleTVRemote_accessory.js.map +1 -0
  16. package/dist/accessories/Camera_accessory.d.ts +2 -0
  17. package/dist/accessories/Camera_accessory.d.ts.map +1 -0
  18. package/dist/accessories/Camera_accessory.js +630 -0
  19. package/dist/accessories/Camera_accessory.js.map +1 -0
  20. package/dist/accessories/Fan_accessory.d.ts +2 -0
  21. package/dist/accessories/Fan_accessory.d.ts.map +1 -0
  22. package/dist/accessories/Fan_accessory.js +80 -0
  23. package/dist/accessories/Fan_accessory.js.map +1 -0
  24. package/dist/accessories/GarageDoorOpener_accessory.d.ts +2 -0
  25. package/dist/accessories/GarageDoorOpener_accessory.d.ts.map +1 -0
  26. package/dist/accessories/GarageDoorOpener_accessory.js +78 -0
  27. package/dist/accessories/GarageDoorOpener_accessory.js.map +1 -0
  28. package/dist/accessories/Light-AdaptiveLighting_accessory.d.ts +2 -0
  29. package/dist/accessories/Light-AdaptiveLighting_accessory.d.ts.map +1 -0
  30. package/dist/accessories/Light-AdaptiveLighting_accessory.js +96 -0
  31. package/dist/accessories/Light-AdaptiveLighting_accessory.js.map +1 -0
  32. package/dist/accessories/Light_accessory.d.ts +2 -0
  33. package/dist/accessories/Light_accessory.d.ts.map +1 -0
  34. package/dist/accessories/Light_accessory.js +148 -0
  35. package/dist/accessories/Light_accessory.js.map +1 -0
  36. package/dist/accessories/Lock_accessory.d.ts +2 -0
  37. package/dist/accessories/Lock_accessory.d.ts.map +1 -0
  38. package/dist/accessories/Lock_accessory.js +76 -0
  39. package/dist/accessories/Lock_accessory.js.map +1 -0
  40. package/dist/accessories/MotionSensor_accessory.d.ts +2 -0
  41. package/dist/accessories/MotionSensor_accessory.d.ts.map +1 -0
  42. package/dist/accessories/MotionSensor_accessory.js +45 -0
  43. package/dist/accessories/MotionSensor_accessory.js.map +1 -0
  44. package/dist/accessories/Outlet_accessory.d.ts +2 -0
  45. package/dist/accessories/Outlet_accessory.d.ts.map +1 -0
  46. package/dist/accessories/Outlet_accessory.js +79 -0
  47. package/dist/accessories/Outlet_accessory.js.map +1 -0
  48. package/dist/accessories/SmartSpeaker_accessory.d.ts +2 -0
  49. package/dist/accessories/SmartSpeaker_accessory.d.ts.map +1 -0
  50. package/dist/accessories/SmartSpeaker_accessory.js +44 -0
  51. package/dist/accessories/SmartSpeaker_accessory.js.map +1 -0
  52. package/dist/accessories/Sprinkler_accessory.d.ts +2 -0
  53. package/dist/accessories/Sprinkler_accessory.d.ts.map +1 -0
  54. package/dist/accessories/Sprinkler_accessory.js +128 -0
  55. package/dist/accessories/Sprinkler_accessory.js.map +1 -0
  56. package/dist/accessories/TV_accessory.d.ts +2 -0
  57. package/dist/accessories/TV_accessory.d.ts.map +1 -0
  58. package/dist/accessories/TV_accessory.js +101 -0
  59. package/dist/accessories/TV_accessory.js.map +1 -0
  60. package/dist/accessories/TemperatureSensor_accessory.d.ts +2 -0
  61. package/dist/accessories/TemperatureSensor_accessory.d.ts.map +1 -0
  62. package/dist/accessories/TemperatureSensor_accessory.js +44 -0
  63. package/dist/accessories/TemperatureSensor_accessory.js.map +1 -0
  64. package/dist/accessories/Wi-FiRouter_accessory.d.ts +3 -0
  65. package/dist/accessories/Wi-FiRouter_accessory.d.ts.map +1 -0
  66. package/dist/accessories/Wi-FiRouter_accessory.js +17 -0
  67. package/dist/accessories/Wi-FiRouter_accessory.js.map +1 -0
  68. package/dist/accessories/Wi-FiSatellite_accessory.d.ts +3 -0
  69. package/dist/accessories/Wi-FiSatellite_accessory.d.ts.map +1 -0
  70. package/dist/accessories/Wi-FiSatellite_accessory.js +19 -0
  71. package/dist/accessories/Wi-FiSatellite_accessory.js.map +1 -0
  72. package/dist/accessories/gstreamer-audioProducer.d.ts +25 -0
  73. package/dist/accessories/gstreamer-audioProducer.d.ts.map +1 -0
  74. package/dist/accessories/gstreamer-audioProducer.js +155 -0
  75. package/dist/accessories/gstreamer-audioProducer.js.map +1 -0
  76. package/dist/accessories/types.d.ts +78 -0
  77. package/dist/accessories/types.d.ts.map +1 -0
  78. package/dist/accessories/types.js +88 -0
  79. package/dist/accessories/types.js.map +1 -0
  80. package/dist/index.d.ts +54 -27
  81. package/dist/index.d.ts.map +1 -1
  82. package/dist/index.js +34 -11
  83. package/dist/index.js.map +1 -1
  84. package/dist/internal-types.d.ts +65 -124
  85. package/dist/internal-types.d.ts.map +1 -1
  86. package/dist/internal-types.js +66 -5
  87. package/dist/internal-types.js.map +1 -1
  88. package/dist/lib/Accessory.d.ts +147 -60
  89. package/dist/lib/Accessory.d.ts.map +1 -1
  90. package/dist/lib/Accessory.js +566 -475
  91. package/dist/lib/Accessory.js.map +1 -1
  92. package/dist/lib/AccessoryLoader.d.ts +20 -8
  93. package/dist/lib/AccessoryLoader.d.ts.map +1 -1
  94. package/dist/lib/AccessoryLoader.js +115 -93
  95. package/dist/lib/AccessoryLoader.js.map +1 -1
  96. package/dist/lib/Advertiser.d.ts +99 -5
  97. package/dist/lib/Advertiser.d.ts.map +1 -1
  98. package/dist/lib/Advertiser.js +564 -22
  99. package/dist/lib/Advertiser.js.map +1 -1
  100. package/dist/lib/Bridge.d.ts +3 -1
  101. package/dist/lib/Bridge.d.ts.map +1 -1
  102. package/dist/lib/Bridge.js +2 -0
  103. package/dist/lib/Bridge.js.map +1 -1
  104. package/dist/lib/Characteristic.d.ts +918 -59
  105. package/dist/lib/Characteristic.d.ts.map +1 -1
  106. package/dist/lib/Characteristic.js +308 -257
  107. package/dist/lib/Characteristic.js.map +1 -1
  108. package/dist/lib/HAPServer.d.ts +114 -43
  109. package/dist/lib/HAPServer.d.ts.map +1 -1
  110. package/dist/lib/HAPServer.js +269 -405
  111. package/dist/lib/HAPServer.js.map +1 -1
  112. package/dist/lib/Service.d.ts +339 -15
  113. package/dist/lib/Service.d.ts.map +1 -1
  114. package/dist/lib/Service.js +59 -50
  115. package/dist/lib/Service.js.map +1 -1
  116. package/dist/lib/camera/Camera.d.ts +15 -6
  117. package/dist/lib/camera/Camera.d.ts.map +1 -1
  118. package/dist/lib/camera/Camera.js +6 -3
  119. package/dist/lib/camera/Camera.js.map +1 -1
  120. package/dist/lib/camera/RTPProxy.d.ts +24 -19
  121. package/dist/lib/camera/RTPProxy.d.ts.map +1 -1
  122. package/dist/lib/camera/RTPProxy.js +230 -207
  123. package/dist/lib/camera/RTPProxy.js.map +1 -1
  124. package/dist/lib/camera/RTPStreamManagement.d.ts +202 -43
  125. package/dist/lib/camera/RTPStreamManagement.d.ts.map +1 -1
  126. package/dist/lib/camera/RTPStreamManagement.js +303 -189
  127. package/dist/lib/camera/RTPStreamManagement.js.map +1 -1
  128. package/dist/lib/camera/RecordingManagement.d.ts +326 -0
  129. package/dist/lib/camera/RecordingManagement.d.ts.map +1 -0
  130. package/dist/lib/camera/RecordingManagement.js +833 -0
  131. package/dist/lib/camera/RecordingManagement.js.map +1 -0
  132. package/dist/lib/camera/index.d.ts +4 -3
  133. package/dist/lib/camera/index.d.ts.map +1 -1
  134. package/dist/lib/camera/index.js +1 -0
  135. package/dist/lib/camera/index.js.map +1 -1
  136. package/dist/lib/controller/AdaptiveLightingController.d.ts +37 -11
  137. package/dist/lib/controller/AdaptiveLightingController.d.ts.map +1 -1
  138. package/dist/lib/controller/AdaptiveLightingController.js +101 -86
  139. package/dist/lib/controller/AdaptiveLightingController.js.map +1 -1
  140. package/dist/lib/controller/CameraController.d.ts +290 -12
  141. package/dist/lib/controller/CameraController.d.ts.map +1 -1
  142. package/dist/lib/controller/CameraController.js +403 -47
  143. package/dist/lib/controller/CameraController.js.map +1 -1
  144. package/dist/lib/controller/Controller.d.ts +31 -10
  145. package/dist/lib/controller/Controller.d.ts.map +1 -1
  146. package/dist/lib/controller/Controller.js +7 -0
  147. package/dist/lib/controller/Controller.js.map +1 -1
  148. package/dist/lib/controller/DoorbellController.d.ts +47 -1
  149. package/dist/lib/controller/DoorbellController.d.ts.map +1 -1
  150. package/dist/lib/controller/DoorbellController.js +65 -11
  151. package/dist/lib/controller/DoorbellController.js.map +1 -1
  152. package/dist/lib/controller/RemoteController.d.ts +111 -42
  153. package/dist/lib/controller/RemoteController.d.ts.map +1 -1
  154. package/dist/lib/controller/RemoteController.js +228 -203
  155. package/dist/lib/controller/RemoteController.js.map +1 -1
  156. package/dist/lib/controller/index.d.ts +4 -4
  157. package/dist/lib/datastream/DataStreamManagement.d.ts +25 -19
  158. package/dist/lib/datastream/DataStreamManagement.d.ts.map +1 -1
  159. package/dist/lib/datastream/DataStreamManagement.js +42 -35
  160. package/dist/lib/datastream/DataStreamManagement.js.map +1 -1
  161. package/dist/lib/datastream/DataStreamParser.d.ts +63 -24
  162. package/dist/lib/datastream/DataStreamParser.d.ts.map +1 -1
  163. package/dist/lib/datastream/DataStreamParser.js +132 -86
  164. package/dist/lib/datastream/DataStreamParser.js.map +1 -1
  165. package/dist/lib/datastream/DataStreamServer.d.ts +154 -50
  166. package/dist/lib/datastream/DataStreamServer.d.ts.map +1 -1
  167. package/dist/lib/datastream/DataStreamServer.js +242 -147
  168. package/dist/lib/datastream/DataStreamServer.js.map +1 -1
  169. package/dist/lib/datastream/index.d.ts +3 -3
  170. package/dist/lib/definitions/CharacteristicDefinitions.d.ts +181 -0
  171. package/dist/lib/definitions/CharacteristicDefinitions.d.ts.map +1 -1
  172. package/dist/lib/definitions/CharacteristicDefinitions.js +968 -499
  173. package/dist/lib/definitions/CharacteristicDefinitions.js.map +1 -1
  174. package/dist/lib/definitions/ServiceDefinitions.d.ts +58 -0
  175. package/dist/lib/definitions/ServiceDefinitions.d.ts.map +1 -1
  176. package/dist/lib/definitions/ServiceDefinitions.js +173 -6
  177. package/dist/lib/definitions/ServiceDefinitions.js.map +1 -1
  178. package/dist/lib/definitions/generate-definitions.d.ts +1 -0
  179. package/dist/lib/definitions/generate-definitions.d.ts.map +1 -1
  180. package/dist/lib/definitions/generate-definitions.js +29 -21
  181. package/dist/lib/definitions/generate-definitions.js.map +1 -1
  182. package/dist/lib/definitions/generator-configuration.d.ts.map +1 -1
  183. package/dist/lib/definitions/generator-configuration.js +62 -29
  184. package/dist/lib/definitions/generator-configuration.js.map +1 -1
  185. package/dist/lib/model/AccessoryInfo.d.ts +16 -13
  186. package/dist/lib/model/AccessoryInfo.d.ts.map +1 -1
  187. package/dist/lib/model/AccessoryInfo.js +88 -83
  188. package/dist/lib/model/AccessoryInfo.js.map +1 -1
  189. package/dist/lib/model/ControllerStorage.d.ts +3 -0
  190. package/dist/lib/model/ControllerStorage.d.ts.map +1 -1
  191. package/dist/lib/model/ControllerStorage.js +7 -4
  192. package/dist/lib/model/ControllerStorage.js.map +1 -1
  193. package/dist/lib/model/HAPStorage.d.ts +3 -0
  194. package/dist/lib/model/HAPStorage.d.ts.map +1 -1
  195. package/dist/lib/model/HAPStorage.js +4 -0
  196. package/dist/lib/model/HAPStorage.js.map +1 -1
  197. package/dist/lib/model/IdentifierCache.d.ts +12 -11
  198. package/dist/lib/model/IdentifierCache.d.ts.map +1 -1
  199. package/dist/lib/model/IdentifierCache.js +68 -65
  200. package/dist/lib/model/IdentifierCache.js.map +1 -1
  201. package/dist/lib/tv/AccessControlManagement.d.ts +12 -1
  202. package/dist/lib/tv/AccessControlManagement.d.ts.map +1 -1
  203. package/dist/lib/tv/AccessControlManagement.js +13 -5
  204. package/dist/lib/tv/AccessControlManagement.js.map +1 -1
  205. package/dist/lib/util/clone.d.ts +2 -1
  206. package/dist/lib/util/clone.d.ts.map +1 -1
  207. package/dist/lib/util/clone.js +2 -0
  208. package/dist/lib/util/clone.js.map +1 -1
  209. package/dist/lib/util/color-utils.d.ts +4 -1
  210. package/dist/lib/util/color-utils.d.ts.map +1 -1
  211. package/dist/lib/util/color-utils.js +5 -2
  212. package/dist/lib/util/color-utils.js.map +1 -1
  213. package/dist/lib/util/eventedhttp.d.ts +62 -22
  214. package/dist/lib/util/eventedhttp.d.ts.map +1 -1
  215. package/dist/lib/util/eventedhttp.js +236 -180
  216. package/dist/lib/util/eventedhttp.js.map +1 -1
  217. package/dist/lib/util/hapCrypto.d.ts +35 -7
  218. package/dist/lib/util/hapCrypto.d.ts.map +1 -1
  219. package/dist/lib/util/hapCrypto.js +86 -78
  220. package/dist/lib/util/hapCrypto.js.map +1 -1
  221. package/dist/lib/util/hapStatusError.d.ts +2 -0
  222. package/dist/lib/util/hapStatusError.d.ts.map +1 -1
  223. package/dist/lib/util/hapStatusError.js +5 -3
  224. package/dist/lib/util/hapStatusError.js.map +1 -1
  225. package/dist/lib/util/net-utils.d.ts +7 -0
  226. package/dist/lib/util/net-utils.d.ts.map +1 -1
  227. package/dist/lib/util/net-utils.js +11 -4
  228. package/dist/lib/util/net-utils.js.map +1 -1
  229. package/dist/lib/util/once.d.ts +6 -1
  230. package/dist/lib/util/once.d.ts.map +1 -1
  231. package/dist/lib/util/once.js +9 -3
  232. package/dist/lib/util/once.js.map +1 -1
  233. package/dist/lib/util/promise-utils.d.ts +15 -0
  234. package/dist/lib/util/promise-utils.d.ts.map +1 -0
  235. package/dist/lib/util/promise-utils.js +36 -0
  236. package/dist/lib/util/promise-utils.js.map +1 -0
  237. package/dist/lib/util/request-util.d.ts +16 -0
  238. package/dist/lib/util/request-util.d.ts.map +1 -1
  239. package/dist/lib/util/request-util.js +43 -27
  240. package/dist/lib/util/request-util.js.map +1 -1
  241. package/dist/lib/util/time.d.ts +7 -1
  242. package/dist/lib/util/time.d.ts.map +1 -1
  243. package/dist/lib/util/time.js +13 -7
  244. package/dist/lib/util/time.js.map +1 -1
  245. package/dist/lib/util/tlv.d.ts +95 -5
  246. package/dist/lib/util/tlv.d.ts.map +1 -1
  247. package/dist/lib/util/tlv.js +96 -24
  248. package/dist/lib/util/tlv.js.map +1 -1
  249. package/dist/lib/util/uuid.d.ts +2 -3
  250. package/dist/lib/util/uuid.d.ts.map +1 -1
  251. package/dist/lib/util/uuid.js +25 -19
  252. package/dist/lib/util/uuid.js.map +1 -1
  253. package/dist/types.d.ts +271 -18
  254. package/dist/types.d.ts.map +1 -1
  255. package/dist/types.js +8 -0
  256. package/dist/types.js.map +1 -1
  257. package/package.json +28 -27
  258. package/dist/lib/util/setupcode.d.ts +0 -5
  259. package/dist/lib/util/setupcode.d.ts.map +0 -1
  260. package/dist/lib/util/setupcode.js +0 -49
  261. package/dist/lib/util/setupcode.js.map +0 -1
  262. package/dist/lib/util/setupid.d.ts +0 -5
  263. package/dist/lib/util/setupid.d.ts.map +0 -1
  264. package/dist/lib/util/setupid.js +0 -52
  265. package/dist/lib/util/setupid.js.map +0 -1
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DataStreamConnection = exports.DataStreamConnectionEvent = exports.DataStreamServer = exports.DataStreamServerEvent = exports.DataSendCloseReason = exports.HDSStatus = exports.Topics = exports.Protocols = void 0;
3
+ exports.DataStreamConnection = exports.HDSConnectionError = exports.HDSConnectionErrorType = exports.DataStreamConnectionEvent = exports.DataStreamServer = exports.DataStreamServerEvent = exports.MessageType = exports.HDSProtocolError = exports.HDSProtocolSpecificErrorReason = exports.HDSStatus = exports.Topics = exports.Protocols = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  var assert_1 = tslib_1.__importDefault(require("assert"));
6
6
  var crypto_1 = tslib_1.__importDefault(require("crypto"));
@@ -9,13 +9,19 @@ var events_1 = require("events");
9
9
  var net_1 = tslib_1.__importDefault(require("net"));
10
10
  var hapCrypto = tslib_1.__importStar(require("../util/hapCrypto"));
11
11
  var DataStreamParser_1 = require("./DataStreamParser");
12
- var debug = debug_1.default('HAP-NodeJS:DataStream:Server');
12
+ var debug = (0, debug_1.default)("HAP-NodeJS:DataStream:Server");
13
+ /**
14
+ * @group HomeKit Data Streams (HDS)
15
+ */
13
16
  var Protocols;
14
17
  (function (Protocols) {
15
18
  Protocols["CONTROL"] = "control";
16
19
  Protocols["TARGET_CONTROL"] = "targetControl";
17
20
  Protocols["DATA_SEND"] = "dataSend";
18
21
  })(Protocols = exports.Protocols || (exports.Protocols = {}));
22
+ /**
23
+ * @group HomeKit Data Streams (HDS)
24
+ */
19
25
  var Topics;
20
26
  (function (Topics) {
21
27
  // control
@@ -28,6 +34,9 @@ var Topics;
28
34
  Topics["ACK"] = "ack";
29
35
  Topics["CLOSE"] = "close";
30
36
  })(Topics = exports.Topics || (exports.Topics = {}));
37
+ /**
38
+ * @group HomeKit Data Streams (HDS)
39
+ */
31
40
  var HDSStatus;
32
41
  (function (HDSStatus) {
33
42
  // noinspection JSUnusedGlobalSymbols
@@ -39,17 +48,44 @@ var HDSStatus;
39
48
  HDSStatus[HDSStatus["MISSING_PROTOCOL"] = 5] = "MISSING_PROTOCOL";
40
49
  HDSStatus[HDSStatus["PROTOCOL_SPECIFIC_ERROR"] = 6] = "PROTOCOL_SPECIFIC_ERROR";
41
50
  })(HDSStatus = exports.HDSStatus || (exports.HDSStatus = {}));
42
- var DataSendCloseReason;
43
- (function (DataSendCloseReason) {
51
+ /**
52
+ * @group HomeKit Data Streams (HDS)
53
+ */
54
+ var HDSProtocolSpecificErrorReason;
55
+ (function (HDSProtocolSpecificErrorReason) {
44
56
  // noinspection JSUnusedGlobalSymbols
45
- DataSendCloseReason[DataSendCloseReason["NORMAL"] = 0] = "NORMAL";
46
- DataSendCloseReason[DataSendCloseReason["NOT_ALLOWED"] = 1] = "NOT_ALLOWED";
47
- DataSendCloseReason[DataSendCloseReason["BUSY"] = 2] = "BUSY";
48
- DataSendCloseReason[DataSendCloseReason["CANCELLED"] = 3] = "CANCELLED";
49
- DataSendCloseReason[DataSendCloseReason["UNSUPPORTED"] = 4] = "UNSUPPORTED";
50
- DataSendCloseReason[DataSendCloseReason["UNEXPECTED_FAILURE"] = 5] = "UNEXPECTED_FAILURE";
51
- DataSendCloseReason[DataSendCloseReason["TIMEOUT"] = 6] = "TIMEOUT";
52
- })(DataSendCloseReason = exports.DataSendCloseReason || (exports.DataSendCloseReason = {}));
57
+ HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["NORMAL"] = 0] = "NORMAL";
58
+ HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["NOT_ALLOWED"] = 1] = "NOT_ALLOWED";
59
+ HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["BUSY"] = 2] = "BUSY";
60
+ HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["CANCELLED"] = 3] = "CANCELLED";
61
+ HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["UNSUPPORTED"] = 4] = "UNSUPPORTED";
62
+ HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["UNEXPECTED_FAILURE"] = 5] = "UNEXPECTED_FAILURE";
63
+ HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["TIMEOUT"] = 6] = "TIMEOUT";
64
+ HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["BAD_DATA"] = 7] = "BAD_DATA";
65
+ HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["PROTOCOL_ERROR"] = 8] = "PROTOCOL_ERROR";
66
+ HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["INVALID_CONFIGURATION"] = 9] = "INVALID_CONFIGURATION";
67
+ })(HDSProtocolSpecificErrorReason = exports.HDSProtocolSpecificErrorReason || (exports.HDSProtocolSpecificErrorReason = {}));
68
+ /**
69
+ * An error indicating a protocol level HDS error.
70
+ * E.g. it may be used to encode a {@link HDSStatus.PROTOCOL_SPECIFIC_ERROR} in the {@link Protocols.DATA_SEND} protocol.
71
+ * @group HomeKit Data Streams (HDS)
72
+ */
73
+ var HDSProtocolError = /** @class */ (function (_super) {
74
+ tslib_1.__extends(HDSProtocolError, _super);
75
+ /**
76
+ * Initializes a new `HDSProtocolError`
77
+ * @param reason - The {@link HDSProtocolSpecificErrorReason}.
78
+ * Values MUST NOT be {@link HDSProtocolSpecificErrorReason.NORMAL}.
79
+ */
80
+ function HDSProtocolError(reason) {
81
+ var _this = _super.call(this, "HDSProtocolError: " + reason) || this;
82
+ (0, assert_1.default)(reason !== 0 /* HDSProtocolSpecificErrorReason.NORMAL */, "Cannot initialize a HDSProtocolError with NORMAL!");
83
+ _this.reason = reason;
84
+ return _this;
85
+ }
86
+ return HDSProtocolError;
87
+ }(Error));
88
+ exports.HDSProtocolError = HDSProtocolError;
53
89
  var ServerState;
54
90
  (function (ServerState) {
55
91
  ServerState[ServerState["UNINITIALIZED"] = 0] = "UNINITIALIZED";
@@ -65,12 +101,18 @@ var ConnectionState;
65
101
  ConnectionState[ConnectionState["CLOSING"] = 3] = "CLOSING";
66
102
  ConnectionState[ConnectionState["CLOSED"] = 4] = "CLOSED";
67
103
  })(ConnectionState || (ConnectionState = {}));
104
+ /**
105
+ * @group HomeKit Data Streams (HDS)
106
+ */
68
107
  var MessageType;
69
108
  (function (MessageType) {
70
109
  MessageType[MessageType["EVENT"] = 1] = "EVENT";
71
110
  MessageType[MessageType["REQUEST"] = 2] = "REQUEST";
72
111
  MessageType[MessageType["RESPONSE"] = 3] = "RESPONSE";
73
- })(MessageType || (MessageType = {}));
112
+ })(MessageType = exports.MessageType || (exports.MessageType = {}));
113
+ /**
114
+ * @group HomeKit Data Streams (HDS)
115
+ */
74
116
  var DataStreamServerEvent;
75
117
  (function (DataStreamServerEvent) {
76
118
  /**
@@ -85,12 +127,13 @@ var DataStreamServerEvent;
85
127
  })(DataStreamServerEvent = exports.DataStreamServerEvent || (exports.DataStreamServerEvent = {}));
86
128
  /**
87
129
  * DataStreamServer which listens for incoming tcp connections and handles identification of new connections
130
+ * @group HomeKit Data Streams (HDS)
88
131
  */
89
132
  var DataStreamServer = /** @class */ (function (_super) {
90
133
  tslib_1.__extends(DataStreamServer, _super);
91
134
  function DataStreamServer() {
92
135
  var _this = _super.call(this) || this;
93
- _this.state = 0 /* UNINITIALIZED */;
136
+ _this.state = 0 /* ServerState.UNINITIALIZED */;
94
137
  _this.preparedSessions = [];
95
138
  _this.connections = [];
96
139
  _this.removeListenersOnceClosed = false;
@@ -102,20 +145,20 @@ var DataStreamServer = /** @class */ (function (_super) {
102
145
  * The handler is only called for a connection if for the give protocol no ProtocolHandler
103
146
  * was registered on the connection level.
104
147
  *
105
- * @param protocol {string | Protocols} - name of the protocol to register the handler for
106
- * @param event {string | Topics} - name of the event (also referred to as topic. See {Topics} for some known ones)
107
- * @param handler {GlobalEventHandler} - function to be called for every occurring event
148
+ * @param protocol - name of the protocol to register the handler for
149
+ * @param event - name of the event (also referred to as topic. See {@link Topics} for some known ones)
150
+ * @param handler - function to be called for every occurring event
108
151
  */
109
152
  DataStreamServer.prototype.onEventMessage = function (protocol, event, handler) {
110
153
  this.internalEventEmitter.on(protocol + "-e-" + event, handler);
111
154
  return this;
112
155
  };
113
156
  /**
114
- * Removes an registered event handler.
157
+ * Removes a registered event handler.
115
158
  *
116
- * @param protocol {string | Protocols} - name of the protocol to unregister the handler for
117
- * @param event {string | Topics} - name of the event (also referred to as topic. See {Topics} for some known ones)
118
- * @param handler {GlobalEventHandler} - registered event handler
159
+ * @param protocol - name of the protocol to unregister the handler for
160
+ * @param event - name of the event (also referred to as topic. See {@link Topics} for some known ones)
161
+ * @param handler - registered event handler
119
162
  */
120
163
  DataStreamServer.prototype.removeEventHandler = function (protocol, event, handler) {
121
164
  this.internalEventEmitter.removeListener(protocol + "-e-" + event, handler);
@@ -126,20 +169,20 @@ var DataStreamServer = /** @class */ (function (_super) {
126
169
  * The handler is only called for a connection if for the give protocol no ProtocolHandler
127
170
  * was registered on the connection level.
128
171
  *
129
- * @param protocol {string | Protocols} - name of the protocol to register the handler for
130
- * @param request {string | Topics} - name of the request (also referred to as topic. See {Topics} for some known ones)
131
- * @param handler {GlobalRequestHandler} - function to be called for every occurring request
172
+ * @param protocol - name of the protocol to register the handler for
173
+ * @param request - name of the request (also referred to as topic. See {@link Topics} for some known ones)
174
+ * @param handler - function to be called for every occurring request
132
175
  */
133
176
  DataStreamServer.prototype.onRequestMessage = function (protocol, request, handler) {
134
177
  this.internalEventEmitter.on(protocol + "-r-" + request, handler);
135
178
  return this;
136
179
  };
137
180
  /**
138
- * Removes an registered request handler.
181
+ * Removes a registered request handler.
139
182
  *
140
- * @param protocol {string | Protocols} - name of the protocol to unregister the handler for
141
- * @param request {string | Topics} - name of the request (also referred to as topic. See {Topics} for some known ones)
142
- * @param handler {GlobalRequestHandler} - registered request handler
183
+ * @param protocol - name of the protocol to unregister the handler for
184
+ * @param request - name of the request (also referred to as topic. See {@link Topics} for some known ones)
185
+ * @param handler - registered request handler
143
186
  */
144
187
  DataStreamServer.prototype.removeRequestHandler = function (protocol, request, handler) {
145
188
  this.internalEventEmitter.removeListener(protocol + "-r-" + request, handler);
@@ -181,32 +224,32 @@ var DataStreamServer = /** @class */ (function (_super) {
181
224
  DataStreamServer.prototype.checkTCPServerEstablished = function (preparedSession, callback) {
182
225
  var _this = this;
183
226
  switch (this.state) {
184
- case 0 /* UNINITIALIZED */:
227
+ case 0 /* ServerState.UNINITIALIZED */:
185
228
  debug("Starting up TCP server.");
186
229
  this.tcpServer = net_1.default.createServer();
187
- this.tcpServer.once('listening', this.listening.bind(this, preparedSession, callback));
188
- this.tcpServer.on('connection', this.onConnection.bind(this));
189
- this.tcpServer.on('close', this.closed.bind(this));
230
+ this.tcpServer.once("listening", this.listening.bind(this, preparedSession, callback));
231
+ this.tcpServer.on("connection", this.onConnection.bind(this));
232
+ this.tcpServer.on("close", this.closed.bind(this));
190
233
  this.tcpServer.listen();
191
- this.state = 1 /* BINDING */;
234
+ this.state = 1 /* ServerState.BINDING */;
192
235
  break;
193
- case 1 /* BINDING */:
236
+ case 1 /* ServerState.BINDING */:
194
237
  debug("TCP server already running. Waiting for it to bind.");
195
- this.tcpServer.once('listening', this.listening.bind(this, preparedSession, callback));
238
+ this.tcpServer.once("listening", this.listening.bind(this, preparedSession, callback));
196
239
  break;
197
- case 2 /* LISTENING */:
240
+ case 2 /* ServerState.LISTENING */:
198
241
  debug("Instructing client to connect to already running TCP server");
199
242
  preparedSession.port = this.tcpPort;
200
243
  callback();
201
244
  break;
202
- case 3 /* CLOSING */:
245
+ case 3 /* ServerState.CLOSING */:
203
246
  debug("TCP socket is currently closing. Trying again when server is fully closed and opening a new one then.");
204
- this.tcpServer.once('close', function () { return setTimeout(function () { return _this.checkTCPServerEstablished(preparedSession, callback); }, 10); });
247
+ this.tcpServer.once("close", function () { return setTimeout(function () { return _this.checkTCPServerEstablished(preparedSession, callback); }, 10); });
205
248
  break;
206
249
  }
207
250
  };
208
251
  DataStreamServer.prototype.listening = function (preparedSession, callback) {
209
- this.state = 2 /* LISTENING */;
252
+ this.state = 2 /* ServerState.LISTENING */;
210
253
  var address = this.tcpServer.address();
211
254
  if (address && typeof address !== "string") { // address is only typeof string when listening to a pipe or unix socket
212
255
  this.tcpPort = address.port;
@@ -217,12 +260,13 @@ var DataStreamServer = /** @class */ (function (_super) {
217
260
  };
218
261
  DataStreamServer.prototype.onConnection = function (socket) {
219
262
  debug("[%s] New DataStream connection was established", socket.remoteAddress);
263
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
220
264
  var connection = new DataStreamConnection(socket);
221
- connection.on("identification" /* IDENTIFICATION */, this.handleSessionIdentification.bind(this, connection));
222
- connection.on("handle-message-globally" /* HANDLE_MESSAGE_GLOBALLY */, this.handleMessageGlobally.bind(this, connection));
223
- connection.on("closed" /* CLOSED */, this.connectionClosed.bind(this, connection));
265
+ connection.on("identification" /* DataStreamConnectionEvent.IDENTIFICATION */, this.handleSessionIdentification.bind(this, connection));
266
+ connection.on("handle-message-globally" /* DataStreamConnectionEvent.HANDLE_MESSAGE_GLOBALLY */, this.handleMessageGlobally.bind(this, connection));
267
+ connection.on("closed" /* DataStreamConnectionEvent.CLOSED */, this.connectionClosed.bind(this, connection));
224
268
  this.connections.push(connection);
225
- this.emit("connection-opened" /* CONNECTION_OPENED */, connection);
269
+ this.emit("connection-opened" /* DataStreamServerEvent.CONNECTION_OPENED */, connection);
226
270
  };
227
271
  DataStreamServer.prototype.handleSessionIdentification = function (connection, firstFrame, callback) {
228
272
  var identifiedSession = undefined;
@@ -254,20 +298,20 @@ var DataStreamServer = /** @class */ (function (_super) {
254
298
  };
255
299
  DataStreamServer.prototype.handleMessageGlobally = function (connection, message) {
256
300
  var _a;
257
- assert_1.default.notStrictEqual(message.type, 3 /* RESPONSE */); // responses can't physically get here
301
+ assert_1.default.notStrictEqual(message.type, 3 /* MessageType.RESPONSE */); // responses can't physically get here
258
302
  var separator = "";
259
303
  var args = [];
260
- if (message.type === 1 /* EVENT */) {
304
+ if (message.type === 1 /* MessageType.EVENT */) {
261
305
  separator = "-e-";
262
306
  }
263
- else if (message.type === 2 /* REQUEST */) {
307
+ else if (message.type === 2 /* MessageType.REQUEST */) {
264
308
  separator = "-r-";
265
309
  args.push(message.id);
266
310
  }
267
311
  args.push(message.message);
268
312
  var hadListeners;
269
313
  try {
270
- hadListeners = (_a = this.internalEventEmitter).emit.apply(_a, tslib_1.__spreadArray([message.protocol + separator + message.topic, connection], tslib_1.__read(args)));
314
+ hadListeners = (_a = this.internalEventEmitter).emit.apply(_a, tslib_1.__spreadArray([message.protocol + separator + message.topic, connection], tslib_1.__read(args), false));
271
315
  }
272
316
  catch (error) {
273
317
  hadListeners = true;
@@ -281,16 +325,16 @@ var DataStreamServer = /** @class */ (function (_super) {
281
325
  DataStreamServer.prototype.connectionClosed = function (connection) {
282
326
  debug("[%s] DataStream connection closed", connection.remoteAddress);
283
327
  this.connections.splice(this.connections.indexOf(connection), 1);
284
- this.emit("connection-closed" /* CONNECTION_CLOSED */, connection);
328
+ this.emit("connection-closed" /* DataStreamServerEvent.CONNECTION_CLOSED */, connection);
285
329
  this.checkCloseable();
286
- if (this.state === 3 /* CLOSING */ && this.removeListenersOnceClosed && this.connections.length === 0) {
330
+ if (this.state === 3 /* ServerState.CLOSING */ && this.removeListenersOnceClosed && this.connections.length === 0) {
287
331
  this.removeAllListeners(); // see this.destroy()
288
332
  }
289
333
  };
290
334
  DataStreamServer.prototype.checkCloseable = function () {
291
- if (this.connections.length === 0 && this.preparedSessions.length === 0 && this.state < 3 /* CLOSING */) {
335
+ if (this.connections.length === 0 && this.preparedSessions.length === 0 && this.state < 3 /* ServerState.CLOSING */) {
292
336
  debug("Last connection disconnected. Closing the server now.");
293
- this.state = 3 /* CLOSING */;
337
+ this.state = 3 /* ServerState.CLOSING */;
294
338
  this.tcpServer.close();
295
339
  }
296
340
  };
@@ -299,7 +343,7 @@ var DataStreamServer = /** @class */ (function (_super) {
299
343
  */
300
344
  DataStreamServer.prototype.destroy = function () {
301
345
  var e_1, _a;
302
- if (this.state > 0 /* UNINITIALIZED */ && this.state < 3 /* CLOSING */) {
346
+ if (this.state > 0 /* ServerState.UNINITIALIZED */ && this.state < 3 /* ServerState.CLOSING */) {
303
347
  this.tcpServer.close();
304
348
  try {
305
349
  for (var _b = tslib_1.__values(this.connections), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -315,14 +359,14 @@ var DataStreamServer = /** @class */ (function (_super) {
315
359
  finally { if (e_1) throw e_1.error; }
316
360
  }
317
361
  }
318
- this.state = 3 /* CLOSING */;
362
+ this.state = 3 /* ServerState.CLOSING */;
319
363
  this.removeListenersOnceClosed = true;
320
364
  this.internalEventEmitter.removeAllListeners();
321
365
  };
322
366
  DataStreamServer.prototype.closed = function () {
323
367
  this.tcpServer = undefined;
324
368
  this.tcpPort = undefined;
325
- this.state = 0 /* UNINITIALIZED */;
369
+ this.state = 0 /* ServerState.UNINITIALIZED */;
326
370
  };
327
371
  DataStreamServer.version = "1.0";
328
372
  DataStreamServer.accessoryToControllerInfo = Buffer.from("HDS-Read-Encryption-Key");
@@ -330,6 +374,9 @@ var DataStreamServer = /** @class */ (function (_super) {
330
374
  return DataStreamServer;
331
375
  }(events_1.EventEmitter));
332
376
  exports.DataStreamServer = DataStreamServer;
377
+ /**
378
+ * @group HomeKit Data Streams (HDS)
379
+ */
333
380
  var DataStreamConnectionEvent;
334
381
  (function (DataStreamConnectionEvent) {
335
382
  /**
@@ -340,7 +387,7 @@ var DataStreamConnectionEvent;
340
387
  */
341
388
  DataStreamConnectionEvent["IDENTIFICATION"] = "identification";
342
389
  /**
343
- * This event is emitted when no handler could be found for the given protocol of a event or request message.
390
+ * This event is emitted when no handler could be found for the given protocol of an event or request message.
344
391
  */
345
392
  DataStreamConnectionEvent["HANDLE_MESSAGE_GLOBALLY"] = "handle-message-globally";
346
393
  /**
@@ -349,8 +396,32 @@ var DataStreamConnectionEvent;
349
396
  DataStreamConnectionEvent["CLOSED"] = "closed";
350
397
  })(DataStreamConnectionEvent = exports.DataStreamConnectionEvent || (exports.DataStreamConnectionEvent = {}));
351
398
  /**
352
- * DataStream connection which holds any necessary state information, encryption an decryption keys, manages
399
+ * @group HomeKit Data Streams (HDS)
400
+ */
401
+ var HDSConnectionErrorType;
402
+ (function (HDSConnectionErrorType) {
403
+ HDSConnectionErrorType[HDSConnectionErrorType["ILLEGAL_STATE"] = 1] = "ILLEGAL_STATE";
404
+ HDSConnectionErrorType[HDSConnectionErrorType["CLOSED_SOCKET"] = 2] = "CLOSED_SOCKET";
405
+ HDSConnectionErrorType[HDSConnectionErrorType["MAX_PAYLOAD_LENGTH"] = 3] = "MAX_PAYLOAD_LENGTH";
406
+ })(HDSConnectionErrorType = exports.HDSConnectionErrorType || (exports.HDSConnectionErrorType = {}));
407
+ /**
408
+ * @group HomeKit Data Streams (HDS)
409
+ */
410
+ var HDSConnectionError = /** @class */ (function (_super) {
411
+ tslib_1.__extends(HDSConnectionError, _super);
412
+ function HDSConnectionError(message, type) {
413
+ var _this = _super.call(this, message) || this;
414
+ _this.type = type;
415
+ return _this;
416
+ }
417
+ return HDSConnectionError;
418
+ }(Error));
419
+ exports.HDSConnectionError = HDSConnectionError;
420
+ /**
421
+ * DataStream connection which holds any necessary state information, encryption and decryption keys, manages
353
422
  * protocol handlers and also handles sending and receiving of data stream frames.
423
+ *
424
+ * @group HomeKit Data Streams (HDS)
354
425
  */
355
426
  var DataStreamConnection = /** @class */ (function (_super) {
356
427
  tslib_1.__extends(DataStreamConnection, _super);
@@ -358,12 +429,12 @@ var DataStreamConnection = /** @class */ (function (_super) {
358
429
  var _a;
359
430
  var _this = _super.call(this) || this;
360
431
  /*
361
- Since our DataStream server does only listen on one port and this port is supplied to every client
362
- which wants to connect, we do not really know which client is who when we receive a tcp connection.
363
- Thus, we find the correct PreparedDataStreamSession object by testing the encryption keys of all available
364
- prepared sessions. Then we can reference this hds connection with the correct hap connection and mark it as identified.
365
- */
366
- _this.state = 0 /* UNIDENTIFIED */;
432
+ Since our DataStream server does only listen on one port and this port is supplied to every client
433
+ which wants to connect, we do not really know which client is who when we receive a tcp connection.
434
+ Thus, we find the correct PreparedDataStreamSession object by testing the encryption keys of all available
435
+ prepared sessions. Then we can reference this hds connection with the correct hap connection and mark it as identified.
436
+ */
437
+ _this.state = 0 /* ConnectionState.UNIDENTIFIED */;
367
438
  _this.protocolHandlers = {}; // used to store protocolHandlers identified by their protocol name
368
439
  _this.responseHandlers = {}; // used to store responseHandlers indexed by their respective requestId
369
440
  _this.responseTimers = {}; // used to store response timeouts indexed by their respective requestId
@@ -376,34 +447,41 @@ var DataStreamConnection = /** @class */ (function (_super) {
376
447
  _this.controllerToAccessoryNonce = 0;
377
448
  _this.controllerToAccessoryNonceBuffer = Buffer.alloc(8);
378
449
  _this.hapConnectionClosedListener = _this.onHAPSessionClosed.bind(_this);
379
- _this.addProtocolHandler("control" /* CONTROL */, {
450
+ _this.addProtocolHandler("control" /* Protocols.CONTROL */, {
380
451
  requestHandler: (_a = {},
381
- _a["hello" /* HELLO */] = _this.handleHello.bind(_this),
382
- _a)
452
+ _a["hello" /* Topics.HELLO */] = _this.handleHello.bind(_this),
453
+ _a),
383
454
  });
384
455
  _this.helloTimer = setTimeout(function () {
385
456
  debug("[%s] Hello message did not arrive in time. Killing the connection", _this.remoteAddress);
386
457
  _this.close();
387
458
  }, 10000);
388
- _this.socket.on('data', _this.onSocketData.bind(_this));
389
- _this.socket.on('error', _this.onSocketError.bind(_this));
390
- _this.socket.on('close', _this.onSocketClose.bind(_this));
459
+ _this.socket.on("data", _this.onSocketData.bind(_this));
460
+ _this.socket.on("error", _this.onSocketError.bind(_this));
461
+ _this.socket.on("close", _this.onSocketClose.bind(_this));
462
+ // this is to mitigate the event emitter "memory leak warning".
463
+ // e.g. with HSV there might be multiple cameras subscribing to the CLOSE event. one subscription for
464
+ // every active recording stream on a camera. The default limit of 10 might be easily reached.
465
+ // Setting a high limit isn't the prefect solution, but will avoid false positives but ensures that
466
+ // a warning is still be printed if running long enough.
467
+ _this.setMaxListeners(100);
391
468
  return _this;
392
469
  }
393
- DataStreamConnection.prototype.handleHello = function (id, _message) {
470
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
471
+ DataStreamConnection.prototype.handleHello = function (id, message) {
394
472
  // that hello is indeed the _first_ message received is verified in onSocketData(...)
395
- debug("[%s] Received hello message from client", this.remoteAddress);
473
+ debug("[%s] Received hello message from client: %o", this.remoteAddress, message);
396
474
  clearTimeout(this.helloTimer);
397
475
  this.helloTimer = undefined;
398
- this.state = 2 /* READY */;
399
- this.sendResponse("control" /* CONTROL */, "hello" /* HELLO */, id);
476
+ this.state = 2 /* ConnectionState.READY */;
477
+ this.sendResponse("control" /* Protocols.CONTROL */, "hello" /* Topics.HELLO */, id);
400
478
  };
401
479
  /**
402
480
  * Registers a new protocol handler to handle incoming messages.
403
481
  * The same protocol cannot be registered multiple times.
404
482
  *
405
- * @param protocol {string | Protocols} - name of the protocol to register the handler for
406
- * @param protocolHandler {DataStreamProtocolHandler} - object to be registered as protocol handler
483
+ * @param protocol - name of the protocol to register the handler for
484
+ * @param protocolHandler - object to be registered as protocol handler
407
485
  */
408
486
  DataStreamConnection.prototype.addProtocolHandler = function (protocol, protocolHandler) {
409
487
  if (this.protocolHandlers[protocol] !== undefined) {
@@ -415,8 +493,8 @@ var DataStreamConnection = /** @class */ (function (_super) {
415
493
  /**
416
494
  * Removes a protocol handler if it is registered.
417
495
  *
418
- * @param protocol {string | Protocols} - name of the protocol to unregister the handler for
419
- * @param protocolHandler {DataStreamProtocolHandler} - object which will be unregistered
496
+ * @param protocol - name of the protocol to unregister the handler for
497
+ * @param protocolHandler - object which will be unregistered
420
498
  */
421
499
  DataStreamConnection.prototype.removeProtocolHandler = function (protocol, protocolHandler) {
422
500
  var current = this.protocolHandlers[protocol];
@@ -427,26 +505,29 @@ var DataStreamConnection = /** @class */ (function (_super) {
427
505
  /**
428
506
  * Sends a new event message to the connected client.
429
507
  *
430
- * @param protocol {string | Protocols} - name of the protocol
431
- * @param event {string | Topics} - name of the event (also referred to as topic. See {Topics} for some known ones)
432
- * @param message {Record<any, any>} - message dictionary which gets sent along the event
508
+ * @param protocol - name of the protocol
509
+ * @param event - name of the event (also referred to as topic. See {@link Topics} for some known ones)
510
+ * @param message - message dictionary which gets sent along the event
433
511
  */
512
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
434
513
  DataStreamConnection.prototype.sendEvent = function (protocol, event, message) {
435
514
  if (message === void 0) { message = {}; }
515
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
436
516
  var header = {};
437
- header["protocol"] = protocol;
438
- header["event"] = event;
517
+ header.protocol = protocol;
518
+ header.event = event;
439
519
  this.sendHDSFrame(header, message);
440
520
  };
441
521
  /**
442
522
  * Sends a new request message to the connected client.
443
523
  *
444
- * @param protocol {string | Protocols} - name of the protocol
445
- * @param request {string | Topics} - name of the request (also referred to as topic. See {Topics} for some known ones)
446
- * @param message {Record<any, any>} - message dictionary which gets sent along the request
447
- * @param callback {ResponseHandler} - handler which gets supplied with an error object if the response didn't
448
- * arrive in time or the status and the message dictionary from the response
524
+ * @param protocol - name of the protocol
525
+ * @param request - name of the request (also referred to as topic. See {@link Topics} for some known ones)
526
+ * @param message - message dictionary which gets sent along the request
527
+ * @param callback - handler which gets supplied with an error object if the response didn't
528
+ * arrive in time or the status and the message dictionary from the response
449
529
  */
530
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
450
531
  DataStreamConnection.prototype.sendRequest = function (protocol, request, message, callback) {
451
532
  var _this = this;
452
533
  if (message === void 0) { message = {}; }
@@ -462,37 +543,41 @@ var DataStreamConnection = /** @class */ (function (_super) {
462
543
  var handler = _this.responseHandlers[requestId];
463
544
  delete _this.responseHandlers[requestId];
464
545
  delete _this.responseTimers[requestId];
465
- // handler should be able to cleanup their stuff
546
+ // handler should be able to clean up their stuff
466
547
  handler(new Error("timeout"), undefined, {});
467
548
  }, 10000); // 10s timer
549
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
468
550
  var header = {};
469
- header["protocol"] = protocol;
470
- header["request"] = request;
471
- header["id"] = new DataStreamParser_1.Int64(requestId);
551
+ header.protocol = protocol;
552
+ header.request = request;
553
+ header.id = new DataStreamParser_1.Int64(requestId);
472
554
  this.sendHDSFrame(header, message);
473
555
  };
474
556
  /**
475
557
  * Send a new response message to a received request message to the client.
476
558
  *
477
- * @param protocol {string | Protocols} - name of the protocol
478
- * @param response {string | Topics} - name of the response (also referred to as topic. See {Topics} for some known ones)
479
- * @param id {number} - id from the request, to associate the response to the request
480
- * @param status {HDSStatus} - status indication if the request was successful. A status of zero indicates success.
481
- * @param message {Record<any, any>} - message dictionary which gets sent along the response
559
+ * @param protocol - name of the protocol
560
+ * @param response - name of the response (also referred to as topic. See {@link Topics} for some known ones)
561
+ * @param id - id from the request, to associate the response to the request
562
+ * @param status - status indication if the request was successful. A status of zero indicates success.
563
+ * @param message - message dictionary which gets sent along the response
482
564
  */
483
- DataStreamConnection.prototype.sendResponse = function (protocol, response, id, status, message) {
565
+ DataStreamConnection.prototype.sendResponse = function (protocol, response, id, status,
566
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
567
+ message) {
484
568
  if (status === void 0) { status = HDSStatus.SUCCESS; }
485
569
  if (message === void 0) { message = {}; }
570
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
486
571
  var header = {};
487
- header["protocol"] = protocol;
488
- header["response"] = response;
489
- header["id"] = new DataStreamParser_1.Int64(id);
490
- header["status"] = new DataStreamParser_1.Int64(status);
572
+ header.protocol = protocol;
573
+ header.response = response;
574
+ header.id = new DataStreamParser_1.Int64(id);
575
+ header.status = new DataStreamParser_1.Int64(status);
491
576
  this.sendHDSFrame(header, message);
492
577
  };
493
578
  DataStreamConnection.prototype.onSocketData = function (data) {
494
579
  var _this = this;
495
- if (this.state >= 3 /* CLOSING */) {
580
+ if (this.state >= 3 /* ConnectionState.CLOSING */) {
496
581
  return;
497
582
  }
498
583
  var frameIndex = 0;
@@ -500,21 +585,22 @@ var DataStreamConnection = /** @class */ (function (_super) {
500
585
  if (frames.length === 0) { // not enough data
501
586
  return;
502
587
  }
503
- if (this.state === 0 /* UNIDENTIFIED */) {
588
+ if (this.state === 0 /* ConnectionState.UNIDENTIFIED */) {
504
589
  // at the beginning we are only interested in trying to decrypt the first frame in order to test decryption keys
505
590
  var firstFrame = frames[frameIndex++];
506
- this.emit("identification" /* IDENTIFICATION */, firstFrame, function (identifiedSession) {
591
+ this.emit("identification" /* DataStreamConnectionEvent.IDENTIFICATION */, firstFrame, function (identifiedSession) {
507
592
  if (identifiedSession) {
508
593
  // horray, we found our connection
509
594
  _this.connection = identifiedSession.connection;
510
595
  _this.accessoryToControllerEncryptionKey = identifiedSession.accessoryToControllerEncryptionKey;
511
596
  _this.controllerToAccessoryEncryptionKey = identifiedSession.controllerToAccessoryEncryptionKey;
512
- _this.state = 1 /* EXPECTING_HELLO */;
597
+ _this.state = 1 /* ConnectionState.EXPECTING_HELLO */;
513
598
  // below listener is removed in .close()
514
- _this.connection.on("closed" /* CLOSED */, _this.hapConnectionClosedListener); // register close listener
599
+ _this.connection.on("closed" /* HAPConnectionEvent.CLOSED */, _this.hapConnectionClosedListener); // register close listener
600
+ debug("[%s] Registering CLOSED handler to HAP connection. Connection currently has %d close handlers!", _this.remoteAddress, _this.connection.listeners("closed" /* HAPConnectionEvent.CLOSED */).length);
515
601
  }
516
602
  });
517
- if (this.state === 0 /* UNIDENTIFIED */) {
603
+ if (this.state === 0 /* ConnectionState.UNIDENTIFIED */) {
518
604
  // did not find a prepared connection, server already closed this connection; nothing to do here
519
605
  return;
520
606
  }
@@ -527,9 +613,9 @@ var DataStreamConnection = /** @class */ (function (_super) {
527
613
  }
528
614
  }
529
615
  var messages = this.decodePayloads(frames); // decode contents of payload
530
- if (this.state === 1 /* EXPECTING_HELLO */) {
616
+ if (this.state === 1 /* ConnectionState.EXPECTING_HELLO */) {
531
617
  var firstMessage = messages[0];
532
- if (firstMessage.protocol !== "control" /* CONTROL */ || firstMessage.type !== 2 /* REQUEST */ || firstMessage.topic !== "hello" /* HELLO */) {
618
+ if (firstMessage.protocol !== "control" /* Protocols.CONTROL */ || firstMessage.type !== 2 /* MessageType.REQUEST */ || firstMessage.topic !== "hello" /* Topics.HELLO */) {
533
619
  // first message is not the expected hello request
534
620
  debug("[%s] First message received was not the expected hello message. Instead got: %o", this.remoteAddress, firstMessage);
535
621
  this.close();
@@ -537,8 +623,8 @@ var DataStreamConnection = /** @class */ (function (_super) {
537
623
  }
538
624
  }
539
625
  messages.forEach(function (message) {
540
- if (message.type === 3 /* RESPONSE */) {
541
- // protocol and topic are currently not tested here; just assumed their are correct;
626
+ if (message.type === 3 /* MessageType.RESPONSE */) {
627
+ // protocol and topic are currently not tested here; just assumed they are correct;
542
628
  // probably they are as the requestId is unique per connection no matter what protocol is used
543
629
  var responseHandler = _this.responseHandlers[message.id];
544
630
  var responseTimer = _this.responseTimers[message.id];
@@ -564,10 +650,10 @@ var DataStreamConnection = /** @class */ (function (_super) {
564
650
  var handler = _this.protocolHandlers[message.protocol];
565
651
  if (handler === undefined) {
566
652
  // send message to the server to check if there are some global handlers for it
567
- _this.emit("handle-message-globally" /* HANDLE_MESSAGE_GLOBALLY */, message);
653
+ _this.emit("handle-message-globally" /* DataStreamConnectionEvent.HANDLE_MESSAGE_GLOBALLY */, message);
568
654
  return;
569
655
  }
570
- if (message.type === 1 /* EVENT */) {
656
+ if (message.type === 1 /* MessageType.EVENT */) {
571
657
  var eventHandler = void 0;
572
658
  if (!handler.eventHandler || !(eventHandler = handler.eventHandler[message.topic])) {
573
659
  debug("[%s] WARNING no event handler was found for message: %o", _this.remoteAddress, message);
@@ -581,7 +667,7 @@ var DataStreamConnection = /** @class */ (function (_super) {
581
667
  debug(error.stack);
582
668
  }
583
669
  }
584
- else if (message.type === 2 /* REQUEST */) {
670
+ else if (message.type === 2 /* MessageType.REQUEST */) {
585
671
  var requestHandler = void 0;
586
672
  if (!handler.requestHandler || !(requestHandler = handler.requestHandler[message.topic])) {
587
673
  debug("[%s] WARNING no request handler was found for message: %o", _this.remoteAddress, message);
@@ -644,11 +730,14 @@ var DataStreamConnection = /** @class */ (function (_super) {
644
730
  frames.push(hdsFrame);
645
731
  }
646
732
  else {
647
- debug("[%s] Encountered unknown payload type %d for payload: %s", this.remoteAddress, plaintextPayload.toString('hex'));
733
+ debug("[%s] Encountered unknown payload type %d for payload: %s", this.remoteAddress, plaintextPayload.toString("hex"));
648
734
  }
649
735
  }
650
736
  return frames;
651
737
  };
738
+ /**
739
+ * @private file-private API
740
+ */
652
741
  DataStreamConnection.prototype.decryptHDSFrame = function (frame, keyOverwrite) {
653
742
  hapCrypto.writeUInt64LE(this.controllerToAccessoryNonce, this.controllerToAccessoryNonceBuffer, 0); // update nonce buffer
654
743
  var key = keyOverwrite || this.controllerToAccessoryEncryptionKey;
@@ -668,7 +757,7 @@ var DataStreamConnection = /** @class */ (function (_super) {
668
757
  frames.forEach(function (frame) {
669
758
  var payload = frame.plaintextPayload;
670
759
  if (!payload) {
671
- throw new Error("Reached illegal state. Encountered HDSFrame with wasn't decrypted yet!");
760
+ throw new HDSConnectionError("Reached illegal state. Encountered HDSFrame with wasn't decrypted yet!", 1 /* HDSConnectionErrorType.ILLEGAL_STATE */);
672
761
  }
673
762
  var headerLength = payload.readUInt8(0);
674
763
  var messageLength = payload.length - headerLength - 1;
@@ -676,7 +765,9 @@ var DataStreamConnection = /** @class */ (function (_super) {
676
765
  var messageBegin = headerBegin + headerLength;
677
766
  var headerPayload = new DataStreamParser_1.DataStreamReader(payload.slice(headerBegin, headerBegin + headerLength));
678
767
  var messagePayload = new DataStreamParser_1.DataStreamReader(payload.slice(messageBegin, messageBegin + messageLength));
768
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
679
769
  var headerDictionary;
770
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
680
771
  var messageDictionary;
681
772
  try {
682
773
  headerDictionary = DataStreamParser_1.DataStreamParser.decode(headerPayload);
@@ -695,24 +786,24 @@ var DataStreamConnection = /** @class */ (function (_super) {
695
786
  return;
696
787
  }
697
788
  var type;
698
- var protocol = headerDictionary["protocol"];
789
+ var protocol = headerDictionary.protocol;
699
790
  var topic;
700
791
  var id = undefined;
701
792
  var status = undefined;
702
- if (headerDictionary["event"] !== undefined) {
703
- type = 1 /* EVENT */;
704
- topic = headerDictionary["event"];
793
+ if (headerDictionary.event !== undefined) {
794
+ type = 1 /* MessageType.EVENT */;
795
+ topic = headerDictionary.event;
705
796
  }
706
- else if (headerDictionary["request"] !== undefined) {
707
- type = 2 /* REQUEST */;
708
- topic = headerDictionary["request"];
709
- id = headerDictionary["id"];
797
+ else if (headerDictionary.request !== undefined) {
798
+ type = 2 /* MessageType.REQUEST */;
799
+ topic = headerDictionary.request;
800
+ id = headerDictionary.id;
710
801
  }
711
- else if (headerDictionary["response"] !== undefined) {
712
- type = 3 /* RESPONSE */;
713
- topic = headerDictionary["response"];
714
- id = headerDictionary["id"];
715
- status = headerDictionary["status"];
802
+ else if (headerDictionary.response !== undefined) {
803
+ type = 3 /* MessageType.RESPONSE */;
804
+ topic = headerDictionary.response;
805
+ id = headerDictionary.id;
806
+ status = headerDictionary.status;
716
807
  }
717
808
  else {
718
809
  debug("[%s] Encountered unknown payload header format: %o (message: %o)", _this.remoteAddress, headerDictionary, messageDictionary);
@@ -730,9 +821,10 @@ var DataStreamConnection = /** @class */ (function (_super) {
730
821
  });
731
822
  return messages;
732
823
  };
824
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
733
825
  DataStreamConnection.prototype.sendHDSFrame = function (header, message) {
734
- if (this.state >= 3 /* CLOSING */) {
735
- throw Error("Cannot send message on closing/closed socket!");
826
+ if (this.state >= 3 /* ConnectionState.CLOSING */) {
827
+ throw new HDSConnectionError("Cannot send message on closing/closed socket!", 2 /* HDSConnectionErrorType.CLOSED_SOCKET */);
736
828
  }
737
829
  var headerWriter = new DataStreamParser_1.DataStreamWriter();
738
830
  var messageWriter = new DataStreamParser_1.DataStreamWriter();
@@ -742,7 +834,7 @@ var DataStreamConnection = /** @class */ (function (_super) {
742
834
  payloadHeaderBuffer.writeUInt8(headerWriter.length(), 0);
743
835
  var payloadBuffer = Buffer.concat([payloadHeaderBuffer, headerWriter.getData(), messageWriter.getData()]);
744
836
  if (payloadBuffer.length > DataStreamConnection.MAX_PAYLOAD_LENGTH) {
745
- throw new Error("Tried sending payload with length larger than the maximum allowed for data stream");
837
+ throw new HDSConnectionError("Tried sending payload with length larger than the maximum allowed for data stream", 3 /* HDSConnectionErrorType.MAX_PAYLOAD_LENGTH */);
746
838
  }
747
839
  var frameTypeBuffer = Buffer.alloc(1);
748
840
  frameTypeBuffer.writeUInt8(1, 0);
@@ -754,26 +846,27 @@ var DataStreamConnection = /** @class */ (function (_super) {
754
846
  var encrypted = hapCrypto.chacha20_poly1305_encryptAndSeal(this.accessoryToControllerEncryptionKey, this.accessoryToControllerNonceBuffer, frameHeader, payloadBuffer);
755
847
  this.socket.write(Buffer.concat([frameHeader, encrypted.ciphertext, encrypted.authTag]));
756
848
  /* Useful for debugging outgoing packages and detecting encoding errors
757
- console.log("SENT DATA: " + payloadBuffer.toString("hex"));
758
- const frame: HDSFrame = {
759
- header: frameHeader,
760
- plaintextPayload: payloadBuffer,
761
- cipheredPayload: cipheredPayload,
762
- authTag: authTag,
763
- };
764
- const sentMessage = this.decodePayloads([frame])[0];
765
- console.log("Sent message: " + JSON.stringify(sentMessage, null, 4));
766
- //*/
849
+ console.log("SENT DATA: " + payloadBuffer.toString("hex"));
850
+ const frame: HDSFrame = {
851
+ header: frameHeader,
852
+ plaintextPayload: payloadBuffer,
853
+ cipheredPayload: cipheredPayload,
854
+ authTag: authTag,
855
+ };
856
+ const sentMessage = this.decodePayloads([frame])[0];
857
+ console.log("Sent message: " + JSON.stringify(sentMessage, null, 4));
858
+ //*/
767
859
  };
768
860
  DataStreamConnection.prototype.close = function () {
769
- var _a;
770
- if (this.state >= 3 /* CLOSING */) {
861
+ if (this.state >= 3 /* ConnectionState.CLOSING */) {
771
862
  return; // connection is already closing/closed
772
863
  }
773
- (_a = this.connection) === null || _a === void 0 ? void 0 : _a.removeListener("closed" /* CLOSED */, this.hapConnectionClosedListener);
774
- this.state = 3 /* CLOSING */;
864
+ this.state = 3 /* ConnectionState.CLOSING */;
775
865
  this.socket.end();
776
866
  };
867
+ DataStreamConnection.prototype.isConsideredClosed = function () {
868
+ return this.state >= 3 /* ConnectionState.CLOSING */;
869
+ };
777
870
  DataStreamConnection.prototype.onHAPSessionClosed = function () {
778
871
  // If the hap connection is closed it is probably also a good idea to close the data stream connection
779
872
  debug("[%s] HAP connection disconnected. Also closing DataStream connection now.", this.remoteAddress);
@@ -784,9 +877,11 @@ var DataStreamConnection = /** @class */ (function (_super) {
784
877
  // onSocketClose will be called next
785
878
  };
786
879
  DataStreamConnection.prototype.onSocketClose = function () {
880
+ var _a;
787
881
  // this instance is now considered completely dead
788
- this.state = 4 /* CLOSED */;
789
- this.emit("closed" /* CLOSED */);
882
+ this.state = 4 /* ConnectionState.CLOSED */;
883
+ this.emit("closed" /* DataStreamConnectionEvent.CLOSED */);
884
+ (_a = this.connection) === null || _a === void 0 ? void 0 : _a.removeListener("closed" /* HAPConnectionEvent.CLOSED */, this.hapConnectionClosedListener);
790
885
  this.removeAllListeners();
791
886
  };
792
887
  DataStreamConnection.MAX_PAYLOAD_LENGTH = 1048575;