@iobroker/js-controller-adapter 7.2.3 → 8.0.0-alpha.2-20260923-99da17fcc

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 (108) hide show
  1. package/build/cjs/index.d.ts +0 -1
  2. package/build/cjs/index.js +5 -8
  3. package/build/cjs/index.js.map +2 -2
  4. package/build/cjs/lib/_Types.d.ts +394 -63
  5. package/build/cjs/lib/adapter/adapter.d.ts +1896 -177
  6. package/build/cjs/lib/adapter/adapter.js +788 -1035
  7. package/build/cjs/lib/adapter/adapter.js.map +2 -2
  8. package/build/cjs/lib/adapter/asyncAdapter.d.ts +167 -0
  9. package/build/cjs/lib/adapter/asyncAdapter.js +252 -0
  10. package/build/cjs/lib/adapter/asyncAdapter.js.map +7 -0
  11. package/build/cjs/lib/adapter/context.d.ts +34 -0
  12. package/build/cjs/lib/adapter/context.js +17 -0
  13. package/build/cjs/lib/adapter/context.js.map +7 -0
  14. package/build/cjs/lib/adapter/defaultObjs.d.ts +5 -0
  15. package/build/cjs/lib/adapter/defaultObjs.js +2 -0
  16. package/build/cjs/lib/adapter/defaultObjs.js.map +2 -2
  17. package/build/cjs/lib/adapter/log.d.ts +25 -0
  18. package/build/cjs/lib/adapter/log.js +29 -0
  19. package/build/cjs/lib/adapter/log.js.map +2 -2
  20. package/build/cjs/lib/adapter/managers/AdapterContextBase.d.ts +49 -0
  21. package/build/cjs/lib/adapter/managers/AdapterContextBase.js +98 -0
  22. package/build/cjs/lib/adapter/managers/AdapterContextBase.js.map +7 -0
  23. package/build/cjs/lib/adapter/managers/AliasManager.d.ts +103 -0
  24. package/build/cjs/lib/adapter/managers/AliasManager.js +293 -0
  25. package/build/cjs/lib/adapter/managers/AliasManager.js.map +7 -0
  26. package/build/cjs/lib/adapter/managers/CertificateManager.d.ts +50 -0
  27. package/build/cjs/lib/adapter/managers/CertificateManager.js +174 -0
  28. package/build/cjs/lib/adapter/managers/CertificateManager.js.map +7 -0
  29. package/build/cjs/lib/adapter/managers/MessagingManager.d.ts +58 -0
  30. package/build/cjs/lib/adapter/managers/MessagingManager.js +289 -0
  31. package/build/cjs/lib/adapter/managers/MessagingManager.js.map +7 -0
  32. package/build/cjs/lib/adapter/managers/ResourceManager.d.ts +69 -0
  33. package/build/cjs/lib/adapter/managers/ResourceManager.js +170 -0
  34. package/build/cjs/lib/adapter/managers/ResourceManager.js.map +7 -0
  35. package/build/cjs/lib/adapter/managers/SubscriptionManager.d.ts +107 -0
  36. package/build/cjs/lib/adapter/managers/SubscriptionManager.js +349 -0
  37. package/build/cjs/lib/adapter/managers/SubscriptionManager.js.map +7 -0
  38. package/build/cjs/lib/adapter/userInterfaceMessagingController.d.ts +10 -0
  39. package/build/cjs/lib/adapter/userInterfaceMessagingController.js +9 -2
  40. package/build/cjs/lib/adapter/userInterfaceMessagingController.js.map +2 -2
  41. package/build/cjs/lib/adapter/utils.js +15 -1
  42. package/build/cjs/lib/adapter/utils.js.map +2 -2
  43. package/build/cjs/lib/adapter/validator.d.ts +10 -5
  44. package/build/cjs/lib/adapter/validator.js +6 -5
  45. package/build/cjs/lib/adapter/validator.js.map +2 -2
  46. package/build/esm/index.d.ts +0 -1
  47. package/build/esm/index.d.ts.map +1 -1
  48. package/build/esm/index.js +0 -1
  49. package/build/esm/index.js.map +1 -1
  50. package/build/esm/lib/_Types.d.ts +394 -63
  51. package/build/esm/lib/_Types.d.ts.map +1 -1
  52. package/build/esm/lib/adapter/adapter.d.ts +1896 -177
  53. package/build/esm/lib/adapter/adapter.d.ts.map +1 -1
  54. package/build/esm/lib/adapter/adapter.js +900 -1215
  55. package/build/esm/lib/adapter/adapter.js.map +1 -1
  56. package/build/esm/lib/adapter/asyncAdapter.d.ts +167 -0
  57. package/build/esm/lib/adapter/asyncAdapter.d.ts.map +1 -0
  58. package/build/esm/lib/adapter/asyncAdapter.js +225 -0
  59. package/build/esm/lib/adapter/asyncAdapter.js.map +1 -0
  60. package/build/esm/lib/adapter/context.d.ts +34 -0
  61. package/build/esm/lib/adapter/context.d.ts.map +1 -0
  62. package/build/esm/lib/adapter/context.js +2 -0
  63. package/build/esm/lib/adapter/context.js.map +1 -0
  64. package/build/esm/lib/adapter/defaultObjs.d.ts +5 -0
  65. package/build/esm/lib/adapter/defaultObjs.d.ts.map +1 -1
  66. package/build/esm/lib/adapter/defaultObjs.js +5 -0
  67. package/build/esm/lib/adapter/defaultObjs.js.map +1 -1
  68. package/build/esm/lib/adapter/log.d.ts +25 -0
  69. package/build/esm/lib/adapter/log.d.ts.map +1 -1
  70. package/build/esm/lib/adapter/log.js +25 -0
  71. package/build/esm/lib/adapter/log.js.map +1 -1
  72. package/build/esm/lib/adapter/managers/AdapterContextBase.d.ts +49 -0
  73. package/build/esm/lib/adapter/managers/AdapterContextBase.d.ts.map +1 -0
  74. package/build/esm/lib/adapter/managers/AdapterContextBase.js +77 -0
  75. package/build/esm/lib/adapter/managers/AdapterContextBase.js.map +1 -0
  76. package/build/esm/lib/adapter/managers/AliasManager.d.ts +103 -0
  77. package/build/esm/lib/adapter/managers/AliasManager.d.ts.map +1 -0
  78. package/build/esm/lib/adapter/managers/AliasManager.js +269 -0
  79. package/build/esm/lib/adapter/managers/AliasManager.js.map +1 -0
  80. package/build/esm/lib/adapter/managers/CertificateManager.d.ts +50 -0
  81. package/build/esm/lib/adapter/managers/CertificateManager.d.ts.map +1 -0
  82. package/build/esm/lib/adapter/managers/CertificateManager.js +142 -0
  83. package/build/esm/lib/adapter/managers/CertificateManager.js.map +1 -0
  84. package/build/esm/lib/adapter/managers/MessagingManager.d.ts +58 -0
  85. package/build/esm/lib/adapter/managers/MessagingManager.d.ts.map +1 -0
  86. package/build/esm/lib/adapter/managers/MessagingManager.js +267 -0
  87. package/build/esm/lib/adapter/managers/MessagingManager.js.map +1 -0
  88. package/build/esm/lib/adapter/managers/ResourceManager.d.ts +69 -0
  89. package/build/esm/lib/adapter/managers/ResourceManager.d.ts.map +1 -0
  90. package/build/esm/lib/adapter/managers/ResourceManager.js +151 -0
  91. package/build/esm/lib/adapter/managers/ResourceManager.js.map +1 -0
  92. package/build/esm/lib/adapter/managers/SubscriptionManager.d.ts +107 -0
  93. package/build/esm/lib/adapter/managers/SubscriptionManager.d.ts.map +1 -0
  94. package/build/esm/lib/adapter/managers/SubscriptionManager.js +374 -0
  95. package/build/esm/lib/adapter/managers/SubscriptionManager.js.map +1 -0
  96. package/build/esm/lib/adapter/userInterfaceMessagingController.d.ts +10 -0
  97. package/build/esm/lib/adapter/userInterfaceMessagingController.d.ts.map +1 -1
  98. package/build/esm/lib/adapter/userInterfaceMessagingController.js +8 -2
  99. package/build/esm/lib/adapter/userInterfaceMessagingController.js.map +1 -1
  100. package/build/esm/lib/adapter/utils.d.ts.map +1 -1
  101. package/build/esm/lib/adapter/utils.js +1 -1
  102. package/build/esm/lib/adapter/utils.js.map +1 -1
  103. package/build/esm/lib/adapter/validator.d.ts +10 -5
  104. package/build/esm/lib/adapter/validator.d.ts.map +1 -1
  105. package/build/esm/lib/adapter/validator.js +8 -5
  106. package/build/esm/lib/adapter/validator.js.map +1 -1
  107. package/build/tsconfig.build.tsbuildinfo +1 -1
  108. package/package.json +25 -20
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../../src/lib/adapter/managers/AliasManager.ts"],
4
+ "sourcesContent": ["import { tools } from '@iobroker/js-controller-common';\nimport deepClone from 'deep-clone';\nimport type { AliasDetails, AliasTargetEntry } from '../../_Types.js';\nimport type { ValidateIdOptions } from '@/lib/adapter/validator.js';\nimport type { AdapterContext } from '@/lib/adapter/context.js';\nimport { AdapterContextBase } from '@/lib/adapter/managers/AdapterContextBase.js';\nimport { ALIAS_STARTS_WITH, SYSTEM_ADMIN_USER } from '@/lib/adapter/constants.js';\n\n/** Validates a (possibly foreign) id; throws on invalid. Bound to the adapter's Validator. */\nexport type ValidateId = (id: string, isForeignId: boolean, options?: ValidateIdOptions | null) => void;\n\n/**\n * Owns the alias subscription cache and its bookkeeping: which real (source) states feed which\n * `alias.*` targets, the alias subscription patterns, and the single `alias.*` object subscription.\n * The source-state change fan-out ({@link resolveSourceChange}) is synchronous by contract so the\n * state-change handler cannot interleave a cache mutation mid-fan-out.\n */\nexport class AliasManager extends AdapterContextBase {\n readonly #aliases = new Map<string, AliasDetails>();\n readonly #aliasPatterns = new Set<string>();\n #aliasObjectsSubscribed = false;\n readonly #validateId: ValidateId;\n\n /**\n * @param ctx Shared adapter context providing live runtime state\n * @param validateId Bound id validator from the adapter's Validator instance\n */\n constructor(ctx: AdapterContext, validateId: ValidateId) {\n super(ctx);\n this.#validateId = validateId;\n }\n\n /**\n * Whether the given source (real) state id currently feeds any alias.\n *\n * @param id the real source state id\n */\n hasSource(id: string): boolean {\n return this.#aliases.has(id);\n }\n\n /** Number of tracked source states. */\n get size(): number {\n return this.#aliases.size;\n }\n\n /**\n * Whether the given subscription pattern is tracked.\n *\n * @param pattern the subscription pattern\n */\n hasPattern(pattern: string): boolean {\n return this.#aliasPatterns.has(pattern);\n }\n\n /**\n * Track a subscription pattern.\n *\n * @param pattern the subscription pattern\n */\n addPattern(pattern: string): void {\n this.#aliasPatterns.add(pattern);\n }\n\n /**\n * Stop tracking a subscription pattern.\n *\n * @param pattern the subscription pattern\n */\n deletePattern(pattern: string): void {\n this.#aliasPatterns.delete(pattern);\n }\n\n /**\n * Whether `id` falls into any tracked alias pattern (wildcards via pattern2RegEx).\n *\n * @param id the id to test against the tracked patterns\n */\n matchesAnyPattern(id: string): boolean {\n for (const aliasPattern of this.#aliasPatterns) {\n const testPattern =\n aliasPattern.slice(-1) === '*' ? new RegExp(tools.pattern2RegEx(aliasPattern)) : aliasPattern;\n if (\n (typeof testPattern === 'string' && aliasPattern === id) ||\n (testPattern instanceof RegExp && testPattern.test(id))\n ) {\n return true;\n }\n }\n return false;\n }\n\n /**\n * Reacts to a change of an `alias.*` object: re-points, updates, or removes the target across the\n * tracked sources, and picks up a newly-appeared alias that falls into a tracked pattern.\n * Live-iterates the cache while awaiting add/remove \u2014 the loop lives here, with the map, to\n * preserve the original mutation-during-iteration semantics exactly.\n *\n * @param id the changed alias object id\n * @param obj the new object (null/undefined if deleted)\n */\n async handleAliasObjectChange(id: string, obj: ioBroker.Object | null | undefined): Promise<void> {\n let isNewAlias = true;\n for (const [sourceId, alias] of this.#aliases) {\n const targetAlias = alias.targets.find(entry => entry.id === id);\n if (targetAlias) {\n isNewAlias = false;\n if (obj?.common?.alias?.id) {\n const newSourceId =\n typeof obj.common.alias.id.read === 'string' ? obj.common.alias.id.read : obj.common.alias.id;\n if (newSourceId !== sourceId) {\n await this.removeAliasSubscribe(sourceId, targetAlias);\n try {\n await this.addAliasSubscribe(obj, targetAlias.pattern);\n } catch (e: any) {\n this.logger.error(`${this.namespaceLog} Could not add alias subscription: ${e.message}`);\n }\n } else {\n targetAlias.min = obj.common.min;\n targetAlias.max = obj.common.max;\n targetAlias.type = obj.common.type;\n targetAlias.alias = deepClone(obj.common.alias);\n }\n } else {\n await this.removeAliasSubscribe(sourceId, targetAlias);\n }\n }\n }\n if (isNewAlias && obj && this.matchesAnyPattern(id)) {\n try {\n await this.addAliasSubscribe(obj, id);\n } catch (e: any) {\n this.logger.warn(`${this.namespaceLog} Could not add alias subscription: ${e.message}`);\n }\n }\n }\n\n /**\n * Removes every target subscribed under the given pattern, live-iterating the cache so a removal\n * mutating the map mid-loop keeps the original semantics. Awaits each removal.\n *\n * @param pattern the subscription pattern being unsubscribed\n */\n async removeTargetsForPattern(pattern: string): Promise<void> {\n const promises = new Array<Promise<void>>();\n for (const [sourceId, alias] of this.#aliases) {\n for (let i = alias.targets.length - 1; i >= 0; i--) {\n if (alias.targets[i].pattern === pattern) {\n promises.push(this.removeAliasSubscribe(sourceId, i));\n }\n }\n }\n await Promise.all(promises);\n }\n\n /**\n * Synchronously computes the fan-out of a source (real) state change to its alias targets.\n * Returns one `{ targetId, state }` per unique target. MUST NOT await \u2014 the caller relies on the\n * cache not mutating between read and emit.\n *\n * @param sourceId the changed real state id\n * @param state the new state value (or null on deletion)\n */\n resolveSourceChange(\n sourceId: string,\n state: ioBroker.State | null | undefined,\n ): Array<{ targetId: string; state: ioBroker.State | null }> {\n const out = new Array<{ targetId: string; state: ioBroker.State | null }>();\n const alias = this.#aliases.get(sourceId);\n if (!alias) {\n return out;\n }\n const uniqueTargets = new Set<string>();\n for (const target of alias.targets) {\n const targetId = target.id;\n if (uniqueTargets.has(targetId)) {\n continue;\n }\n uniqueTargets.add(targetId);\n const aState = state\n ? tools.formatAliasValue({\n sourceCommon: alias.source,\n targetCommon: target,\n state: deepClone(state),\n logger: this.logger,\n logNamespace: this.namespaceLog,\n sourceId,\n targetId,\n })\n : null;\n if (aState || !state) {\n out.push({ targetId, state: aState });\n }\n }\n return out;\n }\n\n /** Subscribe the `alias.*` object range once (idempotent). */\n ensureAliasObjectSubscription(): void {\n if (!this.#aliasObjectsSubscribed) {\n this.#aliasObjectsSubscribed = true;\n this.objects.subscribe(`${ALIAS_STARTS_WITH}*`);\n }\n }\n\n /** Drop the `alias.*` object subscription when no aliases remain. */\n maybeDropAliasObjectSubscription(): void {\n if (!this.#aliases.size && this.#aliasObjectsSubscribed) {\n this.#aliasObjectsSubscribed = false;\n this.objects.unsubscribe(`${ALIAS_STARTS_WITH}*`);\n }\n }\n\n /**\n * Register an alias target for its source state, subscribing the source state (and reading its\n * metadata once). Preserves the create-before-await ordering and the post-await existence\n * re-check that guard a concurrent removal.\n *\n * @param aliasObj the `alias.*` object being subscribed\n * @param pattern the subscription pattern that pulled it in\n */\n async addAliasSubscribe(aliasObj: ioBroker.AnyObject, pattern: string): Promise<void> {\n if (aliasObj.type !== 'state') {\n return;\n }\n if (!aliasObj.common?.alias?.id) {\n this.logger.warn(`${this.namespaceLog} Alias ${aliasObj._id} has no target 5`);\n throw new Error(`Alias ${aliasObj._id} has no target`);\n }\n const sourceId = tools.isObject(aliasObj.common.alias.id)\n ? aliasObj.common.alias.id.read\n : aliasObj.common.alias.id;\n try {\n this.#validateId(sourceId, true, null);\n } catch (e: any) {\n throw new Error(`Error validating alias id of ${aliasObj._id}: ${e.message}`);\n }\n const targetEntry: AliasTargetEntry = {\n alias: deepClone(aliasObj.common.alias),\n id: aliasObj._id,\n pattern,\n type: aliasObj.common.type,\n max: aliasObj.common.max,\n min: aliasObj.common.min,\n unit: aliasObj.common.unit,\n };\n let aliasDetails: AliasDetails;\n if (!this.#aliases.has(sourceId)) {\n aliasDetails = { targets: [] };\n this.#aliases.set(sourceId, aliasDetails);\n } else {\n aliasDetails = this.#aliases.get(sourceId)!;\n }\n if (!aliasDetails.source) {\n await this.states.subscribe(sourceId);\n const sourceObj = await this.objects.getObject(sourceId, { user: SYSTEM_ADMIN_USER });\n if (sourceObj?.common && this.#aliases.has(sourceObj._id)) {\n aliasDetails.source = {\n min: sourceObj.common.min,\n max: sourceObj.common.max,\n type: sourceObj.common.type,\n unit: sourceObj.common.unit,\n };\n }\n }\n aliasDetails.targets.push(targetEntry);\n }\n\n /**\n * Remove one alias target from its source; when the source has no targets left, delete the entry\n * and unsubscribe the source state.\n *\n * @param sourceId the real source state id\n * @param aliasObjOrIdx the target entry or its index in the targets array\n */\n async removeAliasSubscribe(sourceId: string, aliasObjOrIdx: number | AliasTargetEntry): Promise<void> {\n if (!this.#aliases.has(sourceId)) {\n return;\n }\n const alias = this.#aliases.get(sourceId)!;\n const pos = typeof aliasObjOrIdx === 'number' ? aliasObjOrIdx : alias.targets.indexOf(aliasObjOrIdx);\n if (pos !== -1) {\n alias.targets.splice(pos, 1);\n if (!alias.targets.length) {\n this.#aliases.delete(sourceId);\n await this.states.unsubscribe(sourceId);\n }\n }\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;AAAA,kCAAsB;AACtB,wBAAsB;AAItB,gCAAmC;AACnC,uBAAqD;AAW/C,MAAO,qBAAqB,6CAAkB;EAjBpD,OAiBoD;;;EACvC,WAAW,oBAAI,IAAG;EAClB,iBAAiB,oBAA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AAY;IAC5C,OAAO;AACH,qBAAe,KAAK,SAAS,IAAI,QAAQ;IAC7C;AACA,QAAI,CAAC,aAAa,QAAQ;AACtB,YAAM,KAAK,OAAO,UAAU,QAAQ;AACpC,YAAM,YAAY,MAAM,KAAK,QAAQ,UAAU,UAAU,EAAE,MAAM,mCAAiB,CAAE;AACpF,UAAI,WAAW,UAAU,KAAK,SAAS,IAAI,UAAU,GAAG,GAAG;AACvD,qBAAa,SAAS;UAClB,KAAK,UAAU,OAAO;UACtB,KAAK,UAAU,OAAO;UACtB,MAAM,UAAU,OAAO;UACvB,MAAM,UAAU,OAAO;;MAE/B;IACJ;AACA,iBAAa,QAAQ,KAAK,WAAW;EACzC;;;;;;;;EASA,MAAM,qBAAqB,UAAkB,eAAwC;AACjF,QAAI,CAAC,KAAK,SAAS,IAAI,QAAQ,GAAG;AAC9B;IACJ;AACA,UAAM,QAAQ,KAAK,SAAS,IAAI,QAAQ;AACxC,UAAM,MAAM,OAAO,kBAAkB,WAAW,gBAAgB,MAAM,QAAQ,QAAQ,aAAa;AACnG,QAAI,QAAQ,IAAI;AACZ,YAAM,QAAQ,OAAO,KAAK,CAAC;AAC3B,UAAI,CAAC,MAAM,QAAQ,QAAQ;AACvB,aAAK,SAAS,OAAO,QAAQ;AAC7B,cAAM,KAAK,OAAO,YAAY,QAAQ;MAC1C;IACJ;EACJ;;",
6
+ "names": ["deepClone"]
7
+ }
@@ -0,0 +1,50 @@
1
+ import type { InternalGetCertificatesResult } from '../../_Types.js';
2
+ import type { AdapterContext } from '../../../lib/adapter/context.js';
3
+ import { AdapterContextBase } from '../../../lib/adapter/managers/AdapterContextBase.js';
4
+ /** ID of the object holding all configured certificates */
5
+ export declare const CERTIFICATES_OBJECT_ID = "system.certificates";
6
+ /** Fetches SSL certificates from `system.certificates`, resolving file-backed values to their contents. */
7
+ export declare class CertificateManager extends AdapterContextBase {
8
+ #private;
9
+ /**
10
+ * @param ctx Shared adapter context providing live runtime state
11
+ */
12
+ constructor(ctx: AdapterContext);
13
+ /**
14
+ * Tells whether a new version of the `system.certificates` object actually changes one of the
15
+ * certificates handed out by the last {@link getCertificates} call. Unrelated certificates being
16
+ * added, changed or removed are ignored, so that editing some other adapter's certificate does
17
+ * not restart this one.
18
+ *
19
+ * Compares the raw (unresolved) values: for a file-backed certificate that is the path, so
20
+ * repointing it to a different file counts as a change, while a change of the file content is
21
+ * left to the file watcher.
22
+ *
23
+ * @param obj the new `system.certificates` object, or null/undefined if it was deleted
24
+ */
25
+ hasRelevantChange(obj: ioBroker.OtherObject | null | undefined): boolean;
26
+ /**
27
+ * Forgets the certificates handed out by the last {@link getCertificates} call, so a subsequent
28
+ * change of `system.certificates` is no longer treated as relevant by {@link hasRelevantChange}.
29
+ * Used when the adapter stops watching its certificates.
30
+ */
31
+ stopWatching(): void;
32
+ /**
33
+ * Loads the named certificates from `system.certificates`. File-backed values (short strings that
34
+ * are existing paths) are read from disk and their paths collected in `certFilePaths` so the caller
35
+ * can watch them.
36
+ *
37
+ * @param names public/private/chained certificate names
38
+ * @param names.publicName Name of the public certificate
39
+ * @param names.privateName Name of the private key
40
+ * @param names.chainedName Name of the chained certificate
41
+ * @throws {Error} `ERROR_DB_CLOSED` when the objects database is not connected
42
+ * @throws {Error} `ERROR_NOT_FOUND` when the requested certificates are not configured
43
+ */
44
+ getCertificates(names: {
45
+ publicName?: string;
46
+ privateName?: string;
47
+ chainedName?: string;
48
+ }): Promise<InternalGetCertificatesResult>;
49
+ }
50
+ //# sourceMappingURL=CertificateManager.d.ts.map
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+ var CertificateManager_exports = {};
31
+ __export(CertificateManager_exports, {
32
+ CERTIFICATES_OBJECT_ID: () => CERTIFICATES_OBJECT_ID,
33
+ CertificateManager: () => CertificateManager
34
+ });
35
+ module.exports = __toCommonJS(CertificateManager_exports);
36
+ var import_fs_extra = __toESM(require("fs-extra"), 1);
37
+ var import_js_controller_common = require("@iobroker/js-controller-common");
38
+ var import_AdapterContextBase = require("../../../lib/adapter/managers/AdapterContextBase.js");
39
+ const CERTIFICATES_OBJECT_ID = "system.certificates";
40
+ class CertificateManager extends import_AdapterContextBase.AdapterContextBase {
41
+ static {
42
+ __name(this, "CertificateManager");
43
+ }
44
+ /**
45
+ * Raw (unresolved) values of the certificates handed out by the last {@link getCertificates} call,
46
+ * keyed by certificate name. Used to tell a relevant change of `system.certificates` from an
47
+ * unrelated one. `undefined` until certificates have been requested at least once.
48
+ */
49
+ #usedCertificates;
50
+ /**
51
+ * @param ctx Shared adapter context providing live runtime state
52
+ */
53
+ constructor(ctx) {
54
+ super(ctx);
55
+ }
56
+ /**
57
+ * Tells whether a new version of the `system.certificates` object actually changes one of the
58
+ * certificates handed out by the last {@link getCertificates} call. Unrelated certificates being
59
+ * added, changed or removed are ignored, so that editing some other adapter's certificate does
60
+ * not restart this one.
61
+ *
62
+ * Compares the raw (unresolved) values: for a file-backed certificate that is the path, so
63
+ * repointing it to a different file counts as a change, while a change of the file content is
64
+ * left to the file watcher.
65
+ *
66
+ * @param obj the new `system.certificates` object, or null/undefined if it was deleted
67
+ */
68
+ hasRelevantChange(obj) {
69
+ const usedCertificates = this.#usedCertificates;
70
+ if (!usedCertificates) {
71
+ return false;
72
+ }
73
+ const certificates = obj?.native?.certificates;
74
+ for (const [name, value] of usedCertificates) {
75
+ if (certificates?.[name] !== value) {
76
+ return true;
77
+ }
78
+ }
79
+ return false;
80
+ }
81
+ /**
82
+ * Forgets the certificates handed out by the last {@link getCertificates} call, so a subsequent
83
+ * change of `system.certificates` is no longer treated as relevant by {@link hasRelevantChange}.
84
+ * Used when the adapter stops watching its certificates.
85
+ */
86
+ stopWatching() {
87
+ this.#usedCertificates = void 0;
88
+ }
89
+ /**
90
+ * Loads the named certificates from `system.certificates`. File-backed values (short strings that
91
+ * are existing paths) are read from disk and their paths collected in `certFilePaths` so the caller
92
+ * can watch them.
93
+ *
94
+ * @param names public/private/chained certificate names
95
+ * @param names.publicName Name of the public certificate
96
+ * @param names.privateName Name of the private key
97
+ * @param names.chainedName Name of the chained certificate
98
+ * @throws {Error} `ERROR_DB_CLOSED` when the objects database is not connected
99
+ * @throws {Error} `ERROR_NOT_FOUND` when the requested certificates are not configured
100
+ */
101
+ async getCertificates(names) {
102
+ const { publicName, privateName, chainedName } = names;
103
+ const objects = this.objects;
104
+ let obj;
105
+ try {
106
+ obj = await objects.getObject(CERTIFICATES_OBJECT_ID);
107
+ } catch {
108
+ }
109
+ if (!obj?.native.certificates || !publicName || !privateName || !obj.native.certificates[publicName] || !obj.native.certificates[privateName] || chainedName && !obj.native.certificates[chainedName]) {
110
+ this.logger.error(`${this.namespaceLog} Cannot configure secure web server, because no certificates found: ${publicName}, ${privateName}, ${chainedName}`);
111
+ if (publicName === "defaultPublic" || privateName === "defaultPrivate") {
112
+ this.logger.info(`${this.namespaceLog} Default certificates seem to be configured but missing. You can execute "iobroker cert create" in your shell to create these.`);
113
+ }
114
+ throw new Error(import_js_controller_common.tools.ERRORS.ERROR_NOT_FOUND);
115
+ }
116
+ const certFilePaths = new Array();
117
+ let ca;
118
+ if (chainedName) {
119
+ const chained = this.#resolveCert(obj.native.certificates[chainedName], certFilePaths).split("-----END CERTIFICATE-----\r\n");
120
+ if (chained.join("").length >= 512) {
121
+ const caArr = new Array();
122
+ for (const cert of chained) {
123
+ if (cert.replace(/(\r\n|\r|\n)/g, "").trim()) {
124
+ caArr.push(`${cert}-----END CERTIFICATE-----\r
125
+ `);
126
+ }
127
+ }
128
+ ca = caArr.join("");
129
+ }
130
+ }
131
+ const certs = {
132
+ key: this.#resolveCert(obj.native.certificates[privateName], certFilePaths),
133
+ cert: this.#resolveCert(obj.native.certificates[publicName], certFilePaths),
134
+ ca
135
+ };
136
+ this.#usedCertificates = /* @__PURE__ */ new Map();
137
+ for (const name of [publicName, privateName, chainedName]) {
138
+ if (name) {
139
+ this.#usedCertificates.set(name, obj.native.certificates[name]);
140
+ }
141
+ }
142
+ return { certs, useLetsEncrypt: obj.native.letsEncrypt, certFilePaths };
143
+ }
144
+ /**
145
+ * Returns the certificate content. When `value` is a short string that is an existing file path,
146
+ * reads the file, records the path in `certFilePaths` (deduped), and returns the file content.
147
+ * Otherwise returns `value` unchanged.
148
+ *
149
+ * @param value inline certificate or a path to one
150
+ * @param certFilePaths accumulator for file-backed paths (mutated)
151
+ */
152
+ #resolveCert(value, certFilePaths) {
153
+ if (typeof value === "string") {
154
+ try {
155
+ if (value.length < 1024 && import_fs_extra.default.existsSync(value)) {
156
+ const content = import_fs_extra.default.readFileSync(value, "utf8");
157
+ if (!certFilePaths.includes(value)) {
158
+ certFilePaths.push(value);
159
+ }
160
+ return content;
161
+ }
162
+ } catch (e) {
163
+ this.logger.error(`${this.namespaceLog} Could not read certificate from file ${value}: ${e.message}`);
164
+ }
165
+ }
166
+ return value;
167
+ }
168
+ }
169
+ // Annotate the CommonJS export names for ESM import in node:
170
+ 0 && (module.exports = {
171
+ CERTIFICATES_OBJECT_ID,
172
+ CertificateManager
173
+ });
174
+ //# sourceMappingURL=CertificateManager.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../../src/lib/adapter/managers/CertificateManager.ts"],
4
+ "sourcesContent": ["import fs from 'fs-extra';\nimport { tools } from '@iobroker/js-controller-common';\nimport type { InternalGetCertificatesResult } from '../../_Types.js';\nimport type { AdapterContext } from '@/lib/adapter/context.js';\nimport { AdapterContextBase } from '@/lib/adapter/managers/AdapterContextBase.js';\n\n/** ID of the object holding all configured certificates */\nexport const CERTIFICATES_OBJECT_ID = 'system.certificates';\n\n/** Fetches SSL certificates from `system.certificates`, resolving file-backed values to their contents. */\nexport class CertificateManager extends AdapterContextBase {\n /**\n * Raw (unresolved) values of the certificates handed out by the last {@link getCertificates} call,\n * keyed by certificate name. Used to tell a relevant change of `system.certificates` from an\n * unrelated one. `undefined` until certificates have been requested at least once.\n */\n #usedCertificates?: Map<string, string>;\n\n /**\n * @param ctx Shared adapter context providing live runtime state\n */\n constructor(ctx: AdapterContext) {\n super(ctx);\n }\n\n /**\n * Tells whether a new version of the `system.certificates` object actually changes one of the\n * certificates handed out by the last {@link getCertificates} call. Unrelated certificates being\n * added, changed or removed are ignored, so that editing some other adapter's certificate does\n * not restart this one.\n *\n * Compares the raw (unresolved) values: for a file-backed certificate that is the path, so\n * repointing it to a different file counts as a change, while a change of the file content is\n * left to the file watcher.\n *\n * @param obj the new `system.certificates` object, or null/undefined if it was deleted\n */\n hasRelevantChange(obj: ioBroker.OtherObject | null | undefined): boolean {\n const usedCertificates = this.#usedCertificates;\n if (!usedCertificates) {\n // certificates were never requested, so nothing can have changed for us\n return false;\n }\n\n const certificates: Record<string, string> | undefined = obj?.native?.certificates;\n\n for (const [name, value] of usedCertificates) {\n if (certificates?.[name] !== value) {\n return true;\n }\n }\n\n return false;\n }\n\n /**\n * Forgets the certificates handed out by the last {@link getCertificates} call, so a subsequent\n * change of `system.certificates` is no longer treated as relevant by {@link hasRelevantChange}.\n * Used when the adapter stops watching its certificates.\n */\n stopWatching(): void {\n this.#usedCertificates = undefined;\n }\n\n /**\n * Loads the named certificates from `system.certificates`. File-backed values (short strings that\n * are existing paths) are read from disk and their paths collected in `certFilePaths` so the caller\n * can watch them.\n *\n * @param names public/private/chained certificate names\n * @param names.publicName Name of the public certificate\n * @param names.privateName Name of the private key\n * @param names.chainedName Name of the chained certificate\n * @throws {Error} `ERROR_DB_CLOSED` when the objects database is not connected\n * @throws {Error} `ERROR_NOT_FOUND` when the requested certificates are not configured\n */\n async getCertificates(names: {\n publicName?: string;\n privateName?: string;\n chainedName?: string;\n }): Promise<InternalGetCertificatesResult> {\n const { publicName, privateName, chainedName } = names;\n const objects = this.objects;\n\n let obj: ioBroker.OtherObject | undefined | null;\n try {\n obj = await objects.getObject(CERTIFICATES_OBJECT_ID);\n } catch {\n // ignore\n }\n\n if (\n !obj?.native.certificates ||\n !publicName ||\n !privateName ||\n !obj.native.certificates[publicName] ||\n !obj.native.certificates[privateName] ||\n (chainedName && !obj.native.certificates[chainedName])\n ) {\n this.logger.error(\n `${this.namespaceLog} Cannot configure secure web server, because no certificates found: ${publicName}, ${privateName}, ${chainedName}`,\n );\n if (publicName === 'defaultPublic' || privateName === 'defaultPrivate') {\n this.logger.info(\n `${this.namespaceLog} Default certificates seem to be configured but missing. You can execute \"iobroker cert create\" in your shell to create these.`,\n );\n }\n throw new Error(tools.ERRORS.ERROR_NOT_FOUND);\n }\n\n const certFilePaths = new Array<string>();\n\n let ca: string | undefined;\n if (chainedName) {\n const chained = this.#resolveCert(obj.native.certificates[chainedName], certFilePaths).split(\n '-----END CERTIFICATE-----\\r\\n',\n );\n if (chained.join('').length >= 512) {\n const caArr = new Array<string>();\n for (const cert of chained) {\n if (cert.replace(/(\\r\\n|\\r|\\n)/g, '').trim()) {\n caArr.push(`${cert}-----END CERTIFICATE-----\\r\\n`);\n }\n }\n ca = caArr.join('');\n }\n }\n\n const certs: ioBroker.Certificates = {\n key: this.#resolveCert(obj.native.certificates[privateName], certFilePaths),\n cert: this.#resolveCert(obj.native.certificates[publicName], certFilePaths),\n ca,\n };\n\n // remember the raw values, so a change of `system.certificates` can be told from an unrelated one\n this.#usedCertificates = new Map<string, string>();\n for (const name of [publicName, privateName, chainedName]) {\n if (name) {\n this.#usedCertificates.set(name, obj.native.certificates[name]);\n }\n }\n\n return { certs, useLetsEncrypt: obj.native.letsEncrypt, certFilePaths };\n }\n\n /**\n * Returns the certificate content. When `value` is a short string that is an existing file path,\n * reads the file, records the path in `certFilePaths` (deduped), and returns the file content.\n * Otherwise returns `value` unchanged.\n *\n * @param value inline certificate or a path to one\n * @param certFilePaths accumulator for file-backed paths (mutated)\n */\n #resolveCert(value: string, certFilePaths: string[]): string {\n if (typeof value === 'string') {\n try {\n if (value.length < 1024 && fs.existsSync(value)) {\n const content = fs.readFileSync(value, 'utf8');\n if (!certFilePaths.includes(value)) {\n certFilePaths.push(value);\n }\n return content;\n }\n } catch (e: any) {\n this.logger.error(`${this.namespaceLog} Could not read certificate from file ${value}: ${e.message}`);\n }\n }\n return value;\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;AAAA,sBAAe;AACf,kCAAsB;AAGtB,gCAAmC;AAG5B,MAAM,yBAAyB;AAGhC,MAAO,2BAA2B,6CAAkB;EAV1D,OAU0D;;;;;;;;EAMtD;;;;EAKA,YAAY,KAAmB;AAC3B,UAAM,GAAG;EACb;;;;;;;;;;;;;EAcA,kBAAkB,KAA4C;AAC1D,UAAM,mBAAmB,KAAK;AAC9B,QAAI,CAAC,kBAAkB;AAEnB,aAA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KAAK,KAAK,aAAa,IAAI,OAAO,aAAa,WAAW,GAAG,aAAa;MAC1E,MAAM,KAAK,aAAa,IAAI,OAAO,aAAa,UAAU,GAAG,aAAa;MAC1E;;AAIJ,SAAK,oBAAoB,oBAAI,IAAG;AAChC,eAAW,QAAQ,CAAC,YAAY,aAAa,WAAW,GAAG;AACvD,UAAI,MAAM;AACN,aAAK,kBAAkB,IAAI,MAAM,IAAI,OAAO,aAAa,IAAI,CAAC;MAClE;IACJ;AAEA,WAAO,EAAE,OAAO,gBAAgB,IAAI,OAAO,aAAa,cAAa;EACzE;;;;;;;;;EAUA,aAAa,OAAe,eAAuB;AAC/C,QAAI,OAAO,UAAU,UAAU;AAC3B,UAAI;AACA,YAAI,MAAM,SAAS,QAAQ,gBAAAA,QAAG,WAAW,KAAK,GAAG;AAC7C,gBAAM,UAAU,gBAAAA,QAAG,aAAa,OAAO,MAAM;AAC7C,cAAI,CAAC,cAAc,SAAS,KAAK,GAAG;AAChC,0BAAc,KAAK,KAAK;UAC5B;AACA,iBAAO;QACX;MACJ,SAAS,GAAQ;AACb,aAAK,OAAO,MAAM,GAAG,KAAK,YAAY,yCAAyC,KAAK,KAAK,EAAE,OAAO,EAAE;MACxG;IACJ;AACA,WAAO;EACX;;",
6
+ "names": ["fs"]
7
+ }
@@ -0,0 +1,58 @@
1
+ import type { InternalSendToHostOptions, InternalSendToOptions, NotificationOptions, SendToUserInterfaceClientOptions } from '../../_Types.js';
2
+ import type { AdapterContext } from '../../../lib/adapter/context.js';
3
+ import { AdapterContextBase } from '../../../lib/adapter/managers/AdapterContextBase.js';
4
+ /** Owns the adapter's outbound messaging and the pending-reply registry. */
5
+ export declare class MessagingManager extends AdapterContextBase {
6
+ #private;
7
+ /**
8
+ * @param ctx Shared adapter context providing live runtime state
9
+ */
10
+ constructor(ctx: AdapterContext);
11
+ /**
12
+ * Sends a message to another adapter instance.
13
+ *
14
+ * When `opts.expectReply` is true and the target is a specific instance (not broadcast),
15
+ * the returned promise resolves with the reply message when it arrives, or rejects with
16
+ * `Error('Timeout exceeded')` if `opts.options.timeout` elapses.
17
+ *
18
+ * @param opts Normalized send options
19
+ */
20
+ sendTo(opts: InternalSendToOptions): Promise<any>;
21
+ /**
22
+ * Sends a message to a host, or broadcasts to all hosts when `hostName` is `null`.
23
+ *
24
+ * When `opts.expectReply` is true and a specific host is targeted, the returned promise
25
+ * resolves with the reply message when it arrives, or rejects with `Error('Timeout exceeded')`
26
+ * if `opts.options.timeout` elapses. Broadcasts (hostName === null) resolve void.
27
+ *
28
+ * @param opts Normalized send options
29
+ */
30
+ sendToHost(opts: InternalSendToHostOptions): Promise<any>;
31
+ /**
32
+ * Resolves a pending reply promise for an acked messagebox message.
33
+ * Returns true if a pending entry was found and consumed.
34
+ *
35
+ * @param obj incoming message object from the messagebox
36
+ */
37
+ resolveCallback(obj: ioBroker.Message): boolean;
38
+ /**
39
+ * Rejects all pending reply promises and clears their timers (used on stop).
40
+ */
41
+ clearPendingCallbacks(): void;
42
+ /**
43
+ * Sends a message to a single UI client, or broadcasts to all connected UI clients when `clientId` is omitted.
44
+ *
45
+ * @param options clientId and data options
46
+ */
47
+ sendToUI(options: SendToUserInterfaceClientOptions): Promise<void>;
48
+ /**
49
+ * Sends an addNotification command to the host of this adapter instance.
50
+ *
51
+ * @param scope notification scope
52
+ * @param category notification category, or `null` to match by scope regex
53
+ * @param message notification message
54
+ * @param options additional notification options
55
+ */
56
+ registerNotification(scope: string, category: string | null, message: string, options?: NotificationOptions): Promise<void>;
57
+ }
58
+ //# sourceMappingURL=MessagingManager.d.ts.map
@@ -0,0 +1,289 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var MessagingManager_exports = {};
21
+ __export(MessagingManager_exports, {
22
+ MessagingManager: () => MessagingManager
23
+ });
24
+ module.exports = __toCommonJS(MessagingManager_exports);
25
+ var import_AdapterContextBase = require("../../../lib/adapter/managers/AdapterContextBase.js");
26
+ var import_utils = require("../../../lib/adapter/utils.js");
27
+ class MessagingManager extends import_AdapterContextBase.AdapterContextBase {
28
+ static {
29
+ __name(this, "MessagingManager");
30
+ }
31
+ #pending = /* @__PURE__ */ new Map();
32
+ #callbackId = 1;
33
+ #mboxSubscribed = false;
34
+ /**
35
+ * @param ctx Shared adapter context providing live runtime state
36
+ */
37
+ constructor(ctx) {
38
+ super(ctx);
39
+ }
40
+ /**
41
+ * Sends a message to another adapter instance.
42
+ *
43
+ * When `opts.expectReply` is true and the target is a specific instance (not broadcast),
44
+ * the returned promise resolves with the reply message when it arrives, or rejects with
45
+ * `Error('Timeout exceeded')` if `opts.options.timeout` elapses.
46
+ *
47
+ * @param opts Normalized send options
48
+ */
49
+ async sendTo(opts) {
50
+ const { command, message, callback, options, expectReply } = opts;
51
+ let { instanceName } = opts;
52
+ if (!instanceName) {
53
+ throw new Error("No instanceName provided or not a string");
54
+ }
55
+ if (!instanceName.startsWith("system.adapter.")) {
56
+ instanceName = `system.adapter.${instanceName}`;
57
+ }
58
+ const obj = {
59
+ command,
60
+ message,
61
+ from: `system.adapter.${this.namespace}`
62
+ };
63
+ const states = this.states;
64
+ if (typeof message !== "object") {
65
+ this.logger.silly(`${this.namespaceLog} sendTo "${command}" to ${instanceName} from system.adapter.${this.namespace}: ${message}`);
66
+ } else {
67
+ this.logger.silly(`${this.namespaceLog} sendTo "${command}" to ${instanceName} from system.adapter.${this.namespace}`);
68
+ }
69
+ if (!instanceName.match(/\.[0-9]+$/)) {
70
+ const objects = this.objects;
71
+ const res = await objects.getObjectView("system", "instance", {
72
+ startkey: `${instanceName}.`,
73
+ endkey: `${instanceName}.\u9999`
74
+ }).catch(() => void 0);
75
+ if (res) {
76
+ for (const row of res.rows) {
77
+ await states.pushMessage(row.id, obj);
78
+ }
79
+ }
80
+ return;
81
+ }
82
+ if (expectReply) {
83
+ return this.#registerReply(states, instanceName, obj, options);
84
+ }
85
+ if (callback) {
86
+ obj.callback = { ...callback, ack: true };
87
+ }
88
+ await states.pushMessage(instanceName, obj);
89
+ }
90
+ /**
91
+ * Subscribes to this instance's messagebox on demand when the adapter does not advertise
92
+ * messagebox support, so replies to outbound messages are received. Idempotent.
93
+ *
94
+ * @param states the connected states DB client
95
+ */
96
+ #maybeSubscribe(states) {
97
+ const common = this.common;
98
+ if (common && !(0, import_utils.isMessageboxSupported)(common) && !this.#mboxSubscribed) {
99
+ this.#mboxSubscribed = true;
100
+ states.subscribeMessage(`system.adapter.${this.namespace}`).catch((e) => this.logger.error(`${this.namespaceLog} Cannot subscribe to messages: ${e.message}`));
101
+ }
102
+ }
103
+ /**
104
+ * Sets a reply callback header on `obj`, registers a pending-reply resolver keyed by its id,
105
+ * subscribes the messagebox on demand, pushes the message and returns a promise that resolves
106
+ * with the reply once `resolveCallback` is invoked for the matching id, or rejects with
107
+ * `Error('Timeout exceeded')` when `options.timeout` elapses.
108
+ *
109
+ * @param states the connected states DB client
110
+ * @param target the recipient id (instance or host) for `pushMessage`
111
+ * @param obj the message to send; its `callback` header is set here
112
+ * @param options additional send options (e.g. timeout)
113
+ */
114
+ async #registerReply(states, target, obj, options) {
115
+ this.#maybeSubscribe(states);
116
+ const id = this.#callbackId++;
117
+ if (this.#callbackId >= 4294967295) {
118
+ this.#callbackId = 1;
119
+ }
120
+ obj.callback = {
121
+ message: obj.message,
122
+ id,
123
+ ack: false,
124
+ time: Date.now()
125
+ };
126
+ let pendingEntry;
127
+ const replyPromise = new Promise((resolve, reject) => {
128
+ let timer;
129
+ if (options?.timeout) {
130
+ timer = setTimeout(() => {
131
+ if (this.#pending.has(id)) {
132
+ this.#pending.delete(id);
133
+ reject(new Error("Timeout exceeded"));
134
+ }
135
+ }, options.timeout);
136
+ }
137
+ pendingEntry = { resolve, reject, timer, time: Date.now() };
138
+ this.#pending.set(id, pendingEntry);
139
+ });
140
+ const now = Date.now();
141
+ for (const [_id, entry] of this.#pending) {
142
+ if (now - entry.time > 36e5) {
143
+ this.#pending.delete(_id);
144
+ }
145
+ }
146
+ try {
147
+ await states.pushMessage(target, obj);
148
+ } catch (e) {
149
+ if (pendingEntry.timer) {
150
+ clearTimeout(pendingEntry.timer);
151
+ }
152
+ this.#pending.delete(id);
153
+ throw e;
154
+ }
155
+ return replyPromise;
156
+ }
157
+ /**
158
+ * Sends a message to a host, or broadcasts to all hosts when `hostName` is `null`.
159
+ *
160
+ * When `opts.expectReply` is true and a specific host is targeted, the returned promise
161
+ * resolves with the reply message when it arrives, or rejects with `Error('Timeout exceeded')`
162
+ * if `opts.options.timeout` elapses. Broadcasts (hostName === null) resolve void.
163
+ *
164
+ * @param opts Normalized send options
165
+ */
166
+ async sendToHost(opts) {
167
+ const { command, message, expectReply, options, callback } = opts;
168
+ let { hostName } = opts;
169
+ const obj = {
170
+ command,
171
+ message,
172
+ from: `system.adapter.${this.namespace}`
173
+ };
174
+ const states = this.states;
175
+ if (hostName && !hostName.startsWith("system.host.")) {
176
+ hostName = `system.host.${hostName}`;
177
+ }
178
+ if (!hostName) {
179
+ const objects = this.objects;
180
+ const res = await objects.getObjectList({ startkey: "system.host.", endkey: "system.host.\u9999" });
181
+ if (res?.rows.length) {
182
+ for (const row of res.rows) {
183
+ const parts = row.id.split(".");
184
+ if (parts.length === 3) {
185
+ await states.pushMessage(row.id, obj);
186
+ }
187
+ }
188
+ }
189
+ return;
190
+ }
191
+ if (expectReply) {
192
+ return this.#registerReply(states, hostName, obj, options);
193
+ }
194
+ if (callback) {
195
+ obj.callback = { ...callback, ack: true };
196
+ }
197
+ await states.pushMessage(hostName, obj);
198
+ }
199
+ /**
200
+ * Resolves a pending reply promise for an acked messagebox message.
201
+ * Returns true if a pending entry was found and consumed.
202
+ *
203
+ * @param obj incoming message object from the messagebox
204
+ */
205
+ resolveCallback(obj) {
206
+ if (!obj.callback?.ack || !obj.callback.id) {
207
+ return false;
208
+ }
209
+ const entry = this.#pending.get(obj.callback.id);
210
+ if (!entry) {
211
+ return false;
212
+ }
213
+ if (entry.timer) {
214
+ clearTimeout(entry.timer);
215
+ }
216
+ this.#pending.delete(obj.callback.id);
217
+ entry.resolve(obj.message);
218
+ const now = Date.now();
219
+ for (const [id, pending] of this.#pending) {
220
+ if (now - pending.time > 36e5) {
221
+ this.#pending.delete(id);
222
+ }
223
+ }
224
+ return true;
225
+ }
226
+ /**
227
+ * Rejects all pending reply promises and clears their timers (used on stop).
228
+ */
229
+ clearPendingCallbacks() {
230
+ if (this.#pending.size) {
231
+ const err = new Error("Adapter stopped");
232
+ for (const entry of this.#pending.values()) {
233
+ if (entry.timer) {
234
+ clearTimeout(entry.timer);
235
+ }
236
+ entry.reject(err);
237
+ }
238
+ this.#pending.clear();
239
+ }
240
+ }
241
+ /**
242
+ * Sends a message to a single UI client, or broadcasts to all connected UI clients when `clientId` is omitted.
243
+ *
244
+ * @param options clientId and data options
245
+ */
246
+ sendToUI(options) {
247
+ const states = this.states;
248
+ const { clientId, data } = options;
249
+ if (clientId === void 0) {
250
+ return this.uiMessagingController.sendToAllClients({
251
+ data,
252
+ states
253
+ });
254
+ }
255
+ return this.uiMessagingController.sendToClient({
256
+ clientId,
257
+ data,
258
+ states
259
+ });
260
+ }
261
+ /**
262
+ * Sends an addNotification command to the host of this adapter instance.
263
+ *
264
+ * @param scope notification scope
265
+ * @param category notification category, or `null` to match by scope regex
266
+ * @param message notification message
267
+ * @param options additional notification options
268
+ */
269
+ async registerNotification(scope, category, message, options) {
270
+ const states = this.states;
271
+ const obj = {
272
+ command: "addNotification",
273
+ message: {
274
+ scope,
275
+ category,
276
+ message,
277
+ instance: this.namespace,
278
+ contextData: options?.contextData
279
+ },
280
+ from: `system.adapter.${this.namespace}`
281
+ };
282
+ await states.pushMessage(`system.host.${this.host}`, obj);
283
+ }
284
+ }
285
+ // Annotate the CommonJS export names for ESM import in node:
286
+ 0 && (module.exports = {
287
+ MessagingManager
288
+ });
289
+ //# sourceMappingURL=MessagingManager.js.map