Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-opcua-binary-stream

Package Overview
Dependencies
Maintainers
1
Versions
110
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-opcua-binary-stream - npm Package Compare versions

Comparing version 0.5.0 to 2.0.0-alpha.2

dist/binaryStreamSizeCalculator.d.ts

102

dist/binaryStream.d.ts

@@ -9,2 +9,3 @@ /// <reference types="node" />

* It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
* It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
* convention.

@@ -32,4 +33,10 @@ *

export declare class BinaryStream {
_buffer: Buffer;
/**
* the current position inside the buffer
*/
length: number;
/**
* @internal
*/
buffer: Buffer;
constructor(data: undefined | Buffer | number);

@@ -39,3 +46,2 @@ /**

* @method BinaryStream.rewind
* @return null
*/

@@ -46,4 +52,3 @@ rewind(): void;

* value must be in the range of [-127,128]
* @method writeInt8
* @param value
* @param value the value to write
*/

@@ -53,4 +58,3 @@ writeInt8(value: number): void;

* write a single unsigned byte (8 bits) to the stream.
* @method writeUInt8
* @param value
* @param value the value to write
*/

@@ -60,4 +64,3 @@ writeUInt8(value: number): void;

* write a single 16 bit signed integer to the stream.
* @method writeInt16
* @param value
* @param value the value to write
*/

@@ -67,4 +70,3 @@ writeInt16(value: number): void;

* write a single 16 bit unsigned integer to the stream.
* @method writeUInt16
* @param value
* @param value the value to write
*/

@@ -74,4 +76,3 @@ writeUInt16(value: number): void;

* write a single 32 bit signed integer to the stream.
* @method writeInteger
* @param value
* @param value the value to write
*/

@@ -81,4 +82,4 @@ writeInteger(value: number): void;

* write a single 32 bit unsigned integer to the stream.
* @method writeUInt32
* @param value
*
* @param value the value to write
*/

@@ -88,4 +89,3 @@ writeUInt32(value: number): void;

* write a single 32 bit floating number to the stream.
* @method writeFloat
* @param value
* @param value the value to write
*/

@@ -95,11 +95,9 @@ writeFloat(value: number): void;

* write a single 64 bit floating number to the stream.
* @method writeDouble
* @param value
* @param value the value to write
*/
writeDouble(value: number): void;
/**
* @method writeArrayBuffer
* @param arrayBuf {ArrayBuffer}
* @param offset {Number}
* @param length {Number}
* @param arrayBuf a buffer or byte array write
* @param offset the offset position (default =0)
* @param length the number of byte to write
*/

@@ -109,4 +107,3 @@ writeArrayBuffer(arrayBuf: ArrayBuffer, offset?: number, length?: number): void;

* read a single signed byte (8 bits) from the stream.
* @method readByte
* @return {Number}
* @return the value read
*/

@@ -117,4 +114,2 @@ readByte(): number;

* read a single unsigned byte (8 bits) from the stream.
* @method readUInt8
* @return {Number}
*/

@@ -124,4 +119,2 @@ readUInt8(): number;

* read a single signed 16-bit integer from the stream.
* @method readInt16
* @return {Number}
*/

@@ -131,4 +124,2 @@ readInt16(): number;

* read a single unsigned 16-bit integer from the stream.
* @method readUInt16
* @return {Number} q
*/

@@ -138,4 +129,2 @@ readUInt16(): number;

* read a single signed 32-bit integer from the stream.
* @method readInteger
* @return {Number}
*/

@@ -145,4 +134,2 @@ readInteger(): number;

* read a single unsigned 32-bit integer from the stream.
* @method readUInt32
* @return {number} the value read from the stream
*/

@@ -152,4 +139,2 @@ readUInt32(): number;

* read a single 32-bit floating point number from the stream.
* @method readFloat
* @return the value read from the stream
*/

@@ -159,4 +144,2 @@ readFloat(): number;

* read a single 64-bit floating point number from the stream.
* @method readDouble
* @return the value read from the stream
*/

@@ -168,12 +151,9 @@ readDouble(): number;

*
* @method writeByteStream
* @param {Buffer} buf the buffer to write.
* the buffer buf.length the buffer to write
* @param buf the buffer to write.
*/
writeByteStream(buf: Buffer): void;
writeString(value: string): void;
writeString(value: null | string): void;
/**
* @method readArrayBuffer
* @param length
* @returns {Uint8Array}
*/

@@ -183,6 +163,5 @@ readArrayBuffer(length: number): Uint8Array;

* read a byte stream to the stream.
* The method reads the length of the byte array from the stream as a 32 bits integer before reading the byte stream.
* The method reads the length of the byte array from the stream as a 32 bits integer
* before reading the byte stream.
*
* @method readByteStream
* @return {Buffer}
*/

@@ -193,28 +172,7 @@ readByteStream(): Buffer | null;

/**
* a BinaryStreamSizeCalculator can be used to quickly evaluate the required size
* of a buffer by performing the same sequence of write operation.
*
* a BinaryStreamSizeCalculator has the same writeXXX methods as the BinaryStream stream
* object.
*
* @class BinaryStreamSizeCalculator
* @extends BinaryStream
* @constructor
*
* @function calculateByteLength
* calculate the size in bytes of a utf8 string
* @param str {String}
* @internal
*/
export declare class BinaryStreamSizeCalculator {
length: number;
constructor();
rewind(): void;
writeInt8(value: number): void;
writeUInt8(value: number): void;
writeInt16(value: number): void;
writeInteger(value: number): void;
writeUInt32(value: number): void;
writeUInt16(value: number): void;
writeFloat(value: number): void;
writeDouble(value: number): void;
writeArrayBuffer(arrayBuf: ArrayBuffer, offset: number, byteLength: number): void;
writeByteStream(buf: Buffer): void;
writeString(str: string): void;
}
export declare function calculateByteLength(str: string): number;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @module opcua.miscellaneous
* @module node-opcua-binary-stream
*/
const underscore_1 = require("underscore");
require("util");
const node_opcua_assert_1 = require("node-opcua-assert");
require("util");
const node_opcua_buffer_utils_1 = require("node-opcua-buffer-utils");
const underscore_1 = require("underscore");
const MAXUINT32 = 4294967295; // 2**32 -1;

@@ -19,2 +19,3 @@ const noAssert = false;

* It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
* It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
* convention.

@@ -44,10 +45,10 @@ *

if (data === undefined) {
this._buffer = node_opcua_buffer_utils_1.createFastUninitializedBuffer(1024);
this.buffer = node_opcua_buffer_utils_1.createFastUninitializedBuffer(1024);
}
else if (typeof data === "number") {
this._buffer = node_opcua_buffer_utils_1.createFastUninitializedBuffer(data); // new Buffer(/*size=*/data);
this.buffer = node_opcua_buffer_utils_1.createFastUninitializedBuffer(data); // new Buffer(/*size=*/data);
}
else {
node_opcua_assert_1.default(data instanceof Buffer);
this._buffer = data;
this.buffer = data;
}

@@ -59,3 +60,2 @@ this.length = 0;

* @method BinaryStream.rewind
* @return null
*/

@@ -68,11 +68,12 @@ rewind() {

* value must be in the range of [-127,128]
* @method writeInt8
* @param value
* @param value the value to write
*/
writeInt8(value) {
if (performCheck)
node_opcua_assert_1.default(this._buffer.length >= this.length + 1, "not enough space in buffer");
if (performCheck)
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);
}
this.buffer.writeInt8(value, this.length, noAssert);
this.length += 1;

@@ -82,11 +83,12 @@ }

