@homebridge/hap-nodejs 2.2.4-beta.7 → 2.3.0-beta.0

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 (234) hide show
  1. package/README.md +14 -17
  2. package/dist/accessories/AirConditioner_accessory.js +27 -29
  3. package/dist/accessories/AirConditioner_accessory.js.map +1 -1
  4. package/dist/accessories/AppleTVRemote_accessory.js +61 -71
  5. package/dist/accessories/AppleTVRemote_accessory.js.map +1 -1
  6. package/dist/accessories/Camera_accessory.js +151 -134
  7. package/dist/accessories/Camera_accessory.js.map +1 -1
  8. package/dist/accessories/Fan_accessory.js +18 -21
  9. package/dist/accessories/Fan_accessory.js.map +1 -1
  10. package/dist/accessories/GarageDoorOpener_accessory.js +33 -35
  11. package/dist/accessories/GarageDoorOpener_accessory.js.map +1 -1
  12. package/dist/accessories/Light-AdaptiveLighting_accessory.js +41 -44
  13. package/dist/accessories/Light-AdaptiveLighting_accessory.js.map +1 -1
  14. package/dist/accessories/Light_accessory.js +32 -34
  15. package/dist/accessories/Light_accessory.js.map +1 -1
  16. package/dist/accessories/Lock_accessory.js +24 -26
  17. package/dist/accessories/Lock_accessory.js.map +1 -1
  18. package/dist/accessories/MotionSensor_accessory.js +13 -15
  19. package/dist/accessories/MotionSensor_accessory.js.map +1 -1
  20. package/dist/accessories/Outlet_accessory.js +20 -22
  21. package/dist/accessories/Outlet_accessory.js.map +1 -1
  22. package/dist/accessories/SmartSpeaker_accessory.js +18 -20
  23. package/dist/accessories/SmartSpeaker_accessory.js.map +1 -1
  24. package/dist/accessories/Sprinkler_accessory.js +36 -38
  25. package/dist/accessories/Sprinkler_accessory.js.map +1 -1
  26. package/dist/accessories/TV_accessory.js +43 -45
  27. package/dist/accessories/TV_accessory.js.map +1 -1
  28. package/dist/accessories/TemperatureSensor_accessory.js +12 -14
  29. package/dist/accessories/TemperatureSensor_accessory.js.map +1 -1
  30. package/dist/accessories/Wi-FiRouter_accessory.d.ts +1 -1
  31. package/dist/accessories/Wi-FiRouter_accessory.d.ts.map +1 -1
  32. package/dist/accessories/Wi-FiRouter_accessory.js +9 -12
  33. package/dist/accessories/Wi-FiRouter_accessory.js.map +1 -1
  34. package/dist/accessories/Wi-FiSatellite_accessory.d.ts +1 -1
  35. package/dist/accessories/Wi-FiSatellite_accessory.d.ts.map +1 -1
  36. package/dist/accessories/Wi-FiSatellite_accessory.js +11 -14
  37. package/dist/accessories/Wi-FiSatellite_accessory.js.map +1 -1
  38. package/dist/accessories/gstreamer-audioProducer.d.ts +3 -3
  39. package/dist/accessories/gstreamer-audioProducer.d.ts.map +1 -1
  40. package/dist/accessories/gstreamer-audioProducer.js +35 -36
  41. package/dist/accessories/gstreamer-audioProducer.js.map +1 -1
  42. package/dist/accessories/types.d.ts +63 -63
  43. package/dist/accessories/types.d.ts.map +1 -1
  44. package/dist/accessories/types.js +83 -87
  45. package/dist/accessories/types.js.map +1 -1
  46. package/dist/hap.d.ts +50 -0
  47. package/dist/hap.d.ts.map +1 -0
  48. package/dist/hap.js +49 -0
  49. package/dist/hap.js.map +1 -0
  50. package/dist/index.d.ts +3 -49
  51. package/dist/index.d.ts.map +1 -1
  52. package/dist/index.js +5 -53
  53. package/dist/index.js.map +1 -1
  54. package/dist/internal-types.d.ts +1 -1
  55. package/dist/internal-types.d.ts.map +1 -1
  56. package/dist/internal-types.js +19 -19
  57. package/dist/internal-types.js.map +1 -1
  58. package/dist/lib/Accessory.d.ts +37 -37
  59. package/dist/lib/Accessory.d.ts.map +1 -1
  60. package/dist/lib/Accessory.js +265 -257
  61. package/dist/lib/Accessory.js.map +1 -1
  62. package/dist/lib/Advertiser.d.ts +6 -6
  63. package/dist/lib/Advertiser.d.ts.map +1 -1
  64. package/dist/lib/Advertiser.js +143 -139
  65. package/dist/lib/Advertiser.js.map +1 -1
  66. package/dist/lib/Bridge.d.ts +1 -1
  67. package/dist/lib/Bridge.d.ts.map +1 -1
  68. package/dist/lib/Bridge.js +2 -6
  69. package/dist/lib/Bridge.js.map +1 -1
  70. package/dist/lib/Characteristic.d.ts +40 -41
  71. package/dist/lib/Characteristic.d.ts.map +1 -1
  72. package/dist/lib/Characteristic.js +265 -216
  73. package/dist/lib/Characteristic.js.map +1 -1
  74. package/dist/lib/HAPServer.d.ts +30 -29
  75. package/dist/lib/HAPServer.d.ts.map +1 -1
  76. package/dist/lib/HAPServer.js +223 -224
  77. package/dist/lib/HAPServer.js.map +1 -1
  78. package/dist/lib/Service.d.ts +21 -22
  79. package/dist/lib/Service.d.ts.map +1 -1
  80. package/dist/lib/Service.js +60 -68
  81. package/dist/lib/Service.js.map +1 -1
  82. package/dist/lib/camera/RTPProxy.d.ts +2 -1
  83. package/dist/lib/camera/RTPProxy.d.ts.map +1 -1
  84. package/dist/lib/camera/RTPProxy.js +35 -37
  85. package/dist/lib/camera/RTPProxy.js.map +1 -1
  86. package/dist/lib/camera/RTPStreamManagement.d.ts +34 -33
  87. package/dist/lib/camera/RTPStreamManagement.d.ts.map +1 -1
  88. package/dist/lib/camera/RTPStreamManagement.js +178 -132
  89. package/dist/lib/camera/RTPStreamManagement.js.map +1 -1
  90. package/dist/lib/camera/RecordingManagement.d.ts +18 -17
  91. package/dist/lib/camera/RecordingManagement.d.ts.map +1 -1
  92. package/dist/lib/camera/RecordingManagement.js +182 -154
  93. package/dist/lib/camera/RecordingManagement.js.map +1 -1
  94. package/dist/lib/camera/index.d.ts +3 -3
  95. package/dist/lib/camera/index.d.ts.map +1 -1
  96. package/dist/lib/camera/index.js +3 -6
  97. package/dist/lib/camera/index.js.map +1 -1
  98. package/dist/lib/controller/AdaptiveLightingController.d.ts +8 -8
  99. package/dist/lib/controller/AdaptiveLightingController.d.ts.map +1 -1
  100. package/dist/lib/controller/AdaptiveLightingController.js +111 -99
  101. package/dist/lib/controller/AdaptiveLightingController.js.map +1 -1
  102. package/dist/lib/controller/CameraController.d.ts +21 -20
  103. package/dist/lib/controller/CameraController.d.ts.map +1 -1
  104. package/dist/lib/controller/CameraController.js +82 -80
  105. package/dist/lib/controller/CameraController.js.map +1 -1
  106. package/dist/lib/controller/Controller.d.ts +1 -1
  107. package/dist/lib/controller/Controller.d.ts.map +1 -1
  108. package/dist/lib/controller/Controller.js +5 -8
  109. package/dist/lib/controller/Controller.js.map +1 -1
  110. package/dist/lib/controller/DoorbellController.d.ts +4 -4
  111. package/dist/lib/controller/DoorbellController.d.ts.map +1 -1
  112. package/dist/lib/controller/DoorbellController.js +9 -13
  113. package/dist/lib/controller/DoorbellController.js.map +1 -1
  114. package/dist/lib/controller/RemoteController.d.ts +38 -37
  115. package/dist/lib/controller/RemoteController.d.ts.map +1 -1
  116. package/dist/lib/controller/RemoteController.js +188 -158
  117. package/dist/lib/controller/RemoteController.js.map +1 -1
  118. package/dist/lib/controller/index.d.ts +5 -5
  119. package/dist/lib/controller/index.d.ts.map +1 -1
  120. package/dist/lib/controller/index.js +5 -8
  121. package/dist/lib/controller/index.js.map +1 -1
  122. package/dist/lib/datastream/DataStreamManagement.d.ts +2 -2
  123. package/dist/lib/datastream/DataStreamManagement.d.ts.map +1 -1
  124. package/dist/lib/datastream/DataStreamManagement.js +32 -32
  125. package/dist/lib/datastream/DataStreamManagement.js.map +1 -1
  126. package/dist/lib/datastream/DataStreamParser.d.ts +1 -0
  127. package/dist/lib/datastream/DataStreamParser.d.ts.map +1 -1
  128. package/dist/lib/datastream/DataStreamParser.js +53 -69
  129. package/dist/lib/datastream/DataStreamParser.js.map +1 -1
  130. package/dist/lib/datastream/DataStreamServer.d.ts +23 -22
  131. package/dist/lib/datastream/DataStreamServer.d.ts.map +1 -1
  132. package/dist/lib/datastream/DataStreamServer.js +152 -154
  133. package/dist/lib/datastream/DataStreamServer.js.map +1 -1
  134. package/dist/lib/datastream/index.d.ts +3 -3
  135. package/dist/lib/datastream/index.d.ts.map +1 -1
  136. package/dist/lib/datastream/index.js +3 -6
  137. package/dist/lib/datastream/index.js.map +1 -1
  138. package/dist/lib/definitions/CharacteristicDefinitions.d.ts +1 -1
  139. package/dist/lib/definitions/CharacteristicDefinitions.d.ts.map +1 -1
  140. package/dist/lib/definitions/CharacteristicDefinitions.js +1103 -1288
  141. package/dist/lib/definitions/CharacteristicDefinitions.js.map +1 -1
  142. package/dist/lib/definitions/ServiceDefinitions.d.ts +1 -1
  143. package/dist/lib/definitions/ServiceDefinitions.d.ts.map +1 -1
  144. package/dist/lib/definitions/ServiceDefinitions.js +644 -722
  145. package/dist/lib/definitions/ServiceDefinitions.js.map +1 -1
  146. package/dist/lib/definitions/generate-definitions.d.ts +2 -2
  147. package/dist/lib/definitions/generate-definitions.d.ts.map +1 -1
  148. package/dist/lib/definitions/generate-definitions.js +241 -241
  149. package/dist/lib/definitions/generate-definitions.js.map +1 -1
  150. package/dist/lib/definitions/generator-configuration.d.ts +1 -1
  151. package/dist/lib/definitions/generator-configuration.d.ts.map +1 -1
  152. package/dist/lib/definitions/generator-configuration.js +572 -309
  153. package/dist/lib/definitions/generator-configuration.js.map +1 -1
  154. package/dist/lib/definitions/index.d.ts +2 -2
  155. package/dist/lib/definitions/index.d.ts.map +1 -1
  156. package/dist/lib/definitions/index.js +2 -5
  157. package/dist/lib/definitions/index.js.map +1 -1
  158. package/dist/lib/gen/HomeKit.js +1 -2
  159. package/dist/lib/gen/HomeKit.js.map +1 -1
  160. package/dist/lib/model/AccessoryInfo.d.ts +4 -3
  161. package/dist/lib/model/AccessoryInfo.d.ts.map +1 -1
  162. package/dist/lib/model/AccessoryInfo.js +53 -56
  163. package/dist/lib/model/AccessoryInfo.js.map +1 -1
  164. package/dist/lib/model/ControllerStorage.d.ts +3 -3
  165. package/dist/lib/model/ControllerStorage.d.ts.map +1 -1
  166. package/dist/lib/model/ControllerStorage.js +31 -30
  167. package/dist/lib/model/ControllerStorage.js.map +1 -1
  168. package/dist/lib/model/HAPFileStorage.d.ts.map +1 -1
  169. package/dist/lib/model/HAPFileStorage.js +71 -50
  170. package/dist/lib/model/HAPFileStorage.js.map +1 -1
  171. package/dist/lib/model/HAPStorage.d.ts +1 -1
  172. package/dist/lib/model/HAPStorage.d.ts.map +1 -1
  173. package/dist/lib/model/HAPStorage.js +11 -14
  174. package/dist/lib/model/HAPStorage.js.map +1 -1
  175. package/dist/lib/model/IdentifierCache.d.ts +1 -1
  176. package/dist/lib/model/IdentifierCache.d.ts.map +1 -1
  177. package/dist/lib/model/IdentifierCache.js +23 -27
  178. package/dist/lib/model/IdentifierCache.js.map +1 -1
  179. package/dist/lib/tv/AccessControlManagement.d.ts +6 -6
  180. package/dist/lib/tv/AccessControlManagement.d.ts.map +1 -1
  181. package/dist/lib/tv/AccessControlManagement.js +23 -24
  182. package/dist/lib/tv/AccessControlManagement.js.map +1 -1
  183. package/dist/lib/util/checkName.d.ts +2 -2
  184. package/dist/lib/util/checkName.d.ts.map +1 -1
  185. package/dist/lib/util/checkName.js +8 -14
  186. package/dist/lib/util/checkName.js.map +1 -1
  187. package/dist/lib/util/clone.d.ts.map +1 -1
  188. package/dist/lib/util/clone.js +1 -5
  189. package/dist/lib/util/clone.js.map +1 -1
  190. package/dist/lib/util/color-utils.d.ts +1 -1
  191. package/dist/lib/util/color-utils.d.ts.map +1 -1
  192. package/dist/lib/util/color-utils.js +1608 -410
  193. package/dist/lib/util/color-utils.js.map +1 -1
  194. package/dist/lib/util/eventedhttp.d.ts +23 -22
  195. package/dist/lib/util/eventedhttp.d.ts.map +1 -1
  196. package/dist/lib/util/eventedhttp.js +127 -122
  197. package/dist/lib/util/eventedhttp.js.map +1 -1
  198. package/dist/lib/util/hapCrypto.d.ts +2 -1
  199. package/dist/lib/util/hapCrypto.d.ts.map +1 -1
  200. package/dist/lib/util/hapCrypto.js +72 -59
  201. package/dist/lib/util/hapCrypto.js.map +1 -1
  202. package/dist/lib/util/hapStatusError.d.ts +1 -1
  203. package/dist/lib/util/hapStatusError.d.ts.map +1 -1
  204. package/dist/lib/util/hapStatusError.js +4 -8
  205. package/dist/lib/util/hapStatusError.js.map +1 -1
  206. package/dist/lib/util/net-utils.js +13 -19
  207. package/dist/lib/util/net-utils.js.map +1 -1
  208. package/dist/lib/util/once.js +3 -6
  209. package/dist/lib/util/once.js.map +1 -1
  210. package/dist/lib/util/promise-utils.d.ts +1 -1
  211. package/dist/lib/util/promise-utils.d.ts.map +1 -1
  212. package/dist/lib/util/promise-utils.js +9 -11
  213. package/dist/lib/util/promise-utils.js.map +1 -1
  214. package/dist/lib/util/request-util.d.ts +3 -3
  215. package/dist/lib/util/request-util.d.ts.map +1 -1
  216. package/dist/lib/util/request-util.js +13 -22
  217. package/dist/lib/util/request-util.js.map +1 -1
  218. package/dist/lib/util/time.d.ts +1 -0
  219. package/dist/lib/util/time.d.ts.map +1 -1
  220. package/dist/lib/util/time.js +6 -11
  221. package/dist/lib/util/time.js.map +1 -1
  222. package/dist/lib/util/tlv.d.ts +1 -0
  223. package/dist/lib/util/tlv.d.ts.map +1 -1
  224. package/dist/lib/util/tlv.js +38 -44
  225. package/dist/lib/util/tlv.js.map +1 -1
  226. package/dist/lib/util/uuid.d.ts +1 -0
  227. package/dist/lib/util/uuid.d.ts.map +1 -1
  228. package/dist/lib/util/uuid.js +26 -38
  229. package/dist/lib/util/uuid.js.map +1 -1
  230. package/dist/types.d.ts +24 -24
  231. package/dist/types.d.ts.map +1 -1
  232. package/dist/types.js +3 -5
  233. package/dist/types.js.map +1 -1
  234. package/package.json +49 -42
