opcjs-base 0.1.7 → 0.1.9

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.
package/dist/index.cjs CHANGED
@@ -1879,7 +1879,9 @@ var VariantMask2 = {
1879
1879
  };
1880
1880
  var BinaryWriter = class {
1881
1881
  buffer;
1882
+ view;
1882
1883
  position;
1884
+ growthFactor = 2;
1883
1885
  getData() {
1884
1886
  return this.buffer.subarray(0, this.position);
1885
1887
  }
@@ -1923,23 +1925,25 @@ var BinaryWriter = class {
1923
1925
  if (!Number.isInteger(value) || value < 0 || value > 4294967295) {
1924
1926
  throw new CodecError(`UInt32 value ${value} out of range [0, 4294967295]`);
1925
1927
  }
1926
- this.buffer.writeUInt32LE(value, offset);
1928
+ this.view.setUint32(offset, value, true);
1927
1929
  }
1928
1930
  /**
1929
1931
  * Ensure buffer has enough capacity, growing if necessary.
1930
1932
  */
1931
1933
  ensureCapacity(additionalBytes) {
1932
- const required = this.position + additionalBytes;
1933
- if (required > this.buffer.length) {
1934
- const newSize = Math.max(required, this.buffer.length * 2);
1935
- const newBuffer = Buffer.allocUnsafe(newSize);
1936
- this.buffer.copy(newBuffer, 0, 0, this.position);
1934
+ const requiredSize = this.position + additionalBytes;
1935
+ if (requiredSize > this.buffer.length) {
1936
+ const newSize = Math.max(this.buffer.length * this.growthFactor, requiredSize);
1937
+ const newBuffer = new Uint8Array(newSize);
1938
+ newBuffer.set(this.buffer.subarray(0, this.position), 0);
1937
1939
  this.buffer = newBuffer;
1940
+ this.view = new DataView(newBuffer.buffer, newBuffer.byteOffset, newBuffer.byteLength);
1941
+ console.log(`BufferWriter: resized buffer to ${newSize} bytes`);
1938
1942
  }
1939
1943
  }
1940
1944
  writeBoolean(value) {
1941
1945
  this.ensureCapacity(1);
1942
- this.buffer.writeUInt8(value ? 1 : 0, this.position);
1946
+ this.view.setUint8(this.position, value ? 1 : 0);
1943
1947
  this.position += 1;
1944
1948
  }
1945
1949
  writeByte(value) {
@@ -1947,7 +1951,7 @@ var BinaryWriter = class {
1947
1951
  throw new CodecError(`Byte value ${value} out of range [0, 255]`);
1948
1952
  }
1949
1953
  this.ensureCapacity(1);
1950
- this.buffer.writeUInt8(value, this.position);
1954
+ this.view.setUint8(this.position, value);
1951
1955
  this.position += 1;
1952
1956
  }
1953
1957
  writeSByte(value) {
@@ -1955,7 +1959,7 @@ var BinaryWriter = class {
1955
1959
  throw new CodecError(`SByte value ${value} out of range [-128, 127]`);
1956
1960
  }
1957
1961
  this.ensureCapacity(1);
1958
- this.buffer.writeInt8(value, this.position);
1962
+ this.view.setInt8(this.position, value);
1959
1963
  this.position += 1;
1960
1964
  }
1961
1965
  writeInt16(value) {
@@ -1963,7 +1967,7 @@ var BinaryWriter = class {
1963
1967
  throw new CodecError(`Int16 value ${value} out of range [-32768, 32767]`);
1964
1968
  }
1965
1969
  this.ensureCapacity(2);
1966
- this.buffer.writeInt16LE(value, this.position);
1970
+ this.view.setInt16(this.position, value, true);
1967
1971
  this.position += 2;
1968
1972
  }
1969
1973
  writeUInt16(value) {
@@ -1971,7 +1975,7 @@ var BinaryWriter = class {
1971
1975
  throw new CodecError(`UInt16 value ${value} out of range [0, 65535]`);
1972
1976
  }
1973
1977
  this.ensureCapacity(2);
1974
- this.buffer.writeUInt16LE(value, this.position);
1978
+ this.view.setUint16(this.position, value, true);
1975
1979
  this.position += 2;
1976
1980
  }
1977
1981
  writeInt32(value) {
@@ -1979,7 +1983,7 @@ var BinaryWriter = class {
1979
1983
  throw new CodecError(`Int32 value ${value} out of range [-2147483648, 2147483647]`);
1980
1984
  }
1981
1985
  this.ensureCapacity(4);
1982
- this.buffer.writeInt32LE(value, this.position);
1986
+ this.view.setInt32(this.position, value, true);
1983
1987
  this.position += 4;
1984
1988
  }
1985
1989
  writeUInt32(value) {
@@ -1987,27 +1991,27 @@ var BinaryWriter = class {
1987
1991
  throw new CodecError(`UInt32 value ${value} out of range [0, 4294967295]`);
1988
1992
  }
1989
1993
  this.ensureCapacity(4);
1990
- this.buffer.writeUInt32LE(value, this.position);
1994
+ this.view.setUint32(this.position, value, true);
1991
1995
  this.position += 4;
1992
1996
  }
1993
1997
  writeInt64(value) {
1994
1998
  this.ensureCapacity(8);
1995
- this.buffer.writeBigInt64LE(value, this.position);
1999
+ this.view.setBigInt64(this.position, value, true);
1996
2000
  this.position += 8;
1997
2001
  }
1998
2002
  writeUInt64(value) {
1999
2003
  this.ensureCapacity(8);
2000
- this.buffer.writeBigUInt64LE(value, this.position);
2004
+ this.view.setBigUint64(this.position, value, true);
2001
2005
  this.position += 8;
2002
2006
  }
2003
2007
  writeFloat(value) {
2004
2008
  this.ensureCapacity(4);
2005
- this.buffer.writeFloatLE(value, this.position);
2009
+ this.view.setFloat32(this.position, value, true);
2006
2010
  this.position += 4;
2007
2011
  }
2008
2012
  writeDouble(value) {
2009
2013
  this.ensureCapacity(8);
2010
- this.buffer.writeDoubleLE(value, this.position);
2014
+ this.view.setFloat64(this.position, value, true);
2011
2015
  this.position += 8;
2012
2016
  }
2013
2017
  writeString(value) {
@@ -2040,14 +2044,14 @@ var BinaryWriter = class {
2040
2044
  }
2041
2045
  this.ensureCapacity(16);
2042
2046
  const data1 = parseInt(hex.substr(0, 8), 16);
2043
- this.buffer.writeUInt32LE(data1, this.position);
2047
+ this.view.setUint32(this.position, data1, true);
2044
2048
  const data2 = parseInt(hex.substr(8, 4), 16);
2045
- this.buffer.writeUInt16LE(data2, this.position + 4);
2049
+ this.view.setUint16(this.position + 4, data2, true);
2046
2050
  const data3 = parseInt(hex.substr(12, 4), 16);
2047
- this.buffer.writeUInt16LE(data3, this.position + 6);
2051
+ this.view.setUint16(this.position + 6, data3, true);
2048
2052
  for (let i = 0; i < 8; i++) {
2049
2053
  const byte = parseInt(hex.substr(16 + i * 2, 2), 16);
2050
- this.buffer.writeUInt8(byte, this.position + 8 + i);
2054
+ this.view.setUint8(this.position + 8 + i, byte);
2051
2055
  }
2052
2056
  this.position += 16;
2053
2057
  }
@@ -2218,20 +2222,22 @@ var BinaryWriter = class {
2218
2222
  switch (value.encoding) {
2219
2223
  case 0 /* None */:
2220
2224
  break;
2221
- case 1 /* Binary */:
2225
+ case 1 /* Binary */: {
2222
2226
  if (!value.data) {
2223
2227
  throw new CodecError("ExtensionObject with Binary encoding must have data");
2224
2228
  }
2225
2229
  const binaryData = encoder.encodeWithoutId(value.data, "binary");
2226
2230
  this.writeByteString(binaryData);
2227
2231
  break;
2228
- case 2 /* Xml */:
2232
+ }
2233
+ case 2 /* Xml */: {
2229
2234
  if (!value.data) {
2230
2235
  throw new CodecError("ExtensionObject with Xml encoding must have data");
2231
2236
  }
2232
2237
  const xmlString = encoder.encodeWithoutId(value.data, "xml");
2233
2238
  this.writeXmlElement(xmlString);
2234
2239
  break;
2240
+ }
2235
2241
  default:
2236
2242
  throw new CodecError(`Invalid ExtensionObject encoding: ${value.encoding}`);
2237
2243
  }
@@ -2449,7 +2455,9 @@ var BinaryWriter = class {
2449
2455
  }
2450
2456
  }
2451
2457
  constructor(initialSize = 1024) {
2452
- this.buffer = Buffer.allocUnsafe(initialSize);
2458
+ const data = Buffer.allocUnsafe(initialSize);
2459
+ this.buffer = data;
2460
+ this.view = new DataView(data.buffer, data.byteOffset, data.byteLength);
2453
2461
  this.position = 0;
2454
2462
  }
2455
2463
  };