hap-nodejs 0.12.3-beta.26 → 0.12.3-beta.28

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 (139) hide show
  1. package/README.md +0 -1
  2. package/dist/BridgedCore.js +19 -18
  3. package/dist/BridgedCore.js.map +1 -1
  4. package/dist/Core.js +20 -17
  5. package/dist/Core.js.map +1 -1
  6. package/dist/accessories/AirConditioner_accessory.js +24 -24
  7. package/dist/accessories/AirConditioner_accessory.js.map +1 -1
  8. package/dist/accessories/AppleTVRemote_accessory.js +23 -23
  9. package/dist/accessories/AppleTVRemote_accessory.js.map +1 -1
  10. package/dist/accessories/Camera_accessory.js +373 -292
  11. package/dist/accessories/Camera_accessory.js.map +1 -1
  12. package/dist/accessories/Fan_accessory.js +21 -15
  13. package/dist/accessories/Fan_accessory.js.map +1 -1
  14. package/dist/accessories/GarageDoorOpener_accessory.js +12 -12
  15. package/dist/accessories/GarageDoorOpener_accessory.js.map +1 -1
  16. package/dist/accessories/Light-AdaptiveLighting_accessory.js +21 -31
  17. package/dist/accessories/Light-AdaptiveLighting_accessory.js.map +1 -1
  18. package/dist/accessories/Light_accessory.js +48 -45
  19. package/dist/accessories/Light_accessory.js.map +1 -1
  20. package/dist/accessories/Lock_accessory.js +11 -11
  21. package/dist/accessories/Lock_accessory.js.map +1 -1
  22. package/dist/accessories/MotionSensor_accessory.js +8 -8
  23. package/dist/accessories/MotionSensor_accessory.js.map +1 -1
  24. package/dist/accessories/Outlet_accessory.js +10 -10
  25. package/dist/accessories/Outlet_accessory.js.map +1 -1
  26. package/dist/accessories/SmartSpeaker_accessory.js +11 -11
  27. package/dist/accessories/SmartSpeaker_accessory.js.map +1 -1
  28. package/dist/accessories/Sprinkler_accessory.js +19 -19
  29. package/dist/accessories/Sprinkler_accessory.js.map +1 -1
  30. package/dist/accessories/TV_accessory.js +17 -17
  31. package/dist/accessories/TV_accessory.js.map +1 -1
  32. package/dist/accessories/TemperatureSensor_accessory.js +6 -6
  33. package/dist/accessories/TemperatureSensor_accessory.js.map +1 -1
  34. package/dist/accessories/Wi-FiRouter_accessory.js +3 -3
  35. package/dist/accessories/Wi-FiRouter_accessory.js.map +1 -1
  36. package/dist/accessories/Wi-FiSatellite_accessory.js +4 -4
  37. package/dist/accessories/Wi-FiSatellite_accessory.js.map +1 -1
  38. package/dist/accessories/gstreamer-audioProducer.js +47 -36
  39. package/dist/accessories/gstreamer-audioProducer.js.map +1 -1
  40. package/dist/accessories/types.js +2 -2
  41. package/dist/accessories/types.js.map +1 -1
  42. package/dist/index.js +5 -5
  43. package/dist/index.js.map +1 -1
  44. package/dist/lib/Accessory.d.ts.map +1 -1
  45. package/dist/lib/Accessory.js +1088 -780
  46. package/dist/lib/Accessory.js.map +1 -1
  47. package/dist/lib/AccessoryLoader.js +40 -40
  48. package/dist/lib/AccessoryLoader.js.map +1 -1
  49. package/dist/lib/Advertiser.js +524 -392
  50. package/dist/lib/Advertiser.js.map +1 -1
  51. package/dist/lib/Bridge.js +10 -6
  52. package/dist/lib/Bridge.js.map +1 -1
  53. package/dist/lib/Characteristic.d.ts.map +1 -1
  54. package/dist/lib/Characteristic.js +539 -1510
  55. package/dist/lib/Characteristic.js.map +1 -1
  56. package/dist/lib/HAPServer.js +265 -215
  57. package/dist/lib/HAPServer.js.map +1 -1
  58. package/dist/lib/Service.d.ts.map +1 -1
  59. package/dist/lib/Service.js +320 -519
  60. package/dist/lib/Service.js.map +1 -1
  61. package/dist/lib/camera/Camera.js +14 -14
  62. package/dist/lib/camera/Camera.js.map +1 -1
  63. package/dist/lib/camera/RTPProxy.js +104 -112
  64. package/dist/lib/camera/RTPProxy.js.map +1 -1
  65. package/dist/lib/camera/RTPStreamManagement.d.ts.map +1 -1
  66. package/dist/lib/camera/RTPStreamManagement.js +257 -286
  67. package/dist/lib/camera/RTPStreamManagement.js.map +1 -1
  68. package/dist/lib/camera/RecordingManagement.js +384 -319
  69. package/dist/lib/camera/RecordingManagement.js.map +1 -1
  70. package/dist/lib/camera/index.js +1 -1
  71. package/dist/lib/controller/AdaptiveLightingController.d.ts +3 -19
  72. package/dist/lib/controller/AdaptiveLightingController.d.ts.map +1 -1
  73. package/dist/lib/controller/AdaptiveLightingController.js +218 -217
  74. package/dist/lib/controller/AdaptiveLightingController.js.map +1 -1
  75. package/dist/lib/controller/CameraController.js +250 -191
  76. package/dist/lib/controller/CameraController.js.map +1 -1
  77. package/dist/lib/controller/DoorbellController.d.ts +1 -1
  78. package/dist/lib/controller/DoorbellController.js +40 -39
  79. package/dist/lib/controller/DoorbellController.js.map +1 -1
  80. package/dist/lib/controller/RemoteController.js +401 -343
  81. package/dist/lib/controller/RemoteController.js.map +1 -1
  82. package/dist/lib/controller/index.js +1 -1
  83. package/dist/lib/datastream/DataStreamManagement.js +57 -56
  84. package/dist/lib/datastream/DataStreamManagement.js.map +1 -1
  85. package/dist/lib/datastream/DataStreamParser.js +304 -259
  86. package/dist/lib/datastream/DataStreamParser.js.map +1 -1
  87. package/dist/lib/datastream/DataStreamServer.d.ts.map +1 -1
  88. package/dist/lib/datastream/DataStreamServer.js +269 -252
  89. package/dist/lib/datastream/DataStreamServer.js.map +1 -1
  90. package/dist/lib/datastream/index.js +1 -1
  91. package/dist/lib/definitions/CharacteristicDefinitions.js +2858 -2089
  92. package/dist/lib/definitions/CharacteristicDefinitions.js.map +1 -1
  93. package/dist/lib/definitions/ServiceDefinitions.js +1096 -864
  94. package/dist/lib/definitions/ServiceDefinitions.js.map +1 -1
  95. package/dist/lib/definitions/generate-definitions.js +679 -383
  96. package/dist/lib/definitions/generate-definitions.js.map +1 -1
  97. package/dist/lib/definitions/generator-configuration.js +29 -29
  98. package/dist/lib/definitions/generator-configuration.js.map +1 -1
  99. package/dist/lib/definitions/index.js +1 -1
  100. package/dist/lib/model/AccessoryInfo.js +136 -101
  101. package/dist/lib/model/AccessoryInfo.js.map +1 -1
  102. package/dist/lib/model/ControllerStorage.js +89 -86
  103. package/dist/lib/model/ControllerStorage.js.map +1 -1
  104. package/dist/lib/model/HAPStorage.js +16 -15
  105. package/dist/lib/model/HAPStorage.js.map +1 -1
  106. package/dist/lib/model/IdentifierCache.js +49 -49
  107. package/dist/lib/model/IdentifierCache.js.map +1 -1
  108. package/dist/lib/tv/AccessControlManagement.js +44 -40
  109. package/dist/lib/tv/AccessControlManagement.js.map +1 -1
  110. package/dist/lib/util/clone.js +27 -5
  111. package/dist/lib/util/clone.js.map +1 -1
  112. package/dist/lib/util/color-utils.js +12 -8
  113. package/dist/lib/util/color-utils.js.map +1 -1
  114. package/dist/lib/util/eventedhttp.d.ts.map +1 -1
  115. package/dist/lib/util/eventedhttp.js +409 -301
  116. package/dist/lib/util/eventedhttp.js.map +1 -1
  117. package/dist/lib/util/hapCrypto.js +32 -31
  118. package/dist/lib/util/hapCrypto.js.map +1 -1
  119. package/dist/lib/util/hapStatusError.js +12 -9
  120. package/dist/lib/util/hapStatusError.js.map +1 -1
  121. package/dist/lib/util/net-utils.js +53 -32
  122. package/dist/lib/util/net-utils.js.map +1 -1
  123. package/dist/lib/util/once.js +8 -3
  124. package/dist/lib/util/once.js.map +1 -1
  125. package/dist/lib/util/promise-utils.js +13 -8
  126. package/dist/lib/util/promise-utils.js.map +1 -1
  127. package/dist/lib/util/request-util.js +3 -2
  128. package/dist/lib/util/request-util.js.map +1 -1
  129. package/dist/lib/util/time.js +5 -5
  130. package/dist/lib/util/time.js.map +1 -1
  131. package/dist/lib/util/tlv.js +75 -57
  132. package/dist/lib/util/tlv.js.map +1 -1
  133. package/dist/lib/util/uuid.js +19 -15
  134. package/dist/lib/util/uuid.js.map +1 -1
  135. package/package.json +16 -16
  136. package/dist/lib/util/checkName.d.ts +0 -8
  137. package/dist/lib/util/checkName.d.ts.map +0 -1
  138. package/dist/lib/util/checkName.js +0 -17
  139. package/dist/lib/util/checkName.js.map +0 -1