@@ -1,41 +1,39 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HAPServer = exports.HAPServerEventTypes = exports.HAPPairingHTTPCode = exports.HAPHTTPCode = exports.HAPStatus = exports.TLVErrorCode = void 0;
4
- exports.IsKnownHAPStatusError = IsKnownHAPStatusError;
5
- const tslib_1 = require("tslib");
6
- const crypto_1 = tslib_1.__importDefault(require("crypto"));
7
- const debug_1 = tslib_1.__importDefault(require("debug"));
8
- const events_1 = require("events");
9
- const fast_srp_hap_1 = require("fast-srp-hap");
10
- const url_1 = require("url");
11
- const internal_types_1 = require("../internal-types");
12
- const eventedhttp_1 = require("./util/eventedhttp");
13
- const hapCrypto = tslib_1.__importStar(require("./util/hapCrypto"));
14
- const once_1 = require("./util/once");
15
- const tlv = tslib_1.__importStar(require("./util/tlv"));
16
- const debug = (0, debug_1.default)("HAP-NodeJS:HAPServer");
1
+ import { Buffer } from 'node:buffer';
2
+ import crypto from 'node:crypto';
3
+ import { EventEmitter } from 'node:events';
4
+ import { URL } from 'node:url';
5
+ import createDebug from 'debug';
6
+ import { SRP, SrpServer } from 'fast-srp-hap';
7
+ import { consideredTrue } from '../internal-types.js';
8
+ import { EventedHTTPServer, HAPEncryption } from './util/eventedhttp.js';
9
+ import * as hapCrypto from './util/hapCrypto.js';
10
+ import { once } from './util/once.js';
11
+ import * as tlv from './util/tlv.js';
12
+ const debug = createDebug('HAP-NodeJS:HAPServer');
17
13
  /**
18
14
  * TLV error codes for the `TLVValues.ERROR_CODE` field.
19
15
  *
20
16
  * @group HAP Accessory Server
21
17
  */
22
- var TLVErrorCode;
18
+ // eslint-disable-next-line no-restricted-syntax
19
+ export var TLVErrorCode;
23
20
  (function (TLVErrorCode) {
24
21
  // noinspection JSUnusedGlobalSymbols
25
22
  TLVErrorCode[TLVErrorCode["UNKNOWN"] = 1] = "UNKNOWN";
26
23
  TLVErrorCode[TLVErrorCode["INVALID_REQUEST"] = 2] = "INVALID_REQUEST";
27
- // eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values
24
+ // eslint-disable-next-line ts/no-duplicate-enum-values
28
25
  TLVErrorCode[TLVErrorCode["AUTHENTICATION"] = 2] = "AUTHENTICATION";
29
26
  TLVErrorCode[TLVErrorCode["BACKOFF"] = 3] = "BACKOFF";
30
27
  TLVErrorCode[TLVErrorCode["MAX_PEERS"] = 4] = "MAX_PEERS";
31
28
  TLVErrorCode[TLVErrorCode["MAX_TRIES"] = 5] = "MAX_TRIES";
32
29
  TLVErrorCode[TLVErrorCode["UNAVAILABLE"] = 6] = "UNAVAILABLE";
33
- TLVErrorCode[TLVErrorCode["BUSY"] = 7] = "BUSY"; // cannot accept pairing request at this time
34
- })(TLVErrorCode || (exports.TLVErrorCode = TLVErrorCode = {}));
30
+ TLVErrorCode[TLVErrorCode["BUSY"] = 7] = "BUSY";
31
+ })(TLVErrorCode || (TLVErrorCode = {}));
35
32
  /**
36
33
  * @group HAP Accessory Server
37
34
  */
