matterbridge 3.3.3-dev-20251018-66ae7a4 → 3.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/dist/broadcastServer.d.ts +105 -0
- package/dist/broadcastServer.d.ts.map +1 -0
- package/dist/broadcastServer.js +86 -1
- package/dist/broadcastServer.js.map +1 -0
- package/dist/broadcastServerTypes.d.ts +719 -0
- package/dist/broadcastServerTypes.d.ts.map +1 -0
- package/dist/broadcastServerTypes.js +24 -0
- package/dist/broadcastServerTypes.js.map +1 -0
- package/dist/cli.d.ts +26 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +135 -5
- package/dist/cli.js.map +1 -0
- package/dist/cliEmitter.d.ts +50 -0
- package/dist/cliEmitter.d.ts.map +1 -0
- package/dist/cliEmitter.js +37 -0
- package/dist/cliEmitter.js.map +1 -0
- package/dist/cliHistory.d.ts +74 -0
- package/dist/cliHistory.d.ts.map +1 -0
- package/dist/cliHistory.js +44 -0
- package/dist/cliHistory.js.map +1 -0
- package/dist/clusters/export.d.ts +2 -0
- package/dist/clusters/export.d.ts.map +1 -0
- package/dist/clusters/export.js +2 -0
- package/dist/clusters/export.js.map +1 -0
- package/dist/defaultConfigSchema.d.ts +28 -0
- package/dist/defaultConfigSchema.d.ts.map +1 -0
- package/dist/defaultConfigSchema.js +24 -0
- package/dist/defaultConfigSchema.js.map +1 -0
- package/dist/deviceManager.d.ts +117 -0
- package/dist/deviceManager.d.ts.map +1 -0
- package/dist/deviceManager.js +124 -1
- package/dist/deviceManager.js.map +1 -0
- package/dist/devices/airConditioner.d.ts +98 -0
- package/dist/devices/airConditioner.d.ts.map +1 -0
- package/dist/devices/airConditioner.js +57 -0
- package/dist/devices/airConditioner.js.map +1 -0
- package/dist/devices/batteryStorage.d.ts +48 -0
- package/dist/devices/batteryStorage.d.ts.map +1 -0
- package/dist/devices/batteryStorage.js +48 -1
- package/dist/devices/batteryStorage.js.map +1 -0
- package/dist/devices/cooktop.d.ts +60 -0
- package/dist/devices/cooktop.d.ts.map +1 -0
- package/dist/devices/cooktop.js +55 -0
- package/dist/devices/cooktop.js.map +1 -0
- package/dist/devices/dishwasher.d.ts +71 -0
- package/dist/devices/dishwasher.d.ts.map +1 -0
- package/dist/devices/dishwasher.js +57 -0
- package/dist/devices/dishwasher.js.map +1 -0
- package/dist/devices/evse.d.ts +75 -0
- package/dist/devices/evse.d.ts.map +1 -0
- package/dist/devices/evse.js +74 -10
- package/dist/devices/evse.js.map +1 -0
- package/dist/devices/export.d.ts +17 -0
- package/dist/devices/export.d.ts.map +1 -0
- package/dist/devices/export.js +5 -0
- package/dist/devices/export.js.map +1 -0
- package/dist/devices/extractorHood.d.ts +46 -0
- package/dist/devices/extractorHood.d.ts.map +1 -0
- package/dist/devices/extractorHood.js +42 -0
- package/dist/devices/extractorHood.js.map +1 -0
- package/dist/devices/heatPump.d.ts +47 -0
- package/dist/devices/heatPump.d.ts.map +1 -0
- package/dist/devices/heatPump.js +50 -2
- package/dist/devices/heatPump.js.map +1 -0
- package/dist/devices/laundryDryer.d.ts +67 -0
- package/dist/devices/laundryDryer.d.ts.map +1 -0
- package/dist/devices/laundryDryer.js +62 -3
- package/dist/devices/laundryDryer.js.map +1 -0
- package/dist/devices/laundryWasher.d.ts +81 -0
- package/dist/devices/laundryWasher.d.ts.map +1 -0
- package/dist/devices/laundryWasher.js +70 -4
- package/dist/devices/laundryWasher.js.map +1 -0
- package/dist/devices/microwaveOven.d.ts +168 -0
- package/dist/devices/microwaveOven.d.ts.map +1 -0
- package/dist/devices/microwaveOven.js +88 -5
- package/dist/devices/microwaveOven.js.map +1 -0
- package/dist/devices/oven.d.ts +105 -0
- package/dist/devices/oven.d.ts.map +1 -0
- package/dist/devices/oven.js +85 -0
- package/dist/devices/oven.js.map +1 -0
- package/dist/devices/refrigerator.d.ts +118 -0
- package/dist/devices/refrigerator.d.ts.map +1 -0
- package/dist/devices/refrigerator.js +102 -0
- package/dist/devices/refrigerator.js.map +1 -0
- package/dist/devices/roboticVacuumCleaner.d.ts +112 -0
- package/dist/devices/roboticVacuumCleaner.d.ts.map +1 -0
- package/dist/devices/roboticVacuumCleaner.js +100 -9
- package/dist/devices/roboticVacuumCleaner.js.map +1 -0
- package/dist/devices/solarPower.d.ts +40 -0
- package/dist/devices/solarPower.d.ts.map +1 -0
- package/dist/devices/solarPower.js +38 -0
- package/dist/devices/solarPower.js.map +1 -0
- package/dist/devices/speaker.d.ts +87 -0
- package/dist/devices/speaker.d.ts.map +1 -0
- package/dist/devices/speaker.js +84 -0
- package/dist/devices/speaker.js.map +1 -0
- package/dist/devices/temperatureControl.d.ts +166 -0
- package/dist/devices/temperatureControl.d.ts.map +1 -0
- package/dist/devices/temperatureControl.js +25 -3
- package/dist/devices/temperatureControl.js.map +1 -0
- package/dist/devices/waterHeater.d.ts +111 -0
- package/dist/devices/waterHeater.d.ts.map +1 -0
- package/dist/devices/waterHeater.js +82 -2
- package/dist/devices/waterHeater.js.map +1 -0
- package/dist/dgram/coap.d.ts +205 -0
- package/dist/dgram/coap.d.ts.map +1 -0
- package/dist/dgram/coap.js +126 -13
- package/dist/dgram/coap.js.map +1 -0
- package/dist/dgram/dgram.d.ts +141 -0
- package/dist/dgram/dgram.d.ts.map +1 -0
- package/dist/dgram/dgram.js +114 -2
- package/dist/dgram/dgram.js.map +1 -0
- package/dist/dgram/mb_coap.d.ts +24 -0
- package/dist/dgram/mb_coap.d.ts.map +1 -0
- package/dist/dgram/mb_coap.js +41 -3
- package/dist/dgram/mb_coap.js.map +1 -0
- package/dist/dgram/mb_mdns.d.ts +24 -0
- package/dist/dgram/mb_mdns.d.ts.map +1 -0
- package/dist/dgram/mb_mdns.js +80 -15
- package/dist/dgram/mb_mdns.js.map +1 -0
- package/dist/dgram/mdns.d.ts +290 -0
- package/dist/dgram/mdns.d.ts.map +1 -0
- package/dist/dgram/mdns.js +299 -137
- package/dist/dgram/mdns.js.map +1 -0
- package/dist/dgram/multicast.d.ts +67 -0
- package/dist/dgram/multicast.d.ts.map +1 -0
- package/dist/dgram/multicast.js +62 -1
- package/dist/dgram/multicast.js.map +1 -0
- package/dist/dgram/unicast.d.ts +56 -0
- package/dist/dgram/unicast.d.ts.map +1 -0
- package/dist/dgram/unicast.js +54 -0
- package/dist/dgram/unicast.js.map +1 -0
- package/dist/frontend.d.ts +235 -0
- package/dist/frontend.d.ts.map +1 -0
- package/dist/frontend.js +413 -34
- package/dist/frontend.js.map +1 -0
- package/dist/frontendTypes.d.ts +529 -0
- package/dist/frontendTypes.d.ts.map +1 -0
- package/dist/frontendTypes.js +45 -0
- package/dist/frontendTypes.js.map +1 -0
- package/dist/helpers.d.ts +48 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +53 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/logger/export.d.ts +2 -0
- package/dist/logger/export.d.ts.map +1 -0
- package/dist/logger/export.js +1 -0
- package/dist/logger/export.js.map +1 -0
- package/dist/matter/behaviors.d.ts +2 -0
- package/dist/matter/behaviors.d.ts.map +1 -0
- package/dist/matter/behaviors.js +2 -0
- package/dist/matter/behaviors.js.map +1 -0
- package/dist/matter/clusters.d.ts +2 -0
- package/dist/matter/clusters.d.ts.map +1 -0
- package/dist/matter/clusters.js +2 -0
- package/dist/matter/clusters.js.map +1 -0
- package/dist/matter/devices.d.ts +2 -0
- package/dist/matter/devices.d.ts.map +1 -0
- package/dist/matter/devices.js +2 -0
- package/dist/matter/devices.js.map +1 -0
- package/dist/matter/endpoints.d.ts +2 -0
- package/dist/matter/endpoints.d.ts.map +1 -0
- package/dist/matter/endpoints.js +2 -0
- package/dist/matter/endpoints.js.map +1 -0
- package/dist/matter/export.d.ts +5 -0
- package/dist/matter/export.d.ts.map +1 -0
- package/dist/matter/export.js +3 -0
- package/dist/matter/export.js.map +1 -0
- package/dist/matter/types.d.ts +3 -0
- package/dist/matter/types.d.ts.map +1 -0
- package/dist/matter/types.js +3 -0
- package/dist/matter/types.js.map +1 -0
- package/dist/matterbridge.d.ts +469 -0
- package/dist/matterbridge.d.ts.map +1 -0
- package/dist/matterbridge.js +795 -45
- package/dist/matterbridge.js.map +1 -0
- package/dist/matterbridgeAccessoryPlatform.d.ts +42 -0
- package/dist/matterbridgeAccessoryPlatform.d.ts.map +1 -0
- package/dist/matterbridgeAccessoryPlatform.js +36 -0
- package/dist/matterbridgeAccessoryPlatform.js.map +1 -0
- package/dist/matterbridgeBehaviors.d.ts +2399 -0
- package/dist/matterbridgeBehaviors.d.ts.map +1 -0
- package/dist/matterbridgeBehaviors.js +71 -5
- package/dist/matterbridgeBehaviors.js.map +1 -0
- package/dist/matterbridgeDeviceTypes.d.ts +761 -0
- package/dist/matterbridgeDeviceTypes.d.ts.map +1 -0
- package/dist/matterbridgeDeviceTypes.js +630 -17
- package/dist/matterbridgeDeviceTypes.js.map +1 -0
- package/dist/matterbridgeDynamicPlatform.d.ts +42 -0
- package/dist/matterbridgeDynamicPlatform.d.ts.map +1 -0
- package/dist/matterbridgeDynamicPlatform.js +36 -0
- package/dist/matterbridgeDynamicPlatform.js.map +1 -0
- package/dist/matterbridgeEndpoint.d.ts +1545 -0
- package/dist/matterbridgeEndpoint.d.ts.map +1 -0
- package/dist/matterbridgeEndpoint.js +1412 -58
- package/dist/matterbridgeEndpoint.js.map +1 -0
- package/dist/matterbridgeEndpointHelpers.d.ts +560 -0
- package/dist/matterbridgeEndpointHelpers.d.ts.map +1 -0
- package/dist/matterbridgeEndpointHelpers.js +368 -10
- package/dist/matterbridgeEndpointHelpers.js.map +1 -0
- package/dist/matterbridgePlatform.d.ts +402 -0
- package/dist/matterbridgePlatform.d.ts.map +1 -0
- package/dist/matterbridgePlatform.js +341 -1
- package/dist/matterbridgePlatform.js.map +1 -0
- package/dist/matterbridgeTypes.d.ts +209 -0
- package/dist/matterbridgeTypes.d.ts.map +1 -0
- package/dist/matterbridgeTypes.js +26 -0
- package/dist/matterbridgeTypes.js.map +1 -0
- package/dist/pluginManager.d.ts +353 -0
- package/dist/pluginManager.d.ts.map +1 -0
- package/dist/pluginManager.js +325 -3
- package/dist/pluginManager.js.map +1 -0
- package/dist/shelly.d.ts +174 -0
- package/dist/shelly.d.ts.map +1 -0
- package/dist/shelly.js +168 -7
- package/dist/shelly.js.map +1 -0
- package/dist/storage/export.d.ts +2 -0
- package/dist/storage/export.d.ts.map +1 -0
- package/dist/storage/export.js +1 -0
- package/dist/storage/export.js.map +1 -0
- package/dist/update.d.ts +75 -0
- package/dist/update.d.ts.map +1 -0
- package/dist/update.js +69 -0
- package/dist/update.js.map +1 -0
- package/dist/utils/colorUtils.d.ts +99 -0
- package/dist/utils/colorUtils.d.ts.map +1 -0
- package/dist/utils/colorUtils.js +97 -2
- package/dist/utils/colorUtils.js.map +1 -0
- package/dist/utils/commandLine.d.ts +59 -0
- package/dist/utils/commandLine.d.ts.map +1 -0
- package/dist/utils/commandLine.js +54 -0
- package/dist/utils/commandLine.js.map +1 -0
- package/dist/utils/copyDirectory.d.ts +33 -0
- package/dist/utils/copyDirectory.d.ts.map +1 -0
- package/dist/utils/copyDirectory.js +38 -1
- package/dist/utils/copyDirectory.js.map +1 -0
- package/dist/utils/createDirectory.d.ts +34 -0
- package/dist/utils/createDirectory.d.ts.map +1 -0
- package/dist/utils/createDirectory.js +33 -0
- package/dist/utils/createDirectory.js.map +1 -0
- package/dist/utils/createZip.d.ts +39 -0
- package/dist/utils/createZip.d.ts.map +1 -0
- package/dist/utils/createZip.js +47 -2
- package/dist/utils/createZip.js.map +1 -0
- package/dist/utils/deepCopy.d.ts +32 -0
- package/dist/utils/deepCopy.d.ts.map +1 -0
- package/dist/utils/deepCopy.js +39 -0
- package/dist/utils/deepCopy.js.map +1 -0
- package/dist/utils/deepEqual.d.ts +54 -0
- package/dist/utils/deepEqual.d.ts.map +1 -0
- package/dist/utils/deepEqual.js +72 -1
- package/dist/utils/deepEqual.js.map +1 -0
- package/dist/utils/error.d.ts +44 -0
- package/dist/utils/error.d.ts.map +1 -0
- package/dist/utils/error.js +41 -0
- package/dist/utils/error.js.map +1 -0
- package/dist/utils/export.d.ts +13 -0
- package/dist/utils/export.d.ts.map +1 -0
- package/dist/utils/export.js +1 -0
- package/dist/utils/export.js.map +1 -0
- package/dist/utils/hex.d.ts +89 -0
- package/dist/utils/hex.d.ts.map +1 -0
- package/dist/utils/hex.js +124 -0
- package/dist/utils/hex.js.map +1 -0
- package/dist/utils/isvalid.d.ts +103 -0
- package/dist/utils/isvalid.d.ts.map +1 -0
- package/dist/utils/isvalid.js +101 -0
- package/dist/utils/isvalid.js.map +1 -0
- package/dist/utils/jestHelpers.d.ts +137 -0
- package/dist/utils/jestHelpers.d.ts.map +1 -0
- package/dist/utils/jestHelpers.js +153 -3
- package/dist/utils/jestHelpers.js.map +1 -0
- package/dist/utils/network.d.ts +115 -0
- package/dist/utils/network.d.ts.map +1 -0
- package/dist/utils/network.js +108 -5
- package/dist/utils/network.js.map +1 -0
- package/dist/utils/spawn.d.ts +35 -0
- package/dist/utils/spawn.d.ts.map +1 -0
- package/dist/utils/spawn.js +71 -0
- package/dist/utils/spawn.js.map +1 -0
- package/dist/utils/wait.d.ts +54 -0
- package/dist/utils/wait.d.ts.map +1 -0
- package/dist/utils/wait.js +60 -8
- package/dist/utils/wait.js.map +1 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +2 -1
|
@@ -1,21 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description This file contains the Jest helpers.
|
|
3
|
+
* @file src/helpers.test.ts
|
|
4
|
+
* @author Luca Liguori
|
|
5
|
+
* @created 2025-09-03
|
|
6
|
+
* @version 1.0.5
|
|
7
|
+
* @license Apache-2.0
|
|
8
|
+
*
|
|
9
|
+
* Copyright 2025, 2026, 2027 Luca Liguori.
|
|
10
|
+
*
|
|
11
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
12
|
+
* you may not use this file except in compliance with the License.
|
|
13
|
+
* You may obtain a copy of the License at
|
|
14
|
+
*
|
|
15
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
18
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
19
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
+
* See the License for the specific language governing permissions and
|
|
21
|
+
* limitations under the License.
|
|
22
|
+
*/
|
|
1
23
|
import { rmSync } from 'node:fs';
|
|
2
24
|
import { inspect } from 'node:util';
|
|
3
25
|
import path from 'node:path';
|
|
26
|
+
// Imports from Matterbridge
|
|
4
27
|
import { jest } from '@jest/globals';
|
|
5
28
|
import { DeviceTypeId, Endpoint, Environment, ServerNode, ServerNodeStore, VendorId, LogFormat as MatterLogFormat, LogLevel as MatterLogLevel, Lifecycle } from '@matter/main';
|
|
6
29
|
import { AggregatorEndpoint, RootEndpoint } from '@matter/main/endpoints';
|
|
7
30
|
import { MdnsService } from '@matter/main/protocol';
|
|
8
31
|
import { AnsiLogger } from 'node-ansi-logger';
|
|
32
|
+
// Imports from a plugin
|
|
33
|
+
/*
|
|
34
|
+
import { jest } from '@jest/globals';
|
|
35
|
+
import { DeviceTypeId, Endpoint, Environment, MdnsService, ServerNode, ServerNodeStore, VendorId, LogFormat as MatterLogFormat, LogLevel as MatterLogLevel, Lifecycle } from 'matterbridge/matter';
|
|
36
|
+
import { RootEndpoint, AggregatorEndpoint } from 'matterbridge/matter/endpoints';
|
|
37
|
+
import { AnsiLogger } from 'matterbridge/logger';
|
|
38
|
+
*/
|
|
9
39
|
export let loggerLogSpy;
|
|
10
40
|
export let consoleLogSpy;
|
|
11
41
|
export let consoleDebugSpy;
|
|
12
42
|
export let consoleInfoSpy;
|
|
13
43
|
export let consoleWarnSpy;
|
|
14
44
|
export let consoleErrorSpy;
|
|
45
|
+
/**
|
|
46
|
+
* Setup the Jest environment:
|
|
47
|
+
* - it will remove any existing home directory
|
|
48
|
+
* - setup the spies for logging
|
|
49
|
+
*
|
|
50
|
+
* @param {string} name The name of the test suite.
|
|
51
|
+
* @param {boolean} debug If true, the logging is not mocked.
|
|
52
|
+
*
|
|
53
|
+
* ```typescript
|
|
54
|
+
* import { consoleDebugSpy, consoleErrorSpy, consoleInfoSpy, consoleLogSpy, consoleWarnSpy, loggerLogSpy, setDebug, setupTest } from './jestHelpers.js';
|
|
55
|
+
*
|
|
56
|
+
* // Setup the test environment
|
|
57
|
+
* setupTest(NAME, false);
|
|
58
|
+
*
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
15
61
|
export function setupTest(name, debug = false) {
|
|
16
62
|
expect(name).toBeDefined();
|
|
17
63
|
expect(typeof name).toBe('string');
|
|
18
|
-
expect(name.length).toBeGreaterThanOrEqual(4);
|
|
64
|
+
expect(name.length).toBeGreaterThanOrEqual(4); // avoid accidental deletion of short paths like "/" or "C:\"
|
|
65
|
+
// Cleanup any existing home directory
|
|
19
66
|
rmSync(path.join('jest', name), { recursive: true, force: true });
|
|
20
67
|
if (debug) {
|
|
21
68
|
loggerLogSpy = jest.spyOn(AnsiLogger.prototype, 'log');
|
|
@@ -34,6 +81,11 @@ export function setupTest(name, debug = false) {
|
|
|
34
81
|
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
|
|
35
82
|
}
|
|
36
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Set or unset the debug mode.
|
|
86
|
+
*
|
|
87
|
+
* @param {boolean} debug If true, the logging is not mocked.
|
|
88
|
+
*/
|
|
37
89
|
export function setDebug(debug) {
|
|
38
90
|
if (debug) {
|
|
39
91
|
loggerLogSpy.mockRestore();
|
|
@@ -58,11 +110,21 @@ export function setDebug(debug) {
|
|
|
58
110
|
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
|
|
59
111
|
}
|
|
60
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Create a matter Environment for testing:
|
|
115
|
+
* - it will remove any existing home directory
|
|
116
|
+
* - setup the matter environment with homeDir, debug logging and ANSI format
|
|
117
|
+
*
|
|
118
|
+
* @param {string} homeDir Home directory for the environment.
|
|
119
|
+
* @returns {Environment} The default matter environment.
|
|
120
|
+
*/
|
|
61
121
|
export function createTestEnvironment(homeDir) {
|
|
62
122
|
expect(homeDir).toBeDefined();
|
|
63
123
|
expect(typeof homeDir).toBe('string');
|
|
64
|
-
expect(homeDir.length).toBeGreaterThanOrEqual(4);
|
|
124
|
+
expect(homeDir.length).toBeGreaterThanOrEqual(4); // avoid accidental deletion of short paths like "/" or "C:\"
|
|
125
|
+
// Cleanup any existing home directory
|
|
65
126
|
rmSync(homeDir, { recursive: true, force: true });
|
|
127
|
+
// Setup the matter environment
|
|
66
128
|
const environment = Environment.default;
|
|
67
129
|
environment.vars.set('log.level', MatterLogLevel.DEBUG);
|
|
68
130
|
environment.vars.set('log.format', MatterLogFormat.ANSI);
|
|
@@ -71,6 +133,18 @@ export function createTestEnvironment(homeDir) {
|
|
|
71
133
|
environment.vars.set('runtime.exitcode', false);
|
|
72
134
|
return environment;
|
|
73
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* Advance the Node.js event loop deterministically to allow chained asynchronous work (Promises scheduled in
|
|
138
|
+
* microtasks and follow‑up macrotasks) to complete inside tests without adding arbitrary long timeouts.
|
|
139
|
+
*
|
|
140
|
+
* NOTE: This does not guarantee OS level network IO completion—only JavaScript task queue progression inside the
|
|
141
|
+
* current process.
|
|
142
|
+
*
|
|
143
|
+
* @param {number} ticks Number of macrotask (setImmediate) turns to yield (default 3).
|
|
144
|
+
* @param {number} microTurns Number of microtask drains (Promise.resolve chains) after macrotask yielding (default 10).
|
|
145
|
+
* @param {number} pause Final timer delay in ms; set 0 to disable (default 100ms).
|
|
146
|
+
* @returns {Promise<void>} Resolves after the requested event loop advancement has completed.
|
|
147
|
+
*/
|
|
74
148
|
export async function flushAsync(ticks = 3, microTurns = 10, pause = 100) {
|
|
75
149
|
for (let i = 0; i < ticks; i++)
|
|
76
150
|
await new Promise((resolve) => setImmediate(resolve));
|
|
@@ -79,6 +153,19 @@ export async function flushAsync(ticks = 3, microTurns = 10, pause = 100) {
|
|
|
79
153
|
if (pause)
|
|
80
154
|
await new Promise((resolve) => setTimeout(resolve, pause));
|
|
81
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* Flush (await) the lazy endpoint number persistence mechanism used by matter.js.
|
|
158
|
+
*
|
|
159
|
+
* Background:
|
|
160
|
+
* assignNumber() batches persistence (store.saveNumber + updating __nextNumber__) via an internal promise (#numbersPersisted).
|
|
161
|
+
* Calling endpointStores.close() waits for the current batch only. If new endpoints were added in the same macrotask
|
|
162
|
+
* cycle additional micro/macro turns might be needed to ensure the batch started. We defensively yield macrotasks
|
|
163
|
+
* (setImmediate) and then await close() multiple rounds.
|
|
164
|
+
*
|
|
165
|
+
* @param {ServerNode} targetServer The server whose endpoint numbering persistence should be flushed.
|
|
166
|
+
* @param {number} rounds Number of macrotask + close cycles to run (2 is usually sufficient; 1 often works).
|
|
167
|
+
* @returns {Promise<void>} Resolves when pending number persistence batches have completed.
|
|
168
|
+
*/
|
|
82
169
|
export async function flushAllEndpointNumberPersistence(targetServer, rounds = 2) {
|
|
83
170
|
const nodeStore = targetServer.env.get(ServerNodeStore);
|
|
84
171
|
for (let i = 0; i < rounds; i++) {
|
|
@@ -86,6 +173,12 @@ export async function flushAllEndpointNumberPersistence(targetServer, rounds = 2
|
|
|
86
173
|
await nodeStore.endpointStores.close();
|
|
87
174
|
}
|
|
88
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* Collect all endpoints in the server endpoint tree (root -> descendants).
|
|
178
|
+
*
|
|
179
|
+
* @param {Endpoint} root Root endpoint (typically the ServerNode root endpoint cast as Endpoint).
|
|
180
|
+
* @returns {Endpoint[]} Flat array including the root and every descendant once.
|
|
181
|
+
*/
|
|
89
182
|
function collectAllEndpoints(root) {
|
|
90
183
|
const list = [];
|
|
91
184
|
const walk = (ep) => {
|
|
@@ -99,14 +192,26 @@ function collectAllEndpoints(root) {
|
|
|
99
192
|
walk(root);
|
|
100
193
|
return list;
|
|
101
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Assert that every endpoint attached to the server has an assigned and (batch-)persisted endpoint number.
|
|
197
|
+
*
|
|
198
|
+
* This waits for any outstanding number persistence batch (endpointStores.close()), then traverses the endpoint
|
|
199
|
+
* graph and asserts:
|
|
200
|
+
* - Root endpoint: number is 0 (allowing undefined to coerce to 0 via nullish coalescing check).
|
|
201
|
+
* - All other endpoints: number > 0.
|
|
202
|
+
*
|
|
203
|
+
* @param {ServerNode} targetServer The server whose endpoint numbers are verified.
|
|
204
|
+
* @returns {Promise<void>} Resolves when assertions complete.
|
|
205
|
+
*/
|
|
102
206
|
export async function assertAllEndpointNumbersPersisted(targetServer) {
|
|
103
207
|
const nodeStore = targetServer.env.get(ServerNodeStore);
|
|
208
|
+
// Ensure any pending persistence finished (flush any in-flight batch promise)
|
|
104
209
|
await nodeStore.endpointStores.close();
|
|
105
210
|
const all = collectAllEndpoints(targetServer);
|
|
106
211
|
for (const ep of all) {
|
|
107
212
|
const store = nodeStore.storeForEndpoint(ep);
|
|
108
213
|
if (ep.maybeNumber === 0) {
|
|
109
|
-
expect(store.number ?? 0).toBe(0);
|
|
214
|
+
expect(store.number ?? 0).toBe(0); // root
|
|
110
215
|
}
|
|
111
216
|
else {
|
|
112
217
|
expect(store.number).toBeGreaterThan(0);
|
|
@@ -114,7 +219,15 @@ export async function assertAllEndpointNumbersPersisted(targetServer) {
|
|
|
114
219
|
}
|
|
115
220
|
return all.length;
|
|
116
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Start a matter server node for testing.
|
|
224
|
+
*
|
|
225
|
+
* @param {string} name Name of the server (used for logging and product description).
|
|
226
|
+
* @param {number} port TCP port to listen on.
|
|
227
|
+
* @returns {Promise<[ServerNode<ServerNode.RootEndpoint>, Endpoint<AggregatorEndpoint>]>} Resolves to an array containing the created ServerNode and its AggregatorNode.
|
|
228
|
+
*/
|
|
117
229
|
export async function startServerNode(name, port) {
|
|
230
|
+
// Create the server node
|
|
118
231
|
const server = await ServerNode.create({
|
|
119
232
|
id: name + 'ServerNode',
|
|
120
233
|
productDescription: {
|
|
@@ -123,6 +236,7 @@ export async function startServerNode(name, port) {
|
|
|
123
236
|
vendorId: VendorId(0xfff1),
|
|
124
237
|
productId: 0x8000,
|
|
125
238
|
},
|
|
239
|
+
// Provide defaults for the BasicInformation cluster on the Root endpoint
|
|
126
240
|
basicInformation: {
|
|
127
241
|
vendorId: VendorId(0xfff1),
|
|
128
242
|
vendorName: 'Matterbridge',
|
|
@@ -139,21 +253,26 @@ export async function startServerNode(name, port) {
|
|
|
139
253
|
});
|
|
140
254
|
expect(server).toBeDefined();
|
|
141
255
|
expect(server.lifecycle.isReady).toBeTruthy();
|
|
256
|
+
// Create the aggregator node
|
|
142
257
|
const aggregator = new Endpoint(AggregatorEndpoint, {
|
|
143
258
|
id: name + 'AggregatorNode',
|
|
144
259
|
});
|
|
145
260
|
expect(aggregator).toBeDefined();
|
|
261
|
+
// Add the aggregator to the server
|
|
146
262
|
await server.add(aggregator);
|
|
147
263
|
expect(server.parts.has(aggregator.id)).toBeTruthy();
|
|
148
264
|
expect(server.parts.has(aggregator)).toBeTruthy();
|
|
149
265
|
expect(aggregator.lifecycle.isReady).toBeTruthy();
|
|
266
|
+
// Run the server
|
|
150
267
|
expect(server.lifecycle.isOnline).toBeFalsy();
|
|
268
|
+
// Wait for the server to be online
|
|
151
269
|
await new Promise((resolve) => {
|
|
152
270
|
server.lifecycle.online.on(async () => {
|
|
153
271
|
resolve();
|
|
154
272
|
});
|
|
155
273
|
server.start();
|
|
156
274
|
});
|
|
275
|
+
// Check if the server is online
|
|
157
276
|
expect(server.lifecycle.isReady).toBeTruthy();
|
|
158
277
|
expect(server.lifecycle.isOnline).toBeTruthy();
|
|
159
278
|
expect(server.lifecycle.isCommissioned).toBeFalsy();
|
|
@@ -165,21 +284,41 @@ export async function startServerNode(name, port) {
|
|
|
165
284
|
expect(aggregator.lifecycle.isPartsReady).toBeTruthy();
|
|
166
285
|
expect(aggregator.lifecycle.hasId).toBeTruthy();
|
|
167
286
|
expect(aggregator.lifecycle.hasNumber).toBeTruthy();
|
|
287
|
+
// Ensure the queue is empty and pause 100ms
|
|
168
288
|
await flushAsync();
|
|
169
289
|
return [server, aggregator];
|
|
170
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* Stop a matter server node.
|
|
293
|
+
*
|
|
294
|
+
* @param {ServerNode<ServerNode.RootEndpoint>} server The server to stop.
|
|
295
|
+
* @returns {Promise<void>} Resolves when the server has stopped.
|
|
296
|
+
*/
|
|
171
297
|
export async function stopServerNode(server) {
|
|
298
|
+
// Flush any pending endpoint number persistence
|
|
172
299
|
await flushAllEndpointNumberPersistence(server);
|
|
300
|
+
// Ensure all endpoint numbers are persisted
|
|
173
301
|
await assertAllEndpointNumbersPersisted(server);
|
|
302
|
+
// Stop the server
|
|
174
303
|
expect(server).toBeDefined();
|
|
175
304
|
expect(server.lifecycle.isReady).toBeTruthy();
|
|
176
305
|
expect(server.lifecycle.isOnline).toBeTruthy();
|
|
177
306
|
await server.close();
|
|
178
307
|
expect(server.lifecycle.isReady).toBeTruthy();
|
|
179
308
|
expect(server.lifecycle.isOnline).toBeFalsy();
|
|
309
|
+
// stop the mDNS service
|
|
180
310
|
await server.env.get(MdnsService)[Symbol.asyncDispose]();
|
|
311
|
+
// Ensure the queue is empty and pause 100ms
|
|
181
312
|
await flushAsync();
|
|
182
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
* Add a device (endpoint) to a matter server node or an aggregator.
|
|
316
|
+
*
|
|
317
|
+
* @param {ServerNode<ServerNode.RootEndpoint> | Endpoint<AggregatorEndpoint>} owner The server or aggregator to add the device to.
|
|
318
|
+
* @param {Endpoint} device The device to add.
|
|
319
|
+
* @param {number} pause The pause time in milliseconds after addition (default 10ms).
|
|
320
|
+
* @returns {Promise<void>} Resolves when the device has been added and is ready.
|
|
321
|
+
*/
|
|
183
322
|
export async function addDevice(owner, device, pause = 10) {
|
|
184
323
|
expect(owner).toBeDefined();
|
|
185
324
|
expect(device).toBeDefined();
|
|
@@ -192,6 +331,7 @@ export async function addDevice(owner, device, pause = 10) {
|
|
|
192
331
|
catch (error) {
|
|
193
332
|
const errorMessage = error instanceof Error ? error.message : error;
|
|
194
333
|
const errorInspect = inspect(error, { depth: 10 });
|
|
334
|
+
// eslint-disable-next-line no-console
|
|
195
335
|
console.error(`Error adding device ${device.maybeId}.${device.maybeNumber}: ${errorMessage}\nstack: ${errorInspect}`);
|
|
196
336
|
return false;
|
|
197
337
|
}
|
|
@@ -205,6 +345,14 @@ export async function addDevice(owner, device, pause = 10) {
|
|
|
205
345
|
await flushAsync(1, 1, pause);
|
|
206
346
|
return true;
|
|
207
347
|
}
|
|
348
|
+
/**
|
|
349
|
+
* Delete a device (endpoint) from a matter server node or an aggregator.
|
|
350
|
+
*
|
|
351
|
+
* @param {ServerNode<ServerNode.RootEndpoint> | Endpoint<AggregatorEndpoint>} owner The server or aggregator to remove the device from.
|
|
352
|
+
* @param {Endpoint} device The device to remove.
|
|
353
|
+
* @param {number} pause The pause time in milliseconds after deletion (default 10ms).
|
|
354
|
+
* @returns {Promise<void>} Resolves when the device has been removed and is no longer ready.
|
|
355
|
+
*/
|
|
208
356
|
export async function deleteDevice(owner, device, pause = 10) {
|
|
209
357
|
expect(owner).toBeDefined();
|
|
210
358
|
expect(device).toBeDefined();
|
|
@@ -217,6 +365,7 @@ export async function deleteDevice(owner, device, pause = 10) {
|
|
|
217
365
|
catch (error) {
|
|
218
366
|
const errorMessage = error instanceof Error ? error.message : error;
|
|
219
367
|
const errorInspect = inspect(error, { depth: 10 });
|
|
368
|
+
// eslint-disable-next-line no-console
|
|
220
369
|
console.error(`Error deleting device ${device.maybeId}.${device.maybeNumber}: ${errorMessage}\nstack: ${errorInspect}`);
|
|
221
370
|
return false;
|
|
222
371
|
}
|
|
@@ -230,3 +379,4 @@ export async function deleteDevice(owner, device, pause = 10) {
|
|
|
230
379
|
await flushAsync(1, 1, pause);
|
|
231
380
|
return true;
|
|
232
381
|
}
|
|
382
|
+
//# sourceMappingURL=jestHelpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jestHelpers.js","sourceRoot":"","sources":["../../src/utils/jestHelpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,4BAA4B;AAC5B,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,eAAe,EAAE,QAAQ,EAAE,SAAS,IAAI,eAAe,EAAE,QAAQ,IAAI,cAAc,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC/K,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,wBAAwB;AACxB;;;;;EAKE;AAEF,MAAM,CAAC,IAAI,YAAiE,CAAC;AAC7E,MAAM,CAAC,IAAI,aAAqD,CAAC;AACjE,MAAM,CAAC,IAAI,eAAuD,CAAC;AACnE,MAAM,CAAC,IAAI,cAAsD,CAAC;AAClE,MAAM,CAAC,IAAI,cAAsD,CAAC;AAClE,MAAM,CAAC,IAAI,eAAuD,CAAC;AAEnE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,SAAS,CAAC,IAAY,EAAE,QAAiB,KAAK;IAC5D,MAAM,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AK,CAAC;IACf,CAAC;IACD,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;IAC5C,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC,UAAU,EAAE,CAAC;IAClD,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC;IAC7C,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,SAAS,EAAE,CAAC;IACjD,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,UAAU,EAAE,CAAC;IAC5C,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,CAAC;IAChD,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpE,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;IAC9B,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description This file contains the network functions.
|
|
3
|
+
* @file network.ts
|
|
4
|
+
* @author Luca Liguori
|
|
5
|
+
* @created 2024-02-17
|
|
6
|
+
* @version 1.0.1
|
|
7
|
+
* @license Apache-2.0
|
|
8
|
+
*
|
|
9
|
+
* Copyright 2024, 2025, 2026 Luca Liguori.
|
|
10
|
+
*
|
|
11
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
12
|
+
* you may not use this file except in compliance with the License.
|
|
13
|
+
* You may obtain a copy of the License at
|
|
14
|
+
*
|
|
15
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
18
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
19
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
+
* See the License for the specific language governing permissions and
|
|
21
|
+
* limitations under the License.
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Retrieves the first non-internal network interface details.
|
|
25
|
+
*
|
|
26
|
+
* @returns {os.NetworkInterfaceInfo | undefined} The details of the selected network interface, or undefined if not found.
|
|
27
|
+
*/
|
|
28
|
+
export declare function getInterfaceDetails(): {
|
|
29
|
+
interfaceName: string;
|
|
30
|
+
ipv4Address: string | undefined;
|
|
31
|
+
ipv6Address: string | undefined;
|
|
32
|
+
macAddress: string | undefined;
|
|
33
|
+
} | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Retrieves the first non-internal network interface name.
|
|
36
|
+
*
|
|
37
|
+
* @returns {string | undefined} The name of the selected network interface, or undefined if not found.
|
|
38
|
+
*/
|
|
39
|
+
export declare function getInterfaceName(): string | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Retrieves the IPv4 address of the first non-internal network interface.
|
|
42
|
+
*
|
|
43
|
+
* @returns {string | undefined} The IPv4 address of the selected network interface, or undefined if not found.
|
|
44
|
+
*/
|
|
45
|
+
export declare function getIpv4InterfaceAddress(): string | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Retrieves the IPv6 address of the first non-internal network interface.
|
|
48
|
+
*
|
|
49
|
+
* @returns {string | undefined} The IPv4 address of the selected network interface, or undefined if not found.
|
|
50
|
+
*/
|
|
51
|
+
export declare function getIpv6InterfaceAddress(): string | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Retrieves the mac address of the first non-internal network interface.
|
|
54
|
+
*
|
|
55
|
+
* @returns {string | undefined} The mac address, or undefined if not found.
|
|
56
|
+
*/
|
|
57
|
+
export declare function getMacAddress(): string | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Logs the available network interfaces and their details.
|
|
60
|
+
*
|
|
61
|
+
* @returns {void}
|
|
62
|
+
*/
|
|
63
|
+
export declare function logInterfaces(): void;
|
|
64
|
+
/**
|
|
65
|
+
* Resolves the given hostname to an IP address.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} hostname - The hostname to resolve.
|
|
68
|
+
* @param {0 | 4 | 6} [family] - The address family to use (0 for any, 4 for IPv4, 6 for IPv6). Default is 4.
|
|
69
|
+
* @returns {Promise<string | null>} - A promise that resolves to the IP address or null if not found.
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* This function uses DNS lookup to resolve the hostname, which can take some time to complete.
|
|
73
|
+
*/
|
|
74
|
+
export declare function resolveHostname(hostname: string, family?: 0 | 4 | 6): Promise<string | null>;
|
|
75
|
+
/**
|
|
76
|
+
* Retrieves the version of an npm package from the npm registry.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} packageName - The name of the npm package.
|
|
79
|
+
* @param {string} [tag] - The tag of the package version to retrieve (default is 'latest').
|
|
80
|
+
* @param {number} [timeout] - The timeout duration in milliseconds (default is 10000ms).
|
|
81
|
+
* @returns {Promise<string>} A promise that resolves to the version string of the package.
|
|
82
|
+
* @throws {Error} If the request fails or the tag is not found.
|
|
83
|
+
*/
|
|
84
|
+
export declare function getNpmPackageVersion(packageName: string, tag?: string, timeout?: number): Promise<string>;
|
|
85
|
+
/**
|
|
86
|
+
* Retrieves a file from the public directory of the Matterbridge GitHub repository.
|
|
87
|
+
*
|
|
88
|
+
* @param {string} branch - The branch from which to fetch the file.
|
|
89
|
+
* @param {string} file - The file path to fetch.
|
|
90
|
+
* @param {number} [timeout] - The timeout duration in milliseconds (default is 10000ms).
|
|
91
|
+
* @returns {Promise<Record<string, boolean | string | number>>} A promise that resolves to the parsed JSON object from the file.
|
|
92
|
+
* @throws {Error} If the request fails or the JSON parsing fails.
|
|
93
|
+
*/
|
|
94
|
+
export declare function getGitHubUpdate(branch: 'main' | 'dev', file: string, timeout?: number): Promise<Record<string, boolean | string | number>>;
|
|
95
|
+
/**
|
|
96
|
+
* Retrieves the path to the global Node.js modules directory.
|
|
97
|
+
*
|
|
98
|
+
* @returns {Promise<string>} A promise that resolves to the path of the global Node.js modules directory.
|
|
99
|
+
*/
|
|
100
|
+
export declare function getGlobalNodeModules(): Promise<string>;
|
|
101
|
+
/**
|
|
102
|
+
* Function to format bytes to KB, MB, or GB
|
|
103
|
+
*
|
|
104
|
+
* @param {number} bytes - The number of bytes to format
|
|
105
|
+
* @returns {string} The formatted memory usage string
|
|
106
|
+
*/
|
|
107
|
+
export declare function formatMemoryUsage(bytes: number): string;
|
|
108
|
+
/**
|
|
109
|
+
* Function to format system uptime with only the most significant unit
|
|
110
|
+
*
|
|
111
|
+
* @param {number} seconds - The number of seconds to format
|
|
112
|
+
* @returns {string} The formatted uptime string
|
|
113
|
+
*/
|
|
114
|
+
export declare function formatOsUpTime(seconds: number): string;
|
|
115
|
+
//# sourceMappingURL=network.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/utils/network.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAUH;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAI;IAAE,aAAa,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,GAAG,SAAS,CAa7K;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,MAAM,GAAG,SAAS,CAOrD;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,IAAI,MAAM,GAAG,SAAS,CAO5D;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,IAAI,MAAM,GAAG,SAAS,CAO5D;AAED;;;;GAIG;AACH,wBAAgB,aAAa,IAAI,MAAM,GAAG,SAAS,CAOlD;AAED;;;;GAIG;AACH,wBAAgB,aAAa,IAAI,IAAI,CAmBpC;AAED;;;;;;;;;GASG;AACH,wBAAsB,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,GAAE,CAAC,GAAG,CAAC,GAAG,CAAK,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CASrG;AAED;;;;;;;;GAQG;AACH,wBAAsB,oBAAoB,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,GAAE,MAAiB,EAAE,OAAO,GAAE,MAAc,GAAG,OAAO,CAAC,MAAM,CAAC,CA8ChI;AAED;;;;;;;;GAQG;AACH,wBAAsB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,MAAc,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,CAAC,CAAC,CAyCvJ;AAED;;;;GAIG;AACH,wBAAsB,oBAAoB,IAAI,OAAO,CAAC,MAAM,CAAC,CAW5D;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAQvD;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CActD"}
|
package/dist/utils/network.js
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description This file contains the network functions.
|
|
3
|
+
* @file network.ts
|
|
4
|
+
* @author Luca Liguori
|
|
5
|
+
* @created 2024-02-17
|
|
6
|
+
* @version 1.0.1
|
|
7
|
+
* @license Apache-2.0
|
|
8
|
+
*
|
|
9
|
+
* Copyright 2024, 2025, 2026 Luca Liguori.
|
|
10
|
+
*
|
|
11
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
12
|
+
* you may not use this file except in compliance with the License.
|
|
13
|
+
* You may obtain a copy of the License at
|
|
14
|
+
*
|
|
15
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
18
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
19
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
+
* See the License for the specific language governing permissions and
|
|
21
|
+
* limitations under the License.
|
|
22
|
+
*/
|
|
23
|
+
// Node.js modules
|
|
1
24
|
import os from 'node:os';
|
|
25
|
+
// AnsiLogger module
|
|
2
26
|
import { AnsiLogger, BLUE, CYAN, nf } from 'node-ansi-logger';
|
|
27
|
+
/**
|
|
28
|
+
* Retrieves the first non-internal network interface details.
|
|
29
|
+
*
|
|
30
|
+
* @returns {os.NetworkInterfaceInfo | undefined} The details of the selected network interface, or undefined if not found.
|
|
31
|
+
*/
|
|
3
32
|
export function getInterfaceDetails() {
|
|
4
33
|
const result = { interfaceName: '', ipv4Address: undefined, ipv6Address: undefined, macAddress: undefined };
|
|
5
34
|
for (const [interfaceName, interfaceDetails] of Object.entries(os.networkInterfaces())) {
|
|
@@ -21,6 +50,11 @@ export function getInterfaceDetails() {
|
|
|
21
50
|
if (result.interfaceName)
|
|
22
51
|
return result;
|
|
23
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Retrieves the first non-internal network interface name.
|
|
55
|
+
*
|
|
56
|
+
* @returns {string | undefined} The name of the selected network interface, or undefined if not found.
|
|
57
|
+
*/
|
|
24
58
|
export function getInterfaceName() {
|
|
25
59
|
for (const [interfaceName, interfaceDetails] of Object.entries(os.networkInterfaces())) {
|
|
26
60
|
if (!interfaceName || !interfaceDetails || interfaceDetails.length === 0)
|
|
@@ -31,6 +65,11 @@ export function getInterfaceName() {
|
|
|
31
65
|
}
|
|
32
66
|
}
|
|
33
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Retrieves the IPv4 address of the first non-internal network interface.
|
|
70
|
+
*
|
|
71
|
+
* @returns {string | undefined} The IPv4 address of the selected network interface, or undefined if not found.
|
|
72
|
+
*/
|
|
34
73
|
export function getIpv4InterfaceAddress() {
|
|
35
74
|
for (const [interfaceName, interfaceDetails] of Object.entries(os.networkInterfaces())) {
|
|
36
75
|
if (!interfaceName || !interfaceDetails || interfaceDetails.length === 0)
|
|
@@ -41,6 +80,11 @@ export function getIpv4InterfaceAddress() {
|
|
|
41
80
|
}
|
|
42
81
|
}
|
|
43
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Retrieves the IPv6 address of the first non-internal network interface.
|
|
85
|
+
*
|
|
86
|
+
* @returns {string | undefined} The IPv4 address of the selected network interface, or undefined if not found.
|
|
87
|
+
*/
|
|
44
88
|
export function getIpv6InterfaceAddress() {
|
|
45
89
|
for (const [interfaceName, interfaceDetails] of Object.entries(os.networkInterfaces())) {
|
|
46
90
|
if (!interfaceName || !interfaceDetails || interfaceDetails.length === 0)
|
|
@@ -51,6 +95,11 @@ export function getIpv6InterfaceAddress() {
|
|
|
51
95
|
}
|
|
52
96
|
}
|
|
53
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* Retrieves the mac address of the first non-internal network interface.
|
|
100
|
+
*
|
|
101
|
+
* @returns {string | undefined} The mac address, or undefined if not found.
|
|
102
|
+
*/
|
|
54
103
|
export function getMacAddress() {
|
|
55
104
|
for (const [interfaceName, interfaceDetails] of Object.entries(os.networkInterfaces())) {
|
|
56
105
|
if (!interfaceName || !interfaceDetails || interfaceDetails.length === 0)
|
|
@@ -61,9 +110,14 @@ export function getMacAddress() {
|
|
|
61
110
|
}
|
|
62
111
|
}
|
|
63
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Logs the available network interfaces and their details.
|
|
115
|
+
*
|
|
116
|
+
* @returns {void}
|
|
117
|
+
*/
|
|
64
118
|
export function logInterfaces() {
|
|
65
|
-
const log = new AnsiLogger({ logName: 'MatterbridgeUtils', logTimestampFormat: 4
|
|
66
|
-
log.logLevel = "info"
|
|
119
|
+
const log = new AnsiLogger({ logName: 'MatterbridgeUtils', logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logLevel: "info" /* LogLevel.INFO */ });
|
|
120
|
+
log.logLevel = "info" /* LogLevel.INFO */;
|
|
67
121
|
log.logName = 'LogInterfaces';
|
|
68
122
|
log.info('Available Network Interfaces:');
|
|
69
123
|
const availableAddresses = Object.entries(os.networkInterfaces());
|
|
@@ -77,16 +131,36 @@ export function logInterfaces() {
|
|
|
77
131
|
}
|
|
78
132
|
}
|
|
79
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Resolves the given hostname to an IP address.
|
|
136
|
+
*
|
|
137
|
+
* @param {string} hostname - The hostname to resolve.
|
|
138
|
+
* @param {0 | 4 | 6} [family] - The address family to use (0 for any, 4 for IPv4, 6 for IPv6). Default is 4.
|
|
139
|
+
* @returns {Promise<string | null>} - A promise that resolves to the IP address or null if not found.
|
|
140
|
+
*
|
|
141
|
+
* @remarks
|
|
142
|
+
* This function uses DNS lookup to resolve the hostname, which can take some time to complete.
|
|
143
|
+
*/
|
|
80
144
|
export async function resolveHostname(hostname, family = 4) {
|
|
81
145
|
const dns = await import('node:dns');
|
|
82
146
|
try {
|
|
83
|
-
const addresses = await dns.promises.lookup(hostname.toLowerCase()
|
|
147
|
+
const addresses = await dns.promises.lookup(hostname.toLowerCase() /* + '.local'*/, { family });
|
|
84
148
|
return addresses.address;
|
|
149
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
85
150
|
}
|
|
86
151
|
catch (error) {
|
|
87
152
|
return null;
|
|
88
153
|
}
|
|
89
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Retrieves the version of an npm package from the npm registry.
|
|
157
|
+
*
|
|
158
|
+
* @param {string} packageName - The name of the npm package.
|
|
159
|
+
* @param {string} [tag] - The tag of the package version to retrieve (default is 'latest').
|
|
160
|
+
* @param {number} [timeout] - The timeout duration in milliseconds (default is 10000ms).
|
|
161
|
+
* @returns {Promise<string>} A promise that resolves to the version string of the package.
|
|
162
|
+
* @throws {Error} If the request fails or the tag is not found.
|
|
163
|
+
*/
|
|
90
164
|
export async function getNpmPackageVersion(packageName, tag = 'latest', timeout = 10000) {
|
|
91
165
|
const https = await import('node:https');
|
|
92
166
|
return new Promise((resolve, reject) => {
|
|
@@ -100,7 +174,7 @@ export async function getNpmPackageVersion(packageName, tag = 'latest', timeout
|
|
|
100
174
|
let data = '';
|
|
101
175
|
if (res.statusCode !== 200) {
|
|
102
176
|
clearTimeout(timeoutId);
|
|
103
|
-
res.resume();
|
|
177
|
+
res.resume(); // Discard response data to close the socket properly
|
|
104
178
|
reject(new Error(`Failed to fetch data. Status code: ${res.statusCode}`));
|
|
105
179
|
return;
|
|
106
180
|
}
|
|
@@ -111,6 +185,7 @@ export async function getNpmPackageVersion(packageName, tag = 'latest', timeout
|
|
|
111
185
|
clearTimeout(timeoutId);
|
|
112
186
|
try {
|
|
113
187
|
const jsonData = JSON.parse(data);
|
|
188
|
+
// console.log(`Package ${packageName} tag ${tag}`, jsonData);
|
|
114
189
|
const version = jsonData['dist-tags']?.[tag];
|
|
115
190
|
if (version) {
|
|
116
191
|
resolve(version);
|
|
@@ -130,6 +205,15 @@ export async function getNpmPackageVersion(packageName, tag = 'latest', timeout
|
|
|
130
205
|
});
|
|
131
206
|
});
|
|
132
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* Retrieves a file from the public directory of the Matterbridge GitHub repository.
|
|
210
|
+
*
|
|
211
|
+
* @param {string} branch - The branch from which to fetch the file.
|
|
212
|
+
* @param {string} file - The file path to fetch.
|
|
213
|
+
* @param {number} [timeout] - The timeout duration in milliseconds (default is 10000ms).
|
|
214
|
+
* @returns {Promise<Record<string, boolean | string | number>>} A promise that resolves to the parsed JSON object from the file.
|
|
215
|
+
* @throws {Error} If the request fails or the JSON parsing fails.
|
|
216
|
+
*/
|
|
133
217
|
export async function getGitHubUpdate(branch, file, timeout = 10000) {
|
|
134
218
|
const https = await import('node:https');
|
|
135
219
|
return new Promise((resolve, reject) => {
|
|
@@ -143,7 +227,7 @@ export async function getGitHubUpdate(branch, file, timeout = 10000) {
|
|
|
143
227
|
let data = '';
|
|
144
228
|
if (res.statusCode !== 200) {
|
|
145
229
|
clearTimeout(timeoutId);
|
|
146
|
-
res.resume();
|
|
230
|
+
res.resume(); // Discard response data to close the socket properly
|
|
147
231
|
reject(new Error(`Failed to fetch data. Status code: ${res.statusCode}`));
|
|
148
232
|
return;
|
|
149
233
|
}
|
|
@@ -161,12 +245,18 @@ export async function getGitHubUpdate(branch, file, timeout = 10000) {
|
|
|
161
245
|
}
|
|
162
246
|
});
|
|
163
247
|
});
|
|
248
|
+
// istanbul ignore next
|
|
164
249
|
req.on('error', (error) => {
|
|
165
250
|
clearTimeout(timeoutId);
|
|
166
251
|
reject(new Error(`Request failed: ${error instanceof Error ? error.message : error}`));
|
|
167
252
|
});
|
|
168
253
|
});
|
|
169
254
|
}
|
|
255
|
+
/**
|
|
256
|
+
* Retrieves the path to the global Node.js modules directory.
|
|
257
|
+
*
|
|
258
|
+
* @returns {Promise<string>} A promise that resolves to the path of the global Node.js modules directory.
|
|
259
|
+
*/
|
|
170
260
|
export async function getGlobalNodeModules() {
|
|
171
261
|
const { exec } = await import('node:child_process');
|
|
172
262
|
return new Promise((resolve, reject) => {
|
|
@@ -180,6 +270,12 @@ export async function getGlobalNodeModules() {
|
|
|
180
270
|
});
|
|
181
271
|
});
|
|
182
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Function to format bytes to KB, MB, or GB
|
|
275
|
+
*
|
|
276
|
+
* @param {number} bytes - The number of bytes to format
|
|
277
|
+
* @returns {string} The formatted memory usage string
|
|
278
|
+
*/
|
|
183
279
|
export function formatMemoryUsage(bytes) {
|
|
184
280
|
if (bytes >= 1024 ** 3) {
|
|
185
281
|
return `${(bytes / 1024 ** 3).toFixed(2)} GB`;
|
|
@@ -191,6 +287,12 @@ export function formatMemoryUsage(bytes) {
|
|
|
191
287
|
return `${(bytes / 1024).toFixed(2)} KB`;
|
|
192
288
|
}
|
|
193
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* Function to format system uptime with only the most significant unit
|
|
292
|
+
*
|
|
293
|
+
* @param {number} seconds - The number of seconds to format
|
|
294
|
+
* @returns {string} The formatted uptime string
|
|
295
|
+
*/
|
|
194
296
|
export function formatOsUpTime(seconds) {
|
|
195
297
|
if (seconds >= 86400) {
|
|
196
298
|
const days = Math.floor(seconds / 86400);
|
|
@@ -206,3 +308,4 @@ export function formatOsUpTime(seconds) {
|
|
|
206
308
|
}
|
|
207
309
|
return `${seconds} second${seconds !== 1 ? 's' : ''}`;
|
|
208
310
|
}
|
|
311
|
+
//# sourceMappingURL=network.js.map
|