@@ -1,18 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ResolvedAdvertiser = exports.AvahiAdvertiser = exports.DBusInvokeError = exports.BonjourHAPAdvertiser = exports.CiaoAdvertiser = exports.AdvertiserEvent = exports.PairingFeatureFlag = exports.StatusFlag = void 0;
4
- const tslib_1 = require("tslib");
4
+ var tslib_1 = require("tslib");
5
5
  // eslint-disable-next-line @typescript-eslint/triple-slash-reference
6
6
  /// <reference path="../../@types/bonjour-hap.d.ts" />
7
- const ciao_1 = tslib_1.__importDefault(require("@homebridge/ciao"));
8
- const dbus_native_1 = tslib_1.__importDefault(require("@homebridge/dbus-native"));
9
- const assert_1 = tslib_1.__importDefault(require("assert"));
10
- const bonjour_hap_1 = tslib_1.__importDefault(require("bonjour-hap"));
11
- const crypto_1 = tslib_1.__importDefault(require("crypto"));
12
- const debug_1 = tslib_1.__importDefault(require("debug"));
13
- const events_1 = require("events");
14
- const promise_utils_1 = require("./util/promise-utils");
15
- const debug = (0, debug_1.default)("HAP-NodeJS:Advertiser");
7
+ var ciao_1 = tslib_1.__importDefault(require("@homebridge/ciao"));
8
+ var dbus_native_1 = tslib_1.__importDefault(require("@homebridge/dbus-native"));
9
+ var assert_1 = tslib_1.__importDefault(require("assert"));
10
+ var bonjour_hap_1 = tslib_1.__importDefault(require("bonjour-hap"));
11
+ var crypto_1 = tslib_1.__importDefault(require("crypto"));
12
+ var debug_1 = tslib_1.__importDefault(require("debug"));
13
+ var events_1 = require("events");
14
+ var promise_utils_1 = require("./util/promise-utils");
15
+ var debug = (0, debug_1.default)("HAP-NodeJS:Advertiser");
16
16
  /**
17
17
  * This enum lists all bitmasks for all known status flags.
18
18
  * When the bit for the given bitmask is set, it represents the state described by the name.
@@ -57,49 +57,48 @@ var AdvertiserEvent;
57
57
  *
58
58
  * @group Advertiser
59
59
  */
