matterbridge 3.0.6 → 3.0.7-dev-20250615-2a6da14
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 +20 -0
- package/README-DEV.md +66 -35
- package/README.md +1 -1
- package/dist/cli.js +2 -62
- package/dist/clusters/export.js +1 -0
- package/dist/defaultConfigSchema.js +0 -23
- package/dist/deviceManager.js +1 -94
- package/dist/devices/export.js +4 -0
- package/dist/evse.js +9 -65
- package/dist/frontend.js +16 -374
- package/dist/globalMatterbridge.js +0 -20
- package/dist/helpers.js +0 -51
- package/dist/index.js +1 -28
- package/dist/laundryWasher.js +7 -92
- package/dist/logger/export.js +0 -1
- package/dist/matter/behaviors.js +0 -2
- package/dist/matter/clusters.js +0 -2
- package/dist/matter/devices.js +0 -2
- package/dist/matter/endpoints.js +0 -2
- package/dist/matter/export.js +0 -2
- package/dist/matter/types.js +0 -2
- package/dist/matterbridge.js +46 -748
- package/dist/matterbridgeAccessoryPlatform.js +0 -34
- package/dist/matterbridgeBehaviors.js +1 -54
- package/dist/matterbridgeDeviceTypes.js +15 -578
- package/dist/matterbridgeDynamicPlatform.js +0 -34
- package/dist/matterbridgeEndpoint.js +43 -997
- package/dist/matterbridgeEndpointHelpers.js +10 -204
- package/dist/matterbridgePlatform.js +7 -225
- package/dist/matterbridgeTypes.js +0 -24
- package/dist/pluginManager.js +3 -269
- package/dist/roboticVacuumCleaner.js +6 -81
- package/dist/shelly.js +7 -155
- package/dist/storage/export.js +0 -1
- package/dist/update.js +0 -53
- package/dist/utils/colorUtils.js +2 -205
- package/dist/utils/commandLine.js +0 -53
- package/dist/utils/copyDirectory.js +1 -37
- package/dist/utils/createDirectory.js +0 -31
- package/dist/utils/createZip.js +2 -42
- package/dist/utils/deepCopy.js +0 -38
- package/dist/utils/deepEqual.js +1 -71
- package/dist/utils/export.js +0 -1
- package/dist/utils/hex.js +0 -57
- package/dist/utils/isvalid.js +0 -100
- package/dist/utils/network.js +5 -76
- package/dist/utils/spawn.js +0 -16
- package/dist/utils/wait.js +9 -58
- package/dist/waterHeater.js +2 -62
- package/npm-shrinkwrap.json +41 -29
- package/package.json +23 -20
- package/dist/cli.d.ts +0 -29
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/cluster/export.d.ts +0 -2
- package/dist/cluster/export.d.ts.map +0 -1
- package/dist/cluster/export.js +0 -3
- package/dist/cluster/export.js.map +0 -1
- package/dist/defaultConfigSchema.d.ts +0 -27
- package/dist/defaultConfigSchema.d.ts.map +0 -1
- package/dist/defaultConfigSchema.js.map +0 -1
- package/dist/deviceManager.d.ts +0 -114
- package/dist/deviceManager.d.ts.map +0 -1
- package/dist/deviceManager.js.map +0 -1
- package/dist/evse.d.ts +0 -67
- package/dist/evse.d.ts.map +0 -1
- package/dist/evse.js.map +0 -1
- package/dist/frontend.d.ts +0 -256
- package/dist/frontend.d.ts.map +0 -1
- package/dist/frontend.js.map +0 -1
- package/dist/globalMatterbridge.d.ts +0 -32
- package/dist/globalMatterbridge.d.ts.map +0 -1
- package/dist/globalMatterbridge.js.map +0 -1
- package/dist/helpers.d.ts +0 -47
- package/dist/helpers.d.ts.map +0 -1
- package/dist/helpers.js.map +0 -1
- package/dist/index.d.ts +0 -37
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/laundryWasher.d.ts +0 -243
- package/dist/laundryWasher.d.ts.map +0 -1
- package/dist/laundryWasher.js.map +0 -1
- package/dist/logger/export.d.ts +0 -2
- package/dist/logger/export.d.ts.map +0 -1
- package/dist/logger/export.js.map +0 -1
- package/dist/matter/behaviors.d.ts +0 -2
- package/dist/matter/behaviors.d.ts.map +0 -1
- package/dist/matter/behaviors.js.map +0 -1
- package/dist/matter/clusters.d.ts +0 -2
- package/dist/matter/clusters.d.ts.map +0 -1
- package/dist/matter/clusters.js.map +0 -1
- package/dist/matter/devices.d.ts +0 -2
- package/dist/matter/devices.d.ts.map +0 -1
- package/dist/matter/devices.js.map +0 -1
- package/dist/matter/endpoints.d.ts +0 -2
- package/dist/matter/endpoints.d.ts.map +0 -1
- package/dist/matter/endpoints.js.map +0 -1
- package/dist/matter/export.d.ts +0 -5
- package/dist/matter/export.d.ts.map +0 -1
- package/dist/matter/export.js.map +0 -1
- package/dist/matter/types.d.ts +0 -3
- package/dist/matter/types.d.ts.map +0 -1
- package/dist/matter/types.js.map +0 -1
- package/dist/matterbridge.d.ts +0 -445
- package/dist/matterbridge.d.ts.map +0 -1
- package/dist/matterbridge.js.map +0 -1
- package/dist/matterbridgeAccessoryPlatform.d.ts +0 -40
- package/dist/matterbridgeAccessoryPlatform.d.ts.map +0 -1
- package/dist/matterbridgeAccessoryPlatform.js.map +0 -1
- package/dist/matterbridgeBehaviors.d.ts +0 -1333
- package/dist/matterbridgeBehaviors.d.ts.map +0 -1
- package/dist/matterbridgeBehaviors.js.map +0 -1
- package/dist/matterbridgeDeviceTypes.d.ts +0 -644
- package/dist/matterbridgeDeviceTypes.d.ts.map +0 -1
- package/dist/matterbridgeDeviceTypes.js.map +0 -1
- package/dist/matterbridgeDynamicPlatform.d.ts +0 -40
- package/dist/matterbridgeDynamicPlatform.d.ts.map +0 -1
- package/dist/matterbridgeDynamicPlatform.js.map +0 -1
- package/dist/matterbridgeEndpoint.d.ts +0 -1144
- package/dist/matterbridgeEndpoint.d.ts.map +0 -1
- package/dist/matterbridgeEndpoint.js.map +0 -1
- package/dist/matterbridgeEndpointHelpers.d.ts +0 -3083
- package/dist/matterbridgeEndpointHelpers.d.ts.map +0 -1
- package/dist/matterbridgeEndpointHelpers.js.map +0 -1
- package/dist/matterbridgePlatform.d.ts +0 -294
- package/dist/matterbridgePlatform.d.ts.map +0 -1
- package/dist/matterbridgePlatform.js.map +0 -1
- package/dist/matterbridgeTypes.d.ts +0 -196
- package/dist/matterbridgeTypes.d.ts.map +0 -1
- package/dist/matterbridgeTypes.js.map +0 -1
- package/dist/pluginManager.d.ts +0 -273
- package/dist/pluginManager.d.ts.map +0 -1
- package/dist/pluginManager.js.map +0 -1
- package/dist/roboticVacuumCleaner.d.ts +0 -102
- package/dist/roboticVacuumCleaner.d.ts.map +0 -1
- package/dist/roboticVacuumCleaner.js.map +0 -1
- package/dist/shelly.d.ts +0 -161
- package/dist/shelly.d.ts.map +0 -1
- package/dist/shelly.js.map +0 -1
- package/dist/storage/export.d.ts +0 -2
- package/dist/storage/export.d.ts.map +0 -1
- package/dist/storage/export.js.map +0 -1
- package/dist/update.d.ts +0 -58
- package/dist/update.d.ts.map +0 -1
- package/dist/update.js.map +0 -1
- package/dist/utils/colorUtils.d.ts +0 -61
- package/dist/utils/colorUtils.d.ts.map +0 -1
- package/dist/utils/colorUtils.js.map +0 -1
- package/dist/utils/commandLine.d.ts +0 -58
- package/dist/utils/commandLine.d.ts.map +0 -1
- package/dist/utils/commandLine.js.map +0 -1
- package/dist/utils/copyDirectory.d.ts +0 -32
- package/dist/utils/copyDirectory.d.ts.map +0 -1
- package/dist/utils/copyDirectory.js.map +0 -1
- package/dist/utils/createDirectory.d.ts +0 -32
- package/dist/utils/createDirectory.d.ts.map +0 -1
- package/dist/utils/createDirectory.js.map +0 -1
- package/dist/utils/createZip.d.ts +0 -38
- package/dist/utils/createZip.d.ts.map +0 -1
- package/dist/utils/createZip.js.map +0 -1
- package/dist/utils/deepCopy.d.ts +0 -31
- package/dist/utils/deepCopy.d.ts.map +0 -1
- package/dist/utils/deepCopy.js.map +0 -1
- package/dist/utils/deepEqual.d.ts +0 -53
- package/dist/utils/deepEqual.d.ts.map +0 -1
- package/dist/utils/deepEqual.js.map +0 -1
- package/dist/utils/export.d.ts +0 -12
- package/dist/utils/export.d.ts.map +0 -1
- package/dist/utils/export.js.map +0 -1
- package/dist/utils/hex.d.ts +0 -48
- package/dist/utils/hex.d.ts.map +0 -1
- package/dist/utils/hex.js.map +0 -1
- package/dist/utils/isvalid.d.ts +0 -102
- package/dist/utils/isvalid.d.ts.map +0 -1
- package/dist/utils/isvalid.js.map +0 -1
- package/dist/utils/network.d.ts +0 -69
- package/dist/utils/network.d.ts.map +0 -1
- package/dist/utils/network.js.map +0 -1
- package/dist/utils/spawn.d.ts +0 -12
- package/dist/utils/spawn.d.ts.map +0 -1
- package/dist/utils/spawn.js.map +0 -1
- package/dist/utils/wait.d.ts +0 -52
- package/dist/utils/wait.d.ts.map +0 -1
- package/dist/utils/wait.js.map +0 -1
- package/dist/waterHeater.d.ts +0 -90
- package/dist/waterHeater.d.ts.map +0 -1
- package/dist/waterHeater.js.map +0 -1
package/dist/utils/deepEqual.js
CHANGED
|
@@ -1,88 +1,23 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
/**
|
|
3
|
-
* This file contains the deepEqual function.
|
|
4
|
-
*
|
|
5
|
-
* @file deepEqual.ts
|
|
6
|
-
* @author Luca Liguori
|
|
7
|
-
* @date 2025-02-16
|
|
8
|
-
* @version 1.0.0
|
|
9
|
-
*
|
|
10
|
-
* Copyright 2025, 2026, 2027 Luca Liguori.
|
|
11
|
-
*
|
|
12
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
13
|
-
* you may not use this file except in compliance with the License.
|
|
14
|
-
* You may obtain a copy of the License at
|
|
15
|
-
*
|
|
16
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
17
|
-
*
|
|
18
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
19
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
20
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
-
* See the License for the specific language governing permissions and
|
|
22
|
-
* limitations under the License. *
|
|
23
|
-
*/
|
|
24
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
25
|
-
/**
|
|
26
|
-
* Performs a deep comparison between two values to determine if they are equivalent.
|
|
27
|
-
* This comparison includes primitive types, arrays, and objects, allowing for optional
|
|
28
|
-
* exclusion of specific properties from the comparison in objects.
|
|
29
|
-
*
|
|
30
|
-
* @param {any} a The first value to compare.
|
|
31
|
-
* @param {any} b The second value to compare.
|
|
32
|
-
* @param {string[]} [excludeProperties=[]] An array of property names to exclude from the comparison in objects.
|
|
33
|
-
* @returns {boolean} True if the values are deeply equal, excluding any specified properties; otherwise, false.
|
|
34
|
-
*
|
|
35
|
-
* Note: This function utilizes recursion for deep comparison of nested structures and includes a debugging
|
|
36
|
-
* mechanism that can be toggled on or off for detailed comparison logging. It is important to ensure that
|
|
37
|
-
* objects do not contain circular references when enabling debug logging to avoid infinite loops.
|
|
38
|
-
*
|
|
39
|
-
* Example usage:
|
|
40
|
-
* ```
|
|
41
|
-
* const obj1 = { a: 1, b: { c: 2 } };
|
|
42
|
-
* const obj2 = { a: 1, b: { c: 2 } };
|
|
43
|
-
* console.log(deepEqual(obj1, obj2)); // true
|
|
44
|
-
*
|
|
45
|
-
* const arr1 = [1, 2, [3, 4]];
|
|
46
|
-
* const arr2 = [1, 2, [3, 4]];
|
|
47
|
-
* console.log(deepEqual(arr1, arr2)); // true
|
|
48
|
-
*
|
|
49
|
-
* const obj3 = { a: 1, b: { c: 2, d: 3 } };
|
|
50
|
-
* const obj4 = { a: 1, b: { c: 2 } };
|
|
51
|
-
* console.log(deepEqual(obj3, obj4, ['d'])); // true, excluding property 'd' from comparison
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
1
|
export function deepEqual(a, b, excludeProperties = []) {
|
|
55
|
-
// Toggle debugging on or off easily
|
|
56
|
-
// const debug = false;
|
|
57
|
-
// Helper function for conditional logging
|
|
58
2
|
const debugLog = (...messages) => {
|
|
59
|
-
// if (debug) {
|
|
60
|
-
// console.log(...messages);
|
|
61
|
-
// }
|
|
62
3
|
};
|
|
63
|
-
// If both are the same instance, return true (handles primitives and same object references)
|
|
64
4
|
if (a === b) {
|
|
65
5
|
return true;
|
|
66
6
|
}
|
|
67
|
-
// If types are different, return false
|
|
68
7
|
if (typeof a !== typeof b) {
|
|
69
8
|
debugLog(`deepEqual false for typeof a: ${typeof a} typeof b: ${typeof b}`);
|
|
70
9
|
return false;
|
|
71
10
|
}
|
|
72
|
-
// If one of them is null (and we know they are not equal from the first check), return false
|
|
73
11
|
if (a == null || b == null) {
|
|
74
12
|
debugLog('deepEqual false for == null');
|
|
75
13
|
return false;
|
|
76
14
|
}
|
|
77
|
-
// Handle Dates compare by timestamp
|
|
78
15
|
if (a instanceof Date && b instanceof Date) {
|
|
79
16
|
return a.getTime() === b.getTime();
|
|
80
17
|
}
|
|
81
|
-
// Handle Regexps compare by their source & flags
|
|
82
18
|
if (a instanceof RegExp && b instanceof RegExp) {
|
|
83
19
|
return a.source === b.source && a.flags === b.flags;
|
|
84
20
|
}
|
|
85
|
-
// Handle Arrays
|
|
86
21
|
if (Array.isArray(a) && Array.isArray(b)) {
|
|
87
22
|
if (a.length !== b.length) {
|
|
88
23
|
debugLog(`deepEqual false for array a.length(${a.length}) !== b.length(${b.length})`);
|
|
@@ -98,31 +33,26 @@ export function deepEqual(a, b, excludeProperties = []) {
|
|
|
98
33
|
}
|
|
99
34
|
return true;
|
|
100
35
|
}
|
|
101
|
-
// Handle Objects (and exclude null, functions, and arrays)
|
|
102
36
|
if (typeof a === 'object' && typeof b === 'object') {
|
|
103
37
|
const aProps = Object.getOwnPropertyNames(a).filter((prop) => !excludeProperties.includes(prop));
|
|
104
38
|
const bProps = Object.getOwnPropertyNames(b).filter((prop) => !excludeProperties.includes(prop));
|
|
105
|
-
// If their property lengths are different, they're different objects
|
|
106
39
|
if (aProps.length !== bProps.length) {
|
|
107
40
|
debugLog(`deepEqual false for aProps.length(${aProps.length}) !== bProps.length(${bProps.length})`);
|
|
108
41
|
debugLog(`- aProps.length(${aProps.length}):`, aProps);
|
|
109
42
|
debugLog(`- bProps.length(${bProps.length}):`, bProps);
|
|
110
43
|
return false;
|
|
111
44
|
}
|
|
112
|
-
// Check each property in 'a' to see if it's in 'b' and if it's equal (deep check)
|
|
113
45
|
for (const prop of aProps) {
|
|
114
46
|
if (!Object.prototype.hasOwnProperty.call(b, prop)) {
|
|
115
47
|
debugLog(`deepEqual false for !b.hasOwnProperty(${prop})`);
|
|
116
48
|
return false;
|
|
117
49
|
}
|
|
118
50
|
if (!deepEqual(a[prop], b[prop], excludeProperties)) {
|
|
119
|
-
debugLog(`deepEqual false for !deepEqual(a[${prop}], b[${prop}])`
|
|
51
|
+
debugLog(`deepEqual false for !deepEqual(a[${prop}], b[${prop}])`);
|
|
120
52
|
return false;
|
|
121
53
|
}
|
|
122
54
|
}
|
|
123
55
|
return true;
|
|
124
56
|
}
|
|
125
|
-
// If none of the above, the objects are not equal
|
|
126
57
|
return false;
|
|
127
58
|
}
|
|
128
|
-
//# sourceMappingURL=deepEqual.js.map
|
package/dist/utils/export.js
CHANGED
package/dist/utils/hex.js
CHANGED
|
@@ -1,84 +1,27 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file contains the hex functions.
|
|
3
|
-
*
|
|
4
|
-
* @file hex.ts
|
|
5
|
-
* @author Luca Liguori
|
|
6
|
-
* @date 2025-05-06
|
|
7
|
-
* @version 1.0.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
|
-
*/
|
|
23
|
-
/**
|
|
24
|
-
* Converts an ArrayBuffer or Uint8Array to a hexadecimal string.
|
|
25
|
-
*
|
|
26
|
-
* Accepts any {ArrayBufferLike} input: a raw ArrayBuffer (binary data storage) or
|
|
27
|
-
* a Uint8Array (TypedArray view over an ArrayBuffer). While an ArrayBuffer holds
|
|
28
|
-
* the raw memory, a Uint8Array is a typed view into that memory, letting you access
|
|
29
|
-
* its bytes directly. This function normalizes both by creating a Uint8Array view
|
|
30
|
-
* before conversion.
|
|
31
|
-
*
|
|
32
|
-
* @param {ArrayBufferLike} buffer - The buffer or typed-array view to convert.
|
|
33
|
-
* @returns {string} A lowercase hex string representation of the buffer's bytes.
|
|
34
|
-
*
|
|
35
|
-
* @throws {TypeError} If the input is not an ArrayBuffer or ArrayBufferView.
|
|
36
|
-
*/
|
|
37
1
|
export function bufferToHex(buffer) {
|
|
38
|
-
// Check if the input is an ArrayBuffer or ArrayBufferView
|
|
39
2
|
if (!(buffer instanceof ArrayBuffer || ArrayBuffer.isView(buffer))) {
|
|
40
3
|
throw new TypeError('Expected input to be an ArrayBuffer or ArrayBufferView');
|
|
41
4
|
}
|
|
42
|
-
// Create a Uint8Array view over the buffer
|
|
43
5
|
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
44
|
-
// Convert each byte to 2-digit hex
|
|
45
6
|
return Array.from(bytes)
|
|
46
7
|
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
47
8
|
.join('');
|
|
48
9
|
}
|
|
49
|
-
/**
|
|
50
|
-
* Converts a hexadecimal string to a Uint8Array.
|
|
51
|
-
*
|
|
52
|
-
* @param {string} hex - The hex string to convert. Can include uppercase or lowercase characters.
|
|
53
|
-
* @returns {Uint8Array} A Uint8Array representing the corresponding binary data.
|
|
54
|
-
*
|
|
55
|
-
* @throws {TypeError} If the input is not a string.
|
|
56
|
-
* @throws {Error} If the input length is odd or contains non-hex characters.
|
|
57
|
-
*/
|
|
58
10
|
export function hexToBuffer(hex) {
|
|
59
|
-
// Ensure the input is a string
|
|
60
11
|
if (typeof hex !== 'string') {
|
|
61
12
|
throw new TypeError('Expected a string for hex input');
|
|
62
13
|
}
|
|
63
|
-
// Trim any whitespace from the input string
|
|
64
14
|
const cleaned = hex.trim();
|
|
65
|
-
// Check if the length of the hex string is even
|
|
66
15
|
if (cleaned.length % 2 !== 0) {
|
|
67
16
|
throw new Error('Invalid hex string length, must be even');
|
|
68
17
|
}
|
|
69
|
-
// Validate that the string contains only valid hex characters
|
|
70
18
|
if (!/^[0-9a-fA-F]*$/.test(cleaned)) {
|
|
71
19
|
throw new Error('Invalid hex string, contains non-hex characters');
|
|
72
20
|
}
|
|
73
|
-
// Calculate the length of the resulting Uint8Array
|
|
74
21
|
const length = cleaned.length / 2;
|
|
75
|
-
// Create a Uint8Array to hold the binary data
|
|
76
22
|
const result = new Uint8Array(length);
|
|
77
|
-
// Convert each pair of hex characters into a byte
|
|
78
23
|
for (let i = 0; i < length; i++) {
|
|
79
24
|
result[i] = parseInt(cleaned.substr(i * 2, 2), 16);
|
|
80
25
|
}
|
|
81
|
-
// Return the resulting Uint8Array
|
|
82
26
|
return result;
|
|
83
27
|
}
|
|
84
|
-
//# sourceMappingURL=hex.js.map
|
package/dist/utils/isvalid.js
CHANGED
|
@@ -1,44 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file contains the validation functions.
|
|
3
|
-
*
|
|
4
|
-
* @file isvalid.ts
|
|
5
|
-
* @author Luca Liguori
|
|
6
|
-
* @date 2025-02-16
|
|
7
|
-
* @version 1.0.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
|
-
*/
|
|
23
|
-
/**
|
|
24
|
-
* Checks if a given string is a valid IPv4 address.
|
|
25
|
-
*
|
|
26
|
-
* @param {string} ipv4Address - The string to be checked.
|
|
27
|
-
* @returns {boolean} - Returns true if the string is a valid IPv4 address, otherwise returns false.
|
|
28
|
-
*/
|
|
29
1
|
export function isValidIpv4Address(ipv4Address) {
|
|
30
2
|
const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
31
3
|
return ipv4Regex.test(ipv4Address);
|
|
32
4
|
}
|
|
33
|
-
/**
|
|
34
|
-
* Checks if a value is a valid number within the specified range.
|
|
35
|
-
*
|
|
36
|
-
* @param {any} value - The value to be checked.
|
|
37
|
-
* @param {number} min - The minimum value allowed (optional).
|
|
38
|
-
* @param {number} max - The maximum value allowed (optional).
|
|
39
|
-
* @returns {value is number} Returns true if the value is a valid number within the specified range, otherwise false.
|
|
40
|
-
*/
|
|
41
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
42
5
|
export function isValidNumber(value, min, max) {
|
|
43
6
|
if (value === undefined || value === null || typeof value !== 'number' || Number.isNaN(value) || !Number.isFinite(value))
|
|
44
7
|
return false;
|
|
@@ -48,25 +11,9 @@ export function isValidNumber(value, min, max) {
|
|
|
48
11
|
return false;
|
|
49
12
|
return true;
|
|
50
13
|
}
|
|
51
|
-
/**
|
|
52
|
-
* Checks if a value is a valid boolean.
|
|
53
|
-
*
|
|
54
|
-
* @param {any} value - The value to be checked.
|
|
55
|
-
* @returns {value is boolean} `true` if the value is a valid boolean, `false` otherwise.
|
|
56
|
-
*/
|
|
57
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
14
|
export function isValidBoolean(value) {
|
|
59
15
|
return value !== undefined && value !== null && typeof value === 'boolean';
|
|
60
16
|
}
|
|
61
|
-
/**
|
|
62
|
-
* Checks if a value is a valid string.
|
|
63
|
-
*
|
|
64
|
-
* @param {any} value - The value to be checked.
|
|
65
|
-
* @param {number} minLength - The min string length (optional).
|
|
66
|
-
* @param {number} maxLength - The max string length (optional).
|
|
67
|
-
* @returns {value is string} A boolean indicating whether the value is a valid string.
|
|
68
|
-
*/
|
|
69
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
70
17
|
export function isValidString(value, minLength, maxLength) {
|
|
71
18
|
if (value === undefined || value === null || typeof value !== 'string')
|
|
72
19
|
return false;
|
|
@@ -76,25 +23,9 @@ export function isValidString(value, minLength, maxLength) {
|
|
|
76
23
|
return false;
|
|
77
24
|
return true;
|
|
78
25
|
}
|
|
79
|
-
/**
|
|
80
|
-
* Checks if a value is a valid RegExp.
|
|
81
|
-
*
|
|
82
|
-
* @param {any} value - The value to be checked.
|
|
83
|
-
* @returns {value is RegExp} A boolean indicating whether the value is a valid RegExp.
|
|
84
|
-
*/
|
|
85
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
86
26
|
export function isValidRegExp(value) {
|
|
87
27
|
return value !== undefined && value !== null && value instanceof RegExp;
|
|
88
28
|
}
|
|
89
|
-
/**
|
|
90
|
-
* Checks if a value is a valid object.
|
|
91
|
-
*
|
|
92
|
-
* @param {any} value - The value to be checked.
|
|
93
|
-
* @param {number} minLength - The min number of keys (optional).
|
|
94
|
-
* @param {number} maxLength - The max number of keys (optional).
|
|
95
|
-
* @returns {value is object} A boolean indicating whether the value is a valid object.
|
|
96
|
-
*/
|
|
97
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
98
29
|
export function isValidObject(value, minLength, maxLength) {
|
|
99
30
|
if (value === undefined || value === null || typeof value !== 'object' || Array.isArray(value))
|
|
100
31
|
return false;
|
|
@@ -105,15 +36,6 @@ export function isValidObject(value, minLength, maxLength) {
|
|
|
105
36
|
return false;
|
|
106
37
|
return true;
|
|
107
38
|
}
|
|
108
|
-
/**
|
|
109
|
-
* Checks if a value is a valid array.
|
|
110
|
-
*
|
|
111
|
-
* @param {any} value - The value to be checked.
|
|
112
|
-
* @param {number} minLength - The min number of elements (optional).
|
|
113
|
-
* @param {number} maxLength - The max number of elements (optional).
|
|
114
|
-
* @returns {value is unknown[]} A boolean indicating whether the value is a valid array.
|
|
115
|
-
*/
|
|
116
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
117
39
|
export function isValidArray(value, minLength, maxLength) {
|
|
118
40
|
if (value === undefined || value === null || !Array.isArray(value))
|
|
119
41
|
return false;
|
|
@@ -123,33 +45,12 @@ export function isValidArray(value, minLength, maxLength) {
|
|
|
123
45
|
return false;
|
|
124
46
|
return true;
|
|
125
47
|
}
|
|
126
|
-
/**
|
|
127
|
-
* Checks if the given value is null.
|
|
128
|
-
*
|
|
129
|
-
* @param {any} value - The value to check.
|
|
130
|
-
* @returns {value is null} `true` if the value is null, `false` otherwise.
|
|
131
|
-
*/
|
|
132
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
133
48
|
export function isValidNull(value) {
|
|
134
49
|
return value === null;
|
|
135
50
|
}
|
|
136
|
-
/**
|
|
137
|
-
* Checks if a value is undefined.
|
|
138
|
-
*
|
|
139
|
-
* @param {any} value - The value to check.
|
|
140
|
-
* @returns {value is undefined} `true` if the value is undefined, `false` otherwise.
|
|
141
|
-
*/
|
|
142
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
143
51
|
export function isValidUndefined(value) {
|
|
144
52
|
return value === undefined;
|
|
145
53
|
}
|
|
146
|
-
/**
|
|
147
|
-
* Converts a semantic version string like "6.11.0-1011-raspi" to a numeric version code like 61100.
|
|
148
|
-
* Format: major * 10000 + minor * 100 + patch
|
|
149
|
-
*
|
|
150
|
-
* @param {string} versionString The version string to parse
|
|
151
|
-
* @returns {number | undefined} A numeric version code or undefined if parsing fails
|
|
152
|
-
*/
|
|
153
54
|
export function parseVersionString(versionString) {
|
|
154
55
|
if (!isValidString(versionString))
|
|
155
56
|
return undefined;
|
|
@@ -166,4 +67,3 @@ export function parseVersionString(versionString) {
|
|
|
166
67
|
}
|
|
167
68
|
return major * 10000 + minor * 100 + patch;
|
|
168
69
|
}
|
|
169
|
-
//# sourceMappingURL=isvalid.js.map
|
package/dist/utils/network.js
CHANGED
|
@@ -1,37 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file contains the network functions.
|
|
3
|
-
*
|
|
4
|
-
* @file network.ts
|
|
5
|
-
* @author Luca Liguori
|
|
6
|
-
* @date 2024-02-17
|
|
7
|
-
* @version 1.0.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
|
|
24
1
|
import os from 'node:os';
|
|
25
|
-
// AnsiLogger module
|
|
26
2
|
import { AnsiLogger, idn, rs } from '../logger/export.js';
|
|
27
|
-
/**
|
|
28
|
-
* Retrieves the IPv4 address of the first non-internal network interface.
|
|
29
|
-
* @returns {string | undefined} The IPv4 address of the selected network interface, or undefined if not found.
|
|
30
|
-
*/
|
|
31
3
|
export function getIpv4InterfaceAddress() {
|
|
32
4
|
let ipv4Address;
|
|
33
5
|
const networkInterfaces = os.networkInterfaces();
|
|
34
|
-
// console.log('Available Network Interfaces:', networkInterfaces);
|
|
35
6
|
for (const interfaceDetails of Object.values(networkInterfaces)) {
|
|
36
7
|
if (!interfaceDetails) {
|
|
37
8
|
break;
|
|
@@ -45,17 +16,11 @@ export function getIpv4InterfaceAddress() {
|
|
|
45
16
|
break;
|
|
46
17
|
}
|
|
47
18
|
}
|
|
48
|
-
// console.log('Selected Network Interfaces:', ipv4Address);
|
|
49
19
|
return ipv4Address;
|
|
50
20
|
}
|
|
51
|
-
/**
|
|
52
|
-
* Retrieves the IPv6 address of the first non-internal network interface.
|
|
53
|
-
* @returns {string | undefined} The IPv4 address of the selected network interface, or undefined if not found.
|
|
54
|
-
*/
|
|
55
21
|
export function getIpv6InterfaceAddress() {
|
|
56
22
|
let ipv6Address;
|
|
57
23
|
const networkInterfaces = os.networkInterfaces();
|
|
58
|
-
// console.log('Available Network Interfaces:', networkInterfaces);
|
|
59
24
|
for (const interfaceDetails of Object.values(networkInterfaces)) {
|
|
60
25
|
if (!interfaceDetails) {
|
|
61
26
|
break;
|
|
@@ -69,17 +34,11 @@ export function getIpv6InterfaceAddress() {
|
|
|
69
34
|
break;
|
|
70
35
|
}
|
|
71
36
|
}
|
|
72
|
-
// console.log('Selected Network Interfaces:', ipv6Address);
|
|
73
37
|
return ipv6Address;
|
|
74
38
|
}
|
|
75
|
-
/**
|
|
76
|
-
* Retrieves the mac address of the first non-internal network interface.
|
|
77
|
-
* @returns {string | undefined} The mac address, or undefined if not found.
|
|
78
|
-
*/
|
|
79
39
|
export function getMacAddress() {
|
|
80
40
|
let macAddress;
|
|
81
41
|
const networkInterfaces = os.networkInterfaces();
|
|
82
|
-
// console.log('Available Network Interfaces:', networkInterfaces);
|
|
83
42
|
for (const interfaceDetails of Object.values(networkInterfaces)) {
|
|
84
43
|
if (!interfaceDetails) {
|
|
85
44
|
break;
|
|
@@ -95,13 +54,9 @@ export function getMacAddress() {
|
|
|
95
54
|
}
|
|
96
55
|
return macAddress;
|
|
97
56
|
}
|
|
98
|
-
/**
|
|
99
|
-
* Logs the available network interfaces and their details.
|
|
100
|
-
* @param {boolean} log - Whether to enable logging of network interface details (default is true).
|
|
101
|
-
*/
|
|
102
57
|
export function logInterfaces(debug = true) {
|
|
103
|
-
const log = new AnsiLogger({ logName: 'MatterbridgeUtils', logTimestampFormat: 4
|
|
104
|
-
log.logLevel = "info"
|
|
58
|
+
const log = new AnsiLogger({ logName: 'MatterbridgeUtils', logTimestampFormat: 4, logLevel: "info" });
|
|
59
|
+
log.logLevel = "info";
|
|
105
60
|
log.logName = 'LogInterfaces';
|
|
106
61
|
const networkInterfaces = os.networkInterfaces();
|
|
107
62
|
if (debug)
|
|
@@ -117,36 +72,16 @@ export function logInterfaces(debug = true) {
|
|
|
117
72
|
}
|
|
118
73
|
}
|
|
119
74
|
}
|
|
120
|
-
/**
|
|
121
|
-
* Resolves the given hostname to an IP address.
|
|
122
|
-
*
|
|
123
|
-
* @param {string} hostname - The hostname to resolve.
|
|
124
|
-
* @param {0 | 4 | 6} [family=4] - The address family to use (0 for any, 4 for IPv4, 6 for IPv6). Default is 4.
|
|
125
|
-
* @returns {Promise<string | null>} - A promise that resolves to the IP address or null if not found.
|
|
126
|
-
*
|
|
127
|
-
* @remarks
|
|
128
|
-
* This function uses DNS lookup to resolve the hostname, which can take some time to complete.
|
|
129
|
-
*/
|
|
130
75
|
export async function resolveHostname(hostname, family = 4) {
|
|
131
76
|
const dns = await import('node:dns');
|
|
132
77
|
try {
|
|
133
|
-
const addresses = await dns.promises.lookup(hostname.toLowerCase()
|
|
78
|
+
const addresses = await dns.promises.lookup(hostname.toLowerCase(), { family });
|
|
134
79
|
return addresses.address;
|
|
135
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
136
80
|
}
|
|
137
81
|
catch (error) {
|
|
138
82
|
return null;
|
|
139
83
|
}
|
|
140
84
|
}
|
|
141
|
-
/**
|
|
142
|
-
* Retrieves the version of an npm package from the npm registry.
|
|
143
|
-
*
|
|
144
|
-
* @param {string} packageName - The name of the npm package.
|
|
145
|
-
* @param {string} [tag='latest'] - The tag of the package version to retrieve (default is 'latest').
|
|
146
|
-
* @param {number} [timeout=10000] - The timeout duration in milliseconds (default is 10000ms).
|
|
147
|
-
* @returns {Promise<string>} A promise that resolves to the version string of the package.
|
|
148
|
-
* @throws {Error} If the request fails or the tag is not found.
|
|
149
|
-
*/
|
|
150
85
|
export async function getNpmPackageVersion(packageName, tag = 'latest', timeout = 10000) {
|
|
151
86
|
const https = await import('node:https');
|
|
152
87
|
return new Promise((resolve, reject) => {
|
|
@@ -160,8 +95,8 @@ export async function getNpmPackageVersion(packageName, tag = 'latest', timeout
|
|
|
160
95
|
let data = '';
|
|
161
96
|
if (res.statusCode !== 200) {
|
|
162
97
|
clearTimeout(timeoutId);
|
|
163
|
-
res.resume();
|
|
164
|
-
req.destroy();
|
|
98
|
+
res.resume();
|
|
99
|
+
req.destroy();
|
|
165
100
|
reject(new Error(`Failed to fetch data. Status code: ${res.statusCode}`));
|
|
166
101
|
return;
|
|
167
102
|
}
|
|
@@ -172,7 +107,6 @@ export async function getNpmPackageVersion(packageName, tag = 'latest', timeout
|
|
|
172
107
|
clearTimeout(timeoutId);
|
|
173
108
|
try {
|
|
174
109
|
const jsonData = JSON.parse(data);
|
|
175
|
-
// console.log(`Package ${packageName} tag ${tag}`, jsonData);
|
|
176
110
|
const version = jsonData['dist-tags']?.[tag];
|
|
177
111
|
if (version) {
|
|
178
112
|
resolve(version);
|
|
@@ -192,10 +126,6 @@ export async function getNpmPackageVersion(packageName, tag = 'latest', timeout
|
|
|
192
126
|
});
|
|
193
127
|
});
|
|
194
128
|
}
|
|
195
|
-
/**
|
|
196
|
-
* Retrieves the path to the global Node.js modules directory.
|
|
197
|
-
* @returns A promise that resolves to the path of the global Node.js modules directory.
|
|
198
|
-
*/
|
|
199
129
|
export async function getGlobalNodeModules() {
|
|
200
130
|
const { exec } = await import('node:child_process');
|
|
201
131
|
return new Promise((resolve, reject) => {
|
|
@@ -209,4 +139,3 @@ export async function getGlobalNodeModules() {
|
|
|
209
139
|
});
|
|
210
140
|
});
|
|
211
141
|
}
|
|
212
|
-
//# sourceMappingURL=network.js.map
|
package/dist/utils/spawn.js
CHANGED
|
@@ -1,28 +1,13 @@
|
|
|
1
1
|
import { hasParameter } from './commandLine.js';
|
|
2
2
|
export default {
|
|
3
|
-
/**
|
|
4
|
-
* Spawns a child process with the given command and arguments.
|
|
5
|
-
* @param {string} command - The command to execute.
|
|
6
|
-
* @param {string[]} args - The arguments to pass to the command (default: []).
|
|
7
|
-
* @returns {Promise<boolean>} A promise that resolves when the child process exits successfully, or rejects if there is an error.
|
|
8
|
-
*/
|
|
9
3
|
async spawnCommand(matterbridge, command, args) {
|
|
10
4
|
const { spawn } = await import('node:child_process');
|
|
11
|
-
/*
|
|
12
|
-
npm > npm.cmd on windows
|
|
13
|
-
cmd.exe ['dir'] on windows
|
|
14
|
-
await this.spawnCommand('npm', ['install', '-g', 'matterbridge']);
|
|
15
|
-
*/
|
|
16
5
|
const cmdLine = command + ' ' + args.join(' ');
|
|
17
6
|
if (process.platform === 'win32' && command === 'npm') {
|
|
18
|
-
// Must be spawn('cmd.exe', ['/c', 'npm -g install <package>']);
|
|
19
7
|
const argstring = 'npm ' + args.join(' ');
|
|
20
8
|
args.splice(0, args.length, '/c', argstring);
|
|
21
9
|
command = 'cmd.exe';
|
|
22
10
|
}
|
|
23
|
-
// Decide when using sudo on linux and macOS
|
|
24
|
-
// When you need sudo: Spawn stderr: npm error Error: EACCES: permission denied
|
|
25
|
-
// When you don't need sudo: Failed to start child process "npm install -g matterbridge-eve-door": spawn sudo ENOENT
|
|
26
11
|
if (hasParameter('sudo') || (process.platform !== 'win32' && command === 'npm' && !hasParameter('docker') && !hasParameter('nosudo'))) {
|
|
27
12
|
args.unshift(command);
|
|
28
13
|
command = 'sudo';
|
|
@@ -81,4 +66,3 @@ export default {
|
|
|
81
66
|
});
|
|
82
67
|
},
|
|
83
68
|
};
|
|
84
|
-
//# sourceMappingURL=spawn.js.map
|
package/dist/utils/wait.js
CHANGED
|
@@ -1,42 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file contains the wait, waiter and withTimeout functions.
|
|
3
|
-
*
|
|
4
|
-
* @file wait.ts
|
|
5
|
-
* @author Luca Liguori
|
|
6
|
-
* @date 2025-02-16
|
|
7
|
-
* @version 1.0.1
|
|
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
|
-
*/
|
|
23
|
-
// AnsiLogger module
|
|
24
1
|
import { AnsiLogger } from '../logger/export.js';
|
|
25
|
-
export const log = new AnsiLogger({ logName: 'MatterbridgeUtils', logTimestampFormat: 4
|
|
26
|
-
/**
|
|
27
|
-
* Asynchronous waiter function that resolves when the provided condition is met or rejects on timeout.
|
|
28
|
-
* @param {string} name - The name of the waiter.
|
|
29
|
-
* @param {() => boolean} check - A function that checks the condition. Should return a boolean.
|
|
30
|
-
* @param {boolean} [exitWithReject=false] - Optional. If true, the promise will be rejected on timeout. Default is false.
|
|
31
|
-
* @param {number} [resolveTimeout=5000] - Optional. The timeout duration in milliseconds. Default is 5000ms.
|
|
32
|
-
* @param {number} [resolveInterval=500] - Optional. The interval duration in milliseconds between condition checks. Default is 500ms.
|
|
33
|
-
* @param {boolean} [debug=false] - Optional. If true, debug messages will be logged to the console. Default is false.
|
|
34
|
-
* @returns {Promise<boolean>} A promise that resolves to true when the condition is met, or false if the timeout occurs.
|
|
35
|
-
*/
|
|
2
|
+
export const log = new AnsiLogger({ logName: 'MatterbridgeUtils', logTimestampFormat: 4, logLevel: "info" });
|
|
36
3
|
export async function waiter(name, check, exitWithReject = false, resolveTimeout = 5000, resolveInterval = 500, debug = false) {
|
|
37
4
|
if (check())
|
|
38
5
|
return true;
|
|
39
|
-
log.logLevel = "debug"
|
|
6
|
+
log.logLevel = "debug";
|
|
40
7
|
log.logName = 'Waiter';
|
|
41
8
|
if (debug)
|
|
42
9
|
log.debug(`Waiter "${name}" started...`);
|
|
@@ -63,19 +30,11 @@ export async function waiter(name, check, exitWithReject = false, resolveTimeout
|
|
|
63
30
|
}, resolveInterval);
|
|
64
31
|
});
|
|
65
32
|
}
|
|
66
|
-
/**
|
|
67
|
-
* Asynchronously waits for a specified amount of time.
|
|
68
|
-
* @param {number} timeout - The duration to wait in milliseconds. Default is 1000ms.
|
|
69
|
-
* @param {string} name - The name of the wait operation. Default is undefined.
|
|
70
|
-
* @param {boolean} debug - Whether to enable debug logging. Default is false.
|
|
71
|
-
* @returns {Promise<void>} A Promise that resolves after the specified timeout.
|
|
72
|
-
*/
|
|
73
33
|
export async function wait(timeout = 1000, name, debug = false) {
|
|
74
|
-
log.logLevel = "debug"
|
|
34
|
+
log.logLevel = "debug";
|
|
75
35
|
log.logName = 'Wait';
|
|
76
36
|
if (debug)
|
|
77
37
|
log.debug(`Wait "${name}" started...`);
|
|
78
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
79
38
|
return new Promise((resolve, reject) => {
|
|
80
39
|
const timeoutId = setTimeout(() => {
|
|
81
40
|
if (debug)
|
|
@@ -85,13 +44,6 @@ export async function wait(timeout = 1000, name, debug = false) {
|
|
|
85
44
|
}, timeout);
|
|
86
45
|
});
|
|
87
46
|
}
|
|
88
|
-
/**
|
|
89
|
-
* Wraps a promise with a timeout. If the promise does not resolve or reject within the specified time, it will be rejected.
|
|
90
|
-
* @param {Promise<T>} promise - The promise to wrap.
|
|
91
|
-
* @param {number} [timeoutMillisecs=10000] - The timeout duration in milliseconds. Default is 10000ms.
|
|
92
|
-
* @param {boolean} [reThrow=true] - Optional. If true, the promise will rethrow the original promise and will reject on timeout. Default is true.
|
|
93
|
-
* @returns {Promise<T>} A new promise that resolves or rejects based on the original promise and the timeout.
|
|
94
|
-
*/
|
|
95
47
|
export function withTimeout(promise, timeoutMillisecs = 10000, reThrow = true) {
|
|
96
48
|
return new Promise((resolve, reject) => {
|
|
97
49
|
const timer = setTimeout(() => {
|
|
@@ -99,23 +51,22 @@ export function withTimeout(promise, timeoutMillisecs = 10000, reThrow = true) {
|
|
|
99
51
|
reject(new Error('Operation timed out'));
|
|
100
52
|
}
|
|
101
53
|
else {
|
|
102
|
-
resolve(undefined);
|
|
54
|
+
resolve(undefined);
|
|
103
55
|
}
|
|
104
|
-
}, timeoutMillisecs).unref();
|
|
56
|
+
}, timeoutMillisecs).unref();
|
|
105
57
|
promise
|
|
106
58
|
.then((result) => {
|
|
107
|
-
clearTimeout(timer);
|
|
59
|
+
clearTimeout(timer);
|
|
108
60
|
resolve(result);
|
|
109
61
|
})
|
|
110
62
|
.catch((error) => {
|
|
111
|
-
clearTimeout(timer);
|
|
63
|
+
clearTimeout(timer);
|
|
112
64
|
if (reThrow) {
|
|
113
|
-
reject(error);
|
|
65
|
+
reject(error);
|
|
114
66
|
}
|
|
115
67
|
else {
|
|
116
|
-
resolve(undefined);
|
|
68
|
+
resolve(undefined);
|
|
117
69
|
}
|
|
118
70
|
});
|
|
119
71
|
});
|
|
120
72
|
}
|
|
121
|
-
//# sourceMappingURL=wait.js.map
|