node-opcua-binary-stream 2.90.1 → 2.98.1

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,437 +1,437 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateByteLength = exports.BinaryStream = void 0;
4
- /**
5
- * @module node-opcua-binary-stream
6
- */
7
- require("util");
8
- const node_opcua_assert_1 = require("node-opcua-assert");
9
- const node_opcua_buffer_utils_1 = require("node-opcua-buffer-utils");
10
- const MAXUINT32 = 4294967295; // 2**32 -1;
11
- const noAssert = false;
12
- const performCheck = false;
13
- /**
14
- * a BinaryStream can be use to perform sequential read or write
15
- * inside a buffer.
16
- * The BinaryStream maintains a cursor up to date as the caller
17
- * operates on the stream using the various read/write methods.
18
- * It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
19
- * It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
20
- * convention.
21
- *
22
- * data can either be:
23
- *
24
- * * a Buffer , in this case the BinaryStream operates on this Buffer
25
- * * null , in this case a BinaryStream with 1024 bytes is created
26
- * * any data , in this case the object is converted into a binary buffer.
27
- *
28
- * example:
29
- *
30
- * ``` javascript
31
- * var stream = new BinaryStream(32)
32
- * ```
33
- *
34
- * @class BinaryStream
35
- * @param {null|Buffer|Number} data
36
- * @constructor
37
- *
38
- *
39
- *
40
- */
41
- class BinaryStream {
42
- constructor(data) {
43
- if (data === undefined) {
44
- this.buffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(1024);
45
- }
46
- else if (typeof data === "number") {
47
- this.buffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(data);
48
- }
49
- else {
50
- (0, node_opcua_assert_1.assert)(data instanceof Buffer);
51
- this.buffer = data;
52
- }
53
- this.length = 0;
54
- }
55
- /**
56
- * set the cursor to the begining of the stream
57
- * @method BinaryStream.rewind
58
- */
59
- rewind() {
60
- this.length = 0;
61
- }
62
- /**
63
- * write a single signed byte (8 bits) to the stream.
64
- * value must be in the range of [-127,128]
65
- * @param value the value to write
66
- */
67
- writeInt8(value) {
68
- // istanbul ignore next
69
- if (performCheck) {
70
- (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 1, "not enough space in buffer");
71
- (0, node_opcua_assert_1.assert)(value >= -128 && value < 128);
72
- }
73
- this.buffer.writeInt8(value, this.length);
74
- this.length += 1;
75
- }
76
- /**
77
- * write a single unsigned byte (8 bits) to the stream.
78
- * @param value the value to write
79
- */
80
- writeUInt8(value) {
81
- // istanbul ignore next
82
- if (performCheck) {
83
- (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 1, "not enough space in buffer");
84
- (0, node_opcua_assert_1.assert)(value >= 0 && value < 256, " writeUInt8 : out of bound ");
85
- }
86
- this.buffer.writeUInt8(value, this.length);
87
- this.length += 1;
88
- }
89
- /**
90
- * write a single 16 bit signed integer to the stream.
91
- * @param value the value to write
92
- */
93
- writeInt16(value) {
94
- // istanbul ignore next
95
- if (performCheck) {
96
- (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 2, "not enough space in buffer");
97
- }
98
- this.buffer.writeInt16LE(value, this.length);
99
- this.length += 2;
100
- }
101
- /**
102
- * write a single 16 bit unsigned integer to the stream.
103
- * @param value the value to write
104
- */
105
- writeUInt16(value) {
106
- // istanbul ignore next
107
- if (performCheck) {
108
- (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 2, "not enough space in buffer");
109
- }
110
- this.buffer.writeUInt16LE(value, this.length);
111
- this.length += 2;
112
- }
113
- /**
114
- * write a single 32 bit signed integer to the stream.
115
- * @param value the value to write
116
- */
117
- writeInteger(value) {
118
- // istanbul ignore next
119
- if (performCheck) {
120
- (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 4, "not enough space in buffer");
121
- }
122
- this.buffer.writeInt32LE(value, this.length);
123
- this.length += 4;
124
- }
125
- /**
126
- * write a single 32 bit unsigned integer to the stream.
127
- *
128
- * @param value the value to write
129
- */
130
- writeUInt32(value) {
131
- // istanbul ignore next
132
- if (performCheck) {
133
- (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 4, "not enough space in buffer");
134
- (0, node_opcua_assert_1.assert)(isFinite(value));
135
- (0, node_opcua_assert_1.assert)(value >= 0 && value <= MAXUINT32);
136
- }
137
- this.buffer.writeUInt32LE(value, this.length);
138
- this.length += 4;
139
- /*
140
- assert(this.buffer[this.length - 4] === value % 256);
141
- assert(this.buffer[this.length - 3] === (value >>> 8) % 256);
142
- assert(this.buffer[this.length - 2] === (value >>> 16) % 256);
143
- assert(this.buffer[this.length - 1] === (value >>> 24) % 256);
144
- */
145
- }
146
- /**
147
- * write a single 32 bit floating number to the stream.
148
- * @param value the value to write
149
- */
150
- writeFloat(value) {
151
- // istanbul ignore next
152
- if (performCheck) {
153
- (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 4, "not enough space in buffer");
154
- }
155
- this.buffer.writeFloatLE(value, this.length);
156
- this.length += 4;
157
- }
158
- /**
159
- * write a single 64 bit floating number to the stream.
160
- * @param value the value to write
161
- */
162
- writeDouble(value) {
163
- // istanbul ignore next
164
- if (performCheck) {
165
- (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 8, "not enough space in buffer");
166
- }
167
- this.buffer.writeDoubleLE(value, this.length);
168
- this.length += 8;
169
- }
170
- /**
171
- * @param arrayBuf a buffer or byte array write
172
- * @param offset the offset position (default =0)
173
- * @param length the number of byte to write
174
- */
175
- writeArrayBuffer(arrayBuf, offset = 0, length = 0) {
176
- // istanbul ignore next
177
- if (performCheck) {
178
- (0, node_opcua_assert_1.assert)(arrayBuf instanceof ArrayBuffer);
179
- }
180
- const byteArr = new Uint8Array(arrayBuf);
181
- const n = (length || byteArr.length) + offset;
182
- for (let i = offset; i < n; i++) {
183
- this.buffer[this.length++] = byteArr[i];
184
- }
185
- }
186
- // writeArrayBuffer(arrayBuf, offset, length) {
187
- // offset = offset || 0;
188
- //
189
- // assert(arrayBuf instanceof ArrayBuffer);
190
- // const byteArr = new Uint8Array(arrayBuf);
191
- // length = length || byteArr.length;
192
- // if (length === 0) {
193
- // return;
194
- // }
195
- // this.length += my_memcpy(this.buffer, this.length, byteArr, offset, offset + length);
196
- // }
197
- /**
198
- * read a single signed byte (8 bits) from the stream.
199
- * @return the value read
200
- */
201
- readByte() {
202
- const retVal = this.buffer.readInt8(this.length);
203
- this.length += 1;
204
- return retVal;
205
- }
206
- readInt8() {
207
- return this.readByte();
208
- }
209
- /**
210
- * read a single unsigned byte (8 bits) from the stream.
211
- */
212
- readUInt8() {
213
- // istanbul ignore next
214
- if (performCheck) {
215
- (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 1);
216
- }
217
- const retVal = this.buffer.readUInt8(this.length);
218
- this.length += 1;
219
- return retVal;
220
- }
221
- /**
222
- * read a single signed 16-bit integer from the stream.
223
- */
224
- readInt16() {
225
- const retVal = this.buffer.readInt16LE(this.length);
226
- this.length += 2;
227
- return retVal;
228
- }
229
- /**
230
- * read a single unsigned 16-bit integer from the stream.
231
- */
232
- readUInt16() {
233
- const retVal = this.buffer.readUInt16LE(this.length);
234
- this.length += 2;
235
- return retVal;
236
- }
237
- /**
238
- * read a single signed 32-bit integer from the stream.
239
- */
240
- readInteger() {
241
- const retVal = this.buffer.readInt32LE(this.length);
242
- this.length += 4;
243
- return retVal;
244
- }
245
- /**
246
- * read a single unsigned 32-bit integer from the stream.
247
- */
248
- readUInt32() {
249
- const retVal = this.buffer.readUInt32LE(this.length);
250
- this.length += 4;
251
- return retVal;
252
- }
253
- /**
254
- * read a single 32-bit floating point number from the stream.
255
- */
256
- readFloat() {
257
- const retVal = this.buffer.readFloatLE(this.length);
258
- this.length += 4;
259
- return retVal;
260
- }
261
- /**
262
- * read a single 64-bit floating point number from the stream.
263
- */
264
- readDouble() {
265
- const retVal = this.buffer.readDoubleLE(this.length);
266
- this.length += 8;
267
- return retVal;
268
- }
269
- /**
270
- * write a byte stream to the stream.
271
- * The method writes the length of the byte array into the stream as a 32 bits integer before the byte stream.
272
- *
273
- * @param buf the buffer to write.
274
- */
275
- writeByteStream(buf) {
276
- if (!buf) {
277
- this.writeInteger(-1);
278
- return;
279
- }
280
- (0, node_opcua_assert_1.assert)(buf instanceof Buffer);
281
- this.writeInteger(buf.length);
282
- // make sure there is enough room in destination buffer
283
- const remainingBytes = this.buffer.length - this.length;
284
- /* istanbul ignore next */
285
- if (remainingBytes < buf.length) {
286
- throw new Error("BinaryStream.writeByteStream error : not enough bytes left in buffer : bufferLength is " +
287
- buf.length +
288
- " but only " +
289
- remainingBytes +
290
- " left");
291
- }
292
- buf.copy(this.buffer, this.length, 0, buf.length);
293
- this.length += buf.length;
294
- }
295
- writeString(value) {
296
- if (value === undefined || value === null) {
297
- this.writeUInt32(0xffffffff);
298
- return;
299
- }
300
- const byteLength = calculateByteLength(value);
301
- this.writeInteger(byteLength);
302
- if (byteLength === 0) {
303
- return;
304
- }
305
- // make sure there is enough room in destination buffer
306
- const remainingBytes = this.buffer.length - this.length;
307
- /* istanbul ignore next */
308
- if (remainingBytes < byteLength) {
309
- throw new Error("BinaryStream.writeByteStream error : not enough bytes left in buffer : bufferLength is " +
310
- byteLength +
311
- " but only " +
312
- remainingBytes +
313
- " left");
314
- }
315
- this.buffer.write(value, this.length);
316
- this.length += byteLength;
317
- }
318
- // readArrayBuffer(length: number): ArrayBuffer {
319
- // assert(this.length + length <= this.buffer.length, "not enough bytes in buffer");
320
- // const byteArr = new Uint8Array(new ArrayBuffer(length));
321
- // my_memcpy(byteArr, 0, this.buffer, this.length, this.length + length);
322
- // this.length += length;
323
- // return byteArr;
324
- // }
325
- /**
326
- * @method readArrayBuffer
327
- * @param length
328
- */
329
- readArrayBuffer(length) {
330
- if (length > BinaryStream.maxByteStringLength) {
331
- throw new Error(`maxStringLength(${BinaryStream.maxByteStringLength}) has been exceeded in BinaryStream.readArrayBuffer len=${length}`);
332
- }
333
- // istanbul ignore next
334
- if (performCheck) {
335
- (0, node_opcua_assert_1.assert)(this.length + length <= this.buffer.length, "not enough bytes in buffer");
336
- }
337
- const slice = this.buffer.subarray(this.length, this.length + length);
338
- // istanbul ignore next
339
- if (performCheck) {
340
- (0, node_opcua_assert_1.assert)(slice.length === length);
341
- }
342
- const byteArr = new Uint8Array(slice);
343
- // istanbul ignore next
344
- if (performCheck) {
345
- (0, node_opcua_assert_1.assert)(byteArr.length === length);
346
- }
347
- this.length += length;
348
- return byteArr;
349
- }
350
- /**
351
- * read a byte stream to the stream.
352
- * The method reads the length of the byte array from the stream as a 32 bits integer
353
- * before reading the byte stream.
354
- *
355
- */
356
- readByteStream() {
357
- const bufLen = this.readUInt32();
358
- if (bufLen === 0xffffffff) {
359
- return null;
360
- }
361
- if (bufLen === 0) {
362
- return zeroLengthBuffer;
363
- }
364
- if (bufLen > BinaryStream.maxByteStringLength) {
365
- throw new Error(`maxStringLength(${BinaryStream.maxByteStringLength}) has been exceeded in BinaryStream.readArrayBuffer len=${bufLen}`);
366
- }
367
- // check that there is enough space in the buffer
368
- const remainingBytes = this.buffer.length - this.length;
369
- // istanbul ignore next
370
- if (remainingBytes < bufLen) {
371
- throw new Error("BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " +
372
- bufLen +
373
- " but only " +
374
- remainingBytes +
375
- " left");
376
- }
377
- // create a shared memory buffer ! for speed
378
- const buf = this.buffer.subarray(this.length, this.length + bufLen);
379
- this.length += bufLen;
380
- return buf;
381
- }
382
- readString() {
383
- const bufLen = this.readUInt32();
384
- if (bufLen === 0xffffffff) {
385
- return null;
386
- }
387
- if (bufLen === 0) {
388
- return "";
389
- }
390
- if (bufLen > BinaryStream.maxStringLength) {
391
- throw new Error(`maxStringLength(${BinaryStream.maxStringLength}) has been exceeded in BinaryStream.readString len=${bufLen}`);
392
- }
393
- // check that there is enough space in the buffer
394
- const remainingBytes = this.buffer.length - this.length;
395
- // istanbul ignore next
396
- if (remainingBytes < bufLen) {
397
- throw new Error("BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " +
398
- bufLen +
399
- " but only " +
400
- remainingBytes +
401
- " left");
402
- }
403
- const str = this.buffer.toString("utf-8", this.length, this.length + bufLen);
404
- this.length += bufLen;
405
- return str;
406
- }
407
- }
408
- exports.BinaryStream = BinaryStream;
409
- BinaryStream.maxByteStringLength = 16 * 1024 * 1024;
410
- BinaryStream.maxStringLength = 16 * 1024 * 1024;
411
- /**
412
- * @function calculateByteLength
413
- * calculate the size in bytes of a utf8 string
414
- * @param str {String}
415
- * @internal
416
- */
417
- function calculateByteLength(str) {
418
- // returns the byte length of an utf8 string
419
- let s = str.length;
420
- for (let i = s - 1; i >= 0; i--) {
421
- const code = str.charCodeAt(i);
422
- if (code > 0x7f && code <= 0x7ff) {
423
- s++;
424
- }
425
- else if (code > 0x7ff && code <= 0xffff) {
426
- s += 2;
427
- }
428
- if (code >= 0xdc00 && code <= 0xdfff) {
429
- // trail surrogate
430
- i--;
431
- }
432
- }
433
- return s;
434
- }
435
- exports.calculateByteLength = calculateByteLength;
436
- const zeroLengthBuffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(0);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calculateByteLength = exports.BinaryStream = void 0;
4
+ /**
5
+ * @module node-opcua-binary-stream
6
+ */
7
+ require("util");
8
+ const node_opcua_assert_1 = require("node-opcua-assert");
9
+ const node_opcua_buffer_utils_1 = require("node-opcua-buffer-utils");
10
+ const MAXUINT32 = 4294967295; // 2**32 -1;
11
+ const noAssert = false;
12
+ const performCheck = false;
13
+ /**
14
+ * a BinaryStream can be use to perform sequential read or write
15
+ * inside a buffer.
16
+ * The BinaryStream maintains a cursor up to date as the caller
17
+ * operates on the stream using the various read/write methods.
18
+ * It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
19
+ * It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
20
+ * convention.
21
+ *
22
+ * data can either be:
23
+ *
24
+ * * a Buffer , in this case the BinaryStream operates on this Buffer
25
+ * * null , in this case a BinaryStream with 1024 bytes is created
26
+ * * any data , in this case the object is converted into a binary buffer.
27
+ *
28
+ * example:
29
+ *
30
+ * ``` javascript
31
+ * var stream = new BinaryStream(32)
32
+ * ```
33
+ *
34
+ * @class BinaryStream
35
+ * @param {null|Buffer|Number} data
36
+ * @constructor
37
+ *
38
+ *
39
+ *
40
+ */
41
+ class BinaryStream {
42
+ constructor(data) {
43
+ if (data === undefined) {
44
+ this.buffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(1024);
45
+ }
46
+ else if (typeof data === "number") {
47
+ this.buffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(data);
48
+ }
49
+ else {
50
+ (0, node_opcua_assert_1.assert)(data instanceof Buffer);
51
+ this.buffer = data;
52
+ }
53
+ this.length = 0;
54
+ }
55
+ /**
56
+ * set the cursor to the begining of the stream
57
+ * @method BinaryStream.rewind
58
+ */
59
+ rewind() {
60
+ this.length = 0;
61
+ }
62
+ /**
63
+ * write a single signed byte (8 bits) to the stream.
64
+ * value must be in the range of [-127,128]
65
+ * @param value the value to write
66
+ */
67
+ writeInt8(value) {
68
+ // istanbul ignore next
69
+ if (performCheck) {
70
+ (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 1, "not enough space in buffer");
71
+ (0, node_opcua_assert_1.assert)(value >= -128 && value < 128);
72
+ }
73
+ this.buffer.writeInt8(value, this.length);
74
+ this.length += 1;
75
+ }
76
+ /**
77
+ * write a single unsigned byte (8 bits) to the stream.
78
+ * @param value the value to write
79
+ */
80
+ writeUInt8(value) {
81
+ // istanbul ignore next
82
+ if (performCheck) {
83
+ (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 1, "not enough space in buffer");
84
+ (0, node_opcua_assert_1.assert)(value >= 0 && value < 256, " writeUInt8 : out of bound ");
85
+ }
86
+ this.buffer.writeUInt8(value, this.length);
87
+ this.length += 1;
88
+ }
89
+ /**
90
+ * write a single 16 bit signed integer to the stream.
91
+ * @param value the value to write
92
+ */
93
+ writeInt16(value) {
94
+ // istanbul ignore next
95
+ if (performCheck) {
96
+ (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 2, "not enough space in buffer");
97
+ }
98
+ this.buffer.writeInt16LE(value, this.length);
99
+ this.length += 2;
100
+ }
101
+ /**
102
+ * write a single 16 bit unsigned integer to the stream.
103
+ * @param value the value to write
104
+ */
105
+ writeUInt16(value) {
106
+ // istanbul ignore next
107
+ if (performCheck) {
108
+ (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 2, "not enough space in buffer");
109
+ }
110
+ this.buffer.writeUInt16LE(value, this.length);
111
+ this.length += 2;
112
+ }
113
+ /**
114
+ * write a single 32 bit signed integer to the stream.
115
+ * @param value the value to write
116
+ */
117
+ writeInteger(value) {
118
+ // istanbul ignore next
119
+ if (performCheck) {
120
+ (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 4, "not enough space in buffer");
121
+ }
122
+ this.buffer.writeInt32LE(value, this.length);
123
+ this.length += 4;
124
+ }
125
+ /**
126
+ * write a single 32 bit unsigned integer to the stream.
127
+ *
128
+ * @param value the value to write
129
+ */
130
+ writeUInt32(value) {
131
+ // istanbul ignore next
132
+ if (performCheck) {
133
+ (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 4, "not enough space in buffer");
134
+ (0, node_opcua_assert_1.assert)(isFinite(value));
135
+ (0, node_opcua_assert_1.assert)(value >= 0 && value <= MAXUINT32);
136
+ }
137
+ this.buffer.writeUInt32LE(value, this.length);
138
+ this.length += 4;
139
+ /*
140
+ assert(this.buffer[this.length - 4] === value % 256);
141
+ assert(this.buffer[this.length - 3] === (value >>> 8) % 256);
142
+ assert(this.buffer[this.length - 2] === (value >>> 16) % 256);
143
+ assert(this.buffer[this.length - 1] === (value >>> 24) % 256);
144
+ */
145
+ }
146
+ /**
147
+ * write a single 32 bit floating number to the stream.
148
+ * @param value the value to write
149
+ */
150
+ writeFloat(value) {
151
+ // istanbul ignore next
152
+ if (performCheck) {
153
+ (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 4, "not enough space in buffer");
154
+ }
155
+ this.buffer.writeFloatLE(value, this.length);
156
+ this.length += 4;
157
+ }
158
+ /**
159
+ * write a single 64 bit floating number to the stream.
160
+ * @param value the value to write
161
+ */
162
+ writeDouble(value) {
163
+ // istanbul ignore next
164
+ if (performCheck) {
165
+ (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 8, "not enough space in buffer");
166
+ }
167
+ this.buffer.writeDoubleLE(value, this.length);
168
+ this.length += 8;
169
+ }
170
+ /**
171
+ * @param arrayBuf a buffer or byte array write
172
+ * @param offset the offset position (default =0)
173
+ * @param length the number of byte to write
174
+ */
175
+ writeArrayBuffer(arrayBuf, offset = 0, length = 0) {
176
+ // istanbul ignore next
177
+ if (performCheck) {
178
+ (0, node_opcua_assert_1.assert)(arrayBuf instanceof ArrayBuffer);
179
+ }
180
+ const byteArr = new Uint8Array(arrayBuf);
181
+ const n = (length || byteArr.length) + offset;
182
+ for (let i = offset; i < n; i++) {
183
+ this.buffer[this.length++] = byteArr[i];
184
+ }
185
+ }
186
+ // writeArrayBuffer(arrayBuf, offset, length) {
187
+ // offset = offset || 0;
188
+ //
189
+ // assert(arrayBuf instanceof ArrayBuffer);
190
+ // const byteArr = new Uint8Array(arrayBuf);
191
+ // length = length || byteArr.length;
192
+ // if (length === 0) {
193
+ // return;
194
+ // }
195
+ // this.length += my_memcpy(this.buffer, this.length, byteArr, offset, offset + length);
196
+ // }
197
+ /**
198
+ * read a single signed byte (8 bits) from the stream.
199
+ * @return the value read
200
+ */
201
+ readByte() {
202
+ const retVal = this.buffer.readInt8(this.length);
203
+ this.length += 1;
204
+ return retVal;
205
+ }
206
+ readInt8() {
207
+ return this.readByte();
208
+ }
209
+ /**
210
+ * read a single unsigned byte (8 bits) from the stream.
211
+ */
212
+ readUInt8() {
213
+ // istanbul ignore next
214
+ if (performCheck) {
215
+ (0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 1);
216
+ }
217
+ const retVal = this.buffer.readUInt8(this.length);
218
+ this.length += 1;
219
+ return retVal;
220
+ }
221
+ /**
222
+ * read a single signed 16-bit integer from the stream.
223
+ */
224
+ readInt16() {
225
+ const retVal = this.buffer.readInt16LE(this.length);
226
+ this.length += 2;
227
+ return retVal;
228
+ }
229
+ /**
230
+ * read a single unsigned 16-bit integer from the stream.
231
+ */
232
+ readUInt16() {
233
+ const retVal = this.buffer.readUInt16LE(this.length);
234
+ this.length += 2;
235
+ return retVal;
236
+ }
237
+ /**
238
+ * read a single signed 32-bit integer from the stream.
239
+ */
240
+ readInteger() {
241
+ const retVal = this.buffer.readInt32LE(this.length);
242
+ this.length += 4;
243
+ return retVal;
244
+ }
245
+ /**
246
+ * read a single unsigned 32-bit integer from the stream.
247
+ */
248
+ readUInt32() {
249
+ const retVal = this.buffer.readUInt32LE(this.length);
250
+ this.length += 4;
251
+ return retVal;
252
+ }
253
+ /**
254
+ * read a single 32-bit floating point number from the stream.
255
+ */
256
+ readFloat() {
257
+ const retVal = this.buffer.readFloatLE(this.length);
258
+ this.length += 4;
259
+ return retVal;
260
+ }
261
+ /**
262
+ * read a single 64-bit floating point number from the stream.
263
+ */
264
+ readDouble() {
265
+ const retVal = this.buffer.readDoubleLE(this.length);
266
+ this.length += 8;
267
+ return retVal;
268
+ }
269
+ /**
270
+ * write a byte stream to the stream.
271
+ * The method writes the length of the byte array into the stream as a 32 bits integer before the byte stream.
272
+ *
273
+ * @param buf the buffer to write.
274
+ */
275
+ writeByteStream(buf) {
276
+ if (!buf) {
277
+ this.writeInteger(-1);
278
+ return;
279
+ }
280
+ (0, node_opcua_assert_1.assert)(buf instanceof Buffer);
281
+ this.writeInteger(buf.length);
282
+ // make sure there is enough room in destination buffer
283
+ const remainingBytes = this.buffer.length - this.length;
284
+ /* istanbul ignore next */
285
+ if (remainingBytes < buf.length) {
286
+ throw new Error("BinaryStream.writeByteStream error : not enough bytes left in buffer : bufferLength is " +
287
+ buf.length +
288
+ " but only " +
289
+ remainingBytes +
290
+ " left");
291
+ }
292
+ buf.copy(this.buffer, this.length, 0, buf.length);
293
+ this.length += buf.length;
294
+ }
295
+ writeString(value) {
296
+ if (value === undefined || value === null) {
297
+ this.writeUInt32(0xffffffff);
298
+ return;
299
+ }
300
+ const byteLength = calculateByteLength(value);
301
+ this.writeInteger(byteLength);
302
+ if (byteLength === 0) {
303
+ return;
304
+ }
305
+ // make sure there is enough room in destination buffer
306
+ const remainingBytes = this.buffer.length - this.length;
307
+ /* istanbul ignore next */
308
+ if (remainingBytes < byteLength) {
309
+ throw new Error("BinaryStream.writeByteStream error : not enough bytes left in buffer : bufferLength is " +
310
+ byteLength +
311
+ " but only " +
312
+ remainingBytes +
313
+ " left");
314
+ }
315
+ this.buffer.write(value, this.length);
316
+ this.length += byteLength;
317
+ }
318
+ // readArrayBuffer(length: number): ArrayBuffer {
319
+ // assert(this.length + length <= this.buffer.length, "not enough bytes in buffer");
320
+ // const byteArr = new Uint8Array(new ArrayBuffer(length));
321
+ // my_memcpy(byteArr, 0, this.buffer, this.length, this.length + length);
322
+ // this.length += length;
323
+ // return byteArr;
324
+ // }
325
+ /**
326
+ * @method readArrayBuffer
327
+ * @param length
328
+ */
329
+ readArrayBuffer(length) {
330
+ if (length > BinaryStream.maxByteStringLength) {
331
+ throw new Error(`maxStringLength(${BinaryStream.maxByteStringLength}) has been exceeded in BinaryStream.readArrayBuffer len=${length}`);
332
+ }
333
+ // istanbul ignore next
334
+ if (performCheck) {
335
+ (0, node_opcua_assert_1.assert)(this.length + length <= this.buffer.length, "not enough bytes in buffer");
336
+ }
337
+ const slice = this.buffer.subarray(this.length, this.length + length);
338
+ // istanbul ignore next
339
+ if (performCheck) {
340
+ (0, node_opcua_assert_1.assert)(slice.length === length);
341
+ }
342
+ const byteArr = new Uint8Array(slice);
343
+ // istanbul ignore next
344
+ if (performCheck) {
345
+ (0, node_opcua_assert_1.assert)(byteArr.length === length);
346
+ }
347
+ this.length += length;
348
+ return byteArr;
349
+ }
350
+ /**
351
+ * read a byte stream to the stream.
352
+ * The method reads the length of the byte array from the stream as a 32 bits integer
353
+ * before reading the byte stream.
354
+ *
355
+ */
356
+ readByteStream() {
357
+ const bufLen = this.readUInt32();
358
+ if (bufLen === 0xffffffff) {
359
+ return null;
360
+ }
361
+ if (bufLen === 0) {
362
+ return zeroLengthBuffer;
363
+ }
364
+ if (bufLen > BinaryStream.maxByteStringLength) {
365
+ throw new Error(`maxStringLength(${BinaryStream.maxByteStringLength}) has been exceeded in BinaryStream.readArrayBuffer len=${bufLen}`);
366
+ }
367
+ // check that there is enough space in the buffer
368
+ const remainingBytes = this.buffer.length - this.length;
369
+ // istanbul ignore next
370
+ if (remainingBytes < bufLen) {
371
+ throw new Error("BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " +
372
+ bufLen +
373
+ " but only " +
374
+ remainingBytes +
375
+ " left");
376
+ }
377
+ // create a shared memory buffer ! for speed
378
+ const buf = this.buffer.subarray(this.length, this.length + bufLen);
379
+ this.length += bufLen;
380
+ return buf;
381
+ }
382
+ readString() {
383
+ const bufLen = this.readUInt32();
384
+ if (bufLen === 0xffffffff) {
385
+ return null;
386
+ }
387
+ if (bufLen === 0) {
388
+ return "";
389
+ }
390
+ if (bufLen > BinaryStream.maxStringLength) {
391
+ throw new Error(`maxStringLength(${BinaryStream.maxStringLength}) has been exceeded in BinaryStream.readString len=${bufLen}`);
392
+ }
393
+ // check that there is enough space in the buffer
394
+ const remainingBytes = this.buffer.length - this.length;
395
+ // istanbul ignore next
396
+ if (remainingBytes < bufLen) {
397
+ throw new Error("BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " +
398
+ bufLen +
399
+ " but only " +
400
+ remainingBytes +
401
+ " left");
402
+ }
403
+ const str = this.buffer.toString("utf-8", this.length, this.length + bufLen);
404
+ this.length += bufLen;
405
+ return str;
406
+ }
407
+ }
408
+ BinaryStream.maxByteStringLength = 16 * 1024 * 1024;
409
+ BinaryStream.maxStringLength = 16 * 1024 * 1024;
410
+ exports.BinaryStream = BinaryStream;
411
+ /**
412
+ * @function calculateByteLength
413
+ * calculate the size in bytes of a utf8 string
414
+ * @param str {String}
415
+ * @internal
416
+ */
417
+ function calculateByteLength(str) {
418
+ // returns the byte length of an utf8 string
419
+ let s = str.length;
420
+ for (let i = s - 1; i >= 0; i--) {
421
+ const code = str.charCodeAt(i);
422
+ if (code > 0x7f && code <= 0x7ff) {
423
+ s++;
424
+ }
425
+ else if (code > 0x7ff && code <= 0xffff) {
426
+ s += 2;
427
+ }
428
+ if (code >= 0xdc00 && code <= 0xdfff) {
429
+ // trail surrogate
430
+ i--;
431
+ }
432
+ }
433
+ return s;
434
+ }
435
+ exports.calculateByteLength = calculateByteLength;
436
+ const zeroLengthBuffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(0);
437
437
  //# sourceMappingURL=binaryStream.js.map