60
- class CiaoAdvertiser extends events_1.EventEmitter {
61
- static protocolVersion = "1.1";
62
- static protocolVersionService = "1.1.0";
63
- accessoryInfo;
64
- setupHash;
65
- responder;
66
- advertisedService;
67
- constructor(accessoryInfo, responderOptions, serviceOptions) {
68
- super();
69
- this.accessoryInfo = accessoryInfo;
70
- this.setupHash = CiaoAdvertiser.computeSetupHash(accessoryInfo);
71
- this.responder = ciao_1.default.getResponder({
72
- ...responderOptions,
73
- });
74
- this.advertisedService = this.responder.createService({
75
- name: this.accessoryInfo.displayName,
76
- type: "hap" /* ServiceType.HAP */,
77
- txt: CiaoAdvertiser.createTxt(accessoryInfo, this.setupHash),
78
- // host will default now to <displayName>.local, spaces replaced with dashes
79
- ...serviceOptions,
80
- });
81
- this.advertisedService.on("name-change" /* ServiceEvent.NAME_CHANGED */, this.emit.bind(this, "updated-name" /* AdvertiserEvent.UPDATED_NAME */));
82
- debug(`Preparing Advertiser for '${this.accessoryInfo.displayName}' using ciao backend!`);
83
- }
84
- initPort(port) {
60
+ var CiaoAdvertiser = /** @class */ (function (_super) {
61
+ tslib_1.__extends(CiaoAdvertiser, _super);
62
+ function CiaoAdvertiser(accessoryInfo, responderOptions, serviceOptions) {
63
+ var _this = _super.call(this) || this;
64
+ _this.accessoryInfo = accessoryInfo;
65
+ _this.setupHash = CiaoAdvertiser.computeSetupHash(accessoryInfo);
66
+ _this.responder = ciao_1.default.getResponder(tslib_1.__assign({}, responderOptions));
67
+ _this.advertisedService = _this.responder.createService(tslib_1.__assign({ name: _this.accessoryInfo.displayName, type: "hap" /* ServiceType.HAP */, txt: CiaoAdvertiser.createTxt(accessoryInfo, _this.setupHash) }, serviceOptions));
68
+ _this.advertisedService.on("name-change" /* ServiceEvent.NAME_CHANGED */, _this.emit.bind(_this, "updated-name" /* AdvertiserEvent.UPDATED_NAME */));
69
+ debug("Preparing Advertiser for '".concat(_this.accessoryInfo.displayName, "' using ciao backend!"));
70
+ return _this;
71
+ }
72
+ CiaoAdvertiser.prototype.initPort = function (port) {
85
73
  this.advertisedService.updatePort(port);
86
- }
87
- startAdvertising() {
88
- debug(`Starting to advertise '${this.accessoryInfo.displayName}' using ciao backend!`);
74
+ };
75
+ CiaoAdvertiser.prototype.startAdvertising = function () {
76
+ debug("Starting to advertise '".concat(this.accessoryInfo.displayName, "' using ciao backend!"));
89
77
  return this.advertisedService.advertise();
90
- }
91
- updateAdvertisement(silent) {
92
- const txt = CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash);
78
+ };
79
+ CiaoAdvertiser.prototype.updateAdvertisement = function (silent) {
80
+ var txt = CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash);
93
81
  debug("Updating txt record (txt: %o, silent: %d)", txt, silent);
94
82
  this.advertisedService.updateTxt(txt, silent);
95
- }
96
- async destroy() {
97
- // advertisedService.destroy(); is called implicitly via the shutdown call
98
- await this.responder.shutdown();
99
- this.removeAllListeners();
100
- }
101
- static createTxt(accessoryInfo, setupHash) {
102
- const statusFlags = [];
83
+ };
84
+ CiaoAdvertiser.prototype.destroy = function () {
85
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
86
+ return tslib_1.__generator(this, function (_a) {
87
+ switch (_a.label) {
88
+ case 0:
89
+ // advertisedService.destroy(); is called implicitly via the shutdown call
90
+ return [4 /*yield*/, this.responder.shutdown()];
91
+ case 1:
92
+ // advertisedService.destroy(); is called implicitly via the shutdown call
93
+ _a.sent();
94
+ this.removeAllListeners();
95
+ return [2 /*return*/];
96
+ }
97
+ });
98
+ });
99
+ };
100
+ CiaoAdvertiser.createTxt = function (accessoryInfo, setupHash) {
101
+ var statusFlags = [];
103
102
  if (!accessoryInfo.paired()) {
104
103
  statusFlags.push(1 /* StatusFlag.NOT_PAIRED */);
105
104
  }
@@ -110,102 +109,103 @@ class CiaoAdvertiser extends events_1.EventEmitter {
110
109
  md: accessoryInfo.model, // model name
111
110
  pv: CiaoAdvertiser.protocolVersion, // protocol version
112
111
  "s#": 1, // current state number (must be 1)
113
- sf: CiaoAdvertiser.sf(...statusFlags), // status flags
112
+ sf: CiaoAdvertiser.sf.apply(CiaoAdvertiser, tslib_1.__spreadArray([], tslib_1.__read(statusFlags), false)), // status flags
114
113
  ci: accessoryInfo.category,
115
114
  sh: setupHash,
116
115
  };
117
- }
118
- static computeSetupHash(accessoryInfo) {
119
- const hash = crypto_1.default.createHash("sha512");
116
+ };
117
+ CiaoAdvertiser.computeSetupHash = function (accessoryInfo) {
118
+ var hash = crypto_1.default.createHash("sha512");
120
119
  hash.update(accessoryInfo.setupID + accessoryInfo.username.toUpperCase());
121
120
  return hash.digest().slice(0, 4).toString("base64");
122
- }
123
- static ff(...flags) {
124
- let value = 0;
125
- flags.forEach(flag => value |= flag);
121
+ };
122
+ CiaoAdvertiser.ff = function () {
123
+ var flags = [];
124
+ for (var _i = 0; _i < arguments.length; _i++) {
125
+ flags[_i] = arguments[_i];
126
+ }
127
+ var value = 0;
128
+ flags.forEach(function (flag) { return value |= flag; });
126
129
  return value;
127
- }
128
- static sf(...flags) {
129
- let value = 0;
130
- flags.forEach(flag => value |= flag);
130
+ };
131
+ CiaoAdvertiser.sf = function () {
132
+ var flags = [];
133
+ for (var _i = 0; _i < arguments.length; _i++) {
134
+ flags[_i] = arguments[_i];
135
+ }
136
+ var value = 0;
137
+ flags.forEach(function (flag) { return value |= flag; });
131
138
  return value;
132
- }
133
- }
139
+ };
140
+ CiaoAdvertiser.protocolVersion = "1.1";
141
+ CiaoAdvertiser.protocolVersionService = "1.1.0";
142
+ return CiaoAdvertiser;
143
+ }(events_1.EventEmitter));
134
144
  exports.CiaoAdvertiser = CiaoAdvertiser;
135
145
  /**
136
146
  * Advertiser base on the legacy "bonjour-hap" library.
137
147
  *
138
148
  * @group Advertiser
139
149
  */
140
- class BonjourHAPAdvertiser extends events_1.EventEmitter {
141
- accessoryInfo;
142
- setupHash;
143
- serviceOptions;
144
- bonjour;
145
- advertisement;
146
- port;
147
- destroyed = false;
148
- constructor(accessoryInfo, responderOptions, serviceOptions) {
149
- super();
150
- this.accessoryInfo = accessoryInfo;
151
- this.setupHash = CiaoAdvertiser.computeSetupHash(accessoryInfo);
152
- this.serviceOptions = serviceOptions;
153
- this.bonjour = (0, bonjour_hap_1.default)(responderOptions);
154
- debug(`Preparing Advertiser for '${this.accessoryInfo.displayName}' using bonjour-hap backend!`);
155
- }
156
- initPort(port) {
150
+ var BonjourHAPAdvertiser = /** @class */ (function (_super) {
151
+ tslib_1.__extends(BonjourHAPAdvertiser, _super);
152
+ function BonjourHAPAdvertiser(accessoryInfo, responderOptions, serviceOptions) {
153
+ var _this = _super.call(this) || this;
154
+ _this.destroyed = false;
155
+ _this.accessoryInfo = accessoryInfo;
156
+ _this.setupHash = CiaoAdvertiser.computeSetupHash(accessoryInfo);
157
+ _this.serviceOptions = serviceOptions;
158
+ _this.bonjour = (0, bonjour_hap_1.default)(responderOptions);
159
+ debug("Preparing Advertiser for '".concat(_this.accessoryInfo.displayName, "' using bonjour-hap backend!"));
160
+ return _this;
161
+ }
162
+ BonjourHAPAdvertiser.prototype.initPort = function (port) {
157
163
  this.port = port;
158
- }
159
- startAdvertising() {
164
+ };
165
+ BonjourHAPAdvertiser.prototype.startAdvertising = function () {
160
166
  (0, assert_1.default)(!this.destroyed, "Can't advertise on a destroyed bonjour instance!");
161
167
  if (this.port == null) {
162
168
  throw new Error("Tried starting bonjour-hap advertisement without initializing port!");
163
169
  }
164
- debug(`Starting to advertise '${this.accessoryInfo.displayName}' using bonjour-hap backend!`);
170
+ debug("Starting to advertise '".concat(this.accessoryInfo.displayName, "' using bonjour-hap backend!"));
165
171
  if (this.advertisement) {
166
172
  this.destroy();
167
173
  }
168
- const hostname = this.accessoryInfo.username.replace(/:/ig, "_") + ".local";
169
- this.advertisement = this.bonjour.publish({
170
- name: this.accessoryInfo.displayName,
171
- type: "hap",
172
- port: this.port,
173
- txt: CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash),
174
- host: hostname,
175
- addUnsafeServiceEnumerationRecord: true,
176
- ...this.serviceOptions,
177
- });
174
+ var hostname = this.accessoryInfo.username.replace(/:/ig, "_") + ".local";
175
+ this.advertisement = this.bonjour.publish(tslib_1.__assign({ name: this.accessoryInfo.displayName, type: "hap", port: this.port, txt: CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash), host: hostname, addUnsafeServiceEnumerationRecord: true }, this.serviceOptions));
178
176
  return (0, promise_utils_1.PromiseTimeout)(1);
179
- }
180
- updateAdvertisement(silent) {
181
- const txt = CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash);
177
+ };
178
+ BonjourHAPAdvertiser.prototype.updateAdvertisement = function (silent) {
179
+ var txt = CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash);
182
180
  debug("Updating txt record (txt: %o, silent: %d)", txt, silent);
183
181
  if (this.advertisement) {
184
182
  this.advertisement.updateTxt(txt, silent);
185
183
  }
186
- }
187
- destroy() {
184
+ };
185
+ BonjourHAPAdvertiser.prototype.destroy = function () {
186
+ var _this = this;
188
187
  if (this.advertisement) {
189
- this.advertisement.stop(() => {
190
- this.advertisement.destroy();
191
- this.advertisement = undefined;
192
- this.bonjour.destroy();
188
+ this.advertisement.stop(function () {
189
+ _this.advertisement.destroy();
190
+ _this.advertisement = undefined;
191
+ _this.bonjour.destroy();
193
192
  });
194
193
  }
195
194
  else {
196
195
  this.bonjour.destroy();
197
196
  }
198
- }
199
- }
197
+ };
198
+ return BonjourHAPAdvertiser;
199
+ }(events_1.EventEmitter));
200
200
  exports.BonjourHAPAdvertiser = BonjourHAPAdvertiser;