* write a single unsigned byte (8 bits) to the stream.
* @method writeUInt8
* @param value
* @param value the value to write
*/
writeUInt8(value) {
if (performCheck)
node_opcua_assert_1.default(this._buffer.length >= this.length + 1, "not enough space in buffer");
if (performCheck)
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);
}
this.buffer.writeUInt8(value, this.length, noAssert);
this.length += 1;

@@ -96,9 +98,9 @@ }

* write a single 16 bit signed integer to the stream.
* @method writeInt16
* @param value
* @param value the value to write
*/
writeInt16(value) {
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);
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);
this.length += 2;

@@ -108,9 +110,9 @@ }

* write a single 16 bit unsigned integer to the stream.
* @method writeUInt16
* @param value
* @param value the value to write
*/
writeUInt16(value) {
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);
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);
this.length += 2;

@@ -120,9 +122,9 @@ }

* write a single 32 bit signed integer to the stream.
* @method writeInteger
* @param value
* @param value the value to write
*/
writeInteger(value) {
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);
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);
this.length += 4;

@@ -132,19 +134,22 @@ }

* write a single 32 bit unsigned integer to the stream.
* @method writeUInt32
* @param value
*
* @param value the value to write
*/
writeUInt32(value) {
if (performCheck)
node_opcua_assert_1.default(this._buffer.length >= this.length + 4, "not enough space in buffer");
if (performCheck)
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)
}
if (performCheck) {
node_opcua_assert_1.default(value >= 0 && value <= MAXUINT32);
this._buffer.writeUInt32LE(value, this.length, noAssert);
}
this.buffer.writeUInt32LE(value, this.length, noAssert);
this.length += 4;
/*
assert(this._buffer[this.length - 4] === value % 256);
assert(this._buffer[this.length - 3] === (value >>> 8) % 256);
assert(this._buffer[this.length - 2] === (value >>> 16) % 256);
assert(this._buffer[this.length - 1] === (value >>> 24) % 256);
assert(this.buffer[this.length - 4] === value % 256);
assert(this.buffer[this.length - 3] === (value >>> 8) % 256);
assert(this.buffer[this.length - 2] === (value >>> 16) % 256);
assert(this.buffer[this.length - 1] === (value >>> 24) % 256);
*/

@@ -154,9 +159,9 @@ }