38
- var HAPStatus;
35
+ // eslint-disable-next-line no-restricted-syntax
36
+ export var HAPStatus;
39
37
  (function (HAPStatus) {
40
38
  // noinspection JSUnusedGlobalSymbols
41
39
  /**
@@ -91,18 +89,18 @@ var HAPStatus;
91
89
  */
92
90
  HAPStatus[HAPStatus["NOT_ALLOWED_IN_CURRENT_STATE"] = -70412] = "NOT_ALLOWED_IN_CURRENT_STATE";
93
91
  // when adding new status codes, remember to update bounds in IsKnownHAPStatusError below
94
- })(HAPStatus || (exports.HAPStatus = HAPStatus = {}));
92
+ })(HAPStatus || (HAPStatus = {}));
95
93
  /**
96
94
  * Determines if the given status code is a known {@link HAPStatus} error code.
97
95
  *
98
96
  * @group HAP Accessory Server
99
97
  */
100
- function IsKnownHAPStatusError(status) {
98
+ export function IsKnownHAPStatusError(status) {
101
99
  return (
102
100
  // Lower bound (most negative error code)
103
- status >= -70412 /* HAPStatus.NOT_ALLOWED_IN_CURRENT_STATE */ &&
101
+ status >= -70412 /* HAPStatus.NOT_ALLOWED_IN_CURRENT_STATE */
104
102
  // Upper bound (negative error code closest to zero)
105
- status <= -70401 /* HAPStatus.INSUFFICIENT_PRIVILEGES */);
103
+ && status <= -70401 /* HAPStatus.INSUFFICIENT_PRIVILEGES */);
106
104
  }
107
105
  /**
108
106
  * Those status codes are the one listed as appropriate for the HAP spec!
@@ -114,7 +112,8 @@ function IsKnownHAPStatusError(status) {
114
112
  *
115
113
  * @group HAP Accessory Server
116
114
  */
117
- var HAPHTTPCode;
115
+ // eslint-disable-next-line no-restricted-syntax
116
+ export var HAPHTTPCode;
118
117
  (function (HAPHTTPCode) {
119
118
  // noinspection JSUnusedGlobalSymbols
120
119
  HAPHTTPCode[HAPHTTPCode["OK"] = 200] = "OK";
@@ -127,14 +126,15 @@ var HAPHTTPCode;
127
126
  // server error
128
127
  HAPHTTPCode[HAPHTTPCode["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
129
128
  HAPHTTPCode[HAPHTTPCode["SERVICE_UNAVAILABLE"] = 503] = "SERVICE_UNAVAILABLE";
130
- })(HAPHTTPCode || (exports.HAPHTTPCode = HAPHTTPCode = {}));
129
+ })(HAPHTTPCode || (HAPHTTPCode = {}));
131
130
  /**
132
131
  * When in a request is made to the pairing endpoints, and mime type is 'application/pairing+tlv8'
133
132
  * one should use the below status codes.
134
133
  *
135
134
  * @group HAP Accessory Server
136
135
  */
137
- var HAPPairingHTTPCode;
136
+ // eslint-disable-next-line no-restricted-syntax
137
+ export var HAPPairingHTTPCode;
138
138
  (function (HAPPairingHTTPCode) {
139
139
  // noinspection JSUnusedGlobalSymbols
140
140
  HAPPairingHTTPCode[HAPPairingHTTPCode["OK"] = 200] = "OK";
@@ -143,11 +143,12 @@ var HAPPairingHTTPCode;
143
143
  HAPPairingHTTPCode[HAPPairingHTTPCode["TOO_MANY_REQUESTS"] = 429] = "TOO_MANY_REQUESTS";
144
144
  HAPPairingHTTPCode[HAPPairingHTTPCode["CONNECTION_AUTHORIZATION_REQUIRED"] = 470] = "CONNECTION_AUTHORIZATION_REQUIRED";
145
145
  HAPPairingHTTPCode[HAPPairingHTTPCode["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
146
- })(HAPPairingHTTPCode || (exports.HAPPairingHTTPCode = HAPPairingHTTPCode = {}));
146
+ })(HAPPairingHTTPCode || (HAPPairingHTTPCode = {}));
147
147
  /**
148
148
  * @group HAP Accessory Server
149
149
  */
