• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

node-opcua / node-opcua / 22636309940

03 Mar 2026 06:03PM UTC coverage: 92.756% (+1.9%) from 90.81%
22636309940

push

github

erossignon
test: disable ENOENT on overlapping trustCertificate invocations in test environments with concurrency tests

18684 of 22141 branches covered (84.39%)

23 of 37 new or added lines in 1 file covered. (62.16%)

5775 existing lines in 228 files now uncovered.

160668 of 173216 relevant lines covered (92.76%)

875869.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.37
/packages/node-opcua-basic-types/source/integers.ts
1
/***
2✔
2
 * @module node-opcua-basic-types
2✔
3
 */
2✔
4
import { assert } from "node-opcua-assert";
2✔
5
import { BinaryStream, OutputBinaryStream } from "node-opcua-binary-stream";
2✔
6
import { getRandomInt } from "./utils";
2✔
7

2✔
8
export function isValidUInt16(value: number): boolean {
134,654✔
9
    if (!isFinite(value)) {
134,654✔
10
        return false;
2✔
11
    }
2✔
12
    return value >= 0 && value <= 0xffff;
134,654✔
13
}
134,654✔
14

2✔
15
export type UInt8 = number;
2✔
16
export type UInt16 = number;
2✔
17
export type UInt32 = number;
2✔
18
export type UInt64 = UInt32[];
2✔
19

2✔
20
export type Int8 = number;
2✔
21
export type Int16 = number;
2✔
22
export type Int32 = number;
2✔
23
export type Int64 = UInt32[];
2✔
24

2✔
25
export type Byte = UInt8;
2✔
26
export type SByte = Int8;
2✔
27

2✔
28
// ---------------------------------------
2✔
29

2✔
30
export function randomUInt16(): UInt16 {
1,150✔
31
    return getRandomInt(0, 0xffff) as UInt16;
1,150✔
32
}
1,150✔
33

2✔
34
export function encodeUInt16(value: UInt16, stream: OutputBinaryStream): void {
3,988,732✔
35
    stream.writeUInt16(value);
3,988,732✔
36
}
3,988,732✔
37

2✔
38
export function decodeUInt16(stream: BinaryStream, value?: number): UInt16 {
1,921,210✔
39
    return stream.readUInt16() as UInt16;
1,921,210✔
40
}
1,921,210✔
41

2✔
42
export function isValidInt16(value: number): boolean {
2,386✔
43
    if (!isFinite(value)) {
2,386✔
44
        return false;
2✔
45
    }
2✔
46
    return value >= -0x8000 && value <= 0x7fff;
2,386✔
47
}
2,386✔
48

2✔
49
export function randomInt16(): Int16 {
577✔
50
    return getRandomInt(-0x8000, 0x7fff);
577✔
51
}
577✔
52

2✔
53
export function encodeInt16(value: Int16, stream: OutputBinaryStream): void {
7,742✔
54
    assert(isFinite(value));
7,742✔
55
    stream.writeInt16(value);
7,742✔
56
}
7,742✔
57

2✔
58
export function decodeInt16(stream: BinaryStream, value?: number): Int16 {
2,058✔
59
    return stream.readInt16() as Int16;
2,058✔
60
}
2,058✔
61

2✔
62
export function isValidInt32(value: number): boolean {
364,902✔
63
    if (!isFinite(value)) {
364,902✔
64
        return false;
2✔
65
    }
2✔
66
    return value >= -0x80000000 && value <= 0x7fffffff;
364,902✔
67
}
364,902✔
68

2✔
69
export function randomInt32(): Int32 {
577✔
70
    return getRandomInt(-0x80000000, 0x7fffffff) as Int32;
577✔
71
}
577✔
72

2✔
73
export function encodeInt32(value: Int32, stream: OutputBinaryStream): void {
220,526✔
74
    assert(isFinite(value));
220,526✔
75
    stream.writeInteger(value);
220,526✔
76
}
220,526✔
77

2✔
78
export function decodeInt32(stream: BinaryStream, value?: number): Int32 {
88,305✔
79
    return stream.readInteger() as Int32;
88,305✔
80
}
88,305✔
81