* write a single 32 bit floating number to the stream.
* @method writeFloat
* @param value
* @param value the value to write
*/
writeFloat(value) {
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);
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);
this.length += 4;

@@ -166,24 +171,24 @@ }

* write a single 64 bit floating number to the stream.
* @method writeDouble
* @param value
* @param value the value to write
*/
writeDouble(value) {
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);
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);
this.length += 8;
}
/**
* @method writeArrayBuffer
* @param arrayBuf {ArrayBuffer}
* @param offset {Number}
* @param length {Number}
* @param arrayBuf a buffer or byte array write
* @param offset the offset position (default =0)
* @param length the number of byte to write
*/
writeArrayBuffer(arrayBuf, offset = 0, length = 0) {
if (performCheck)
if (performCheck) {
node_opcua_assert_1.default(arrayBuf instanceof ArrayBuffer);
}
const byteArr = new Uint8Array(arrayBuf);
const n = (length || byteArr.length) + offset;
for (let i = offset; i < n; i++) {
this._buffer[this.length++] = byteArr[i];
this.buffer[this.length++] = byteArr[i];
}

@@ -200,11 +205,10 @@ }

// }
// this.length += my_memcpy(this._buffer, this.length, byteArr, offset, offset + length);
// this.length += my_memcpy(this.buffer, this.length, byteArr, offset, offset + length);
// }
/**
* read a single signed byte (8 bits) from the stream.
* @method readByte
* @return {Number}
* @return the value read
*/
readByte() {
const retVal = this._buffer.readInt8(this.length, noAssert);
const retVal = this.buffer.readInt8(this.length, noAssert);
this.length += 1;

@@ -218,9 +222,8 @@ return retVal;

* read a single unsigned byte (8 bits) from the stream.
* @method readUInt8
* @return {Number}
*/
readUInt8() {
if (performCheck)
node_opcua_assert_1.default(this._buffer.length >= this.length + 1);
const retVal = this._buffer.readUInt8(this.length, noAssert);
if (performCheck) {
node_opcua_assert_1.default(this.buffer.length >= this.length + 1);
}
const retVal = this.buffer.readUInt8(this.length, noAssert);
this.length += 1;

@@ -231,7 +234,5 @@ return retVal;

* read a single signed 16-bit integer from the stream.
* @method readInt16
* @return {Number}
*/
readInt16() {
const retVal = this._buffer.readInt16LE(this.length, noAssert);
const retVal = this.buffer.readInt16LE(this.length, noAssert);
this.length += 2;

@@ -242,7 +243,5 @@ return retVal;

* read a single unsigned 16-bit integer from the stream.
* @method readUInt16
* @return {Number} q
*/
readUInt16() {
const retVal = this._buffer.readUInt16LE(this.length, noAssert);
const retVal = this.buffer.readUInt16LE(this.length, noAssert);
this.length += 2;

@@ -253,7 +252,5 @@ return retVal;

* read a single signed 32-bit integer from the stream.
* @method readInteger
* @return {Number}
*/
readInteger() {
const retVal = this._buffer.readInt32LE(this.length, noAssert);
const retVal = this.buffer.readInt32LE(this.length, noAssert);
this.length += 4;

@@ -264,7 +261,5 @@ return retVal;

* read a single unsigned 32-bit integer from the stream.
* @method readUInt32
* @return {number} the value read from the stream
*/
readUInt32() {
const retVal = this._buffer.readUInt32LE(this.length, noAssert);
const retVal = this.buffer.readUInt32LE(this.length, noAssert);
this.length += 4;

@@ -275,7 +270,5 @@ return retVal;

* read a single 32-bit floating point number from the stream.
* @method readFloat
* @return the value read from the stream
*/
readFloat() {
const retVal = this._buffer.readFloatLE(this.length, noAssert);
const retVal = this.buffer.readFloatLE(this.length, noAssert);
this.length += 4;

@@ -286,7 +279,5 @@ return retVal;

* read a single 64-bit floating point number from the stream.
* @method readDouble
* @return the value read from the stream
*/
readDouble() {
const retVal = this._buffer.readDoubleLE(this.length, noAssert);
const retVal = this.buffer.readDoubleLE(this.length, noAssert);
this.length += 8;

@@ -299,5 +290,3 @@ return retVal;

*
* @method writeByteStream
* @param {Buffer} buf the buffer to write.
* the buffer buf.length the buffer to write
* @param buf the buffer to write.
*/

@@ -312,3 +301,3 @@ writeByteStream(buf) {

// make sure there is enough room in destination buffer
const remainingBytes = this._buffer.length - this.length;
const remainingBytes = this.buffer.length - this.length;
/* istanbul ignore next */

@@ -322,3 +311,3 @@ if (remainingBytes < buf.length) {

}
buf.copy(this._buffer, this.length, 0, buf.length);
buf.copy(this.buffer, this.length, 0, buf.length);
this.length += buf.length;

@@ -334,3 +323,3 @@ }

// make sure there is enough room in destination buffer
const remainingBytes = this._buffer.length - this.length;
const remainingBytes = this.buffer.length - this.length;
/* istanbul ignore next */

@@ -345,3 +334,3 @@ if (remainingBytes < byteLength) {

if (byteLength > 0) {
this._buffer.write(value, this.length);
this.buffer.write(value, this.length);
this.length += byteLength;

@@ -351,5 +340,5 @@ }

// readArrayBuffer(length: number): ArrayBuffer {
// assert(this.length + length <= this._buffer.length, "not enough bytes in buffer");
// assert(this.length + length <= this.buffer.length, "not enough bytes in buffer");
// const byteArr = new Uint8Array(new ArrayBuffer(length));
// my_memcpy(byteArr, 0, this._buffer, this.length, this.length + length);
// my_memcpy(byteArr, 0, this.buffer, this.length, this.length + length);
// this.length += length;

@@ -361,13 +350,15 @@ // return byteArr;

* @param length
* @returns {Uint8Array}
*/
readArrayBuffer(length) {
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);
if (performCheck)
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);
if (performCheck) {
node_opcua_assert_1.default(slice.length === length);
}
const byteArr = new Uint8Array(slice);
if (performCheck)
if (performCheck) {
node_opcua_assert_1.default(byteArr.length === length);
}
this.length += length;

@@ -378,6 +369,5 @@ return byteArr;

* read a byte stream to the stream.
* The method reads the length of the byte array from the stream as a 32 bits integer before reading the byte stream.
* The method reads the length of the byte array from the stream as a 32 bits integer
* before reading the byte stream.
*
* @method readByteStream
* @return {Buffer}
*/

@@ -393,3 +383,3 @@ readByteStream() {

// check that there is enough space in the buffer
const remainingBytes = this._buffer.length - this.length;
const remainingBytes = this.buffer.length - this.length;
if (remainingBytes < bufLen) {

@@ -403,3 +393,3 @@ throw new Error("BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " +

// create a shared memory buffer ! for speed
const buf = this._buffer.slice(this.length, this.length + bufLen);
const buf = this.buffer.slice(this.length, this.length + bufLen);
this.length += bufLen;

@@ -417,3 +407,3 @@ return buf;

// check that there is enough space in the buffer
const remainingBytes = this._buffer.length - this.length;
const remainingBytes = this.buffer.length - this.length;
if (remainingBytes < bufLen) {

@@ -426,3 +416,3 @@ throw new Error("BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " +

}
const str = this._buffer.toString("utf-8", this.length, this.length + bufLen);
const str = this.buffer.toString("utf-8", this.length, this.length + bufLen);
this.length += bufLen;

@@ -437,2 +427,3 @@ return str;

* @param str {String}
* @internal
*/

@@ -457,71 +448,4 @@ function calculateByteLength(str) {

}
exports.calculateByteLength = calculateByteLength;
const zeroLengthBuffer = node_opcua_buffer_utils_1.createFastUninitializedBuffer(0);
/**
* a BinaryStreamSizeCalculator can be used to quickly evaluate the required size
* of a buffer by performing the same sequence of write operation.
*
* a BinaryStreamSizeCalculator has the same writeXXX methods as the BinaryStream stream
* object.
*
* @class BinaryStreamSizeCalculator
* @extends BinaryStream
* @constructor
*
*/
class BinaryStreamSizeCalculator {
constructor() {
this.length = 0;
}
rewind() {
this.length = 0;
}
writeInt8(value) {
this.length += 1;
}
writeUInt8(value) {
this.length += 1;
}
writeInt16(value) {
this.length += 2;
}
writeInteger(value) {
this.length += 4;
}
writeUInt32(value) {
this.length += 4;
}
writeUInt16(value) {
this.length += 2;
}
writeFloat(value) {
this.length += 4;
}
writeDouble(value) {
this.length += 8;
}
writeArrayBuffer(arrayBuf, offset, byteLength) {
offset = offset || 0;
node_opcua_assert_1.default(arrayBuf instanceof ArrayBuffer);
this.length += byteLength || arrayBuf.byteLength;
}
writeByteStream(buf) {
if (!buf) {
this.writeUInt32(0);
}
else {
this.writeUInt32(buf.length);
this.length += buf.length;
}
}
writeString(str) {
if (str === undefined || str === null) {
this.writeUInt32(-1);
return;
}
const bufLength = calculateByteLength(str);
this.writeUInt32(bufLength);
this.length += bufLength;
}
}
exports.BinaryStreamSizeCalculator = BinaryStreamSizeCalculator;
//# sourceMappingURL=binaryStream.js.map
{
"name": "node-opcua-binary-stream",
"version": "0.5.0",
"version": "2.0.0-alpha.2",
"description": "pure nodejs OPCUA SDK - module -binary-stream",
"main": "dist/binaryStream.js",
"types": "dist/binaryStream.d.ts",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsc",
"test": "mocha test"
"test": "mocha test",
"clean": "node -e \"require('rimraf').sync('dist');\"",
"lint": "tslint source/**/*.ts"
},
"dependencies": {
"colors": "^1.3.2",
"node-opcua-assert": "^0.5.0",
"node-opcua-buffer-utils": "^0.5.0",
"node-opcua-assert": "^2.0.0-alpha.2",
"node-opcua-buffer-utils": "^2.0.0-alpha.2",
"underscore": "^1.9.1"

@@ -19,3 +21,3 @@ },

"@types/underscore": "^1.8.9",
"node-opcua-benchmarker": "^0.5.0",
"node-opcua-benchmarker": "^2.0.0-alpha.2",
"should": "13.2.3"

@@ -38,3 +40,3 @@ },

"homepage": "http://node-opcua.github.io/",
"gitHead": "7c2f2dcb6ebdc49e57da1a028406a307df502d4e"
"gitHead": "7f57dd90e9d8bd1abcb8d330b3470999ac51e33f"
}
/**
* @module opcua.miscellaneous
* @module node-opcua-binary-stream
*/
import assert from "node-opcua-assert";
import { isFinite } from "underscore";
import "util";
import {createFastUninitializedBuffer} from "node-opcua-buffer-utils";
import {isFinite} from "underscore";
import assert from "node-opcua-assert";
import { createFastUninitializedBuffer } from "node-opcua-buffer-utils";
const MAXUINT32 = 4294967295; // 2**32 -1;

@@ -19,2 +20,3 @@ const noAssert = false;

* It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
* It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
* convention.

@@ -42,13 +44,21 @@ *

export class BinaryStream {
_buffer: Buffer;
length: number;
/**
* the current position inside the buffer
*/
public length: number;
/**
* @internal
*/
public buffer: Buffer;
constructor(data: undefined | Buffer | number) {
if (data === undefined) {
this._buffer = createFastUninitializedBuffer(1024);
this.buffer = createFastUninitializedBuffer(1024);
} else if (typeof data === "number") {
this._buffer = createFastUninitializedBuffer(data); // new Buffer(/*size=*/data);
this.buffer = createFastUninitializedBuffer(data); // new Buffer(/*size=*/data);
} else {
assert(data instanceof Buffer);
this._buffer = data;
this.buffer = data;
}

@@ -61,5 +71,4 @@ this.length = 0;

* @method BinaryStream.rewind
* @return null
*/
rewind() {
public rewind(): void {
this.length = 0;

@@ -71,9 +80,8 @@ }

* value must be in the range of [-127,128]
* @method writeInt8
* @param value
* @param value the value to write
*/
writeInt8(value: number): void {
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);
public writeInt8(value: number): void {
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);
this.length += 1;

@@ -84,9 +92,8 @@ }

* write a single unsigned byte (8 bits) to the stream.
* @method writeUInt8
* @param value
* @param value the value to write
*/
writeUInt8(value: number): void {
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);
public writeUInt8(value: number): void {
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);
this.length += 1;

@@ -97,8 +104,7 @@ }

* write a single 16 bit signed integer to the stream.
* @method writeInt16
* @param value
* @param value the value to write
*/
writeInt16(value: number): void {
if (performCheck) assert(this._buffer.length >= this.length + 2, "not enough space in buffer");
this._buffer.writeInt16LE(value, this.length, noAssert);
public writeInt16(value: number): void {
if (performCheck) { assert(this.buffer.length >= this.length + 2, "not enough space in buffer"); }
this.buffer.writeInt16LE(value, this.length, noAssert);
this.length += 2;

@@ -109,8 +115,7 @@ }

* write a single 16 bit unsigned integer to the stream.
* @method writeUInt16
* @param value
* @param value the value to write
*/
writeUInt16(value: number): void {
if (performCheck) assert(this._buffer.length >= this.length + 2, "not enough space in buffer");
this._buffer.writeUInt16LE(value, this.length, noAssert);
public writeUInt16(value: number): void {
if (performCheck) { assert(this.buffer.length >= this.length + 2, "not enough space in buffer"); }
this.buffer.writeUInt16LE(value, this.length, noAssert);
this.length += 2;

@@ -121,8 +126,7 @@ }

* write a single 32 bit signed integer to the stream.
* @method writeInteger
* @param value
* @param value the value to write
*/
writeInteger(value: number): void {
if (performCheck) assert(this._buffer.length >= this.length + 4, "not enough space in buffer");
this._buffer.writeInt32LE(value, this.length, noAssert);
public writeInteger(value: number): void {
if (performCheck) { assert(this.buffer.length >= this.length + 4, "not enough space in buffer"); }
this.buffer.writeInt32LE(value, this.length, noAssert);
this.length += 4;

@@ -133,16 +137,16 @@ }

* write a single 32 bit unsigned integer to the stream.
* @method writeUInt32
* @param value
*
* @param value the value to write
*/
writeUInt32(value: number): void {
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);
public writeUInt32(value: number): void {
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);
this.length += 4;
/*
assert(this._buffer[this.length - 4] === value % 256);
assert(this._buffer[this.length - 3] === (value >>> 8) % 256);
assert(this._buffer[this.length - 2] === (value >>> 16) % 256);
assert(this._buffer[this.length - 1] === (value >>> 24) % 256);
assert(this.buffer[this.length - 4] === value % 256);
assert(this.buffer[this.length - 3] === (value >>> 8) % 256);
assert(this.buffer[this.length - 2] === (value >>> 16) % 256);
assert(this.buffer[this.length - 1] === (value >>> 24) % 256);
*/

@@ -153,8 +157,7 @@ }

* write a single 32 bit floating number to the stream.
* @method writeFloat
* @param value
* @param value the value to write
*/
writeFloat(value: number): void {
if (performCheck) assert(this._buffer.length >= this.length + 4, "not enough space in buffer");
this._buffer.writeFloatLE(value, this.length, noAssert);
public writeFloat(value: number): void {
if (performCheck) { assert(this.buffer.length >= this.length + 4, "not enough space in buffer"); }
this.buffer.writeFloatLE(value, this.length, noAssert);
this.length += 4;

@@ -165,8 +168,7 @@ }

* write a single 64 bit floating number to the stream.
* @method writeDouble
* @param value
* @param value the value to write
*/
writeDouble(value: number): void {
if (performCheck) assert(this._buffer.length >= this.length + 8, "not enough space in buffer");
this._buffer.writeDoubleLE(value, this.length, noAssert);
public writeDouble(value: number): void {
if (performCheck) { assert(this.buffer.length >= this.length + 8, "not enough space in buffer"); }
this.buffer.writeDoubleLE(value, this.length, noAssert);
this.length += 8;

@@ -176,13 +178,12 @@ }

/**
* @method writeArrayBuffer
* @param arrayBuf {ArrayBuffer}
* @param offset {Number}
* @param length {Number}
* @param arrayBuf a buffer or byte array write
* @param offset the offset position (default =0)
* @param length the number of byte to write
*/
writeArrayBuffer(arrayBuf: ArrayBuffer, offset=0, length=0): void {
if (performCheck) assert(arrayBuf instanceof ArrayBuffer);
public writeArrayBuffer(arrayBuf: ArrayBuffer, offset = 0, length = 0): void {
if (performCheck) { assert(arrayBuf instanceof ArrayBuffer); }
const byteArr = new Uint8Array(arrayBuf);
const n = (length || byteArr.length) + offset;
for (let i = offset; i < n; i++) {
this._buffer[this.length++] = byteArr[i];
this.buffer[this.length++] = byteArr[i];
}

@@ -200,3 +201,3 @@ }

// }
// this.length += my_memcpy(this._buffer, this.length, byteArr, offset, offset + length);
// this.length += my_memcpy(this.buffer, this.length, byteArr, offset, offset + length);
// }

@@ -206,7 +207,6 @@

* read a single signed byte (8 bits) from the stream.
* @method readByte
* @return {Number}
* @return the value read
*/
readByte(): number {
const retVal = this._buffer.readInt8(this.length, noAssert);
public readByte(): number {
const retVal = this.buffer.readInt8(this.length, noAssert);
this.length += 1;

@@ -216,3 +216,3 @@ return retVal;

readInt8(): number {
public readInt8(): number {
return this.readByte();

@@ -223,8 +223,6 @@ }

* read a single unsigned byte (8 bits) from the stream.
* @method readUInt8
* @return {Number}
*/
readUInt8(): number {
if (performCheck) assert(this._buffer.length >= this.length + 1);
const retVal = this._buffer.readUInt8(this.length, noAssert);
public readUInt8(): number {
if (performCheck) { assert(this.buffer.length >= this.length + 1); }
const retVal = this.buffer.readUInt8(this.length, noAssert);
this.length += 1;

@@ -236,7 +234,5 @@ return retVal;

* read a single signed 16-bit integer from the stream.
* @method readInt16
* @return {Number}
*/
readInt16(): number {
const retVal = this._buffer.readInt16LE(this.length, noAssert);
public readInt16(): number {
const retVal = this.buffer.readInt16LE(this.length, noAssert);
this.length += 2;

@@ -248,7 +244,5 @@ return retVal;

* read a single unsigned 16-bit integer from the stream.
* @method readUInt16
* @return {Number} q
*/
readUInt16(): number {
const retVal = this._buffer.readUInt16LE(this.length, noAssert);
public readUInt16(): number {
const retVal = this.buffer.readUInt16LE(this.length, noAssert);
this.length += 2;

@@ -260,7 +254,5 @@ return retVal;

* read a single signed 32-bit integer from the stream.
* @method readInteger
* @return {Number}
*/
readInteger(): number {
const retVal = this._buffer.readInt32LE(this.length, noAssert);
public readInteger(): number {
const retVal = this.buffer.readInt32LE(this.length, noAssert);
this.length += 4;

@@ -272,7 +264,5 @@ return retVal;

* read a single unsigned 32-bit integer from the stream.
* @method readUInt32
* @return {number} the value read from the stream
*/
readUInt32(): number {
const retVal = this._buffer.readUInt32LE(this.length, noAssert);
public readUInt32(): number {
const retVal = this.buffer.readUInt32LE(this.length, noAssert);
this.length += 4;

@@ -284,7 +274,5 @@ return retVal;

* read a single 32-bit floating point number from the stream.
* @method readFloat
* @return the value read from the stream
*/
readFloat(): number {
const retVal = this._buffer.readFloatLE(this.length, noAssert);
public readFloat(): number {
const retVal = this.buffer.readFloatLE(this.length, noAssert);
this.length += 4;

@@ -296,7 +284,5 @@ return retVal;

* read a single 64-bit floating point number from the stream.
* @method readDouble
* @return the value read from the stream
*/
readDouble(): number {
const retVal = this._buffer.readDoubleLE(this.length, noAssert);
public readDouble(): number {
const retVal = this.buffer.readDoubleLE(this.length, noAssert);
this.length += 8;

@@ -310,7 +296,5 @@ return retVal;

*
* @method writeByteStream
* @param {Buffer} buf the buffer to write.
* the buffer buf.length the buffer to write
* @param buf the buffer to write.
*/
writeByteStream(buf: Buffer): void {
public writeByteStream(buf: Buffer): void {
if (!buf) {

@@ -323,3 +307,3 @@ this.writeInteger(-1);

// make sure there is enough room in destination buffer
const remainingBytes = this._buffer.length - this.length;
const remainingBytes = this.buffer.length - this.length;

@@ -333,10 +317,10 @@ /* istanbul ignore next */

remainingBytes +
" left"
" left",
);
}
buf.copy(this._buffer, this.length, 0, buf.length);
buf.copy(this.buffer, this.length, 0, buf.length);
this.length += buf.length;
}
writeString(value: string): void {
public writeString(value: null|string): void {
if (value === undefined || value === null) {

@@ -349,3 +333,3 @@ this.writeInteger(-1);

// make sure there is enough room in destination buffer
const remainingBytes = this._buffer.length - this.length;
const remainingBytes = this.buffer.length - this.length;
/* istanbul ignore next */

@@ -358,7 +342,7 @@ if (remainingBytes < byteLength) {

remainingBytes +
" left"
" left",
);
}
if (byteLength > 0) {
this._buffer.write(value, this.length);
this.buffer.write(value, this.length);
this.length += byteLength;

@@ -369,5 +353,5 @@ }

// readArrayBuffer(length: number): ArrayBuffer {
// assert(this.length + length <= this._buffer.length, "not enough bytes in buffer");
// assert(this.length + length <= this.buffer.length, "not enough bytes in buffer");
// const byteArr = new Uint8Array(new ArrayBuffer(length));
// my_memcpy(byteArr, 0, this._buffer, this.length, this.length + length);
// my_memcpy(byteArr, 0, this.buffer, this.length, this.length + length);
// this.length += length;

@@ -379,10 +363,9 @@ // return byteArr;

* @param length
* @returns {Uint8Array}
*/
readArrayBuffer(length: number): Uint8Array {
if (performCheck) assert(this.length + length <= this._buffer.length, "not enough bytes in buffer");
const slice = this._buffer.slice(this.length, this.length + length);
if (performCheck) assert(slice.length === length);
public readArrayBuffer(length: number): Uint8Array {
if (performCheck) { assert(this.length + length <= this.buffer.length, "not enough bytes in buffer"); }
const slice = this.buffer.slice(this.length, this.length + length);
if (performCheck) { assert(slice.length === length); }
const byteArr = new Uint8Array(slice);
if (performCheck) assert(byteArr.length === length);
if (performCheck) { assert(byteArr.length === length); }
this.length += length;

@@ -394,8 +377,7 @@ return byteArr;

* read a byte stream to the stream.
* The method reads the length of the byte array from the stream as a 32 bits integer before reading the byte stream.
* The method reads the length of the byte array from the stream as a 32 bits integer
* before reading the byte stream.
*
* @method readByteStream
* @return {Buffer}
*/
readByteStream(): Buffer | null {
public readByteStream(): Buffer | null {
const bufLen = this.readUInt32();

@@ -409,3 +391,3 @@ if (bufLen === 0xffffffff) {

// check that there is enough space in the buffer
const remainingBytes = this._buffer.length - this.length;
const remainingBytes = this.buffer.length - this.length;
if (remainingBytes < bufLen) {

@@ -417,7 +399,7 @@ throw new Error(

remainingBytes +
" left"
" left",
);
}
// create a shared memory buffer ! for speed
const buf = this._buffer.slice(this.length, this.length + bufLen);
const buf = this.buffer.slice(this.length, this.length + bufLen);
this.length += bufLen;

@@ -427,3 +409,3 @@ return buf;

readString(): string | null {
public readString(): string | null {
const bufLen = this.readUInt32();

@@ -437,3 +419,3 @@ if (bufLen === 0xffffffff) {

// check that there is enough space in the buffer
const remainingBytes = this._buffer.length - this.length;
const remainingBytes = this.buffer.length - this.length;
if (remainingBytes < bufLen) {

@@ -445,6 +427,6 @@ throw new Error(

remainingBytes +
" left"
" left",
);
}
const str = this._buffer.toString("utf-8", this.length, this.length + bufLen);
const str = this.buffer.toString("utf-8", this.length, this.length + bufLen);
this.length += bufLen;

@@ -459,4 +441,5 @@ return str;

* @param str {String}
* @internal
*/
function calculateByteLength(str: string) {
export function calculateByteLength(str: string) {
// returns the byte length of an utf8 string

@@ -480,83 +463,1 @@ let s = str.length;

const zeroLengthBuffer = createFastUninitializedBuffer(0);
/**
* a BinaryStreamSizeCalculator can be used to quickly evaluate the required size
* of a buffer by performing the same sequence of write operation.
*
* a BinaryStreamSizeCalculator has the same writeXXX methods as the BinaryStream stream
* object.
*
* @class BinaryStreamSizeCalculator
* @extends BinaryStream
* @constructor
*
*/
export class BinaryStreamSizeCalculator {
length: number;
constructor() {
this.length = 0;
}
rewind(): void {
this.length = 0;
}
writeInt8(value: number): void {
this.length += 1;
}
writeUInt8(value: number): void {
this.length += 1;
}
writeInt16(value: number): void {
this.length += 2;
}
writeInteger(value: number): void {
this.length += 4;
}
writeUInt32(value: number): void {
this.length += 4;
}
writeUInt16(value: number): void {
this.length += 2;
}
writeFloat(value: number): void {
this.length += 4;
}
writeDouble(value: number): void {
this.length += 8;
}
writeArrayBuffer(arrayBuf: ArrayBuffer, offset: number, byteLength: number): void {
offset = offset || 0;
assert(arrayBuf instanceof ArrayBuffer);
this.length += byteLength || arrayBuf.byteLength;
}
writeByteStream(buf: Buffer): void {
if (!buf) {
this.writeUInt32(0);
} else {
this.writeUInt32(buf.length);
this.length += buf.length;
}
}
writeString(str: string): void {
if (str === undefined || str === null) {
this.writeUInt32(-1);
return;
}
const bufLength = calculateByteLength(str);
this.writeUInt32(bufLength);
this.length += bufLength;
}
}

@@ -54,8 +54,8 @@ "use strict";

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;
stream.buffer[2 * 3] = 3;

@@ -72,7 +72,7 @@ stream.rewind();

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;
stream.buffer[2 * 3] = 3;

@@ -107,3 +107,3 @@

for (let i = offset; i < n; i++) {
this._buffer[this.length++] = byteArr[i];
this.buffer[this.length++] = byteArr[i];
}

@@ -114,4 +114,4 @@ };

assert(this.length + length <= this._buffer.length, "not enough bytes in buffer");
const slice = this._buffer.slice(this.length, this.length + length);
assert(this.length + length <= this.buffer.length, "not enough bytes in buffer");
const slice = this.buffer.slice(this.length, this.length + length);
assert(slice.length === length);

@@ -125,5 +125,5 @@ const byteArr = new Uint8Array(slice);

//var result = new Uint8Array(this._buffer, this.length, length);
//var result = new Uint8Array(this.buffer, this.length, length);
// returns a new Buffer that shares the same allocated memory as the given ArrayBuffer.
const result = Buffer.from(this._buffer.buffer, this.length, length);
const result = Buffer.from(this.buffer.buffer, this.length, length);
this.length += length;

@@ -134,3 +134,3 @@ return Buffer.from(result);

BinaryStream.prototype.readArrayBuffer2 = function (length) {
const slice = this._buffer.slice(this.length, this.length + length);
const slice = this.buffer.slice(this.length, this.length + length);
this.length += length;

@@ -141,4 +141,4 @@ return Buffer.from(slice);

BinaryStream.prototype.readArrayBuffer3 = function (length) {
//xx assert(this.length + length <= this._buffer.length, "not enough bytes in buffer");
const slice = this._buffer.slice(this.length, this.length + length);
//xx assert(this.length + length <= this.buffer.length, "not enough bytes in buffer");
const slice = this.buffer.slice(this.length, this.length + length);
//xx assert(slice.length === length);

@@ -193,3 +193,3 @@ const byteArr = new Uint8Array(slice);

binStream_writeArrayBuffer.call(binStream, largeArray.buffer, 0, largeArray.byteLength);
//xx console.log(binStream._buffer.slice(0,100).toString("hex"));
//xx console.log(binStream.buffer.slice(0,100).toString("hex"));

@@ -196,0 +196,0 @@ binStream.rewind();

{
"extends" : "../tsconfig.json",
"compilerOptions": {
"skipLibCheck": true,
"target": "es6",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"sourceMap": true,
"sourceRoot": "./source",
"strict": true
"rootDir": "source",
"outDir": "dist"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc