node-opcua-utils 2.69.0 → 2.76.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,57 +1,57 @@
1
- /// <reference types="node" />
2
- /**
3
- * @module node-opcua-utils
4
- */
5
- import { EventEmitter } from "events";
6
- declare type ArbitraryClockTick = number;
7
- declare type DurationInMillisecond = number;
8
- export interface IWatchdogData2 {
9
- key: number;
10
- subscriber: ISubscriber;
11
- timeout: DurationInMillisecond;
12
- lastSeen: ArbitraryClockTick;
13
- visitCount: number;
14
- }
15
- export interface ISubscriber {
16
- _watchDog?: WatchDog;
17
- _watchDogData?: IWatchdogData2;
18
- watchdogReset: () => void;
19
- keepAlive?: () => void;
20
- onClientSeen?: () => void;
21
- }
22
- export declare class WatchDog extends EventEmitter {
23
- static lastSeenToDuration(lastSeen: number): number;
24
- static emptyKeepAlive: () => void;
25
- /**
26
- * returns the number of subscribers using the WatchDog object.
27
- */
28
- get subscriberCount(): number;
29
- private readonly _watchdogDataMap;
30
- private _counter;
31
- private _currentTime;
32
- private _timer;
33
- private readonly _visitSubscriberB;
34
- constructor();
35
- /**
36
- * add a subscriber to the WatchDog.
37
- * @method addSubscriber
38
- *
39
- * add a subscriber to the WatchDog.
40
- *
41
- * This method modifies the subscriber be adding a
42
- * new method to it called 'keepAlive'
43
- * The subscriber must also provide a "watchdogReset". watchdogReset will be called
44
- * if the subscriber failed to call keepAlive withing the timeout period.
45
- * @param subscriber
46
- * @param timeout
47
- * @return the numerical key associated with this subscriber
48
- */
49
- addSubscriber(subscriber: ISubscriber, timeout: number): number;
50
- removeSubscriber(subscriber: ISubscriber): void;
51
- shutdown(): void;
52
- getCurrentSystemTick(): ArbitraryClockTick;
53
- private _visit_subscriber;
54
- private _start_timer;
55
- private _stop_timer;
56
- }
57
- export {};
1
+ /// <reference types="node" />
2
+ /**
3
+ * @module node-opcua-utils
4
+ */
5
+ import { EventEmitter } from "events";
6
+ declare type ArbitraryClockTick = number;
7
+ declare type DurationInMillisecond = number;
8
+ export interface IWatchdogData2 {
9
+ key: number;
10
+ subscriber: ISubscriber;
11
+ timeout: DurationInMillisecond;
12
+ lastSeen: ArbitraryClockTick;
13
+ visitCount: number;
14
+ }
15
+ export interface ISubscriber {
16
+ _watchDog?: WatchDog;
17
+ _watchDogData?: IWatchdogData2;
18
+ watchdogReset: () => void;
19
+ keepAlive?: () => void;
20
+ onClientSeen?: () => void;
21
+ }
22
+ export declare class WatchDog extends EventEmitter {
23
+ static lastSeenToDuration(lastSeen: number): number;
24
+ static emptyKeepAlive: () => void;
25
+ /**
26
+ * returns the number of subscribers using the WatchDog object.
27
+ */
28
+ get subscriberCount(): number;
29
+ private readonly _watchdogDataMap;
30
+ private _counter;
31
+ private _currentTime;
32
+ private _timer;
33
+ private readonly _visitSubscriberB;
34
+ constructor();
35
+ /**
36
+ * add a subscriber to the WatchDog.
37
+ * @method addSubscriber
38
+ *
39
+ * add a subscriber to the WatchDog.
40
+ *
41
+ * This method modifies the subscriber be adding a
42
+ * new method to it called 'keepAlive'
43
+ * The subscriber must also provide a "watchdogReset". watchdogReset will be called
44
+ * if the subscriber failed to call keepAlive withing the timeout period.
45
+ * @param subscriber
46
+ * @param timeout
47
+ * @return the numerical key associated with this subscriber
48
+ */
49
+ addSubscriber(subscriber: ISubscriber, timeout: number): number;
50
+ removeSubscriber(subscriber: ISubscriber): void;
51
+ shutdown(): void;
52
+ getCurrentSystemTick(): ArbitraryClockTick;
53
+ private _visit_subscriber;
54
+ private _start_timer;
55
+ private _stop_timer;
56
+ }
57
+ export {};
package/dist/watchdog.js CHANGED
@@ -1,143 +1,143 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WatchDog = void 0;
4
- /**
5
- * @module node-opcua-utils
6
- */
7
- const events_1 = require("events");
8
- const node_opcua_assert_1 = require("node-opcua-assert");
9
- const get_clock_tick_1 = require("./get_clock_tick");
10
- function hasExpired(watchDogData, currentTime) {
11
- const elapsedTime = currentTime - watchDogData.lastSeen;
12
- return elapsedTime > watchDogData.timeout;
13
- }
14
- function keepAliveFunc() {
15
- (0, node_opcua_assert_1.assert)(this._watchDog instanceof WatchDog);
16
- // istanbul ignore next
17
- if (!this._watchDogData || !this._watchDog) {
18
- throw new Error("Internal error");
19
- }
20
- (0, node_opcua_assert_1.assert)(typeof this._watchDogData.key === "number");
21
- this._watchDogData.lastSeen = this._watchDog.getCurrentSystemTick();
22
- if (this.onClientSeen) {
23
- this.onClientSeen();
24
- }
25
- }
26
- class WatchDog extends events_1.EventEmitter {
27
- constructor() {
28
- super();
29
- this._watchdogDataMap = {};
30
- this._counter = 0;
31
- this._currentTime = this.getCurrentSystemTick();
32
- this._visitSubscriberB = this._visit_subscriber.bind(this);
33
- this._timer = null; // as NodeJS.Timer;
34
- }
35
- static lastSeenToDuration(lastSeen) {
36
- return (0, get_clock_tick_1.get_clock_tick)() - lastSeen;
37
- }
38
- /**
39
- * returns the number of subscribers using the WatchDog object.
40
- */
41
- get subscriberCount() {
42
- return Object.keys(this._watchdogDataMap).length;
43
- }
44
- /**
45
- * add a subscriber to the WatchDog.
46
- * @method addSubscriber
47
- *
48
- * add a subscriber to the WatchDog.
49
- *
50
- * This method modifies the subscriber be adding a
51
- * new method to it called 'keepAlive'
52
- * The subscriber must also provide a "watchdogReset". watchdogReset will be called
53
- * if the subscriber failed to call keepAlive withing the timeout period.
54
- * @param subscriber
55
- * @param timeout
56
- * @return the numerical key associated with this subscriber
57
- */
58
- addSubscriber(subscriber, timeout) {
59
- this._currentTime = this.getCurrentSystemTick();
60
- timeout = timeout || 1000;
61
- (0, node_opcua_assert_1.assert)(typeof timeout === "number", " invalid timeout ");
62
- (0, node_opcua_assert_1.assert)(typeof subscriber.watchdogReset === "function", " the subscriber must provide a watchdogReset method ");
63
- (0, node_opcua_assert_1.assert)(typeof subscriber.keepAlive !== "function" || subscriber.keepAlive === WatchDog.emptyKeepAlive);
64
- this._counter += 1;
65
- const key = this._counter;
66
- subscriber._watchDog = this;
67
- subscriber._watchDogData = {
68
- key,
69
- lastSeen: this._currentTime,
70
- subscriber,
71
- timeout,
72
- visitCount: 0
73
- };
74
- this._watchdogDataMap[key] = subscriber._watchDogData;
75
- if (subscriber.onClientSeen) {
76
- subscriber.onClientSeen();
77
- }
78
- subscriber.keepAlive = keepAliveFunc.bind(subscriber);
79
- // start timer when the first subscriber comes in
80
- if (this.subscriberCount === 1) {
81
- (0, node_opcua_assert_1.assert)(this._timer === null);
82
- this._start_timer();
83
- }
84
- (0, node_opcua_assert_1.assert)(this._timer !== null);
85
- return key;
86
- }
87
- removeSubscriber(subscriber) {
88
- if (!subscriber._watchDog) {
89
- return; // already removed !!!
90
- }
91
- if (!subscriber._watchDogData) {
92
- throw new Error("Internal error");
93
- }
94
- (0, node_opcua_assert_1.assert)(subscriber._watchDog instanceof WatchDog);
95
- (0, node_opcua_assert_1.assert)(typeof subscriber._watchDogData.key === "number");
96
- (0, node_opcua_assert_1.assert)(typeof subscriber.keepAlive === "function");
97
- (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(this._watchdogDataMap, subscriber._watchDogData.key));
98
- delete this._watchdogDataMap[subscriber._watchDogData.key];
99
- delete subscriber._watchDog;
100
- // leave it as it might be usefull, delete subscriber._watchDogData;
101
- subscriber.keepAlive = WatchDog.emptyKeepAlive;
102
- // delete timer when the last subscriber comes out
103
- if (this.subscriberCount === 0) {
104
- this._stop_timer();
105
- }
106
- }
107
- shutdown() {
108
- (0, node_opcua_assert_1.assert)(this._timer === null && Object.keys(this._watchdogDataMap).length === 0, " leaking subscriber in watchdog");
109
- }
110
- getCurrentSystemTick() {
111
- return (0, get_clock_tick_1.get_clock_tick)();
112
- }
113
- _visit_subscriber() {
114
- this._currentTime = this.getCurrentSystemTick();
115
- const expiredSubscribers = Object.values(this._watchdogDataMap).filter((watchDogData) => {
116
- watchDogData.visitCount += 1;
117
- return hasExpired(watchDogData, this._currentTime);
118
- });
119
- if (expiredSubscribers.length) {
120
- this.emit("timeout", expiredSubscribers);
121
- }
122
- expiredSubscribers.forEach((watchDogData) => {
123
- this.removeSubscriber(watchDogData.subscriber);
124
- watchDogData.subscriber.watchdogReset();
125
- });
126
- }
127
- _start_timer() {
128
- (0, node_opcua_assert_1.assert)(this._timer === null, " setInterval already called ?");
129
- this._timer = setInterval(this._visitSubscriberB, 1000);
130
- }
131
- _stop_timer() {
132
- (0, node_opcua_assert_1.assert)(this._timer !== null, "_stop_timer already called ?");
133
- if (this._timer !== null) {
134
- clearInterval(this._timer);
135
- this._timer = null;
136
- }
137
- }
138
- }
139
- exports.WatchDog = WatchDog;
140
- WatchDog.emptyKeepAlive = () => {
141
- /* */
142
- };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WatchDog = void 0;
4
+ /**
5
+ * @module node-opcua-utils
6
+ */
7
+ const events_1 = require("events");
8
+ const node_opcua_assert_1 = require("node-opcua-assert");
9
+ const get_clock_tick_1 = require("./get_clock_tick");
10
+ function hasExpired(watchDogData, currentTime) {
11
+ const elapsedTime = currentTime - watchDogData.lastSeen;
12
+ return elapsedTime > watchDogData.timeout;
13
+ }
14
+ function keepAliveFunc() {
15
+ (0, node_opcua_assert_1.assert)(this._watchDog instanceof WatchDog);
16
+ // istanbul ignore next
17
+ if (!this._watchDogData || !this._watchDog) {
18
+ throw new Error("Internal error");
19
+ }
20
+ (0, node_opcua_assert_1.assert)(typeof this._watchDogData.key === "number");
21
+ this._watchDogData.lastSeen = this._watchDog.getCurrentSystemTick();
22
+ if (this.onClientSeen) {
23
+ this.onClientSeen();
24
+ }
25
+ }
26
+ class WatchDog extends events_1.EventEmitter {
27
+ constructor() {
28
+ super();
29
+ this._watchdogDataMap = {};
30
+ this._counter = 0;
31
+ this._currentTime = this.getCurrentSystemTick();
32
+ this._visitSubscriberB = this._visit_subscriber.bind(this);
33
+ this._timer = null; // as NodeJS.Timer;
34
+ }
35
+ static lastSeenToDuration(lastSeen) {
36
+ return (0, get_clock_tick_1.get_clock_tick)() - lastSeen;
37
+ }
38
+ /**
39
+ * returns the number of subscribers using the WatchDog object.
40
+ */
41
+ get subscriberCount() {
42
+ return Object.keys(this._watchdogDataMap).length;
43
+ }
44
+ /**
45
+ * add a subscriber to the WatchDog.
46
+ * @method addSubscriber
47
+ *
48
+ * add a subscriber to the WatchDog.
49
+ *
50
+ * This method modifies the subscriber be adding a
51
+ * new method to it called 'keepAlive'
52
+ * The subscriber must also provide a "watchdogReset". watchdogReset will be called
53
+ * if the subscriber failed to call keepAlive withing the timeout period.
54
+ * @param subscriber
55
+ * @param timeout
56
+ * @return the numerical key associated with this subscriber
57
+ */
58
+ addSubscriber(subscriber, timeout) {
59
+ this._currentTime = this.getCurrentSystemTick();
60
+ timeout = timeout || 1000;
61
+ (0, node_opcua_assert_1.assert)(typeof timeout === "number", " invalid timeout ");
62
+ (0, node_opcua_assert_1.assert)(typeof subscriber.watchdogReset === "function", " the subscriber must provide a watchdogReset method ");
63
+ (0, node_opcua_assert_1.assert)(typeof subscriber.keepAlive !== "function" || subscriber.keepAlive === WatchDog.emptyKeepAlive);
64
+ this._counter += 1;
65
+ const key = this._counter;
66
+ subscriber._watchDog = this;
67
+ subscriber._watchDogData = {
68
+ key,
69
+ lastSeen: this._currentTime,
70
+ subscriber,
71
+ timeout,
72
+ visitCount: 0
73
+ };
74
+ this._watchdogDataMap[key] = subscriber._watchDogData;
75
+ if (subscriber.onClientSeen) {
76
+ subscriber.onClientSeen();
77
+ }
78
+ subscriber.keepAlive = keepAliveFunc.bind(subscriber);
79
+ // start timer when the first subscriber comes in
80
+ if (this.subscriberCount === 1) {
81
+ (0, node_opcua_assert_1.assert)(this._timer === null);
82
+ this._start_timer();
83
+ }
84
+ (0, node_opcua_assert_1.assert)(this._timer !== null);
85
+ return key;
86
+ }
87
+ removeSubscriber(subscriber) {
88
+ if (!subscriber._watchDog) {
89
+ return; // already removed !!!
90
+ }
91
+ if (!subscriber._watchDogData) {
92
+ throw new Error("Internal error");
93
+ }
94
+ (0, node_opcua_assert_1.assert)(subscriber._watchDog instanceof WatchDog);
95
+ (0, node_opcua_assert_1.assert)(typeof subscriber._watchDogData.key === "number");
96
+ (0, node_opcua_assert_1.assert)(typeof subscriber.keepAlive === "function");
97
+ (0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(this._watchdogDataMap, subscriber._watchDogData.key));
98
+ delete this._watchdogDataMap[subscriber._watchDogData.key];
99
+ delete subscriber._watchDog;
100
+ // leave it as it might be usefull, delete subscriber._watchDogData;
101
+ subscriber.keepAlive = WatchDog.emptyKeepAlive;
102
+ // delete timer when the last subscriber comes out
103
+ if (this.subscriberCount === 0) {
104
+ this._stop_timer();
105
+ }
106
+ }
107
+ shutdown() {
108
+ (0, node_opcua_assert_1.assert)(this._timer === null && Object.keys(this._watchdogDataMap).length === 0, " leaking subscriber in watchdog");
109
+ }
110
+ getCurrentSystemTick() {
111
+ return (0, get_clock_tick_1.get_clock_tick)();
112
+ }
113
+ _visit_subscriber() {
114
+ this._currentTime = this.getCurrentSystemTick();
115
+ const expiredSubscribers = Object.values(this._watchdogDataMap).filter((watchDogData) => {
116
+ watchDogData.visitCount += 1;
117
+ return hasExpired(watchDogData, this._currentTime);
118
+ });
119
+ if (expiredSubscribers.length) {
120
+ this.emit("timeout", expiredSubscribers);
121
+ }
122
+ expiredSubscribers.forEach((watchDogData) => {
123
+ this.removeSubscriber(watchDogData.subscriber);
124
+ watchDogData.subscriber.watchdogReset();
125
+ });
126
+ }
127
+ _start_timer() {
128
+ (0, node_opcua_assert_1.assert)(this._timer === null, " setInterval already called ?");
129
+ this._timer = setInterval(this._visitSubscriberB, 1000);
130
+ }
131
+ _stop_timer() {
132
+ (0, node_opcua_assert_1.assert)(this._timer !== null, "_stop_timer already called ?");
133
+ if (this._timer !== null) {
134
+ clearInterval(this._timer);
135
+ this._timer = null;
136
+ }
137
+ }
138
+ }
139
+ exports.WatchDog = WatchDog;
140
+ WatchDog.emptyKeepAlive = () => {
141
+ /* */
142
+ };
143
143
  //# sourceMappingURL=watchdog.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-opcua-utils",
3
- "version": "2.69.0",
3
+ "version": "2.76.0",
4
4
  "description": "pure nodejs OPCUA SDK - module -utils",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -13,10 +13,9 @@
13
13
  },
14
14
  "dependencies": {
15
15
  "chalk": "4.1.2",
16
- "node-opcua-assert": "2.66.0"
16
+ "node-opcua-assert": "2.76.0"
17
17
  },
18
18
  "devDependencies": {
19
- "@types/node": "17.0.36",
20
19
  "should": "^13.2.3",
21
20
  "sinon": "^14.0.0"
22
21
  },
@@ -35,5 +34,5 @@
35
34
  "internet of things"
36
35
  ],
37
36
  "homepage": "http://node-opcua.github.io/",
38
- "gitHead": "6c88d05e8c82ce4bc9c8af9f0a8eb6136f31d2ce"
37
+ "gitHead": "75d9b8cf894c8fbadf77d2c4a48a730d055465e7"
39
38
  }