150
- var HAPServerEventTypes;
150
+ // eslint-disable-next-line no-restricted-syntax
151
+ export var HAPServerEventTypes;
151
152
  (function (HAPServerEventTypes) {
152
153
  /**
153
154
  * Emitted when the server is fully set up and ready to receive connections.
@@ -190,7 +191,7 @@ var HAPServerEventTypes;
190
191
  HAPServerEventTypes["SET_CHARACTERISTICS"] = "set-characteristics";
191
192
  HAPServerEventTypes["REQUEST_RESOURCE"] = "request-resource";
192
193
  HAPServerEventTypes["CONNECTION_CLOSED"] = "connection-closed";
193
- })(HAPServerEventTypes || (exports.HAPServerEventTypes = HAPServerEventTypes = {}));
194
+ })(HAPServerEventTypes || (HAPServerEventTypes = {}));
194
195
  /**
195
196
  * The actual HAP server that iOS devices talk to.
196
197
  *
@@ -212,8 +213,8 @@ var HAPServerEventTypes;
212
213
  *
213
214
  * @group HAP Accessory Server
214
215
  */
215
- // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
216
- class HAPServer extends events_1.EventEmitter {
216
+ // eslint-disable-next-line ts/no-unsafe-declaration-merging
217
+ export class HAPServer extends EventEmitter {
217
218
  accessoryInfo;
218
219
  httpServer;
219
220
  /**
@@ -233,13 +234,13 @@ class HAPServer extends events_1.EventEmitter {
233
234
  this.accessoryInfo = accessoryInfo;
234
235
  this.allowInsecureRequest = false;
235
236
  // internal server that does all the actual communication
236
- this.httpServer = new eventedhttp_1.EventedHTTPServer();
237
+ this.httpServer = new EventedHTTPServer();
237
238
  this.httpServer.on("listening" /* EventedHTTPServerEvent.LISTENING */, this.onListening.bind(this));
238
239
  this.httpServer.on("request" /* EventedHTTPServerEvent.REQUEST */, this.handleRequestOnHAPConnection.bind(this));
239
240
  this.httpServer.on("connection-closed" /* EventedHTTPServerEvent.CONNECTION_CLOSED */, this.handleConnectionClosed.bind(this));
240
241
  }
241
242
  listen(port = 0, host) {
242
- if (host === "::") {
243
+ if (host === '::') {
243
244
  // this will work around "EAFNOSUPPORT: address family not supported" errors
244
245
  // on systems where IPv6 is not supported/enabled, we just use the node default then by supplying undefined
245
246
  host = undefined;
@@ -271,14 +272,14 @@ class HAPServer extends events_1.EventEmitter {
271
272
  * @param value - The newly set value of the characteristic.
272
273
  * @param originator - If specified, the connection will not get an event message.
273
274
  * @param immediateDelivery - The HAP spec requires some characteristics to be delivery immediately.
274
- * Namely, for the {@link Characteristic.ButtonEvent} and the {@link Characteristic.ProgrammableSwitchEvent} characteristics.
275
+ * Namely, for the {@link Characteristic.ButtonEvent} and the {@link Characteristic.ProgrammableSwitchEvent} characteristics.
275
276
  */
276
277
  sendEventNotifications(aid, iid, value, originator, immediateDelivery) {
277
278
  try {
278
279
  this.httpServer.broadcastEvent(aid, iid, value, originator, immediateDelivery);
279
280
  }
280
281
  catch (error) {
281
- console.warn("[" + this.accessoryInfo.username + "] Error when sending event notifications: " + error.message);
282
+ console.warn(`[${this.accessoryInfo.username}] Error when sending event notifications: ${error.message}`);
282
283
  }
283
284
  }
284
285
  onListening(port, hostname) {
@@ -286,14 +287,14 @@ class HAPServer extends events_1.EventEmitter {
286
287
  }
287
288
  // Called when an HTTP request was detected.
288
289
  handleRequestOnHAPConnection(connection, request, response) {
289
- debug("[%s] HAP Request: %s %s", this.accessoryInfo.username, request.method, request.url);
290
+ debug('[%s] HAP Request: %s %s', this.accessoryInfo.username, request.method, request.url);
290
291
  const maxBodySize = connection.isAuthenticated() || this.allowInsecureRequest
291
292
  ? HAPServer.MAX_REQUEST_BODY_SIZE
292
293
  : HAPServer.MAX_UNAUTHENTICATED_REQUEST_BODY_SIZE;
293
294
  const buffers = [];
294
295
  let bodySize = 0;
295
296
  let bodyTooLarge = false;
296
- request.on("data", (data) => {
297
+ request.on('data', (data) => {
297
298
  if (bodyTooLarge) {
298
299
  return;
299
300
  }
@@ -301,33 +302,33 @@ class HAPServer extends events_1.EventEmitter {
301
302
  if (bodySize > maxBodySize) {
302
303
  bodyTooLarge = true;
303
304
  buffers.length = 0;
304
- debug("[%s] Rejecting request %s as its body exceeds %d bytes", this.accessoryInfo.username, request.url, maxBodySize);
305
+ debug('[%s] Rejecting request %s as its body exceeds %d bytes', this.accessoryInfo.username, request.url, maxBodySize);
305
306
  // closing the connection stops the client from sending the rest of the body
306
- response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */, "Connection": "close" });
307
+ response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */, 'Connection': 'close' });
307
308
  response.end(JSON.stringify({ status: -70407 /* HAPStatus.OUT_OF_RESOURCE */ }));
308
309
  return;
309
310
  }
310
311
  buffers.push(data);
311
312
  });
312
- request.on("end", () => {
313
+ request.on('end', () => {
313
314
  if (bodyTooLarge) {
314
315
  return;
315
316
  }
316
317
  let url;
317
318
  try {
318
- url = new url_1.URL(request.url, "http://hap-nodejs.local"); // parse the url (query strings etc)
319
+ url = new URL(request.url, 'http://hap-nodejs.local'); // parse the url (query strings etc)
319
320
  }
320
321
  catch {
321
322
  // e.g. "//[" parses as an invalid host. this runs in an event listener, so a throw here would crash the process
322
- debug("[%s] Received request with an invalid url: %s", this.accessoryInfo.username, request.url);
323
- response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
323
+ debug('[%s] Received request with an invalid url: %s', this.accessoryInfo.username, request.url);
324
+ response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
324
325
  response.end(JSON.stringify({ status: -70410 /* HAPStatus.INVALID_VALUE_IN_REQUEST */ }));
325
326
  return;
326
327
  }
327
328
  const handler = this.getHandler(url);
328
329
  if (!handler) {
329
- debug("[%s] WARNING: Handler for %s not implemented", this.accessoryInfo.username, request.url);
330
- response.writeHead(404 /* HAPHTTPCode.NOT_FOUND */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
330
+ debug('[%s] WARNING: Handler for %s not implemented', this.accessoryInfo.username, request.url);
331
+ response.writeHead(404 /* HAPHTTPCode.NOT_FOUND */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
331
332
  response.end(JSON.stringify({ status: -70409 /* HAPStatus.RESOURCE_DOES_NOT_EXIST */ }));
332
333
  }
333
334
  else {
@@ -336,8 +337,8 @@ class HAPServer extends events_1.EventEmitter {
336
337
  handler(connection, url, request, data, response);
337
338
  }
338
339
  catch (error) {
339
- debug("[%s] Error executing route handler: %s", this.accessoryInfo.username, error.stack);
340
- response.writeHead(500 /* HAPHTTPCode.INTERNAL_SERVER_ERROR */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
340
+ debug('[%s] Error executing route handler: %s', this.accessoryInfo.username, error.stack);
341
+ response.writeHead(500 /* HAPHTTPCode.INTERNAL_SERVER_ERROR */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
341
342
  response.end(JSON.stringify({ status: -70403 /* HAPStatus.RESOURCE_BUSY */ })); // resource busy try again, does somehow fit?
342
343
  }
343
344
  }
@@ -348,21 +349,21 @@ class HAPServer extends events_1.EventEmitter {
348
349
  }
349
350
  getHandler(url) {
350
351
  switch (url.pathname.toLowerCase()) {
351
- case "/identify":
352
+ case '/identify':
352
353
  return this.handleIdentifyRequest.bind(this);
353
- case "/pair-setup":
354
+ case '/pair-setup':
354
355
  return this.handlePairSetup.bind(this);
355
- case "/pair-verify":
356
+ case '/pair-verify':
356
357
  return this.handlePairVerify.bind(this);
357
- case "/pairings":
358
+ case '/pairings':
358
359
  return this.handlePairings.bind(this);
359
- case "/accessories":
360
+ case '/accessories':
360
361
  return this.handleAccessories.bind(this);
361
- case "/characteristics":
362
+ case '/characteristics':
362
363
  return this.handleCharacteristics.bind(this);
363
- case "/prepare":
364
+ case '/prepare':
364
365
  return this.handlePrepareWrite.bind(this);
365
- case "/resource":
366
+ case '/resource':
366
367
  return this.handleResource.bind(this);
367
368
  default:
368
369
  return undefined;
@@ -374,19 +375,19 @@ class HAPServer extends events_1.EventEmitter {
374
375
  handleIdentifyRequest(connection, url, request, data, response) {
375
376
  // POST body is empty
376
377
  if (this.accessoryInfo.paired() && !this.allowInsecureRequest) {
377
- response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
378
+ response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
378
379
  response.end(JSON.stringify({ status: -70401 /* HAPStatus.INSUFFICIENT_PRIVILEGES */ }));
379
380
  return;
380
381
  }
381
- this.emit("identify" /* HAPServerEventTypes.IDENTIFY */, (0, once_1.once)(err => {
382
+ this.emit("identify" /* HAPServerEventTypes.IDENTIFY */, once((err) => {
382
383
  if (!err) {
383
- debug("[%s] Identification success", this.accessoryInfo.username);
384
+ debug('[%s] Identification success', this.accessoryInfo.username);
384
385
  response.writeHead(204 /* HAPHTTPCode.NO_CONTENT */);
385
386
  response.end();
386
387
  }
387
388
  else {
388
- debug("[%s] Identification error: %s", this.accessoryInfo.username, err.message);
389
- response.writeHead(500 /* HAPHTTPCode.INTERNAL_SERVER_ERROR */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
389
+ debug('[%s] Identification error: %s', this.accessoryInfo.username, err.message);
390
+ response.writeHead(500 /* HAPHTTPCode.INTERNAL_SERVER_ERROR */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
390
391
  response.end(JSON.stringify({ status: -70403 /* HAPStatus.RESOURCE_BUSY */ }));
391
392
  }
392
393
  }));
@@ -394,13 +395,13 @@ class HAPServer extends events_1.EventEmitter {
394
395
  handlePairSetup(connection, url, request, data, response) {
395
396
  // Can only be directly paired with one iOS device
396
397
  if (!this.allowInsecureRequest && this.accessoryInfo.paired()) {
397
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
398
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
398
399
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 6 /* TLVErrorCode.UNAVAILABLE */));
399
400
  return;
400
401
  }
401
402
  if (this.unsuccessfulPairAttempts > 100) {
402
- debug("[%s] Reached maximum amount of unsuccessful pair attempts!", this.accessoryInfo.username);
403
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
403
+ debug('[%s] Reached maximum amount of unsuccessful pair attempts!', this.accessoryInfo.username);
404
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
404
405
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 5 /* TLVErrorCode.MAX_TRIES */));
405
406
  return;
406
407
  }
@@ -409,13 +410,13 @@ class HAPServer extends events_1.EventEmitter {
409
410
  tlvData = tlv.decode(data);
410
411
  }
411
412
  catch (error) {
412
- debug("[%s] Pair-Setup: failed to decode TLV request: %s", this.accessoryInfo.username, error.message);
413
- response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { "Content-Type": "application/pairing+tlv8" });
413
+ debug('[%s] Pair-Setup: failed to decode TLV request: %s', this.accessoryInfo.username, error.message);
414
+ response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
414
415
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
415
416
  return;
416
417
  }
417
418
  if (!tlvData[6 /* TLVValues.SEQUENCE_NUM */]) {
418
- response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { "Content-Type": "application/pairing+tlv8" });
419
+ response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
419
420
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
420
421
  return;
421
422
  }
@@ -431,33 +432,31 @@ class HAPServer extends events_1.EventEmitter {
431
432
  }
432
433
  else {
433
434
  // Invalid state/sequence number
434
- response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { "Content-Type": "application/pairing+tlv8" });
435
+ response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
435
436
  response.end(tlv.encode(6 /* TLVValues.STATE */, sequence + 1, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
436
- return;
437
437
  }
438
438
  }
439
439
  handlePairSetupM1(connection, request, response) {
440
- debug("[%s] Pair step 1/5", this.accessoryInfo.username);
441
- const salt = crypto_1.default.randomBytes(16);
442
- const srpParams = fast_srp_hap_1.SRP.params.hap;
443
- fast_srp_hap_1.SRP.genKey(32).then(key => {
440
+ debug('[%s] Pair step 1/5', this.accessoryInfo.username);
441
+ const salt = crypto.randomBytes(16);
442
+ const srpParams = SRP.params.hap;
443
+ SRP.genKey(32).then((key) => {
444
444
  // create a new SRP server
445
- const srpServer = new fast_srp_hap_1.SrpServer(srpParams, salt, Buffer.from("Pair-Setup"), Buffer.from(this.accessoryInfo.pincode), key);
445
+ const srpServer = new SrpServer(srpParams, salt, Buffer.from('Pair-Setup'), Buffer.from(this.accessoryInfo.pincode), key);
446
446
  const srpB = srpServer.computeB();
447
447
  // attach it to the current TCP session
448
448
  connection.srpServer = srpServer;
449
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
449
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
450
450
  response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 2 /* PairingStates.M2 */, 2 /* TLVValues.SALT */, salt, 3 /* TLVValues.PUBLIC_KEY */, srpB));
451
451
  connection._pairSetupState = 2 /* PairingStates.M2 */;
452
- }).catch(error => {
453
- debug("[%s] Error occurred when generating srp key: %s", this.accessoryInfo.username, error.message);
454
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
452
+ }).catch((error) => {
453
+ debug('[%s] Error occurred when generating srp key: %s', this.accessoryInfo.username, error.message);
454
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
455
455
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
456
- return;
457
456
  });
458
457
  }
459
458
  handlePairSetupM3(connection, request, response, tlvData) {
460
- debug("[%s] Pair step 2/5", this.accessoryInfo.username);
459
+ debug('[%s] Pair step 2/5', this.accessoryInfo.username);
461
460
  const A = tlvData[3 /* TLVValues.PUBLIC_KEY */]; // "A is a public key that exists only for a single login session."
462
461
  const M1 = tlvData[4 /* TLVValues.PASSWORD_PROOF */]; // "M1 is the proof that you actually know your own password."
463
462
  // pull the SRP server we created in stepOne out of the current session
@@ -469,26 +468,26 @@ class HAPServer extends events_1.EventEmitter {
469
468
  catch (err) {
470
469
  // most likely the client supplied an incorrect pincode.
471
470
  this.unsuccessfulPairAttempts++;
472
- debug("[%s] Error while checking pincode: %s", this.accessoryInfo.username, err.message);
473
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
471
+ debug('[%s] Error while checking pincode: %s', this.accessoryInfo.username, err.message);
472
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
474
473
  response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
475
474
  connection._pairSetupState = undefined;
476
475
  return;
477
476
  }
478
477
  // "M2 is the proof that the server actually knows your password."
479
478
  const M2 = srpServer.computeM2();
480
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
479
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
481
480
  response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */, 4 /* TLVValues.PASSWORD_PROOF */, M2));
482
481
  connection._pairSetupState = 4 /* PairingStates.M4 */;
483
482
  }
484
483
  handlePairSetupM5(connection, request, response, tlvData) {
485
- debug("[%s] Pair step 3/5", this.accessoryInfo.username);
484
+ debug('[%s] Pair step 3/5', this.accessoryInfo.username);
486
485
  // pull the SRP server we created in stepOne out of the current session
487
486
  const srpServer = connection.srpServer;
488
487
  const encryptedData = tlvData[5 /* TLVValues.ENCRYPTED_DATA */];
489
488
  if (!encryptedData || encryptedData.length < 16) {
490
- debug("[%s] M5: Encrypted data missing or too short", this.accessoryInfo.username);
491
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
489
+ debug('[%s] M5: Encrypted data missing or too short', this.accessoryInfo.username);
490
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
492
491
  response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
493
492
  connection._pairSetupState = undefined;
494
493
  return;
@@ -498,16 +497,16 @@ class HAPServer extends events_1.EventEmitter {
498
497
  encryptedData.copy(messageData, 0, 0, encryptedData.length - 16);
499
498
  encryptedData.copy(authTagData, 0, encryptedData.length - 16, encryptedData.length);
500
499
  const S_private = srpServer.computeK();
501
- const encSalt = Buffer.from("Pair-Setup-Encrypt-Salt");
502
- const encInfo = Buffer.from("Pair-Setup-Encrypt-Info");
503
- const outputKey = hapCrypto.HKDF("sha512", encSalt, S_private, encInfo, 32);
500
+ const encSalt = Buffer.from('Pair-Setup-Encrypt-Salt');
501
+ const encInfo = Buffer.from('Pair-Setup-Encrypt-Info');
502
+ const outputKey = hapCrypto.HKDF('sha512', encSalt, S_private, encInfo, 32);
504
503
  let plaintext;
505
504
  try {
506
- plaintext = hapCrypto.chacha20_poly1305_decryptAndVerify(outputKey, Buffer.from("PS-Msg05"), null, messageData, authTagData);
505
+ plaintext = hapCrypto.chacha20_poly1305_decryptAndVerify(outputKey, Buffer.from('PS-Msg05'), null, messageData, authTagData);
507
506
  }
508
507
  catch (error) {
509
- debug("[%s] Error while decrypting and verifying M5 subTlv: %s", this.accessoryInfo.username, error);
510
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
508
+ debug('[%s] Error while decrypting and verifying M5 subTlv: %s', this.accessoryInfo.username, error);
509
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
511
510
  response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
512
511
  connection._pairSetupState = undefined;
513
512
  return;
@@ -518,8 +517,8 @@ class HAPServer extends events_1.EventEmitter {
518
517
  M5Packet = tlv.decode(plaintext);
519
518
  }
520
519
  catch (error) {
521
- debug("[%s] M5: Failed to decode decrypted TLV payload: %s", this.accessoryInfo.username, error.message);
522
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
520
+ debug('[%s] M5: Failed to decode decrypted TLV payload: %s', this.accessoryInfo.username, error.message);
521
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
523
522
  response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
524
523
  connection._pairSetupState = undefined;
525
524
  return;
@@ -528,8 +527,8 @@ class HAPServer extends events_1.EventEmitter {
528
527
  const clientLTPK = M5Packet[3 /* TLVValues.PUBLIC_KEY */];
529
528
  const clientProof = M5Packet[10 /* TLVValues.PROOF */];
530
529
  if (!clientUsername || !clientLTPK || !clientProof) {
531
- debug("[%s] M5: Missing required TLV fields in decrypted payload", this.accessoryInfo.username);
532
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
530
+ debug('[%s] M5: Missing required TLV fields in decrypted payload', this.accessoryInfo.username);
531
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
533
532
  response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
534
533
  connection._pairSetupState = undefined;
535
534
  return;
@@ -538,15 +537,15 @@ class HAPServer extends events_1.EventEmitter {
538
537
  }
539
538
  // M5-2
540
539
  handlePairSetupM5_2(connection, request, response, clientUsername, clientLTPK, clientProof, hkdfEncKey) {
541
- debug("[%s] Pair step 4/5", this.accessoryInfo.username);
540
+ debug('[%s] Pair step 4/5', this.accessoryInfo.username);
542
541
  const S_private = connection.srpServer.computeK();
543
- const controllerSalt = Buffer.from("Pair-Setup-Controller-Sign-Salt");
544
- const controllerInfo = Buffer.from("Pair-Setup-Controller-Sign-Info");
545
- const outputKey = hapCrypto.HKDF("sha512", controllerSalt, S_private, controllerInfo, 32);
542
+ const controllerSalt = Buffer.from('Pair-Setup-Controller-Sign-Salt');
543
+ const controllerInfo = Buffer.from('Pair-Setup-Controller-Sign-Info');
544
+ const outputKey = hapCrypto.HKDF('sha512', controllerSalt, S_private, controllerInfo, 32);
546
545
  const completeData = Buffer.concat([outputKey, clientUsername, clientLTPK]);
547
546
  if (!hapCrypto.ed25519Verify(completeData, clientProof, clientLTPK)) {
548
- debug("[%s] Invalid signature", this.accessoryInfo.username);
549
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
547
+ debug('[%s] Invalid signature', this.accessoryInfo.username);
548
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
550
549
  response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 6 /* PairingStates.M6 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
551
550
  connection._pairSetupState = undefined;
552
551
  return;
@@ -555,29 +554,29 @@ class HAPServer extends events_1.EventEmitter {
555
554
  }
556
555
  // M5 - F + M6
557
556
  handlePairSetupM5_3(connection, request, response, clientUsername, clientLTPK, hkdfEncKey) {
558
- debug("[%s] Pair step 5/5", this.accessoryInfo.username);
557
+ debug('[%s] Pair step 5/5', this.accessoryInfo.username);
559
558
  const S_private = connection.srpServer.computeK();
560
- const accessorySalt = Buffer.from("Pair-Setup-Accessory-Sign-Salt");
561
- const accessoryInfo = Buffer.from("Pair-Setup-Accessory-Sign-Info");
562
- const outputKey = hapCrypto.HKDF("sha512", accessorySalt, S_private, accessoryInfo, 32);
559
+ const accessorySalt = Buffer.from('Pair-Setup-Accessory-Sign-Salt');
560
+ const accessoryInfo = Buffer.from('Pair-Setup-Accessory-Sign-Info');
561
+ const outputKey = hapCrypto.HKDF('sha512', accessorySalt, S_private, accessoryInfo, 32);
563
562
  const serverLTPK = this.accessoryInfo.signPk;
564
563
  const usernameData = Buffer.from(this.accessoryInfo.username);
565
564
  const material = Buffer.concat([outputKey, usernameData, serverLTPK]);
566
565
  const privateKey = Buffer.from(this.accessoryInfo.signSk);
567
566
  const serverProof = hapCrypto.ed25519Sign(material, privateKey);
568
567
  const message = tlv.encode(1 /* TLVValues.USERNAME */, usernameData, 3 /* TLVValues.PUBLIC_KEY */, serverLTPK, 10 /* TLVValues.PROOF */, serverProof);
569
- const encrypted = hapCrypto.chacha20_poly1305_encryptAndSeal(hkdfEncKey, Buffer.from("PS-Msg06"), null, message);
568
+ const encrypted = hapCrypto.chacha20_poly1305_encryptAndSeal(hkdfEncKey, Buffer.from('PS-Msg06'), null, message);
570
569
  // finally, notify listeners that we have been paired with a client
571
- this.emit("pair" /* HAPServerEventTypes.PAIR */, clientUsername.toString(), clientLTPK, (0, once_1.once)(err => {
570
+ this.emit("pair" /* HAPServerEventTypes.PAIR */, clientUsername.toString(), clientLTPK, once((err) => {
572
571
  if (err) {
573
- debug("[%s] Error adding pairing info: %s", this.accessoryInfo.username, err.message);
574
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
572
+ debug('[%s] Error adding pairing info: %s', this.accessoryInfo.username, err.message);
573
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
575
574
  response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 6 /* PairingStates.M6 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
576
575
  connection._pairSetupState = undefined;
577
576
  return;
578
577
  }
579
578
  // send final pairing response to client
580
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
579
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
581
580
  response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 6 /* PairingStates.M6 */, 5 /* TLVValues.ENCRYPTED_DATA */, Buffer.concat([encrypted.ciphertext, encrypted.authTag])));
582
581
  connection._pairSetupState = undefined;
583
582
  }));
@@ -588,13 +587,13 @@ class HAPServer extends events_1.EventEmitter {
588
587
  tlvData = tlv.decode(data);
589
588
  }
590
589
  catch (error) {
591
- debug("[%s] Pair-Verify: failed to decode TLV request: %s", this.accessoryInfo.username, error.message);
592
- response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { "Content-Type": "application/pairing+tlv8" });
590
+ debug('[%s] Pair-Verify: failed to decode TLV request: %s', this.accessoryInfo.username, error.message);
591
+ response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
593
592
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
594
593
  return;
595
594
  }
596
595
  if (!tlvData[6 /* TLVValues.SEQUENCE_NUM */]) {
597
- response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { "Content-Type": "application/pairing+tlv8" });
596
+ response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
598
597
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
599
598
  return;
600
599
  }
@@ -602,8 +601,8 @@ class HAPServer extends events_1.EventEmitter {
602
601
  if (connection.isAuthenticated()) {
603
602
  // M1 would replace the session keys of this connection with empty ones: it would keep counting as
604
603
  // authenticated while accepting and sending plaintext
605
- debug("[%s] Pair-Verify: rejecting pair-verify on an already verified connection", this.accessoryInfo.username);
606
- response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { "Content-Type": "application/pairing+tlv8" });
604
+ debug('[%s] Pair-Verify: rejecting pair-verify on an already verified connection', this.accessoryInfo.username);
605
+ response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
607
606
  response.end(tlv.encode(6 /* TLVValues.STATE */, sequence + 1, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
608
607
  return;
609
608
  }
@@ -615,13 +614,12 @@ class HAPServer extends events_1.EventEmitter {
615
614
  }
616
615
  else {
617
616
  // Invalid state/sequence number
618
- response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { "Content-Type": "application/pairing+tlv8" });
617
+ response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
619
618
  response.end(tlv.encode(6 /* TLVValues.STATE */, sequence + 1, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
620
- return;
621
619
  }
622
620
  }
623
621
  handlePairVerifyM1(connection, request, response, tlvData) {
624
- debug("[%s] Pair verify step 1/2", this.accessoryInfo.username);
622
+ debug('[%s] Pair verify step 1/2', this.accessoryInfo.username);
625
623
  const clientPublicKey = tlvData[3 /* TLVValues.PUBLIC_KEY */]; // Buffer
626
624
  // generate new encryption keys for this session
627
625
  const keyPair = hapCrypto.generateCurve25519KeyPair();
@@ -632,23 +630,23 @@ class HAPServer extends events_1.EventEmitter {
632
630
  const material = Buffer.concat([publicKey, usernameData, clientPublicKey]);
633
631
  const privateKey = Buffer.from(this.accessoryInfo.signSk);
634
632
  const serverProof = hapCrypto.ed25519Sign(material, privateKey);
635
- const encSalt = Buffer.from("Pair-Verify-Encrypt-Salt");
636
- const encInfo = Buffer.from("Pair-Verify-Encrypt-Info");
637
- const outputKey = hapCrypto.HKDF("sha512", encSalt, sharedSec, encInfo, 32).subarray(0, 32);
638
- connection.encryption = new eventedhttp_1.HAPEncryption(clientPublicKey, secretKey, publicKey, sharedSec, outputKey);
633
+ const encSalt = Buffer.from('Pair-Verify-Encrypt-Salt');
634
+ const encInfo = Buffer.from('Pair-Verify-Encrypt-Info');
635
+ const outputKey = hapCrypto.HKDF('sha512', encSalt, sharedSec, encInfo, 32).subarray(0, 32);
636
+ connection.encryption = new HAPEncryption(clientPublicKey, secretKey, publicKey, sharedSec, outputKey);
639
637
  // compose the response data in TLV format
640
638
  const message = tlv.encode(1 /* TLVValues.USERNAME */, usernameData, 10 /* TLVValues.PROOF */, serverProof);
641
- const encrypted = hapCrypto.chacha20_poly1305_encryptAndSeal(outputKey, Buffer.from("PV-Msg02"), null, message);
642
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
639
+ const encrypted = hapCrypto.chacha20_poly1305_encryptAndSeal(outputKey, Buffer.from('PV-Msg02'), null, message);
640
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
643
641
  response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 2 /* PairingStates.M2 */, 5 /* TLVValues.ENCRYPTED_DATA */, Buffer.concat([encrypted.ciphertext, encrypted.authTag]), 3 /* TLVValues.PUBLIC_KEY */, publicKey));
644
642
  connection._pairVerifyState = 2 /* PairingStates.M2 */;
645
643
  }
646
644
  handlePairVerifyM3(connection, request, response, objects) {
647
- debug("[%s] Pair verify step 2/2", this.accessoryInfo.username);
645
+ debug('[%s] Pair verify step 2/2', this.accessoryInfo.username);
648
646
  const encryptedData = objects[5 /* TLVValues.ENCRYPTED_DATA */];
649
647
  if (!encryptedData || encryptedData.length < 16) {
650
- debug("[%s] M3: Encrypted data missing or too short", this.accessoryInfo.username);
651
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
648
+ debug('[%s] M3: Encrypted data missing or too short', this.accessoryInfo.username);
649
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
652
650
  response.end(tlv.encode(6 /* TLVValues.STATE */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
653
651
  connection._pairVerifyState = undefined;
654
652
  return;
@@ -661,11 +659,11 @@ class HAPServer extends events_1.EventEmitter {
661
659
  const enc = connection.encryption;
662
660
  let plaintext;
663
661
  try {
664
- plaintext = hapCrypto.chacha20_poly1305_decryptAndVerify(enc.hkdfPairEncryptionKey, Buffer.from("PV-Msg03"), null, messageData, authTagData);
662
+ plaintext = hapCrypto.chacha20_poly1305_decryptAndVerify(enc.hkdfPairEncryptionKey, Buffer.from('PV-Msg03'), null, messageData, authTagData);
665
663
  }
666
664
  catch (error) {
667
- debug("[%s] M3: Failed to decrypt and/or verify: %s", this.accessoryInfo.username, error);
668
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
665
+ debug('[%s] M3: Failed to decrypt and/or verify: %s', this.accessoryInfo.username, error);
666
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
669
667
  response.end(tlv.encode(6 /* TLVValues.STATE */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
670
668
  connection._pairVerifyState = undefined;
671
669
  return;
@@ -675,8 +673,8 @@ class HAPServer extends events_1.EventEmitter {
675
673
  decoded = tlv.decode(plaintext);
676
674
  }
677
675
  catch (error) {
678
- debug("[%s] M3: Failed to decode decrypted TLV payload: %s", this.accessoryInfo.username, error.message);
679
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
676
+ debug('[%s] M3: Failed to decode decrypted TLV payload: %s', this.accessoryInfo.username, error.message);
677
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
680
678
  response.end(tlv.encode(6 /* TLVValues.STATE */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
681
679
  connection._pairVerifyState = undefined;
682
680
  return;
@@ -684,8 +682,8 @@ class HAPServer extends events_1.EventEmitter {
684
682
  const clientUsername = decoded[1 /* TLVValues.USERNAME */];
685
683
  const proof = decoded[10 /* TLVValues.PROOF */];
686
684
  if (!clientUsername || !proof) {
687
- debug("[%s] M3: Missing required TLV fields in decrypted payload", this.accessoryInfo.username);
688
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
685
+ debug('[%s] M3: Missing required TLV fields in decrypted payload', this.accessoryInfo.username);
686
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
689
687
  response.end(tlv.encode(6 /* TLVValues.STATE */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
690
688
  connection._pairVerifyState = undefined;
691
689
  return;
@@ -696,30 +694,30 @@ class HAPServer extends events_1.EventEmitter {
696
694
  // if we're not actually paired, then there's nothing to verify - this client thinks it's paired with us, but we
697
695
  // disagree. Respond with invalid request (seems to match HomeKit Accessory Simulator behavior)
698
696
  if (!clientPublicKey) {
699
- debug("[%s] Client %s attempting to verify, but we are not paired; rejecting client", this.accessoryInfo.username, clientUsername);
700
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
697
+ debug('[%s] Client %s attempting to verify, but we are not paired; rejecting client', this.accessoryInfo.username, clientUsername);
698
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
701
699
  response.end(tlv.encode(6 /* TLVValues.STATE */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
702
700
  connection._pairVerifyState = undefined;
703
701
  return;
704
702
  }
705
703
  if (!hapCrypto.ed25519Verify(material, proof, clientPublicKey)) {
706
- debug("[%s] Client %s provided an invalid signature", this.accessoryInfo.username, clientUsername);
707
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
704
+ debug('[%s] Client %s provided an invalid signature', this.accessoryInfo.username, clientUsername);
705
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
708
706
  response.end(tlv.encode(6 /* TLVValues.STATE */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
709
707
  connection._pairVerifyState = undefined;
710
708
  return;
711
709
  }
712
- debug("[%s] Client %s verification complete", this.accessoryInfo.username, clientUsername);
713
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
710
+ debug('[%s] Client %s verification complete', this.accessoryInfo.username, clientUsername);
711
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
714
712
  response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */));
715
713
  // now that the client has been verified, we must "upgrade" our pseudo-HTTP connection to include
716
714
  // TCP-level encryption. We'll do this by adding some more encryption vars to the session, and using them
717
715
  // in future calls to onEncrypt, onDecrypt.
718
- const encSalt = Buffer.from("Control-Salt");
719
- const infoRead = Buffer.from("Control-Read-Encryption-Key");
720
- const infoWrite = Buffer.from("Control-Write-Encryption-Key");
721
- enc.accessoryToControllerKey = hapCrypto.HKDF("sha512", encSalt, enc.sharedSecret, infoRead, 32);
722
- enc.controllerToAccessoryKey = hapCrypto.HKDF("sha512", encSalt, enc.sharedSecret, infoWrite, 32);
716
+ const encSalt = Buffer.from('Control-Salt');
717
+ const infoRead = Buffer.from('Control-Read-Encryption-Key');
718
+ const infoWrite = Buffer.from('Control-Write-Encryption-Key');
719
+ enc.accessoryToControllerKey = hapCrypto.HKDF('sha512', encSalt, enc.sharedSecret, infoRead, 32);
720
+ enc.controllerToAccessoryKey = hapCrypto.HKDF('sha512', encSalt, enc.sharedSecret, infoWrite, 32);
723
721
  // Our connection is now completely setup. We now want to subscribe this connection to special
724
722
  connection.connectionAuthenticated(clientUsername.toString());
725
723
  connection._pairVerifyState = undefined;
@@ -727,7 +725,7 @@ class HAPServer extends events_1.EventEmitter {
727
725
  handlePairings(connection, url, request, data, response) {
728
726
  // Only accept /pairing request if there is a secure session
729
727
  if (!this.allowInsecureRequest && !connection.isAuthenticated()) {
730
- response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
728
+ response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
731
729
  response.end(JSON.stringify({ status: -70401 /* HAPStatus.INSUFFICIENT_PRIVILEGES */ }));
732
730
  return;
733
731
  }
@@ -736,73 +734,72 @@ class HAPServer extends events_1.EventEmitter {
736
734
  objects = tlv.decode(data);
737
735
  }
738
736
  catch (error) {
739
- debug("[%s] Pairings: failed to decode TLV request: %s", this.accessoryInfo.username, error.message);
740
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
737
+ debug('[%s] Pairings: failed to decode TLV request: %s', this.accessoryInfo.username, error.message);
738
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
741
739
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
742
740
  return;
743
741
  }
744
742
  if (!objects[0 /* TLVValues.METHOD */] || !objects[6 /* TLVValues.STATE */]) {
745
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
743
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
746
744
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
747
745
  return;
748
746
  }
749
747
  const method = objects[0 /* TLVValues.METHOD */][0]; // value is single byte with request type
750
748
  const state = objects[6 /* TLVValues.STATE */][0];
751
749
  if (state !== 1 /* PairingStates.M1 */) {
752
- debug("[%s] Pairings: received unexpected state %d", this.accessoryInfo.username, state);
753
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
750
+ debug('[%s] Pairings: received unexpected state %d', this.accessoryInfo.username, state);
751
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
754
752
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
755
753
  return;
756
754
  }
757
755
  if (method === 3 /* PairMethods.ADD_PAIRING */) {
758
756
  if (!objects[1 /* TLVValues.IDENTIFIER */] || !objects[3 /* TLVValues.PUBLIC_KEY */] || !objects[11 /* TLVValues.PERMISSIONS */]) {
759
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
757
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
760
758
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
761
759
  return;
762
760
  }
763
761
  const identifier = objects[1 /* TLVValues.IDENTIFIER */].toString();
764
762
  const publicKey = objects[3 /* TLVValues.PUBLIC_KEY */];
765
763
  const permissions = objects[11 /* TLVValues.PERMISSIONS */][0];
766
- this.emit("add-pairing" /* HAPServerEventTypes.ADD_PAIRING */, connection, identifier, publicKey, permissions, (0, once_1.once)((error) => {
764
+ this.emit("add-pairing" /* HAPServerEventTypes.ADD_PAIRING */, connection, identifier, publicKey, permissions, once((error) => {
767
765
  if (error > 0) {
768
- debug("[%s] Pairings: failed ADD_PAIRING with code %d", this.accessoryInfo.username, error);
769
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
766
+ debug('[%s] Pairings: failed ADD_PAIRING with code %d', this.accessoryInfo.username, error);
767
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
770
768
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, error));
771
769
  return;
772
770
  }
773
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
771
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
774
772
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */));
775
- debug("[%s] Pairings: successfully executed ADD_PAIRING", this.accessoryInfo.username);
773
+ debug('[%s] Pairings: successfully executed ADD_PAIRING', this.accessoryInfo.username);
776
774
  }));
777
775
  }
778
776
  else if (method === 4 /* PairMethods.REMOVE_PAIRING */) {
779
777
  if (!objects[1 /* TLVValues.IDENTIFIER */]) {
780
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
778
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
781
779
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
782
780
  return;
783
781
  }
784
782
  const identifier = objects[1 /* TLVValues.IDENTIFIER */].toString();
785
- this.emit("remove-pairing" /* HAPServerEventTypes.REMOVE_PAIRING */, connection, identifier, (0, once_1.once)((error) => {
783
+ this.emit("remove-pairing" /* HAPServerEventTypes.REMOVE_PAIRING */, connection, identifier, once((error) => {
786
784
  if (error > 0) {
787
- debug("[%s] Pairings: failed REMOVE_PAIRING with code %d", this.accessoryInfo.username, error);
788
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
785
+ debug('[%s] Pairings: failed REMOVE_PAIRING with code %d', this.accessoryInfo.username, error);
786
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
789
787
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, error));
790
788
  return;
791
789
  }
792
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
790
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
793
791
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */));
794
- debug("[%s] Pairings: successfully executed REMOVE_PAIRING", this.accessoryInfo.username);
792
+ debug('[%s] Pairings: successfully executed REMOVE_PAIRING', this.accessoryInfo.username);
795
793
  }));
796
794
  }
797
795
  else if (method === 5 /* PairMethods.LIST_PAIRINGS */) {
798
- this.emit("list-pairings" /* HAPServerEventTypes.LIST_PAIRINGS */, connection, (0, once_1.once)((error, data) => {
796
+ this.emit("list-pairings" /* HAPServerEventTypes.LIST_PAIRINGS */, connection, once((error, data) => {
799
797
  if (error > 0) {
800
- debug("[%s] Pairings: failed LIST_PAIRINGS with code %d", this.accessoryInfo.username, error);
801
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
798
+ debug('[%s] Pairings: failed LIST_PAIRINGS with code %d', this.accessoryInfo.username, error);
799
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
802
800
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, error));
803
801
  return;
804
802
  }
805
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
806
803
  const tlvList = [];
807
804
  data.forEach((value, index) => {
808
805
  if (index > 0) {
@@ -811,54 +808,54 @@ class HAPServer extends events_1.EventEmitter {
811
808
  tlvList.push(1 /* TLVValues.IDENTIFIER */, value.username, 3 /* TLVValues.PUBLIC_KEY */, value.publicKey, 11 /* TLVValues.PERMISSIONS */, value.permission);
812
809
  });
813
810
  const list = tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, ...tlvList);
814
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" /* HAPMimeTypes.PAIRING_TLV8 */ });
811
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': "application/pairing+tlv8" /* HAPMimeTypes.PAIRING_TLV8 */ });
815
812
  response.end(list);
816
- debug("[%s] Pairings: successfully executed LIST_PAIRINGS", this.accessoryInfo.username);
813
+ debug('[%s] Pairings: successfully executed LIST_PAIRINGS', this.accessoryInfo.username);
817
814
  }));
818
815
  }
819
816
  else {
820
- debug("[%s] Pairings: received unsupported method %d", this.accessoryInfo.username, method);
821
- response.writeHead(200 /* HAPPairingHTTPCode.OK */, { "Content-Type": "application/pairing+tlv8" });
817
+ debug('[%s] Pairings: received unsupported method %d', this.accessoryInfo.username, method);
818
+ response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
822
819
  response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
823
820
  }
824
821
  }
825
822
  handleAccessories(connection, url, request, data, response) {
826
823
  if (!this.allowInsecureRequest && !connection.isAuthenticated()) {
827
- response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
824
+ response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
828
825
  response.end(JSON.stringify({ status: -70401 /* HAPStatus.INSUFFICIENT_PRIVILEGES */ }));
829
826
  return;
830
827
  }
831
828
  // call out to listeners to retrieve the latest accessories JSON
832
- this.emit("accessories" /* HAPServerEventTypes.ACCESSORIES */, connection, (0, once_1.once)((error, result) => {
829
+ this.emit("accessories" /* HAPServerEventTypes.ACCESSORIES */, connection, once((error, result) => {
833
830
  if (error) {
834
- response.writeHead(error.httpCode, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
831
+ response.writeHead(error.httpCode, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
835
832
  response.end(JSON.stringify({ status: error.status }));
836
833
  }
837
834
  else {
838
- response.writeHead(200 /* HAPHTTPCode.OK */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
835
+ response.writeHead(200 /* HAPHTTPCode.OK */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
839
836
  response.end(JSON.stringify(result));
840
837
  }
841
838
  }));
842
839
  }
843
840
  handleCharacteristics(connection, url, request, data, response) {
844
841
  if (!this.allowInsecureRequest && !connection.isAuthenticated()) {
845
- response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
842
+ response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
846
843
  response.end(JSON.stringify({ status: -70401 /* HAPStatus.INSUFFICIENT_PRIVILEGES */ }));
847
844
  return;
848
845
  }
849
- if (request.method === "GET") {
846
+ if (request.method === 'GET') {
850
847
  const searchParams = url.searchParams;
851
- const idParam = searchParams.get("id");
848
+ const idParam = searchParams.get('id');
852
849
  if (!idParam) {
853
- response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
850
+ response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
854
851
  response.end(JSON.stringify({ status: -70410 /* HAPStatus.INVALID_VALUE_IN_REQUEST */ }));
855
852
  return;
856
853
  }
857
854
  const ids = [];
858
- for (const entry of idParam.split(",")) { // ["1.9","2.14"]
859
- const split = entry.split("."); // ["1","9"]
855
+ for (const entry of idParam.split(',')) { // ["1.9","2.14"]
856
+ const split = entry.split('.'); // ["1","9"]
860
857
  if (split.length !== 2 || !/^\d+$/.test(split[0]) || !/^\d+$/.test(split[1])) {
861
- response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
858
+ response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
862
859
  response.end(JSON.stringify({ status: -70410 /* HAPStatus.INVALID_VALUE_IN_REQUEST */ }));
863
860
  return;
864
861
  }
@@ -868,15 +865,15 @@ class HAPServer extends events_1.EventEmitter {
868
865
  });
869
866
  }
870
867
  const readRequest = {
871
- ids: ids,
872
- includeMeta: (0, internal_types_1.consideredTrue)(searchParams.get("meta")),
873
- includePerms: (0, internal_types_1.consideredTrue)(searchParams.get("perms")),
874
- includeType: (0, internal_types_1.consideredTrue)(searchParams.get("type")),
875
- includeEvent: (0, internal_types_1.consideredTrue)(searchParams.get("ev")),
868
+ ids,
869
+ includeMeta: consideredTrue(searchParams.get('meta')),
870
+ includePerms: consideredTrue(searchParams.get('perms')),
871
+ includeType: consideredTrue(searchParams.get('type')),
872
+ includeEvent: consideredTrue(searchParams.get('ev')),
876
873
  };
877
- this.emit("get-characteristics" /* HAPServerEventTypes.GET_CHARACTERISTICS */, connection, readRequest, (0, once_1.once)((error, readResponse) => {
874
+ this.emit("get-characteristics" /* HAPServerEventTypes.GET_CHARACTERISTICS */, connection, readRequest, once((error, readResponse) => {
878
875
  if (error) {
879
- response.writeHead(error.httpCode, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
876
+ response.writeHead(error.httpCode, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
880
877
  response.end(JSON.stringify({ status: error.status }));
881
878
  return;
882
879
  }
@@ -896,32 +893,34 @@ class HAPServer extends events_1.EventEmitter {
896
893
  }
897
894
  }
898
895
  // 207 "multi-status" is returned when an error occurs reading a characteristic. otherwise 200 is returned
899
- response.writeHead(errorOccurred ? 207 /* HAPHTTPCode.MULTI_STATUS */ : 200 /* HAPHTTPCode.OK */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
900
- response.end(JSON.stringify({ characteristics: characteristics }));
896
+ response.writeHead(errorOccurred ? 207 /* HAPHTTPCode.MULTI_STATUS */ : 200 /* HAPHTTPCode.OK */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
897
+ response.end(JSON.stringify({ characteristics }));
901
898
  }));
902
899
  }
903
- else if (request.method === "PUT") {
900
+ else if (request.method === 'PUT') {
904
901
  if (!connection.isAuthenticated()) {
905
902
  const authorization = request.headers?.authorization;
906
- const authorizationBuffer = authorization ? Buffer.from(authorization) : undefined;
903
+ const authorizationBuffer = authorization
904
+ ? Buffer.from(authorization)
905
+ : undefined;
907
906
  const pincodeBuffer = Buffer.from(this.accessoryInfo.pincode);
908
907
  if (!authorizationBuffer
909
908
  || authorizationBuffer.length !== pincodeBuffer.length
910
- || !crypto_1.default.timingSafeEqual(authorizationBuffer, pincodeBuffer)) {
911
- response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
909
+ || !crypto.timingSafeEqual(authorizationBuffer, pincodeBuffer)) {
910
+ response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
912
911
  response.end(JSON.stringify({ status: -70401 /* HAPStatus.INSUFFICIENT_PRIVILEGES */ }));
913
912
  return;
914
913
  }
915
914
  }
916
915
  if (data.length === 0) {
917
- response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
916
+ response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
918
917
  response.end(JSON.stringify({ status: -70410 /* HAPStatus.INVALID_VALUE_IN_REQUEST */ }));
919
918
  return;
920
919
  }
921
- const writeRequest = JSON.parse(data.toString("utf8"));
922
- this.emit("set-characteristics" /* HAPServerEventTypes.SET_CHARACTERISTICS */, connection, writeRequest, (0, once_1.once)((error, writeResponse) => {
920
+ const writeRequest = JSON.parse(data.toString('utf8'));
921
+ this.emit("set-characteristics" /* HAPServerEventTypes.SET_CHARACTERISTICS */, connection, writeRequest, once((error, writeResponse) => {
923
922
  if (error) {
924
- response.writeHead(error.httpCode, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
923
+ response.writeHead(error.httpCode, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
925
924
  response.end(JSON.stringify({ status: error.status }));
926
925
  return;
927
926
  }
@@ -936,8 +935,8 @@ class HAPServer extends events_1.EventEmitter {
936
935
  }
937
936
  if (multiStatus) {
938
937
  // 207 is "multi-status" since HomeKit may be setting multiple things and any one can fail independently
939
- response.writeHead(207 /* HAPHTTPCode.MULTI_STATUS */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
940
- response.end(JSON.stringify({ characteristics: characteristics }));
938
+ response.writeHead(207 /* HAPHTTPCode.MULTI_STATUS */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
939
+ response.end(JSON.stringify({ characteristics }));
941
940
  }
942
941
  else {
943
942
  // if everything went fine send 204 no content response
@@ -947,87 +946,87 @@ class HAPServer extends events_1.EventEmitter {
947
946
  }));
948
947
  }
949
948
  else {
950
- response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ }); // method not allowed
949
+ response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ }); // method not allowed
951
950
  response.end(JSON.stringify({ status: -70410 /* HAPStatus.INVALID_VALUE_IN_REQUEST */ }));
952
951
  }
953
952
  }
954
953
  handlePrepareWrite(connection, url, request, data, response) {
955
954
  if (!this.allowInsecureRequest && !connection.isAuthenticated()) {
956
- response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
955
+ response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
957
956
  response.end(JSON.stringify({ status: -70401 /* HAPStatus.INSUFFICIENT_PRIVILEGES */ }));
958
957
  return;
959
958
  }
960
- if (request.method === "PUT") {
959
+ if (request.method === 'PUT') {
961
960
  if (data.length === 0) {
962
- response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
961
+ response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
963
962
  response.end(JSON.stringify({ status: -70410 /* HAPStatus.INVALID_VALUE_IN_REQUEST */ }));
964
963
  return;
965
964
  }
966
965
  const prepareRequest = JSON.parse(data.toString());
967
966
  const ttl = prepareRequest.ttl;
968
- if (prepareRequest.pid && typeof ttl === "number" && ttl > 0 && ttl <= HAPServer.MAX_TIMED_WRITE_TTL) {
969
- debug("[%s] Received prepare write request with pid %d and ttl %d", this.accessoryInfo.username, prepareRequest.pid, prepareRequest.ttl);
967
+ if (prepareRequest.pid && typeof ttl === 'number' && ttl > 0 && ttl <= HAPServer.MAX_TIMED_WRITE_TTL) {
968
+ debug('[%s] Received prepare write request with pid %d and ttl %d', this.accessoryInfo.username, prepareRequest.pid, prepareRequest.ttl);
970
969
  if (connection.timedWriteTimeout) { // clear any currently existing timeouts
971
970
  clearTimeout(connection.timedWriteTimeout);
972
971
  }
973
972
  connection.timedWritePid = prepareRequest.pid;
974
973
  connection.timedWriteTimeout = setTimeout(() => {
975
- debug("[%s] Timed write request timed out for pid %d", this.accessoryInfo.username, prepareRequest.pid);
974
+ debug('[%s] Timed write request timed out for pid %d', this.accessoryInfo.username, prepareRequest.pid);
976
975
  connection.timedWritePid = undefined;
977
976
  connection.timedWriteTimeout = undefined;
978
977
  }, ttl);
979
978
  connection.timedWriteTimeout.unref(); // the HAPConnection clears it when it closes
980
- response.writeHead(200 /* HAPHTTPCode.OK */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
979
+ response.writeHead(200 /* HAPHTTPCode.OK */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
981
980
  response.end(JSON.stringify({ status: 0 /* HAPStatus.SUCCESS */ }));
982
- return;
983
981
  }
984
982
  else {
985
- response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
983
+ response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
986
984
  response.end(JSON.stringify({ status: -70410 /* HAPStatus.INVALID_VALUE_IN_REQUEST */ }));
987
985
  }
988
986
  }
989
987
  else {
990
- response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
988
+ response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
991
989
  response.end(JSON.stringify({ status: -70410 /* HAPStatus.INVALID_VALUE_IN_REQUEST */ }));
992
990
  }
993
991
  }
994
992
  handleResource(connection, url, request, data, response) {
995
993
  if (!connection.isAuthenticated()) {
996
994
  const authorization = request.headers?.authorization;
997
- const authorizationBuffer = authorization ? Buffer.from(authorization) : undefined;
995
+ const authorizationBuffer = authorization
996
+ ? Buffer.from(authorization)
997
+ : undefined;
998
998
  const pincodeBuffer = Buffer.from(this.accessoryInfo.pincode);
999
999
  if (!(this.allowInsecureRequest && authorizationBuffer
1000
1000
  && authorizationBuffer.length === pincodeBuffer.length
1001
- && crypto_1.default.timingSafeEqual(authorizationBuffer, pincodeBuffer))) {
1002
- response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
1001
+ && crypto.timingSafeEqual(authorizationBuffer, pincodeBuffer))) {
1002
+ response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
1003
1003
  response.end(JSON.stringify({ status: -70401 /* HAPStatus.INSUFFICIENT_PRIVILEGES */ }));
1004
1004
  return;
1005
1005
  }
1006
1006
  }
1007
- if (request.method === "POST") {
1007
+ if (request.method === 'POST') {
1008
1008
  if (data.length === 0) {
1009
- response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
1009
+ response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
1010
1010
  response.end(JSON.stringify({ status: -70410 /* HAPStatus.INVALID_VALUE_IN_REQUEST */ }));
1011
1011
  return;
1012
1012
  }
1013
1013
  const resourceRequest = JSON.parse(data.toString());
1014
1014
  // call out to listeners to retrieve the resource, snapshot only right now
1015
- this.emit("request-resource" /* HAPServerEventTypes.REQUEST_RESOURCE */, resourceRequest, (0, once_1.once)((error, resource) => {
1015
+ this.emit("request-resource" /* HAPServerEventTypes.REQUEST_RESOURCE */, resourceRequest, once((error, resource) => {
1016
1016
  if (error) {
1017
- response.writeHead(error.httpCode, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
1017
+ response.writeHead(error.httpCode, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
1018
1018
  response.end(JSON.stringify({ status: error.status }));
1019
1019
  }
1020
1020
  else {
1021
- response.writeHead(200 /* HAPHTTPCode.OK */, { "Content-Type": "image/jpeg" /* HAPMimeTypes.IMAGE_JPEG */ });
1021
+ response.writeHead(200 /* HAPHTTPCode.OK */, { 'Content-Type': "image/jpeg" /* HAPMimeTypes.IMAGE_JPEG */ });
1022
1022
  response.end(resource);
1023
1023
  }
1024
1024
  }));
1025
1025
  }
1026
1026
  else {
1027
- response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { "Content-Type": "application/hap+json" /* HAPMimeTypes.HAP_JSON */ }); // method not allowed
1027
+ response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ }); // method not allowed
1028
1028
  response.end(JSON.stringify({ status: -70410 /* HAPStatus.INVALID_VALUE_IN_REQUEST */ }));
1029
1029
  }
1030
1030
  }
1031
1031
  }
1032
- exports.HAPServer = HAPServer;
1033
1032
  //# sourceMappingURL=HAPServer.js.map