2✔
82
export function isValidUInt32(value: number): boolean {
3,592,682✔
83
    if (!isFinite(value)) {
3,592,682✔
84
        return false;
2✔
85
    }
2✔
86
    return value >= 0 && value <= 0xffffffff;
3,592,682✔
87
}
3,592,682✔
88

2✔
89
export function randomUInt32(): UInt32 {
577✔
90
    return getRandomInt(0, 0xffffffff);
577✔
91
}
577✔
92

2✔
93
export function encodeUInt32(value: UInt32, stream: OutputBinaryStream): void {
22,343,746✔
94
    stream.writeUInt32(value);
22,343,746✔
95
}
22,343,746✔
96

2✔
97
export function decodeUInt32(stream: BinaryStream, value?: number): UInt32 {
2,157,436✔
98
    return stream.readUInt32() as UInt32;
2,157,436✔
99
}
2,157,436✔
100

2✔
101
export function isValidInt8(value: number): boolean {
7,594✔
102
    if (!isFinite(value)) {
7,594✔
103
        return false;
4✔
104
    }
4✔
105
    return value >= -0x80 && value <= 0x7f;
7,594✔
106
}
7,594✔
107

2✔
108
export function randomInt8(): Int8 {
581✔
109
    return getRandomInt(-0x7f, 0x7e);
581✔
110
}
581✔
111

2✔
112
export function encodeInt8(value: Int8, stream: OutputBinaryStream): void {
7,368✔
113
    assert(isValidInt8(value));
7,368✔
114
    stream.writeInt8(value);
7,368✔
115
}
7,368✔
116

2✔
117
export function decodeInt8(stream: BinaryStream, value?: number): Int8 {
1,954✔
118
    return stream.readInt8();
1,954✔
119
}
1,954✔
120

2✔
121
export const isValidSByte = isValidInt8;
2✔
122
export const randomSByte = randomInt8;
2✔
123
export const encodeSByte = encodeInt8;
2✔
124
export const decodeSByte = decodeInt8;
2✔
125

2✔
126
export function isValidUInt8(value: number): boolean {
1,005,299✔
127
    if (!isFinite(value)) {
1,005,299✔
128
        return false;
2✔
129
    }
2✔
130
    return value >= 0x00 && value <= 0xff;
1,005,299✔
131
}
1,005,299✔
132

2✔
133
export function randomUInt8(): UInt8 {
581✔
134
    return getRandomInt(0x00, 0xff);
581✔
135
}
581✔
136

2✔
137
export function encodeUInt8(value: UInt8, stream: OutputBinaryStream): void {
4,616,082✔
138
    stream.writeUInt8(value);
4,616,082✔
139
}
4,616,082✔
140

2✔
141
export function decodeUInt8(stream: BinaryStream, value?: number): UInt8 {
2,195,426✔
142
    return stream.readUInt8();
2,195,426✔
143
}
2,195,426✔
144

2✔
145
export const isValidByte = isValidUInt8;
2✔
146
export const randomByte = randomUInt8;
2✔
147
export const encodeByte = encodeUInt8;
2✔
148
export const decodeByte = decodeUInt8;
2✔
149

2✔
150
export function isValidUInt64(value?: number | number[]): boolean {
21,398✔
151
    return value instanceof Array && value.length === 2;
21,398✔
152
}
21,398✔
153

2✔
154
export function randomUInt64(): UInt64 {
1,150✔
155
    return [getRandomInt(0, 0xffffffff), getRandomInt(0, 0xffffffff)];
1,150✔
156
}
1,150✔
157

2✔
158
export function encodeUInt64(value: UInt64 | number, stream: OutputBinaryStream): void {
4,134,242✔
159
    if (typeof value === "number") {
4,134,242✔
160
        const arr = coerceUInt64(value);
4✔
161
        stream.writeUInt32(arr[1]);
4✔
162
        stream.writeUInt32(arr[0]);
4✔
163
    } else {
4,134,242✔
164
        stream.writeUInt32((value as number[])[1]);
4,134,238✔
165
        stream.writeUInt32((value as number[])[0]);
4,134,238✔
166
    }
4,134,238✔
167
}
4,134,242✔
168

2✔
169
export function decodeUInt64(stream: BinaryStream, value?: UInt64): UInt64 {
2,058,528✔
170
    const low = stream.readUInt32() as UInt32;
2,058,528✔
171
    const high = stream.readUInt32() as UInt32;
2,058,528✔
172
    return constructInt64(high, low);
2,058,528✔
173
}
2,058,528✔
174

