UNPKG

@hms-networks/kolibri-js-core

Version:

Kolibri Core library containing common kolibri models and utils

388 lines 13.1 kB
/* * Copyright 2021 HMS Industrial Networks AB * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http: //www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import Long from 'long'; import { constants } from '../../common/constant'; var OpcodeBuffer = /** @class */ (function () { function OpcodeBuffer(arg) { if (typeof arg === 'number') { this._buf = Buffer.alloc(arg); } else { this._buf = arg; } this.index = 0; } Object.defineProperty(OpcodeBuffer.prototype, "buf", { get: function () { return this._buf; }, enumerable: false, configurable: true }); OpcodeBuffer.prototype.readBoolean = function () { var result = this.buf.readUInt8(this.index); this.index += 1; return result !== 0; }; ; OpcodeBuffer.prototype.readUInt8 = function () { var result = this.buf.readUInt8(this.index); this.index += 1; return result; }; ; OpcodeBuffer.prototype.readInt8 = function () { var result = this.buf.readInt8(this.index); this.index += 1; return result; }; ; OpcodeBuffer.prototype.readUInt16 = function () { var result = this.buf.readUInt16BE(this.index); this.index += 2; return result; }; ; OpcodeBuffer.prototype.readInt16 = function () { var result = this.buf.readInt16BE(this.index); this.index += 2; return result; }; ; OpcodeBuffer.prototype.readUInt32 = function () { var result = this.buf.readUInt32BE(this.index); this.index += 4; return result; }; ; OpcodeBuffer.prototype.readInt32 = function () { var result = this.buf.readInt32BE(this.index); this.index += 4; return result; }; ; OpcodeBuffer.prototype.readUInt64 = function () { // @ts-ignore Long types not correct here var result = Long.fromBytesBE(this.buf.slice(this.index, this.index + 8), true); this.index += 8; return result; }; ; OpcodeBuffer.prototype.readInt64 = function () { // @ts-ignore Long types not correct here var result = Long.fromBytesBE(this.buf.slice(this.index, this.index + 8), false); this.index += 8; return result; }; ; OpcodeBuffer.prototype.readFloat32 = function () { var result = this.buf.readFloatBE(this.index); this.index += 4; return result; }; ; OpcodeBuffer.prototype.readFloat64 = function () { var result = this.buf.readDoubleBE(this.index); this.index += 8; return result; }; ; OpcodeBuffer.prototype.readUtf8String = function () { var length = this.readUInt16(); if (length > constants.DT_STRING_MAXLEN) { throw new RangeError('string too long'); } var result = this.buf.toString('utf8', this.index, this.index + length); this.index += length; return result; }; ; OpcodeBuffer.prototype.readAsciiString = function () { var length = this.readUInt16(); if (length > constants.DT_STRING_MAXLEN) { throw new RangeError('string too long'); } var result = this.buf.toString('ascii', this.index, this.index + length); this.index += length; return result; }; ; OpcodeBuffer.prototype.readByteArray = function () { var length = this.readUInt16(); if (length > constants.DT_BYTEARRAY_MAXLEN) { throw new RangeError('byte array too long'); } var result = this.buf.toString('hex', this.index, this.index + length); this.index += length; return result; }; ; OpcodeBuffer.prototype.readOctets = function (length) { var result = Buffer.allocUnsafe(length); this.buf.copy(result, 0, this.index, this.index + length); this.index += length; return result; }; ; OpcodeBuffer.prototype.readDataType = function (dataType) { switch (dataType) { case constants.DT_BOOLEAN: return this.readBoolean(); case constants.DT_UNSIGNED_8BIT: return this.readUInt8(); case constants.DT_SIGNED_8BIT: return this.readInt8(); case constants.DT_UNSIGNED_16BIT: return this.readUInt16(); case constants.DT_SIGNED_16BIT: return this.readInt16(); case constants.DT_UNSIGNED_32BIT: return this.readUInt32(); case constants.DT_SIGNED_32BIT: return this.readInt32(); case constants.DT_UNSIGNED_64BIT: return this.readUInt64(); case constants.DT_SIGNED_64BIT: return this.readInt64(); case constants.DT_FLOATING_32BIT: return this.readFloat32(); case constants.DT_FLOATING_64BIT: return this.readFloat64(); case constants.DT_STRING: return this.readUtf8String(); case constants.DT_BYTEARRAY: return this.readByteArray(); default: throw new TypeError('invalid data type'); } }; ; //------------------------------------------------------------------------------ // OpcodeBuffer write methods //------------------------------------------------------------------------------ OpcodeBuffer.prototype.writeBoolean = function (value) { if (typeof value !== 'boolean') { throw new TypeError('invalid data type'); } this.buf[this.index] = value === true ? 1 : 0; this.index += 1; }; ; OpcodeBuffer.prototype.writeUInt8 = function (value) { this.validate(value); this.validateNumber(value); this.buf.writeUInt8(value, this.index); this.index += 1; }; ; OpcodeBuffer.prototype.writeInt8 = function (value) { this.validate(value); this.validateNumber(value); this.buf.writeInt8(value, this.index); this.index += 1; }; ; OpcodeBuffer.prototype.writeUInt16 = function (value) { this.validate(value); this.validateNumber(value); this.buf.writeUInt16BE(value, this.index); this.index += 2; }; ; OpcodeBuffer.prototype.writeInt16 = function (value) { this.validate(value); this.validateNumber(value); this.buf.writeInt16BE(value, this.index); this.index += 2; }; ; OpcodeBuffer.prototype.writeUInt32 = function (value) { this.validate(value); this.validateNumber(value); this.buf.writeUInt32BE(value, this.index); this.index += 4; }; ; OpcodeBuffer.prototype.writeInt32 = function (value) { this.validate(value); this.validateNumber(value); this.buf.writeInt32BE(value, this.index); this.index += 4; }; ; OpcodeBuffer.prototype.writeUInt64 = function (value) { this.validate(value); var longValue = Long.fromValue(value); if (longValue.lessThan(Long.ZERO)) { throw new RangeError('value out of bounds'); } Buffer.from(longValue.toBytesBE()).copy(this.buf, this.index); this.index += 8; }; ; OpcodeBuffer.prototype.writeInt64 = function (value) { this.validate(value); var longValue = Long.fromValue(value); Buffer.from(longValue.toBytesBE()).copy(this.buf, this.index); this.index += 8; }; ; OpcodeBuffer.prototype.writeFloat32 = function (value) { if (typeof value !== 'number' || (value !== 0 && Math.abs(value) < constants.DT_FLOATING_32BIT_MIN) || Math.abs(value) > constants.DT_FLOATING_32BIT_MAX) { throw new RangeError('value out of bounds'); } this.buf.writeFloatBE(value, this.index); this.index += 4; }; ; OpcodeBuffer.prototype.writeFloat64 = function (value) { if (typeof value !== 'number' || value === Number.NEGATIVE_INFINITY || value === Number.POSITIVE_INFINITY) { throw new RangeError('value out of bounds'); } this.buf.writeDoubleBE(value, this.index); this.index += 8; }; ; OpcodeBuffer.prototype.writeUtf8String = function (value) { if (typeof value !== 'string') { throw new TypeError('invalid data type'); } var length = Buffer.byteLength(value, 'utf8'); if (length > constants.DT_STRING_MAXLEN) { throw new RangeError('string too long'); } this.writeUInt16(length); this.buf.write(value, this.index, length, 'utf8'); this.index += length; }; ; OpcodeBuffer.prototype.writeAsciiString = function (value) { if (typeof value !== 'string') { throw new TypeError('invalid data type'); } var length = Buffer.byteLength(value, 'ascii'); if (length > constants.DT_STRING_MAXLEN) { throw new RangeError('string too long'); } this.writeUInt16(length); this.buf.write(value, this.index, length, 'ascii'); this.index += length; }; ; OpcodeBuffer.prototype.writeByteArray = function (value) { if (typeof value !== 'string') { throw new TypeError('invalid data type'); } var length = Buffer.byteLength(value, 'hex'); if (length > constants.DT_BYTEARRAY_MAXLEN) { throw new RangeError('byte array too long'); } this.writeUInt16(length); this.buf.write(value, this.index, length, 'hex'); this.index += length; }; ; OpcodeBuffer.prototype.writeOctets = function (value, length) { value.copy(this.buf, this.index, 0, length); this.index += length; }; ; OpcodeBuffer.prototype.writeDataType = function (value, dataType) { switch (dataType) { case constants.DT_BOOLEAN: this.writeBoolean(value); break; case constants.DT_UNSIGNED_8BIT: this.writeUInt8(value); break; case constants.DT_SIGNED_8BIT: this.writeInt8(value); break; case constants.DT_UNSIGNED_16BIT: this.writeUInt16(value); break; case constants.DT_SIGNED_16BIT: this.writeInt16(value); break; case constants.DT_UNSIGNED_32BIT: this.writeUInt32(value); break; case constants.DT_SIGNED_32BIT: this.writeInt32(value); break; case constants.DT_UNSIGNED_64BIT: this.writeUInt64(value); break; case constants.DT_SIGNED_64BIT: this.writeInt64(value); break; case constants.DT_FLOATING_32BIT: this.writeFloat32(value); break; case constants.DT_FLOATING_64BIT: this.writeFloat64(value); break; case constants.DT_STRING: this.writeUtf8String(value); break; case constants.DT_BYTEARRAY: this.writeByteArray(value); break; default: throw new TypeError('invalid data type'); } }; ; OpcodeBuffer.prototype.length = function () { return this.buf.length; }; ; OpcodeBuffer.prototype.setIndex = function (index) { if (index === undefined) { index = 0; } this.index = index; }; ; OpcodeBuffer.prototype.isEob = function () { return this.index === this.buf.length; }; ; // eslint-disable-next-line no-undef OpcodeBuffer.prototype.toString = function (encoding, start, end) { return this.buf.toString(encoding, start, end); }; ; OpcodeBuffer.prototype.validate = function (value) { if (value === undefined || value === null) { throw TypeError('invalid value: ' + value); } }; ; OpcodeBuffer.prototype.validateNumber = function (value) { if (typeof value !== 'number') { throw TypeError('value is not a number: ' + value); } }; ; return OpcodeBuffer; }()); export { OpcodeBuffer }; //# sourceMappingURL=opcode_buffer.js.map