201
201
  function messageBusConnectionResult(bus) {
202
- return new Promise((resolve, reject) => {
203
- const errorHandler = (error) => {
202
+ return new Promise(function (resolve, reject) {
203
+ var errorHandler = function (error) {
204
204
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
205
205
  bus.connection.removeListener("connect", connectHandler);
206
206
  reject(error);
207
207
  };
208
- const connectHandler = () => {
208
+ var connectHandler = function () {
209
209
  bus.connection.removeListener("error", errorHandler);
210
210
  resolve();
211
211
  };
@@ -216,34 +216,30 @@ function messageBusConnectionResult(bus) {
216
216
  /**
217
217
  * @group Advertiser
218
218
  */
219
- class DBusInvokeError extends Error {
220
- errorName;
219
+ var DBusInvokeError = /** @class */ (function (_super) {
220
+ tslib_1.__extends(DBusInvokeError, _super);
221
221
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
222
- constructor(errorObject) {
223
- super();
224
- Object.setPrototypeOf(this, DBusInvokeError.prototype);
225
- this.name = "DBusInvokeError";
226
- this.errorName = errorObject.name;
222
+ function DBusInvokeError(errorObject) {
223
+ var _this = _super.call(this) || this;
224
+ Object.setPrototypeOf(_this, DBusInvokeError.prototype);
225
+ _this.name = "DBusInvokeError";
226
+ _this.errorName = errorObject.name;
227
227
  if (Array.isArray(errorObject.message) && errorObject.message.length === 1) {
228
- this.message = errorObject.message[0];
228
+ _this.message = errorObject.message[0];
229
229
  }
230
230
  else {
231
- this.message = errorObject.message.toString();
231
+ _this.message = errorObject.message.toString();
232
232
  }
233
+ return _this;
233
234
  }
234
- }
235
+ return DBusInvokeError;
236
+ }(Error));
235
237
  exports.DBusInvokeError = DBusInvokeError;
236
238
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
237
239
  function dbusInvoke(bus, destination, path, dbusInterface, member, others) {
238
- return new Promise((resolve, reject) => {
239
- const command = {
240
- destination,
241
- path,
242
- interface: dbusInterface,
243
- member,
244
- ...(others || {}),
245
- };
246
- bus.invoke(command, (err, result) => {
240
+ return new Promise(function (resolve, reject) {
241
+ var command = tslib_1.__assign({ destination: destination, path: path, interface: dbusInterface, member: member }, (others || {}));
242
+ bus.invoke(command, function (err, result) {
247
243
  if (err) {
248
244
  reject(new DBusInvokeError(err));
249
245
  }
@@ -278,167 +274,232 @@ var AvahiServerState;
278
274
  *
279
275
  * @group Advertiser
280
276
  */
281
- class AvahiAdvertiser extends events_1.EventEmitter {
282
- accessoryInfo;
283
- setupHash;
284
- port;
285
- bus;
286
- avahiServerInterface;
287
- path;
288
- stateChangeHandler;
289
- constructor(accessoryInfo) {
290
- super();
291
- this.accessoryInfo = accessoryInfo;
292
- this.setupHash = CiaoAdvertiser.computeSetupHash(accessoryInfo);
293
- debug(`Preparing Advertiser for '${this.accessoryInfo.displayName}' using Avahi backend!`);
294
- this.bus = dbus_native_1.default.systemBus();
295
- this.stateChangeHandler = this.handleStateChangedEvent.bind(this);
296
- }
297
- createTxt() {
277
+ var AvahiAdvertiser = /** @class */ (function (_super) {
278
+ tslib_1.__extends(AvahiAdvertiser, _super);
279
+ function AvahiAdvertiser(accessoryInfo) {
280
+ var _this = _super.call(this) || this;
281
+ _this.accessoryInfo = accessoryInfo;
282
+ _this.setupHash = CiaoAdvertiser.computeSetupHash(accessoryInfo);
283
+ debug("Preparing Advertiser for '".concat(_this.accessoryInfo.displayName, "' using Avahi backend!"));
284
+ _this.bus = dbus_native_1.default.systemBus();
285
+ _this.stateChangeHandler = _this.handleStateChangedEvent.bind(_this);
286
+ return _this;
287
+ }
288
+ AvahiAdvertiser.prototype.createTxt = function () {
298
289
  return Object
299
290
  .entries(CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash))
300
- .map((el) => Buffer.from(el[0] + "=" + el[1]));
301
- }
302
- initPort(port) {
291
+ .map(function (el) { return Buffer.from(el[0] + "=" + el[1]); });
292
+ };
293
+ AvahiAdvertiser.prototype.initPort = function (port) {
303
294
  this.port = port;
304
- }
305
- async startAdvertising() {
306
- if (this.port == null) {
307
- throw new Error("Tried starting Avahi advertisement without initializing port!");
308
- }
309
- if (!this.bus) {
310
- throw new Error("Tried to start Avahi advertisement on a destroyed advertiser!");
311
- }
312
- debug(`Starting to advertise '${this.accessoryInfo.displayName}' using Avahi backend!`);
313
- this.path = await AvahiAdvertiser.avahiInvoke(this.bus, "/", "Server", "EntryGroupNew");
314
- await AvahiAdvertiser.avahiInvoke(this.bus, this.path, "EntryGroup", "AddService", {
315
- body: [
316
- -1, // interface
317
- -1, // protocol
318
- 0, // flags
319
- this.accessoryInfo.displayName, // name
320
- "_hap._tcp", // type
321
- "", // domain
322
- "", // host
323
- this.port, // port
324
- this.createTxt(), // txt
325
- ],
326
- signature: "iiussssqaay",
295
+ };
296
+ AvahiAdvertiser.prototype.startAdvertising = function () {
297
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
298
+ var _a, _b, error_1;
299
+ return tslib_1.__generator(this, function (_c) {
300
+ switch (_c.label) {
301
+ case 0:
302
+ if (this.port == null) {
303
+ throw new Error("Tried starting Avahi advertisement without initializing port!");
304
+ }
305
+ if (!this.bus) {
306
+ throw new Error("Tried to start Avahi advertisement on a destroyed advertiser!");
307
+ }
308
+ debug("Starting to advertise '".concat(this.accessoryInfo.displayName, "' using Avahi backend!"));
309
+ _a = this;
310
+ return [4 /*yield*/, AvahiAdvertiser.avahiInvoke(this.bus, "/", "Server", "EntryGroupNew")];
311
+ case 1:
312
+ _a.path = (_c.sent());
313
+ return [4 /*yield*/, AvahiAdvertiser.avahiInvoke(this.bus, this.path, "EntryGroup", "AddService", {
314
+ body: [
315
+ -1, // interface
316
+ -1, // protocol
317
+ 0, // flags
318
+ this.accessoryInfo.displayName, // name
319
+ "_hap._tcp", // type
320
+ "", // domain
321
+ "", // host
322
+ this.port, // port
323
+ this.createTxt(), // txt
324
+ ],
325
+ signature: "iiussssqaay",
326
+ })];
327
+ case 2:
328
+ _c.sent();
329
+ return [4 /*yield*/, AvahiAdvertiser.avahiInvoke(this.bus, this.path, "EntryGroup", "Commit")];
330
+ case 3:
331
+ _c.sent();
332
+ _c.label = 4;
333
+ case 4:
334
+ _c.trys.push([4, 7, , 8]);
335
+ if (!!this.avahiServerInterface) return [3 /*break*/, 6];
336
+ _b = this;
337
+ return [4 /*yield*/, AvahiAdvertiser.avahiInterface(this.bus, "Server")];
338
+ case 5:
339
+ _b.avahiServerInterface = _c.sent();
340
+ this.avahiServerInterface.on("StateChanged", this.stateChangeHandler);
341
+ _c.label = 6;
342
+ case 6: return [3 /*break*/, 8];
343
+ case 7:
344
+ error_1 = _c.sent();
345
+ // We have some problem on Synology https://github.com/homebridge/HAP-NodeJS/issues/993
346
+ console.warn("Failed to create listener for avahi-daemon server state. The system will not be notified about restarts of avahi-daemon " +
347
+ "and will therefore stay undiscoverable in those instances. Error message: " + error_1);
348
+ if (error_1.stack) {
349
+ debug("Detailed error: " + error_1.stack);
350
+ }
351
+ return [3 /*break*/, 8];
352
+ case 8: return [2 /*return*/];
353
+ }
354
+ });
327
355
  });
328
- await AvahiAdvertiser.avahiInvoke(this.bus, this.path, "EntryGroup", "Commit");
329
- try {
330
- if (!this.avahiServerInterface) {
331
- this.avahiServerInterface = await AvahiAdvertiser.avahiInterface(this.bus, "Server");
332
- this.avahiServerInterface.on("StateChanged", this.stateChangeHandler);
333
- }
334
- }
335
- catch (error) {
336
- // We have some problem on Synology https://github.com/homebridge/HAP-NodeJS/issues/993
337
- console.warn("Failed to create listener for avahi-daemon server state. The system will not be notified about restarts of avahi-daemon " +
338
- "and will therefore stay undiscoverable in those instances. Error message: " + error);
339
- if (error.stack) {
340
- debug("Detailed error: " + error.stack);
341
- }
342
- }
343
- }
356
+ };
344
357
  /**
345
358
  * Event handler for the `StateChanged` event of the `org.freedesktop.Avahi.Server` DBus interface.
346
359
  *
347
360
  * This is called once the state of the running avahi-daemon changes its running state.
348
361
  * @param state - The state the server changed into {@see AvahiServerState}.
349
362
  */
350
- handleStateChangedEvent(state) {
363
+ AvahiAdvertiser.prototype.handleStateChangedEvent = function (state) {
351
364
  if (state === 2 /* AvahiServerState.RUNNING */ && this.path) {
352
365
  debug("Found Avahi daemon to have restarted!");
353
366
  this.startAdvertising()
354
- .catch(reason => console.error("Could not (re-)create mDNS advertisement. The HAP-Server won't be discoverable: " + reason));
355
- }
356
- }
357
- async updateAdvertisement(silent) {
358
- if (!this.bus) {
359
- throw new Error("Tried to update Avahi advertisement on a destroyed advertiser!");
360
- }
361
- if (!this.path) {
362
- debug("Tried to update advertisement without a valid `path`!");
363
- return;
364
- }
365
- debug("Updating txt record (txt: %o, silent: %d)", CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash), silent);
366
- try {
367
- await AvahiAdvertiser.avahiInvoke(this.bus, this.path, "EntryGroup", "UpdateServiceTxt", {
368
- body: [-1, -1, 0, this.accessoryInfo.displayName, "_hap._tcp", "", this.createTxt()],
369
- signature: "iiusssaay",
367
+ .catch(function (reason) { return console.error("Could not (re-)create mDNS advertisement. The HAP-Server won't be discoverable: " + reason); });
368
+ }
369
+ };
370
+ AvahiAdvertiser.prototype.updateAdvertisement = function (silent) {
371
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
372
+ var error_2;
373
+ return tslib_1.__generator(this, function (_a) {
374
+ switch (_a.label) {
375
+ case 0:
376
+ if (!this.bus) {
377
+ throw new Error("Tried to update Avahi advertisement on a destroyed advertiser!");
378
+ }
379
+ if (!this.path) {
380
+ debug("Tried to update advertisement without a valid `path`!");
381
+ return [2 /*return*/];
382
+ }
383
+ debug("Updating txt record (txt: %o, silent: %d)", CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash), silent);
384
+ _a.label = 1;
385
+ case 1:
386
+ _a.trys.push([1, 3, , 4]);
387
+ return [4 /*yield*/, AvahiAdvertiser.avahiInvoke(this.bus, this.path, "EntryGroup", "UpdateServiceTxt", {
388
+ body: [-1, -1, 0, this.accessoryInfo.displayName, "_hap._tcp", "", this.createTxt()],
389
+ signature: "iiusssaay",
390
+ })];
391
+ case 2:
392
+ _a.sent();
393
+ return [3 /*break*/, 4];
394
+ case 3:
395
+ error_2 = _a.sent();
396
+ console.error("Failed to update avahi advertisement: " + error_2);
397
+ return [3 /*break*/, 4];
398
+ case 4: return [2 /*return*/];
399
+ }
370
400
  });
371
- }
372
- catch (error) {
373
- console.error("Failed to update avahi advertisement: " + error);
374
- }
375
- }
376
- async destroy() {
377
- if (!this.bus) {
378
- throw new Error("Tried to destroy Avahi advertisement on a destroyed advertiser!");
379
- }
380
- if (this.path) {
381
- try {
382
- await AvahiAdvertiser.avahiInvoke(this.bus, this.path, "EntryGroup", "Free");
383
- }
384
- catch (error) {
385
- // Typically, this fails if e.g. avahi service was stopped in the meantime.
386
- debug("Destroying Avahi advertisement failed: " + error);
387
- }
388
- this.path = undefined;
389
- }
390
- if (this.avahiServerInterface) {
391
- this.avahiServerInterface.removeListener("StateChanged", this.stateChangeHandler);
392
- this.avahiServerInterface = undefined;
393
- }
394
- this.bus.connection.stream.destroy();
395
- this.bus = undefined;
396
- }
397
- static async isAvailable() {
398
- const bus = dbus_native_1.default.systemBus();
399
- try {
400
- try {
401
- await messageBusConnectionResult(bus);
402
- }
403
- catch (error) {
404
- debug("Avahi/DBus classified unavailable due to missing dbus interface!");
405
- return false;
406
- }
407
- try {
408
- const version = await this.avahiInvoke(bus, "/", "Server", "GetVersionString");
409
- debug("Detected Avahi over DBus interface running version '%s'.", version);
410
- }
411
- catch (error) {
412
- debug("Avahi/DBus classified unavailable due to missing avahi interface!");
413
- return false;
414
- }
415
- return true;
416
- }
417
- finally {
418
- bus.connection.stream.destroy();
419
- }
420
- }
401
+ });
402
+ };
403
+ AvahiAdvertiser.prototype.destroy = function () {
404
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
405
+ var error_3;
406
+ return tslib_1.__generator(this, function (_a) {
407
+ switch (_a.label) {
408
+ case 0:
409
+ if (!this.bus) {
410
+ throw new Error("Tried to destroy Avahi advertisement on a destroyed advertiser!");
411
+ }
412
+ if (!this.path) return [3 /*break*/, 5];
413
+ _a.label = 1;
414
+ case 1:
415
+ _a.trys.push([1, 3, , 4]);
416
+ return [4 /*yield*/, AvahiAdvertiser.avahiInvoke(this.bus, this.path, "EntryGroup", "Free")];
417
+ case 2:
418
+ _a.sent();
419
+ return [3 /*break*/, 4];
420
+ case 3:
421
+ error_3 = _a.sent();
422
+ // Typically, this fails if e.g. avahi service was stopped in the meantime.
423
+ debug("Destroying Avahi advertisement failed: " + error_3);
424
+ return [3 /*break*/, 4];
425
+ case 4:
426
+ this.path = undefined;
427
+ _a.label = 5;
428
+ case 5:
429
+ if (this.avahiServerInterface) {
430
+ this.avahiServerInterface.removeListener("StateChanged", this.stateChangeHandler);
431
+ this.avahiServerInterface = undefined;
432
+ }
433
+ this.bus.connection.stream.destroy();
434
+ this.bus = undefined;
435
+ return [2 /*return*/];
436
+ }
437
+ });
438
+ });
439
+ };
440
+ AvahiAdvertiser.isAvailable = function () {
441
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
442
+ var bus, error_4, version, error_5;
443
+ return tslib_1.__generator(this, function (_a) {
444
+ switch (_a.label) {
445
+ case 0:
446
+ bus = dbus_native_1.default.systemBus();
447
+ _a.label = 1;
448
+ case 1:
449
+ _a.trys.push([1, , 9, 10]);
450
+ _a.label = 2;
451
+ case 2:
452
+ _a.trys.push([2, 4, , 5]);
453
+ return [4 /*yield*/, messageBusConnectionResult(bus)];
454
+ case 3:
455
+ _a.sent();
456
+ return [3 /*break*/, 5];
457
+ case 4:
458
+ error_4 = _a.sent();
459
+ debug("Avahi/DBus classified unavailable due to missing dbus interface!");
460
+ return [2 /*return*/, false];
461
+ case 5:
462
+ _a.trys.push([5, 7, , 8]);
463
+ return [4 /*yield*/, this.avahiInvoke(bus, "/", "Server", "GetVersionString")];
464
+ case 6:
465
+ version = _a.sent();
466
+ debug("Detected Avahi over DBus interface running version '%s'.", version);
467
+ return [3 /*break*/, 8];
468
+ case 7:
469
+ error_5 = _a.sent();
470
+ debug("Avahi/DBus classified unavailable due to missing avahi interface!");
471
+ return [2 /*return*/, false];
472
+ case 8: return [2 /*return*/, true];
473
+ case 9:
474
+ bus.connection.stream.destroy();
475
+ return [7 /*endfinally*/];
476
+ case 10: return [2 /*return*/];
477
+ }
478
+ });
479
+ });
480
+ };
421
481
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
422
- static avahiInvoke(bus, path, dbusInterface, member, others) {
423
- return dbusInvoke(bus, "org.freedesktop.Avahi", path, `org.freedesktop.Avahi.${dbusInterface}`, member, others);
424
- }
425
- static avahiInterface(bus, dbusInterface) {
426
- return new Promise((resolve, reject) => {
482
+ AvahiAdvertiser.avahiInvoke = function (bus, path, dbusInterface, member, others) {
483
+ return dbusInvoke(bus, "org.freedesktop.Avahi", path, "org.freedesktop.Avahi.".concat(dbusInterface), member, others);
484
+ };
485
+ AvahiAdvertiser.avahiInterface = function (bus, dbusInterface) {
486
+ return new Promise(function (resolve, reject) {
427
487
  bus
428
488
  .getService("org.freedesktop.Avahi")
429
- .getInterface("/", "org.freedesktop.Avahi." + dbusInterface, (error, iface) => {
489
+ .getInterface("/", "org.freedesktop.Avahi." + dbusInterface, function (error, iface) {
430
490
  if (error || !iface) {
431
- reject(error ?? new Error("Interface not present!"));
491
+ reject(error !== null && error !== void 0 ? error : new Error("Interface not present!"));
432
492
  }
433
493
  else {
434
494
  resolve(iface);
435
495
  }
436
496
  });
437
497
  });
438
- }
439
- }
498
+ };
499
+ return AvahiAdvertiser;
500
+ }(events_1.EventEmitter));
440
501
  exports.AvahiAdvertiser = AvahiAdvertiser;
441
- const RESOLVED_PERMISSIONS_ERRORS = [
502
+ var RESOLVED_PERMISSIONS_ERRORS = [
442
503
  "org.freedesktop.DBus.Error.AccessDenied",
443
504
  "org.freedesktop.DBus.Error.AuthFailed",
444
505
  "org.freedesktop.DBus.Error.InteractiveAuthorizationRequired",
@@ -449,146 +510,217 @@ const RESOLVED_PERMISSIONS_ERRORS = [
449
510
  *
450
511
  * @group Advertiser
451
512
  */
452
- class ResolvedAdvertiser extends events_1.EventEmitter {
453
- accessoryInfo;
454
- setupHash;
455
- port;
456
- bus;
457
- path;
458
- constructor(accessoryInfo) {
459
- super();
460
- this.accessoryInfo = accessoryInfo;
461
- this.setupHash = CiaoAdvertiser.computeSetupHash(accessoryInfo);
462
- this.bus = dbus_native_1.default.systemBus();
463
- debug(`Preparing Advertiser for '${this.accessoryInfo.displayName}' using systemd-resolved backend!`);
464
- }
465
- createTxt() {
513
+ var ResolvedAdvertiser = /** @class */ (function (_super) {
514
+ tslib_1.__extends(ResolvedAdvertiser, _super);
515
+ function ResolvedAdvertiser(accessoryInfo) {
516
+ var _this = _super.call(this) || this;
517
+ _this.accessoryInfo = accessoryInfo;
518
+ _this.setupHash = CiaoAdvertiser.computeSetupHash(accessoryInfo);
519
+ _this.bus = dbus_native_1.default.systemBus();
520
+ debug("Preparing Advertiser for '".concat(_this.accessoryInfo.displayName, "' using systemd-resolved backend!"));
521
+ return _this;
522
+ }
523
+ ResolvedAdvertiser.prototype.createTxt = function () {
466
524
  return Object
467
525
  .entries(CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash))
468
- .map((el) => [el[0].toString(), Buffer.from(el[1].toString())]);
469
- }
470
- initPort(port) {
526
+ .map(function (el) { return [el[0].toString(), Buffer.from(el[1].toString())]; });
527
+ };
528
+ ResolvedAdvertiser.prototype.initPort = function (port) {
471
529
  this.port = port;
472
- }
473
- async startAdvertising() {
474
- if (this.port == null) {
475
- throw new Error("Tried starting systemd-resolved advertisement without initializing port!");
476
- }
477
- if (!this.bus) {
478
- throw new Error("Tried to start systemd-resolved advertisement on a destroyed advertiser!");
479
- }
480
- debug(`Starting to advertise '${this.accessoryInfo.displayName}' using systemd-resolved backend!`);
481
- try {
482
- this.path = await ResolvedAdvertiser.managerInvoke(this.bus, "RegisterService", {
483
- body: [
484
- this.accessoryInfo.displayName, // name
485
- this.accessoryInfo.displayName, // name_template
486
- "_hap._tcp", // type
487
- this.port, // service_port
488
- 0, // service_priority
489
- 0, // service_weight
490
- [this.createTxt()], // txt_datas
491
- ],
492
- signature: "sssqqqaa{say}",
530
+ };
531
+ ResolvedAdvertiser.prototype.startAdvertising = function () {
532
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
533
+ var _a, error_6;
534
+ return tslib_1.__generator(this, function (_b) {
535
+ switch (_b.label) {
536
+ case 0:
537
+ if (this.port == null) {
538
+ throw new Error("Tried starting systemd-resolved advertisement without initializing port!");
539
+ }
540
+ if (!this.bus) {
541
+ throw new Error("Tried to start systemd-resolved advertisement on a destroyed advertiser!");
542
+ }
543
+ debug("Starting to advertise '".concat(this.accessoryInfo.displayName, "' using systemd-resolved backend!"));
544
+ _b.label = 1;
545
+ case 1:
546
+ _b.trys.push([1, 3, , 4]);
547
+ _a = this;
548
+ return [4 /*yield*/, ResolvedAdvertiser.managerInvoke(this.bus, "RegisterService", {
549
+ body: [
550
+ this.accessoryInfo.displayName, // name
551
+ this.accessoryInfo.displayName, // name_template
552
+ "_hap._tcp", // type
553
+ this.port, // service_port
554
+ 0, // service_priority
555
+ 0, // service_weight
556
+ [this.createTxt()], // txt_datas
557
+ ],
558
+ signature: "sssqqqaa{say}",
559
+ })];
560
+ case 2:
561
+ _a.path = _b.sent();
562
+ return [3 /*break*/, 4];
563
+ case 3:
564
+ error_6 = _b.sent();
565
+ if (error_6 instanceof DBusInvokeError) {
566
+ if (RESOLVED_PERMISSIONS_ERRORS.includes(error_6.errorName)) {
567
+ error_6.message = "Permissions issue. See https://homebridge.io/w/mDNS-Options for more info. ".concat(error_6.message);
568
+ }
569
+ }
570
+ throw error_6;
571
+ case 4: return [2 /*return*/];
572
+ }
493
573
  });
494
- }
495
- catch (error) {
496
- if (error instanceof DBusInvokeError) {
497
- if (RESOLVED_PERMISSIONS_ERRORS.includes(error.errorName)) {
498
- error.message = `Permissions issue. See https://homebridge.io/w/mDNS-Options for more info. ${error.message}`;
574
+ });
575
+ };
576
+ ResolvedAdvertiser.prototype.updateAdvertisement = function (silent) {
577
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
578
+ return tslib_1.__generator(this, function (_a) {
579
+ switch (_a.label) {
580
+ case 0:
581
+ if (!this.bus) {
582
+ throw new Error("Tried to update systemd-resolved advertisement on a destroyed advertiser!");
583
+ }
584
+ debug("Updating txt record (txt: %o, silent: %d)", CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash), silent);
585
+ // Currently, systemd-resolved has no way to update an existing record.
586
+ return [4 /*yield*/, this.stopAdvertising()];
587
+ case 1:
588
+ // Currently, systemd-resolved has no way to update an existing record.
589
+ _a.sent();
590
+ return [4 /*yield*/, this.startAdvertising()];
591
+ case 2:
592
+ _a.sent();
593
+ return [2 /*return*/];
499
594
  }
500
- }
501
- throw error;
502
- }
503
- }
504
- async updateAdvertisement(silent) {
505
- if (!this.bus) {
506
- throw new Error("Tried to update systemd-resolved advertisement on a destroyed advertiser!");
507
- }
508
- debug("Updating txt record (txt: %o, silent: %d)", CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash), silent);
509
- // Currently, systemd-resolved has no way to update an existing record.
510
- await this.stopAdvertising();
511
- await this.startAdvertising();
512
- }
513
- async stopAdvertising() {
514
- if (!this.bus) {
515
- throw new Error("Tried to destroy systemd-resolved advertisement on a destroyed advertiser!");
516
- }
517
- if (this.path) {
518
- try {
519
- await ResolvedAdvertiser.managerInvoke(this.bus, "UnregisterService", {
520
- body: [this.path],
521
- signature: "o",
522
- });
523
- }
524
- catch (error) {
525
- // Typically, this fails if e.g. systemd-resolved service was stopped in the meantime.
526
- debug("Destroying systemd-resolved advertisement failed: " + error);
527
- }
528
- this.path = undefined;
529
- }
530
- }
531
- async destroy() {
532
- if (!this.bus) {
533
- throw new Error("Tried to destroy systemd-resolved advertisement on a destroyed advertiser!");
534
- }
535
- await this.stopAdvertising();
536
- this.bus.connection.stream.destroy();
537
- this.bus = undefined;
538
- }
539
- static async isAvailable() {
540
- const bus = dbus_native_1.default.systemBus();
541
- try {
542
- try {
543
- await messageBusConnectionResult(bus);
544
- }
545
- catch (error) {
546
- debug("systemd-resolved/DBus classified unavailable due to missing dbus interface!");
547
- return false;
548
- }
549
- try {
550
- // Ensure that systemd-resolved is accessible.
551
- await this.managerInvoke(bus, "ResolveHostname", {
552
- body: [0, "127.0.0.1", 0, 0],
553
- signature: "isit",
554
- });
555
- debug("Detected systemd-resolved over DBus interface running version.");
556
- }
557
- catch (error) {
558
- debug("systemd-resolved/DBus classified unavailable due to missing systemd-resolved interface!");
559
- return false;
560
- }
561
- try {
562
- const mdnsStatus = await this.resolvedInvoke(bus, "org.freedesktop.DBus.Properties", "Get", {
563
- body: ["org.freedesktop.resolve1.Manager", "MulticastDNS"],
564
- signature: "ss",
565
- });
566
- if (mdnsStatus[0][0].type !== "s") {
567
- throw new Error("Invalid type for MulticastDNS");
595
+ });
596
+ });
597
+ };
598
+ ResolvedAdvertiser.prototype.stopAdvertising = function () {
599
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
600
+ var error_7;
601
+ return tslib_1.__generator(this, function (_a) {
602
+ switch (_a.label) {
603
+ case 0:
604
+ if (!this.bus) {
605
+ throw new Error("Tried to destroy systemd-resolved advertisement on a destroyed advertiser!");
606
+ }
607
+ if (!this.path) return [3 /*break*/, 5];
608
+ _a.label = 1;
609
+ case 1:
610
+ _a.trys.push([1, 3, , 4]);
611
+ return [4 /*yield*/, ResolvedAdvertiser.managerInvoke(this.bus, "UnregisterService", {
612
+ body: [this.path],
613
+ signature: "o",
614
+ })];
615
+ case 2:
616
+ _a.sent();
617
+ return [3 /*break*/, 4];
618
+ case 3:
619
+ error_7 = _a.sent();
620
+ // Typically, this fails if e.g. systemd-resolved service was stopped in the meantime.
621
+ debug("Destroying systemd-resolved advertisement failed: " + error_7);
622
+ return [3 /*break*/, 4];
623
+ case 4:
624
+ this.path = undefined;
625
+ _a.label = 5;
626
+ case 5: return [2 /*return*/];
568
627
  }
569
- if (mdnsStatus[1][0] !== "yes") {
570
- debug("systemd-resolved/DBus classified unavailable because MulticastDNS is not enabled!");
571
- return false;
628
+ });
629
+ });
630
+ };
631
+ ResolvedAdvertiser.prototype.destroy = function () {
632
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
633
+ return tslib_1.__generator(this, function (_a) {
634
+ switch (_a.label) {
635
+ case 0:
636
+ if (!this.bus) {
637
+ throw new Error("Tried to destroy systemd-resolved advertisement on a destroyed advertiser!");
638
+ }
639
+ return [4 /*yield*/, this.stopAdvertising()];
640
+ case 1:
641
+ _a.sent();
642
+ this.bus.connection.stream.destroy();
643
+ this.bus = undefined;
644
+ return [2 /*return*/];
572
645
  }
573
- }
574
- catch (error) {
575
- debug("systemd-resolved/DBus classified unavailable due to failure checking system status: " + error);
576
- return false;
577
- }
578
- return true;
579
- }
580
- finally {
581
- bus.connection.stream.destroy();
582
- }
583
- }
646
+ });
647
+ });
648
+ };
649
+ ResolvedAdvertiser.isAvailable = function () {
650
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
651
+ var bus, error_8, error_9, mdnsStatus, error_10;
652
+ return tslib_1.__generator(this, function (_a) {
653
+ switch (_a.label) {
654
+ case 0:
655
+ bus = dbus_native_1.default.systemBus();
656
+ _a.label = 1;
657
+ case 1:
658
+ _a.trys.push([1, , 12, 13]);
659
+ _a.label = 2;
660
+ case 2:
661
+ _a.trys.push([2, 4, , 5]);
662
+ return [4 /*yield*/, messageBusConnectionResult(bus)];
663
+ case 3:
664
+ _a.sent();
665
+ return [3 /*break*/, 5];
666
+ case 4:
667
+ error_8 = _a.sent();
668
+ debug("systemd-resolved/DBus classified unavailable due to missing dbus interface!");
669
+ return [2 /*return*/, false];
670
+ case 5:
671
+ _a.trys.push([5, 7, , 8]);
672
+ // Ensure that systemd-resolved is accessible.
673
+ return [4 /*yield*/, this.managerInvoke(bus, "ResolveHostname", {
674
+ body: [0, "127.0.0.1", 0, 0],
675
+ signature: "isit",
676
+ })];
677
+ case 6:
678
+ // Ensure that systemd-resolved is accessible.
679
+ _a.sent();
680
+ debug("Detected systemd-resolved over DBus interface running version.");
681
+ return [3 /*break*/, 8];
682
+ case 7:
683
+ error_9 = _a.sent();
684
+ debug("systemd-resolved/DBus classified unavailable due to missing systemd-resolved interface!");
685
+ return [2 /*return*/, false];
686
+ case 8:
687
+ _a.trys.push([8, 10, , 11]);
688
+ return [4 /*yield*/, this.resolvedInvoke(bus, "org.freedesktop.DBus.Properties", "Get", {
689
+ body: ["org.freedesktop.resolve1.Manager", "MulticastDNS"],
690
+ signature: "ss",
691
+ })];
692
+ case 9:
693
+ mdnsStatus = _a.sent();
694
+ if (mdnsStatus[0][0].type !== "s") {
695
+ throw new Error("Invalid type for MulticastDNS");
696
+ }
697
+ if (mdnsStatus[1][0] !== "yes") {
698
+ debug("systemd-resolved/DBus classified unavailable because MulticastDNS is not enabled!");
699
+ return [2 /*return*/, false];
700
+ }
701
+ return [3 /*break*/, 11];
702
+ case 10:
703
+ error_10 = _a.sent();
704
+ debug("systemd-resolved/DBus classified unavailable due to failure checking system status: " + error_10);
705
+ return [2 /*return*/, false];
706
+ case 11: return [2 /*return*/, true];
707
+ case 12:
708
+ bus.connection.stream.destroy();
709
+ return [7 /*endfinally*/];
710
+ case 13: return [2 /*return*/];
711
+ }
712
+ });
713
+ });
714
+ };
584
715
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
585
- static resolvedInvoke(bus, dbusInterface, member, others) {
716
+ ResolvedAdvertiser.resolvedInvoke = function (bus, dbusInterface, member, others) {
586
717
  return dbusInvoke(bus, "org.freedesktop.resolve1", "/org/freedesktop/resolve1", dbusInterface, member, others);
587
- }
718
+ };
588
719
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
589
- static managerInvoke(bus, member, others) {
720
+ ResolvedAdvertiser.managerInvoke = function (bus, member, others) {
590
721
  return this.resolvedInvoke(bus, "org.freedesktop.resolve1.Manager", member, others);
591
- }
592
- }
722
+ };
723
+ return ResolvedAdvertiser;
724
+ }(events_1.EventEmitter));
593
725
  exports.ResolvedAdvertiser = ResolvedAdvertiser;
594
726
  //# sourceMappingURL=Advertiser.js.map