node-opcua-utils 2.51.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.
Files changed (55) hide show
  1. package/.mocharc.yml +10 -0
  2. package/LICENSE +20 -0
  3. package/dist/buffer_ellipsis.d.ts +5 -0
  4. package/dist/buffer_ellipsis.js +17 -0
  5. package/dist/buffer_ellipsis.js.map +1 -0
  6. package/dist/compare_buffers.d.ts +2 -0
  7. package/dist/compare_buffers.js +27 -0
  8. package/dist/compare_buffers.js.map +1 -0
  9. package/dist/get_clock_tick.d.ts +4 -0
  10. package/dist/get_clock_tick.js +18 -0
  11. package/dist/get_clock_tick.js.map +1 -0
  12. package/dist/get_function_parameters_name.d.ts +1 -0
  13. package/dist/get_function_parameters_name.js +19 -0
  14. package/dist/get_function_parameters_name.js.map +1 -0
  15. package/dist/index.d.ts +27 -0
  16. package/dist/index.js +69 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/line_file.d.ts +9 -0
  19. package/dist/line_file.js +24 -0
  20. package/dist/line_file.js.map +1 -0
  21. package/dist/match_uri.d.ts +11 -0
  22. package/dist/match_uri.js +24 -0
  23. package/dist/match_uri.js.map +1 -0
  24. package/dist/object_classname.d.ts +9 -0
  25. package/dist/object_classname.js +16 -0
  26. package/dist/object_classname.js.map +1 -0
  27. package/dist/replace_buffer_with_hex_dump.d.ts +4 -0
  28. package/dist/replace_buffer_with_hex_dump.js +21 -0
  29. package/dist/replace_buffer_with_hex_dump.js.map +1 -0
  30. package/dist/set_deprecated.d.ts +1 -0
  31. package/dist/set_deprecated.js +26 -0
  32. package/dist/set_deprecated.js.map +1 -0
  33. package/dist/string_utils.d.ts +66 -0
  34. package/dist/string_utils.js +128 -0
  35. package/dist/string_utils.js.map +1 -0
  36. package/dist/timestamp.d.ts +1 -0
  37. package/dist/timestamp.js +17 -0
  38. package/dist/timestamp.js.map +1 -0
  39. package/dist/watchdog.d.ts +56 -0
  40. package/dist/watchdog.js +156 -0
  41. package/dist/watchdog.js.map +1 -0
  42. package/package.json +38 -0
  43. package/source/buffer_ellipsis.ts +13 -0
  44. package/source/compare_buffers.ts +23 -0
  45. package/source/get_clock_tick.ts +18 -0
  46. package/source/get_function_parameters_name.ts +13 -0
  47. package/source/index.ts +46 -0
  48. package/source/line_file.ts +24 -0
  49. package/source/match_uri.ts +19 -0
  50. package/source/object_classname.ts +11 -0
  51. package/source/replace_buffer_with_hex_dump.ts +15 -0
  52. package/source/set_deprecated.ts +28 -0
  53. package/source/string_utils.ts +121 -0
  54. package/source/timestamp.ts +15 -0
  55. package/source/watchdog.ts +200 -0
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.timestamp = void 0;
4
+ function w(s, length) {
5
+ return ("000" + s).substr(-length);
6
+ }
7
+ function t(d) {
8
+ return w(d.getUTCHours(), 2) + ":"
9
+ + w(d.getUTCMinutes(), 2) + ":"
10
+ + w(d.getUTCSeconds(), 2) + ":"
11
+ + w(d.getMilliseconds(), 3);
12
+ }
13
+ function timestamp() {
14
+ return t(new Date());
15
+ }
16
+ exports.timestamp = timestamp;
17
+ //# sourceMappingURL=timestamp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timestamp.js","sourceRoot":"","sources":["../source/timestamp.ts"],"names":[],"mappings":";;;AAAA,SAAS,CAAC,CAAC,CAAkB,EAAE,MAAc;IACzC,OAAO,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AACvC,CAAC;AAED,SAAS,CAAC,CAAC,CAAO;IACd,OAAO,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG;UAC5B,CAAC,CAAC,CAAC,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG;UAC7B,CAAC,CAAC,CAAC,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG;UAC7B,CAAC,CAAC,CAAC,CAAC,eAAe,EAAE,EAAE,CAAC,CAAC,CAAC;AACpC,CAAC;AAED,SAAgB,SAAS;IACrB,OAAO,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC;AACzB,CAAC;AAFD,8BAEC"}
@@ -0,0 +1,56 @@
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 emptyKeepAlive: () => void;
24
+ /**
25
+ * returns the number of subscribers using the WatchDog object.
26
+ */
27
+ get subscriberCount(): number;
28
+ private readonly _watchdogDataMap;
29
+ private _counter;
30
+ private _currentTime;
31
+ private _timer;
32
+ private readonly _visitSubscriberB;
33
+ constructor();
34
+ /**
35
+ * add a subscriber to the WatchDog.
36
+ * @method addSubscriber
37
+ *
38
+ * add a subscriber to the WatchDog.
39
+ *
40
+ * This method modifies the subscriber be adding a
41
+ * new method to it called 'keepAlive'
42
+ * The subscriber must also provide a "watchdogReset". watchdogReset will be called
43
+ * if the subscriber failed to call keepAlive withing the timeout period.
44
+ * @param subscriber
45
+ * @param timeout
46
+ * @return the numerical key associated with this subscriber
47
+ */
48
+ addSubscriber(subscriber: ISubscriber, timeout: number): number;
49
+ removeSubscriber(subscriber: ISubscriber): void;
50
+ shutdown(): void;
51
+ getCurrentSystemTick(): ArbitraryClockTick;
52
+ private _visit_subscriber;
53
+ private _start_timer;
54
+ private _stop_timer;
55
+ }
56
+ export {};
@@ -0,0 +1,156 @@
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
+ /**
10
+ * a arbitrary clock which is system dependant and
11
+ * insensible to clock drifts ....
12
+ *
13
+ */
14
+ function _getCurrentSystemTick() {
15
+ if (process && process.hrtime) {
16
+ const h = process.hrtime();
17
+ const n = h[1] / 1000000;
18
+ (0, node_opcua_assert_1.assert)(n <= 1000);
19
+ return h[0] * 1000 + n;
20
+ }
21
+ else {
22
+ // fallback to Date as process.hrtime doesn't exit
23
+ return Date.now();
24
+ }
25
+ }
26
+ function hasExpired(watchDogData, currentTime) {
27
+ const elapsedTime = currentTime - watchDogData.lastSeen;
28
+ return elapsedTime > watchDogData.timeout;
29
+ }
30
+ function keepAliveFunc() {
31
+ (0, node_opcua_assert_1.assert)(this._watchDog instanceof WatchDog);
32
+ // istanbul ignore next
33
+ if (!this._watchDogData || !this._watchDog) {
34
+ throw new Error("Internal error");
35
+ }
36
+ (0, node_opcua_assert_1.assert)(typeof this._watchDogData.key === "number");
37
+ this._watchDogData.lastSeen = this._watchDog.getCurrentSystemTick();
38
+ if (this.onClientSeen) {
39
+ this.onClientSeen();
40
+ }
41
+ }
42
+ class WatchDog extends events_1.EventEmitter {
43
+ constructor() {
44
+ super();
45
+ this._watchdogDataMap = {};
46
+ this._counter = 0;
47
+ this._currentTime = this.getCurrentSystemTick();
48
+ this._visitSubscriberB = this._visit_subscriber.bind(this);
49
+ this._timer = null; // as NodeJS.Timer;
50
+ }
51
+ /**
52
+ * returns the number of subscribers using the WatchDog object.
53
+ */
54
+ get subscriberCount() {
55
+ return Object.keys(this._watchdogDataMap).length;
56
+ }
57
+ /**
58
+ * add a subscriber to the WatchDog.
59
+ * @method addSubscriber
60
+ *
61
+ * add a subscriber to the WatchDog.
62
+ *
63
+ * This method modifies the subscriber be adding a
64
+ * new method to it called 'keepAlive'
65
+ * The subscriber must also provide a "watchdogReset". watchdogReset will be called
66
+ * if the subscriber failed to call keepAlive withing the timeout period.
67
+ * @param subscriber
68
+ * @param timeout
69
+ * @return the numerical key associated with this subscriber
70
+ */
71
+ addSubscriber(subscriber, timeout) {
72
+ this._currentTime = this.getCurrentSystemTick();
73
+ timeout = timeout || 1000;
74
+ (0, node_opcua_assert_1.assert)(typeof timeout === "number", " invalid timeout ");
75
+ (0, node_opcua_assert_1.assert)(typeof subscriber.watchdogReset === "function", " the subscriber must provide a watchdogReset method ");
76
+ (0, node_opcua_assert_1.assert)(typeof subscriber.keepAlive !== "function" || subscriber.keepAlive === WatchDog.emptyKeepAlive);
77
+ this._counter += 1;
78
+ const key = this._counter;
79
+ subscriber._watchDog = this;
80
+ subscriber._watchDogData = {
81
+ key,
82
+ lastSeen: this._currentTime,
83
+ subscriber,
84
+ timeout,
85
+ visitCount: 0
86
+ };
87
+ this._watchdogDataMap[key] = subscriber._watchDogData;
88
+ if (subscriber.onClientSeen) {
89
+ subscriber.onClientSeen();
90
+ }
91
+ subscriber.keepAlive = keepAliveFunc.bind(subscriber);
92
+ // start timer when the first subscriber comes in
93
+ if (this.subscriberCount === 1) {
94
+ (0, node_opcua_assert_1.assert)(this._timer === null);
95
+ this._start_timer();
96
+ }
97
+ (0, node_opcua_assert_1.assert)(this._timer !== null);
98
+ return key;
99
+ }
100
+ removeSubscriber(subscriber) {
101
+ if (!subscriber._watchDog) {
102
+ return; // already removed !!!
103
+ }
104
+ if (!subscriber._watchDogData) {
105
+ throw new Error("Internal error");
106
+ }
107
+ (0, node_opcua_assert_1.assert)(subscriber._watchDog instanceof WatchDog);
108
+ (0, node_opcua_assert_1.assert)(typeof subscriber._watchDogData.key === "number");
109
+ (0, node_opcua_assert_1.assert)(typeof subscriber.keepAlive === "function");
110
+ (0, node_opcua_assert_1.assert)(this._watchdogDataMap.hasOwnProperty(subscriber._watchDogData.key));
111
+ delete this._watchdogDataMap[subscriber._watchDogData.key];
112
+ delete subscriber._watchDog;
113
+ delete subscriber._watchDogData;
114
+ subscriber.keepAlive = WatchDog.emptyKeepAlive;
115
+ // delete timer when the last subscriber comes out
116
+ if (this.subscriberCount === 0) {
117
+ this._stop_timer();
118
+ }
119
+ }
120
+ shutdown() {
121
+ (0, node_opcua_assert_1.assert)(this._timer === null && Object.keys(this._watchdogDataMap).length === 0, " leaking subscriber in watchdog");
122
+ }
123
+ getCurrentSystemTick() {
124
+ return _getCurrentSystemTick();
125
+ }
126
+ _visit_subscriber() {
127
+ this._currentTime = this.getCurrentSystemTick();
128
+ const expiredSubscribers = Object.values(this._watchdogDataMap).filter((watchDogData) => {
129
+ watchDogData.visitCount += 1;
130
+ return hasExpired(watchDogData, this._currentTime);
131
+ });
132
+ if (expiredSubscribers.length) {
133
+ this.emit("timeout", expiredSubscribers);
134
+ }
135
+ expiredSubscribers.forEach((watchDogData) => {
136
+ this.removeSubscriber(watchDogData.subscriber);
137
+ watchDogData.subscriber.watchdogReset();
138
+ });
139
+ }
140
+ _start_timer() {
141
+ (0, node_opcua_assert_1.assert)(this._timer === null, " setInterval already called ?");
142
+ this._timer = setInterval(this._visitSubscriberB, 1000);
143
+ }
144
+ _stop_timer() {
145
+ (0, node_opcua_assert_1.assert)(this._timer !== null, "_stop_timer already called ?");
146
+ if (this._timer !== null) {
147
+ clearInterval(this._timer);
148
+ this._timer = null;
149
+ }
150
+ }
151
+ }
152
+ exports.WatchDog = WatchDog;
153
+ WatchDog.emptyKeepAlive = () => {
154
+ /* */
155
+ };
156
+ //# sourceMappingURL=watchdog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"watchdog.js","sourceRoot":"","sources":["../source/watchdog.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,mCAAsC;AACtC,yDAA2C;AAK3C;;;;GAIG;AACH,SAAS,qBAAqB;IAC1B,IAAI,OAAO,IAAI,OAAO,CAAC,MAAM,EAAE;QAC3B,MAAM,CAA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IC;AAzIU,uBAAc,GAAG,GAAG,EAAE;IACzB,KAAK;AACT,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "node-opcua-utils",
3
+ "version": "2.51.0",
4
+ "description": "pure nodejs OPCUA SDK - module -utils",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc -b",
9
+ "test": "mocha",
10
+ "clean": "node -e \"require('rimraf').sync('dist');\""
11
+ },
12
+ "dependencies": {
13
+ "browser-process-hrtime": "^1.0.0",
14
+ "chalk": "4.1.2",
15
+ "node-opcua-assert": "2.51.0"
16
+ },
17
+ "devDependencies": {
18
+ "@types/node": "16.7.10",
19
+ "should": "^13.2.3",
20
+ "sinon": "^11.1.2"
21
+ },
22
+ "author": "Etienne Rossignon",
23
+ "license": "MIT",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git://github.com/node-opcua/node-opcua.git"
27
+ },
28
+ "keywords": [
29
+ "OPCUA",
30
+ "opcua",
31
+ "m2m",
32
+ "iot",
33
+ "opc ua",
34
+ "internet of things"
35
+ ],
36
+ "homepage": "http://node-opcua.github.io/",
37
+ "gitHead": "75feb111daf7ec65fa0111e4fa5beb8987fd4945"
38
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @module node-opcua-utils
3
+ */
4
+ export function buffer_ellipsis(buffer: Buffer, start?: number, end?: number): string {
5
+ start = start || 0;
6
+ end = end || buffer.length;
7
+ if (end - start < 40) {
8
+ return buffer.slice(start, end).toString("hex");
9
+ }
10
+ return buffer.slice(start, start + 10).toString("hex") + " ... " + buffer.slice(end - 10, end).toString("hex");
11
+ }
12
+
13
+ exports.buffer_ellipsis = buffer_ellipsis;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @module node-opcua-utils
3
+ */
4
+ // tslint:disable:no-console
5
+ import * as chalk from "chalk";
6
+ import { buffer_ellipsis } from "./buffer_ellipsis";
7
+
8
+ export function compare_buffers(buf1: Buffer, buf2: Buffer, max_length?: number): void {
9
+ max_length = max_length || buf2.length;
10
+ const block_length = 80;
11
+ let cursor = 0;
12
+ while (cursor < max_length) {
13
+ const slice1 = buf1.slice(cursor, cursor + block_length);
14
+ const slice2 = buf2.slice(cursor, cursor + block_length);
15
+ if (slice2.toString("hex") !== slice1.toString("hex")) {
16
+ console.log("pos = ", cursor);
17
+ console.log("slice1 :", chalk.yellow(buffer_ellipsis(slice1)));
18
+ console.log("slice2 :", chalk.blue(buffer_ellipsis(slice2)));
19
+ }
20
+ cursor += block_length;
21
+ }
22
+ // xx buf1.length.should.equal(max_length);
23
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @module node-opcua-utils
3
+ */
4
+ export let get_clock_tick: () => number;
5
+ require("browser-process-hrtime");
6
+
7
+ if (process && process.hrtime) {
8
+ const tickOrigin = process.hrtime()[0];
9
+ // clock it as a double in millisecond
10
+ // so we can measure very tiny time intervals
11
+ get_clock_tick = () => {
12
+ const hrt = process.hrtime();
13
+ const r = (hrt[0] - tickOrigin) * 1000.0 + Math.ceil((hrt[1] / 1e6) * 1000) / 1000;
14
+ return r;
15
+ };
16
+ } else {
17
+ get_clock_tick = () => Date.now();
18
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @module node-opcua-utils
3
+ */
4
+ const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;
5
+ const ARGUMENT_NAMES = /([^\s,]+)/g;
6
+
7
+ // tslint:disable-next-line:ban-types
8
+ export function getFunctionParameterNames(func: Function) {
9
+ const fnStr = func.toString().replace(STRIP_COMMENTS, "");
10
+ let result = fnStr.slice(fnStr.indexOf("(") + 1, fnStr.indexOf(")")).match(ARGUMENT_NAMES);
11
+ if (result === null) { result = []; }
12
+ return result;
13
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * @module node-opcua-utils
3
+ */
4
+ // tslint:disable:no-bitwise
5
+ import { assert } from "node-opcua-assert";
6
+
7
+ /**
8
+ * set a flag
9
+ * @method set_flag
10
+ */
11
+ export function set_flag(value: number, mask: number | { value: number }): number {
12
+ if ((mask as any).value) {
13
+ mask = (mask as any).value;
14
+ }
15
+ assert(!mask.hasOwnProperty("value"));
16
+ assert(mask !== undefined);
17
+ return value | (mask as number);
18
+ }
19
+ /**
20
+ * check if a set of bits are set in the values
21
+ * @method check_flag
22
+ */
23
+ export function check_flag(value: number, mask: number | { value: number }): boolean {
24
+ if ((mask as any).value) {
25
+ mask = (mask as any).value;
26
+ }
27
+ assert(!mask.hasOwnProperty("value"));
28
+ return (value & (mask as number)) === (mask as number);
29
+ }
30
+
31
+ export function isNullOrUndefined(value: any): boolean {
32
+ return value === undefined || value === null;
33
+ }
34
+
35
+ export { buffer_ellipsis } from "./buffer_ellipsis";
36
+ export { capitalizeFirstLetter, lowerFirstLetter } from "./string_utils";
37
+ export { getObjectClassName } from "./object_classname";
38
+ export { get_clock_tick } from "./get_clock_tick";
39
+ export { compare_buffers } from "./compare_buffers";
40
+ export { getFunctionParameterNames } from "./get_function_parameters_name";
41
+ export * from "./watchdog";
42
+ export { setDeprecated } from "./set_deprecated";
43
+ export { replaceBufferWithHexDump } from "./replace_buffer_with_hex_dump";
44
+ export * from "./timestamp";
45
+ export * from "./line_file";
46
+ export * from "./match_uri";
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @module node-opcua-utils
3
+ */
4
+
5
+ export class LineFile {
6
+ private _line: string[];
7
+
8
+ constructor() {
9
+ this._line = [];
10
+ }
11
+
12
+ public write(...arg: string[]): void {
13
+ let str = "";
14
+ // tslint:disable:prefer-for-of
15
+ for (let i = 0; i < arguments.length; i++) {
16
+ str += arguments[i];
17
+ }
18
+ this._line.push(str);
19
+ }
20
+
21
+ public toString(endOfLine: "\n" | "\r" = "\n"): string {
22
+ return this._line.join(endOfLine);
23
+ }
24
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @module node-opcua-utils
3
+ */
4
+ /**
5
+ * returns true if two endpoint matches the same address:
6
+ *
7
+ * @see https://www.dnscheck.co/faq
8
+ * @see https://tools.ietf.org/html/rfc4343 : Domain Name System (DNS) Case Insensitivity Clarification
9
+ *
10
+ */
11
+ export function matchUri(endpointUri1: string | null, endpointUri2: string | null) {
12
+ if (endpointUri1 === null) {
13
+ return endpointUri2 === null;
14
+ }
15
+ if (endpointUri2 === null) {
16
+ return endpointUri1 === null;
17
+ }
18
+ return endpointUri1.toLowerCase() === endpointUri2.toLowerCase();
19
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @module node-opcua-utils
3
+ */
4
+ /**
5
+ * @method getObjectClassName
6
+ * @param obj
7
+ * @return {string}
8
+ */
9
+ export function getObjectClassName(obj: object): string {
10
+ return Object.prototype.toString.call(obj).slice(8, -1);
11
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @module node-opcua-utils
3
+ */
4
+ export function replaceBufferWithHexDump(obj: any): any {
5
+ for (const p in obj) {
6
+ if (obj.hasOwnProperty(p)) {
7
+ if (obj[p] instanceof Buffer) {
8
+ obj[p] = "<BUFFER>" + obj[p].toString("hex") + "</BUFFER>";
9
+ } else if (typeof obj[p] === "object") {
10
+ replaceBufferWithHexDump(obj[p]);
11
+ }
12
+ }
13
+ }
14
+ return obj;
15
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @module node-opcua-utils
3
+ */
4
+ // tslint:disable:ban-types
5
+ import * as chalk from "chalk";
6
+ import { assert } from "node-opcua-assert";
7
+
8
+ /* istanbul ignore next */
9
+ export function setDeprecated(constructor: Function, methodName: string, helpString: string): void {
10
+
11
+ const oldMethod = constructor.prototype[methodName];
12
+
13
+ assert(oldMethod instanceof Function,
14
+ "expecting a valid " + methodName + "method on class " + constructor.constructor.name);
15
+
16
+ let counter = 0;
17
+ constructor.prototype[methodName] = function() {
18
+ if (counter % 1000 === 0) {
19
+ // tslint:disable:no-console
20
+ console.log(chalk.green("Warning !"),
21
+ chalk.green(chalk.bold(constructor.name + "#" + methodName), " is now deprecated"));
22
+ console.log(" ", helpString);
23
+ }
24
+ counter++;
25
+ return oldMethod.apply(this, arguments);
26
+ };
27
+
28
+ }
@@ -0,0 +1,121 @@
1
+ /**
2
+ * @module node-opcua-utils
3
+ */
4
+ // tslint:disable:variable-name
5
+ export function capitalizeFirstLetter(str: string): string {
6
+ if (str == null) {
7
+ return str;
8
+ }
9
+ return str.substr(0, 1).toUpperCase() + str.substr(1);
10
+ }
11
+
12
+ const ACode = "A".charCodeAt(0);
13
+ const ZCode = "Z".charCodeAt(0);
14
+ export function isUpperCaseChar(c: string): boolean {
15
+ const code = c.charCodeAt(0);
16
+ return code >= ACode && code <= ZCode;
17
+ }
18
+ const aCode = "a".charCodeAt(0);
19
+ const zCode = "z".charCodeAt(0);
20
+ export function isAlpha(c: string): boolean {
21
+ const code = c.charCodeAt(0);
22
+ return (code >= ACode && code <= ZCode) || (code >= aCode && code <= zCode);
23
+ }
24
+
25
+ export function countUpperCaseSlow(str: string): number {
26
+ return str.split("").reduce((p, c) => p + (isUpperCaseChar(c) ? 1 : 0), 0);
27
+ }
28
+ export function countAlphaSlow(str: string): number {
29
+ return str.split("").reduce((p, c) => p + (isAlpha(c) ? 1 : 0), 0);
30
+ }
31
+
32
+ export function countUpperCase(str: string): number {
33
+ let count = 0;
34
+ const n = str.length
35
+ for (let i=0; i < n ; i++) {
36
+ count += isUpperCaseChar(str[i]) ? 1 : 0;
37
+ }
38
+ return count;
39
+ }
40
+ export function countAlpha(str: string): number {
41
+ let count = 0;
42
+ const n = str.length
43
+ for (let i=0; i < n ; i++) {
44
+ count += isAlpha(str[i]) ? 1 : 0;
45
+ }
46
+ return count;
47
+ }
48
+
49
+ /**
50
+ *
51
+ * lowerFirstLetter convert a OPCUA Identifier to a javascript Identifier
52
+ *
53
+ * @summary
54
+ *
55
+ * OPCUA and Javascript use two different rules to build identifiers.
56
+ *
57
+ * OPCUA Identifier usually starts with a upper case letter and word are join together, this is known as
58
+ * the Pascal case, or CapitalizedWords convention. (for instance HelloWorld)
59
+ * But sometime, OPCUA indentifiers do not follow this convention strictly and we can find various
60
+ * other convention being applied such as underscore between word, or addintion of ACRONYMED prefixes.
61
+ * On it's own, this causes great confusion and inconsistancy in programming style.
62
+ *
63
+ * Javascritpt uses a slightly different convention called camelCase where word are joined together
64
+ * and inner words starts with a capital letter whereas first word starts with a lower case letter.
65
+ * (for instance helloWorld)
66
+ *
67
+ * In node-opcua we have taken the opinionated decision to consistently use camelCase convention for
68
+ * object properties so that all the code look nice and consistent.
69
+ * the lowerFirstLetter method can be used to easily convert from the OPCUA naming convention
70
+ * to javascript naming convention by applying the following rules.
71
+ *
72
+ * * each ascii sequence in a identifier will be converted to lower camel case.
73
+ * * when an identifier only contains upper case letter then it will be untouched. ( i.e CQDF => CQFD)
74
+ * (this rules helps to preserve acronyms)
75
+ * * when a identifier starts with more than one UpperCase letter but still contain lowercase letter
76
+ * then the first Uppercase letter excluding the last one will be converted to lower case
77
+ * ( ie: EURange = > euRange)
78
+ * * when a identifier contains several sequences delimited with underscores (_) the above rules
79
+ * will be applied to each of the element of the sequence
80
+ * ( ie: ALM_FlowOutOfTolerance => ALM_flowOutOfTolerance ( ALM=>ALM , FlowOutOfTolerance=>flowOutOfTolerance)
81
+ *
82
+ * @reference
83
+ * * https://en.wikipedia.org/wiki/Camel_case
84
+ * * https://en.wikipedia.org/wiki/Hungarian_notation
85
+ * * http://wiki.c2.com/?UnderscoreVersusCapitalAndLowerCaseVariableNaming
86
+ *
87
+ *
88
+ *
89
+ * @example
90
+ * HelloWorld => helloWorld
91
+ * XAxis => xAxis
92
+ * EURange => euRange
93
+ * DATE => DATE
94
+ * XYZ => XYZ
95
+ * AB => AB
96
+ * Ab => ab
97
+ * A => a
98
+ * T1ABC8 => T1ABC8
99
+ * F_ABC_D => F_ABC_D
100
+ * ALM_Timeout => ALM_timeout
101
+ * SV_GasOn => SV_gasOn
102
+ * DI_VAL_FlowImp => DI_VAL_flowImp
103
+ */
104
+ export function lowerFirstLetter(str: string): string {
105
+ if (str == null) {
106
+ return str;
107
+ }
108
+ // at least, 2 all upper
109
+ if (str.length >= 2 && countUpperCase(str) === countAlpha(str)) {
110
+ return str;
111
+ }
112
+
113
+ if (str.match(/\_/)) {
114
+ return str.split("_").map(lowerFirstLetter).join("_");
115
+ }
116
+ let result = str.substr(0, 1).toLowerCase() + str.substr(1);
117
+ if (result.length > 3 && isUpperCaseChar(str[1]) && isUpperCaseChar(str[2])) {
118
+ result = str.substr(0, 2).toLowerCase() + str.substr(2);
119
+ }
120
+ return result;
121
+ }
@@ -0,0 +1,15 @@
1
+ function w(s: string | number, length: number): string {
2
+ return ("000" + s).substr(-length);
3
+ }
4
+
5
+ function t(d: Date): string {
6
+ return w(d.getUTCHours(), 2) + ":"
7
+ + w(d.getUTCMinutes(), 2) + ":"
8
+ + w(d.getUTCSeconds(), 2) + ":"
9
+ + w(d.getMilliseconds(), 3);
10
+ }
11
+
12
+ export function timestamp() {
13
+ return t(new Date());
14
+ }
15
+