2✔
175
export function constructInt64(high: UInt32, low: UInt32): Int64 {
5,021,962✔
176
    if (high === 0 && low < 0) {
5,021,962✔
177
        high = 0xffffffff;
135,048✔
178
        low = 0xffffffff + low + 1;
135,048✔
179
    }
135,048✔
180
    assert(low >= 0 && low <= 0xffffffff);
5,021,962✔
181
    assert(high >= 0 && high <= 0xffffffff);
5,021,962✔
182
    return [high, low];
5,021,962✔
183
}
5,021,962✔
184

2✔
185
export function 
15,800,222✔
186
coerceUInt64(value: number | UInt64 | Int32 | string | null): UInt64 {
15,800,222✔
187
    let high;
15,800,222✔
188
    let low;
15,800,222✔
189
    let v;
15,800,222✔
190
    if (value === null || value === undefined) {
15,800,222✔
191
        return [0, 0];
8✔
192
    }
8✔
193
    if (value instanceof Array) {
15,800,222✔
194
        assert(typeof value[0] === "number");
12,836,780✔
195
        assert(typeof value[1] === "number");
12,836,780✔
196
        return value;
12,836,780✔
197
    }
12,836,780✔
198
    if (typeof value === "string") {
15,800,222✔
199
        v = value.split(",");
86✔
200
        if (v.length === 1) {
86✔
201
            // was a single string, good news ! BigInt can be used with nodejs >=12
82✔
202
            let a = BigInt(value);
82✔
203
            if (a < BigInt(0)) {
82✔
204
                const mask = BigInt("0xFFFFFFFFFFFFFFFF");
12✔
205
                a = (mask + a + BigInt(1)) & mask;
12✔
206
            }
12✔
207
            high = Number(a >> BigInt(32));
82✔
208
            low = Number(a & BigInt(0xffffffff));
82✔
209
        } else {
86✔
210
            high = parseInt(v[0], 10);
4✔
211
            low = parseInt(v[1], 10);
4✔
212
        }
4✔
213
        return constructInt64(high, low);
86✔
214
    }
86✔
215
    if (value > 0xffffffff) {
15,800,222✔
216
        // beware : as per javascript, value is a double here !
6✔
217
        //          our conversion will suffer from some inaccuracy
6✔
218
        high = Math.floor(value / 0x100000000);
6✔
219
        low = value - high * 0x100000000;
6✔
220
        return constructInt64(high, low);
6✔
221
    }
6✔
222
    return constructInt64(0, value);
2,963,342✔
223
}
2,963,342✔
224

2✔
225
export function randomInt64(): Int64 {
1,150✔
226
    // High, low
1,150✔
227
    return [getRandomInt(0, 0xffffffff), getRandomInt(0, 0xffffffff)];
1,150✔
228
}
1,150✔
229

2✔
230
export const coerceInt64 = coerceUInt64;
2✔
231
export const isValidInt64 = isValidUInt64;
2✔
232
export const encodeInt64 = encodeUInt64;
2✔
233
export const decodeInt64 = decodeUInt64;
2✔
234

2✔
235
export function coerceInt8(value: number | string | null): Int8 {
34✔
236
    if (value === null || value === undefined) {
34✔
237
        return 0;
4✔
238
    }
4✔
239
    if (typeof value === "number") {
34✔
240
        return value;
28✔
241
    }
28✔
242
    return parseInt(value, 10);
2✔
243
}
2✔
244

2✔
245
export function coerceUInt8(value: number | string | null): UInt8 {
34✔
246
    if (value === null || value === undefined) {
34✔
247
        return 0;
4✔
248
    }
4✔
249
    if (typeof value === "number") {
34✔
250
        return value;
28✔
251
    }
28✔
252
    return parseInt(value, 10);
2✔
253
}
2✔
254

2✔
255
export function coerceByte(value: number | string | null): UInt8 {
1,130,726✔
256
    if (value === null || value === undefined) {
1,130,726✔
257
        return 0;
1,087,142✔
258
    }
1,087,142✔
259
    if (typeof value === "number") {
1,130,726✔
260
        return value;
42,268✔
261
    }
42,268✔
262
    return parseInt(value, 10);
1,316✔
263
}
1,316✔
264

