node-opcua-binary-stream
Advanced tools
Comparing version 0.4.0 to 0.4.2
@@ -6,3 +6,3 @@ "use strict"; | ||
*/ | ||
const assert = require("node-opcua-assert").assert; | ||
const node_opcua_assert_1 = require("node-opcua-assert"); | ||
require("util"); | ||
@@ -35,3 +35,3 @@ const node_opcua_buffer_utils_1 = require("node-opcua-buffer-utils"); | ||
* @class BinaryStream | ||
* @param {null|Buffer|number} data | ||
* @param {null|Buffer|Number} data | ||
* @constructor | ||
@@ -51,3 +51,3 @@ * | ||
else { | ||
assert(data instanceof Buffer); | ||
node_opcua_assert_1.default(data instanceof Buffer); | ||
this._buffer = data; | ||
@@ -69,7 +69,9 @@ } | ||
* @method writeInt8 | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeInt8(value) { | ||
!performCheck || assert(this._buffer.length >= this.length + 1, "not enough space in buffer"); | ||
!performCheck || assert(value >= -128 && value < 128); | ||
if (performCheck) | ||
node_opcua_assert_1.default(this._buffer.length >= this.length + 1, "not enough space in buffer"); | ||
if (performCheck) | ||
node_opcua_assert_1.default(value >= -128 && value < 128); | ||
this._buffer.writeInt8(value, this.length, noAssert); | ||
@@ -81,7 +83,9 @@ this.length += 1; | ||
* @method writeUInt8 | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeUInt8(value) { | ||
!performCheck || assert(this._buffer.length >= this.length + 1, "not enough space in buffer"); | ||
!performCheck || assert(value >= 0 && value < 256 && " writeUInt8 : out of bound "); | ||
if (performCheck) | ||
node_opcua_assert_1.default(this._buffer.length >= this.length + 1, "not enough space in buffer"); | ||
if (performCheck) | ||
node_opcua_assert_1.default(value >= 0 && value < 256, " writeUInt8 : out of bound "); | ||
this._buffer.writeUInt8(value, this.length, noAssert); | ||
@@ -93,6 +97,7 @@ this.length += 1; | ||
* @method writeInt16 | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeInt16(value) { | ||
!performCheck || assert(this._buffer.length >= this.length + 2, "not enough space in buffer"); | ||
if (performCheck) | ||
node_opcua_assert_1.default(this._buffer.length >= this.length + 2, "not enough space in buffer"); | ||
this._buffer.writeInt16LE(value, this.length, noAssert); | ||
@@ -104,6 +109,7 @@ this.length += 2; | ||
* @method writeUInt16 | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeUInt16(value) { | ||
!performCheck || assert(this._buffer.length >= this.length + 2, "not enough space in buffer"); | ||
if (performCheck) | ||
node_opcua_assert_1.default(this._buffer.length >= this.length + 2, "not enough space in buffer"); | ||
this._buffer.writeUInt16LE(value, this.length, noAssert); | ||
@@ -115,6 +121,7 @@ this.length += 2; | ||
* @method writeInteger | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeInteger(value) { | ||
!performCheck || assert(this._buffer.length >= this.length + 4, "not enough space in buffer"); | ||
if (performCheck) | ||
node_opcua_assert_1.default(this._buffer.length >= this.length + 4, "not enough space in buffer"); | ||
this._buffer.writeInt32LE(value, this.length, noAssert); | ||
@@ -126,8 +133,11 @@ this.length += 4; | ||
* @method writeUInt32 | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeUInt32(value) { | ||
!performCheck || assert(this._buffer.length >= this.length + 4, "not enough space in buffer"); | ||
!performCheck || assert(underscore_1.isFinite(value)); | ||
!performCheck || assert(value >= 0 && value <= MAXUINT32); | ||
if (performCheck) | ||
node_opcua_assert_1.default(this._buffer.length >= this.length + 4, "not enough space in buffer"); | ||
if (performCheck) | ||
node_opcua_assert_1.default(underscore_1.isFinite(value)); | ||
if (performCheck) | ||
node_opcua_assert_1.default(value >= 0 && value <= MAXUINT32); | ||
this._buffer.writeUInt32LE(value, this.length, noAssert); | ||
@@ -145,6 +155,7 @@ this.length += 4; | ||
* @method writeFloat | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeFloat(value) { | ||
!performCheck || assert(this._buffer.length >= this.length + 4, "not enough space in buffer"); | ||
if (performCheck) | ||
node_opcua_assert_1.default(this._buffer.length >= this.length + 4, "not enough space in buffer"); | ||
this._buffer.writeFloatLE(value, this.length, noAssert); | ||
@@ -159,3 +170,4 @@ this.length += 4; | ||
writeDouble(value) { | ||
!performCheck || assert(this._buffer.length >= this.length + 8, "not enough space in buffer"); | ||
if (performCheck) | ||
node_opcua_assert_1.default(this._buffer.length >= this.length + 8, "not enough space in buffer"); | ||
this._buffer.writeDoubleLE(value, this.length, noAssert); | ||
@@ -170,4 +182,5 @@ this.length += 8; | ||
*/ | ||
writeArrayBuffer(arrayBuf, offset = 0, length) { | ||
!performCheck || assert(arrayBuf instanceof ArrayBuffer); | ||
writeArrayBuffer(arrayBuf, offset = 0, length = 0) { | ||
if (performCheck) | ||
node_opcua_assert_1.default(arrayBuf instanceof ArrayBuffer); | ||
const byteArr = new Uint8Array(arrayBuf); | ||
@@ -209,3 +222,4 @@ const n = (length || byteArr.length) + offset; | ||
readUInt8() { | ||
!performCheck || assert(this._buffer.length >= this.length + 1); | ||
if (performCheck) | ||
node_opcua_assert_1.default(this._buffer.length >= this.length + 1); | ||
const retVal = this._buffer.readUInt8(this.length, noAssert); | ||
@@ -258,3 +272,3 @@ this.length += 1; | ||
* @method readFloat | ||
* @return {number} the value read from the stream | ||
* @return the value read from the stream | ||
*/ | ||
@@ -269,3 +283,3 @@ readFloat() { | ||
* @method readDouble | ||
* @return {Number} the value read from the stream | ||
* @return the value read from the stream | ||
*/ | ||
@@ -290,12 +304,12 @@ readDouble() { | ||
} | ||
assert(buf instanceof Buffer); | ||
node_opcua_assert_1.default(buf instanceof Buffer); | ||
this.writeInteger(buf.length); | ||
// make sure there is enough room in destination buffer | ||
const remaining_bytes = this._buffer.length - this.length; | ||
const remainingBytes = this._buffer.length - this.length; | ||
/* istanbul ignore next */ | ||
if (remaining_bytes < buf.length) { | ||
if (remainingBytes < buf.length) { | ||
throw new Error("BinaryStream.writeByteStream error : not enough bytes left in buffer : bufferLength is " + | ||
buf.length + | ||
" but only " + | ||
remaining_bytes + | ||
remainingBytes + | ||
" left"); | ||
@@ -314,9 +328,9 @@ } | ||
// make sure there is enough room in destination buffer | ||
const remaining_bytes = this._buffer.length - this.length; | ||
const remainingBytes = this._buffer.length - this.length; | ||
/* istanbul ignore next */ | ||
if (remaining_bytes < byteLength) { | ||
if (remainingBytes < byteLength) { | ||
throw new Error("BinaryStream.writeByteStream error : not enough bytes left in buffer : bufferLength is " + | ||
byteLength + | ||
" but only " + | ||
remaining_bytes + | ||
remainingBytes + | ||
" left"); | ||
@@ -342,7 +356,10 @@ } | ||
readArrayBuffer(length) { | ||
!performCheck || assert(this.length + length <= this._buffer.length, "not enough bytes in buffer"); | ||
if (performCheck) | ||
node_opcua_assert_1.default(this.length + length <= this._buffer.length, "not enough bytes in buffer"); | ||
const slice = this._buffer.slice(this.length, this.length + length); | ||
!performCheck || assert(slice.length === length); | ||
if (performCheck) | ||
node_opcua_assert_1.default(slice.length === length); | ||
const byteArr = new Uint8Array(slice); | ||
!performCheck || assert(byteArr.length === length); | ||
if (performCheck) | ||
node_opcua_assert_1.default(byteArr.length === length); | ||
this.length += length; | ||
@@ -367,11 +384,11 @@ return byteArr; | ||
// check that there is enough space in the buffer | ||
const remaining_bytes = this._buffer.length - this.length; | ||
if (remaining_bytes < bufLen) { | ||
const remainingBytes = this._buffer.length - this.length; | ||
if (remainingBytes < bufLen) { | ||
throw new Error("BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " + | ||
bufLen + | ||
" but only " + | ||
remaining_bytes + | ||
remainingBytes + | ||
" left"); | ||
} | ||
//create a shared memory buffer ! for speed | ||
// create a shared memory buffer ! for speed | ||
const buf = this._buffer.slice(this.length, this.length + bufLen); | ||
@@ -390,8 +407,8 @@ this.length += bufLen; | ||
// check that there is enough space in the buffer | ||
const remaining_bytes = this._buffer.length - this.length; | ||
if (remaining_bytes < bufLen) { | ||
const remainingBytes = this._buffer.length - this.length; | ||
if (remainingBytes < bufLen) { | ||
throw new Error("BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " + | ||
bufLen + | ||
" but only " + | ||
remaining_bytes + | ||
remainingBytes + | ||
" left"); | ||
@@ -421,3 +438,3 @@ } | ||
if (code >= 0xdc00 && code <= 0xdfff) { | ||
//trail surrogate | ||
// trail surrogate | ||
i--; | ||
@@ -475,3 +492,3 @@ } | ||
offset = offset || 0; | ||
assert(arrayBuf instanceof ArrayBuffer); | ||
node_opcua_assert_1.default(arrayBuf instanceof ArrayBuffer); | ||
this.length += byteLength || arrayBuf.byteLength; | ||
@@ -488,8 +505,8 @@ } | ||
} | ||
writeString(string) { | ||
if (string === undefined || string === null) { | ||
writeString(str) { | ||
if (str === undefined || str === null) { | ||
this.writeUInt32(-1); | ||
return; | ||
} | ||
const bufLength = calculateByteLength(string); | ||
const bufLength = calculateByteLength(str); | ||
this.writeUInt32(bufLength); | ||
@@ -496,0 +513,0 @@ this.length += bufLength; |
{ | ||
"name": "node-opcua-binary-stream", | ||
"version": "0.4.0", | ||
"version": "0.4.2", | ||
"description": "pure nodejs OPCUA SDK - module -binary-stream", | ||
@@ -14,7 +14,8 @@ "main": "dist/binaryStream.js", | ||
"node-opcua-assert": "^0.4.0", | ||
"node-opcua-buffer-utils": "^0.4.0" | ||
"node-opcua-buffer-utils": "^0.4.2", | ||
"underscore": "^1.9.1" | ||
}, | ||
"devDependencies": { | ||
"@types/underscore": "^1.8.8", | ||
"node-opcua-benchmarker": "^0.4.0", | ||
"node-opcua-benchmarker": "^0.4.2", | ||
"should": "13.2.1" | ||
@@ -21,0 +22,0 @@ }, |
/** | ||
* @module opcua.miscellaneous | ||
*/ | ||
const assert = require("node-opcua-assert").assert; | ||
import assert from "node-opcua-assert"; | ||
import "util"; | ||
@@ -34,3 +34,3 @@ import {createFastUninitializedBuffer} from "node-opcua-buffer-utils"; | ||
* @class BinaryStream | ||
* @param {null|Buffer|number} data | ||
* @param {null|Buffer|Number} data | ||
* @constructor | ||
@@ -45,3 +45,3 @@ * | ||
constructor(data: any) { | ||
constructor(data: undefined | Buffer | number) { | ||
if (data === undefined) { | ||
@@ -71,7 +71,7 @@ this._buffer = createFastUninitializedBuffer(1024); | ||
* @method writeInt8 | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeInt8(value: number): void { | ||
!performCheck || assert(this._buffer.length >= this.length + 1, "not enough space in buffer"); | ||
!performCheck || assert(value >= -128 && value < 128); | ||
if (performCheck) assert(this._buffer.length >= this.length + 1, "not enough space in buffer"); | ||
if (performCheck) assert(value >= -128 && value < 128); | ||
this._buffer.writeInt8(value, this.length, noAssert); | ||
@@ -84,7 +84,7 @@ this.length += 1; | ||
* @method writeUInt8 | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeUInt8(value: number): void { | ||
!performCheck || assert(this._buffer.length >= this.length + 1, "not enough space in buffer"); | ||
!performCheck || assert(value >= 0 && value < 256 && " writeUInt8 : out of bound "); | ||
if (performCheck) assert(this._buffer.length >= this.length + 1, "not enough space in buffer"); | ||
if (performCheck) assert(value >= 0 && value < 256, " writeUInt8 : out of bound "); | ||
this._buffer.writeUInt8(value, this.length, noAssert); | ||
@@ -97,6 +97,6 @@ this.length += 1; | ||
* @method writeInt16 | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeInt16(value: number): void { | ||
!performCheck || assert(this._buffer.length >= this.length + 2, "not enough space in buffer"); | ||
if (performCheck) assert(this._buffer.length >= this.length + 2, "not enough space in buffer"); | ||
this._buffer.writeInt16LE(value, this.length, noAssert); | ||
@@ -109,6 +109,6 @@ this.length += 2; | ||
* @method writeUInt16 | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeUInt16(value: number): void { | ||
!performCheck || assert(this._buffer.length >= this.length + 2, "not enough space in buffer"); | ||
if (performCheck) assert(this._buffer.length >= this.length + 2, "not enough space in buffer"); | ||
this._buffer.writeUInt16LE(value, this.length, noAssert); | ||
@@ -121,6 +121,6 @@ this.length += 2; | ||
* @method writeInteger | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeInteger(value: number): void { | ||
!performCheck || assert(this._buffer.length >= this.length + 4, "not enough space in buffer"); | ||
if (performCheck) assert(this._buffer.length >= this.length + 4, "not enough space in buffer"); | ||
this._buffer.writeInt32LE(value, this.length, noAssert); | ||
@@ -133,8 +133,8 @@ this.length += 4; | ||
* @method writeUInt32 | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeUInt32(value: number): void { | ||
!performCheck || assert(this._buffer.length >= this.length + 4, "not enough space in buffer"); | ||
!performCheck || assert(isFinite(value)); | ||
!performCheck || assert(value >= 0 && value <= MAXUINT32); | ||
if (performCheck) assert(this._buffer.length >= this.length + 4, "not enough space in buffer"); | ||
if (performCheck) assert(isFinite(value)); | ||
if (performCheck) assert(value >= 0 && value <= MAXUINT32); | ||
this._buffer.writeUInt32LE(value, this.length, noAssert); | ||
@@ -153,6 +153,6 @@ this.length += 4; | ||
* @method writeFloat | ||
* @param {Number} value | ||
* @param value | ||
*/ | ||
writeFloat(value: number): void { | ||
!performCheck || assert(this._buffer.length >= this.length + 4, "not enough space in buffer"); | ||
if (performCheck) assert(this._buffer.length >= this.length + 4, "not enough space in buffer"); | ||
this._buffer.writeFloatLE(value, this.length, noAssert); | ||
@@ -168,3 +168,3 @@ this.length += 4; | ||
writeDouble(value: number): void { | ||
!performCheck || assert(this._buffer.length >= this.length + 8, "not enough space in buffer"); | ||
if (performCheck) assert(this._buffer.length >= this.length + 8, "not enough space in buffer"); | ||
this._buffer.writeDoubleLE(value, this.length, noAssert); | ||
@@ -180,4 +180,4 @@ this.length += 8; | ||
*/ | ||
writeArrayBuffer(arrayBuf: ArrayBuffer, offset: number = 0, length: number): void { | ||
!performCheck || assert(arrayBuf instanceof ArrayBuffer); | ||
writeArrayBuffer(arrayBuf: ArrayBuffer, offset=0, length=0): void { | ||
if (performCheck) assert(arrayBuf instanceof ArrayBuffer); | ||
const byteArr = new Uint8Array(arrayBuf); | ||
@@ -223,3 +223,3 @@ const n = (length || byteArr.length) + offset; | ||
readUInt8(): number { | ||
!performCheck || assert(this._buffer.length >= this.length + 1); | ||
if (performCheck) assert(this._buffer.length >= this.length + 1); | ||
const retVal = this._buffer.readUInt8(this.length, noAssert); | ||
@@ -277,3 +277,3 @@ this.length += 1; | ||
* @method readFloat | ||
* @return {number} the value read from the stream | ||
* @return the value read from the stream | ||
*/ | ||
@@ -289,3 +289,3 @@ readFloat(): number { | ||
* @method readDouble | ||
* @return {Number} the value read from the stream | ||
* @return the value read from the stream | ||
*/ | ||
@@ -314,6 +314,6 @@ readDouble(): number { | ||
// make sure there is enough room in destination buffer | ||
const remaining_bytes = this._buffer.length - this.length; | ||
const remainingBytes = this._buffer.length - this.length; | ||
/* istanbul ignore next */ | ||
if (remaining_bytes < buf.length) { | ||
if (remainingBytes < buf.length) { | ||
throw new Error( | ||
@@ -323,3 +323,3 @@ "BinaryStream.writeByteStream error : not enough bytes left in buffer : bufferLength is " + | ||
" but only " + | ||
remaining_bytes + | ||
remainingBytes + | ||
" left" | ||
@@ -340,5 +340,5 @@ ); | ||
// make sure there is enough room in destination buffer | ||
const remaining_bytes = this._buffer.length - this.length; | ||
const remainingBytes = this._buffer.length - this.length; | ||
/* istanbul ignore next */ | ||
if (remaining_bytes < byteLength) { | ||
if (remainingBytes < byteLength) { | ||
throw new Error( | ||
@@ -348,3 +348,3 @@ "BinaryStream.writeByteStream error : not enough bytes left in buffer : bufferLength is " + | ||
" but only " + | ||
remaining_bytes + | ||
remainingBytes + | ||
" left" | ||
@@ -372,7 +372,7 @@ ); | ||
readArrayBuffer(length: number): Uint8Array { | ||
!performCheck || assert(this.length + length <= this._buffer.length, "not enough bytes in buffer"); | ||
if (performCheck) assert(this.length + length <= this._buffer.length, "not enough bytes in buffer"); | ||
const slice = this._buffer.slice(this.length, this.length + length); | ||
!performCheck || assert(slice.length === length); | ||
if (performCheck) assert(slice.length === length); | ||
const byteArr = new Uint8Array(slice); | ||
!performCheck || assert(byteArr.length === length); | ||
if (performCheck) assert(byteArr.length === length); | ||
this.length += length; | ||
@@ -398,4 +398,4 @@ return byteArr; | ||
// check that there is enough space in the buffer | ||
const remaining_bytes = this._buffer.length - this.length; | ||
if (remaining_bytes < bufLen) { | ||
const remainingBytes = this._buffer.length - this.length; | ||
if (remainingBytes < bufLen) { | ||
throw new Error( | ||
@@ -405,7 +405,7 @@ "BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " + | ||
" but only " + | ||
remaining_bytes + | ||
remainingBytes + | ||
" left" | ||
); | ||
} | ||
//create a shared memory buffer ! for speed | ||
// create a shared memory buffer ! for speed | ||
const buf = this._buffer.slice(this.length, this.length + bufLen); | ||
@@ -425,4 +425,4 @@ this.length += bufLen; | ||
// check that there is enough space in the buffer | ||
const remaining_bytes = this._buffer.length - this.length; | ||
if (remaining_bytes < bufLen) { | ||
const remainingBytes = this._buffer.length - this.length; | ||
if (remainingBytes < bufLen) { | ||
throw new Error( | ||
@@ -432,3 +432,3 @@ "BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " + | ||
" but only " + | ||
remaining_bytes + | ||
remainingBytes + | ||
" left" | ||
@@ -459,3 +459,3 @@ ); | ||
if (code >= 0xdc00 && code <= 0xdfff) { | ||
//trail surrogate | ||
// trail surrogate | ||
i--; | ||
@@ -540,8 +540,8 @@ } | ||
writeString(string: string): void { | ||
if (string === undefined || string === null) { | ||
writeString(str: string): void { | ||
if (str === undefined || str === null) { | ||
this.writeUInt32(-1); | ||
return; | ||
} | ||
const bufLength = calculateByteLength(string); | ||
const bufLength = calculateByteLength(str); | ||
this.writeUInt32(bufLength); | ||
@@ -548,0 +548,0 @@ this.length += bufLength; |
@@ -44,16 +44,18 @@ "use strict"; | ||
const arr = new Int16Array(25); | ||
for (let i=0;i<25;i++) { arr[i] = 512+i; } | ||
for (let i = 0; i < 25; i++) { | ||
arr[i] = 512 + i; | ||
} | ||
console.log((new Uint8Array(arr.buffer)).join(" ")); | ||
stream.writeArrayBuffer(arr.buffer) | ||
console.log((new Uint8Array(arr.buffer)).join(" ")); | ||
stream.writeArrayBuffer(arr.buffer); | ||
// let's verify that a copy has been made | ||
// changing written array shall not affect innder buffer | ||
// changing written array shall not affect inner buffer | ||
stream._buffer[2*3].should.eql(3); | ||
stream._buffer[2*3]= 33; | ||
stream._buffer[2 * 3].should.eql(3); | ||
stream._buffer[2 * 3] = 33; | ||
arr[3].should.not.eql(33); | ||
arr[3].should.eql(512+3); | ||
stream._buffer[2*3]= 3; | ||
arr[3].should.eql(512 + 3); | ||
stream._buffer[2 * 3] = 3; | ||
@@ -68,9 +70,9 @@ stream.rewind(); | ||
arr2[3].should.eql(512+3); | ||
arr2[3].should.eql(512 + 3); | ||
stream._buffer[2*3].should.eql(3); | ||
stream._buffer[2*3]= 33; | ||
stream._buffer[2 * 3].should.eql(3); | ||
stream._buffer[2 * 3] = 33; | ||
arr2[3].should.not.eql(33); | ||
arr2[3].should.eql(512+3); | ||
stream._buffer[2*3]= 3; | ||
arr2[3].should.eql(512 + 3); | ||
stream._buffer[2 * 3] = 3; | ||
@@ -199,2 +201,3 @@ | ||
} | ||
it("should provide a working writeArrayBuffer", function () { | ||
@@ -314,4 +317,4 @@ | ||
arr.length.should.eql(arr.byteLength); | ||
arr.length.should.eql(largeArray.byteLength,"byteLength should match"); | ||
(arr.length %8).should.eql(0,"must be a multiple of 8"); | ||
arr.length.should.eql(largeArray.byteLength, "byteLength should match"); | ||
(arr.length % 8).should.eql(0, "must be a multiple of 8"); | ||
@@ -318,0 +321,0 @@ isValidBuffer(new Float64Array(arr.buffer), largeArray).should.eql(true); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57715
1238
4
+ Addedunderscore@^1.9.1
+ Addedunderscore@1.13.7(transitive)