2✔
265
export function coerceSByte(value: number | string | null): Int8 {
16✔
266
    if (value === null || value === undefined) {
16✔
267
        return 0;
4✔
268
    }
4✔
269
    if (typeof value === "number") {
16✔
270
        return value;
10✔
271
    }
10✔
272
    return parseInt(value, 10);
2✔
273
}
2✔
274

2✔
275
export function coerceUInt16(value: number | string | null): UInt16 {
2,954✔
276
    if (value === null || value === undefined) {
2,954✔
277
        return 0;
4✔
278
    }
4✔
279
    if (typeof value === "number") {
2,954✔
280
        return value;
2,948✔
281
    }
2,948✔
282
    return parseInt(value, 10);
2✔
283
}
2✔
284

2✔
285
export function coerceInt16(value: number | string | null): Int16 {
1,784✔
286
    if (value === null || value === undefined) {
1,784✔
287
        return 0;
4✔
288
    }
4✔
289
    if (typeof value === "number") {
1,784✔
290
        return value;
1,778✔
291
    }
1,778✔
292
    return parseInt(value, 10);
2✔
293
}
2✔
294

2✔
295
interface EnumItemLike {
2✔
296
    value: number;
2✔
297
}
2✔
298
export function coerceUInt32(value: null | string | number | EnumItemLike): UInt32 {
5,626,487✔
299
    if (value === null || value === undefined) {
5,626,487✔
300
        return 0;
4✔
301
    }
4✔
302
    if (value && Object.prototype.hasOwnProperty.call(value, "value")) {
5,626,487✔
303
        return coerceUInt32((value as EnumItemLike).value);
2✔
304
    }
2✔
305
    if (typeof value === "number") {
5,626,487✔
306
        return value;
5,626,477✔
307
    }
5,626,477✔
308
    return parseInt(value as string, 10);
4✔
309
}
4✔
310

2✔
311
export function coerceInt32(value: null | Int64 | UInt64 | number | string): Int32 {
4,091,274✔
312
    if (value === null || value === undefined) {
4,091,274✔
313
        return 0;
36,074✔
314
    }
36,074✔
315
    if (value instanceof Array) {
4,091,274✔
316
        // Int64 as a [high,low]
42✔
317
        return coerceInt64toInt32(value);
42✔
318
    }
42✔
319
    if (typeof value === "number") {
4,091,274✔
320
        return value;
3,229,698✔
321
    }
3,229,698✔
322
    return parseInt(value, 10);
825,460✔
323
}
825,460✔
324

2✔
325
const signMask = 1n << 31n;
2✔
326
const shiftHigh = 1n << 32n;
2✔
327
export function Int64ToBigInt(value: Int64): bigint {
3,726✔
328
    const h = BigInt(value[0]);
3,726✔
329
    const l = BigInt(value[1]);
3,726✔
330
    if ((h & signMask) === signMask) {
3,726✔
331
        const v = (h & ~signMask) * shiftHigh + l - 0x8000000000000000n;
436✔
332
        return v;
436✔
333
    } else {
3,726✔
334
        const v = h * shiftHigh + l;
3,290✔
335
        return v;
3,290✔
336
    }
3,290✔
337
}
3,726✔
UNCOV
338
export function UInt64ToBigInt(value: UInt64): bigint {
×
339
    const h = BigInt(value[0]);
×
340
    const l = BigInt(value[1]);
×
341
    const v = h * shiftHigh + l;
×
342
    return v;
×
UNCOV
343
}
×
344

2✔
345
export function coerceInt64toInt32(value: Int64 | Int32): Int32 {
4,118✔
346
    if (value instanceof Array) {
4,118✔
347
        const b = Int64ToBigInt(value);
3,726✔
348
        return Number(b);
3,726✔
349
    }
3,726✔
350
    return value;
392✔
351
}
392✔
UNCOV
352
export function coerceUInt64toInt32(value: UInt64 | UInt32): Int32 {
×
353
    if (value instanceof Array) {
×
354
        const b = UInt64ToBigInt(value);
×
355
        return Number(b);
×
UNCOV
356
    }
×
357
    return value;
×
UNCOV
358
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc