opcjs-base 0.1.7 → 0.1.10

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.d.cts CHANGED
@@ -1792,7 +1792,9 @@ declare class BinaryReader implements IReader {
1792
1792
  */
1793
1793
  declare class BinaryWriter implements IWriter {
1794
1794
  private buffer;
1795
+ private view;
1795
1796
  private position;
1797
+ private readonly growthFactor;
1796
1798
  getData(): Uint8Array;
1797
1799
  /** Returns the number of bytes written so far. */
1798
1800
  getLength(): number;
package/dist/index.d.ts CHANGED
@@ -1792,7 +1792,9 @@ declare class BinaryReader implements IReader {
1792
1792
  */
1793
1793
  declare class BinaryWriter implements IWriter {
1794
1794
  private buffer;
1795
+ private view;
1795
1796
  private position;
1797
+ private readonly growthFactor;
1796
1798
  getData(): Uint8Array;
1797
1799
  /** Returns the number of bytes written so far. */
1798
1800
  getLength(): number;
package/dist/index.js CHANGED
@@ -1549,12 +1549,12 @@ var BinaryReader = class _BinaryReader {
1549
1549
  }
1550
1550
  case 1 /* Binary */: {
1551
1551
  const reader = new _BinaryReader(this.readByteString());
1552
- data = decoder.decodeWithTypeId(typeId.identifier, reader);
1552
+ data = decoder.decodeWithEncodingId(typeId.identifier, reader);
1553
1553
  break;
1554
1554
  }
1555
1555
  case 2 /* Xml */: {
1556
1556
  const reader = new XmlReader(this.readString());
1557
- data = decoder.decodeWithTypeId(typeId.identifier, reader);
1557
+ data = decoder.decodeWithEncodingId(typeId.identifier, reader);
1558
1558
  break;
1559
1559
  }
1560
1560
  }
@@ -1877,7 +1877,9 @@ var VariantMask2 = {
1877
1877
  };
1878
1878
  var BinaryWriter = class {
1879
1879
  buffer;
1880
+ view;
1880
1881
  position;
1882
+ growthFactor = 2;
1881
1883
  getData() {
1882
1884
  return this.buffer.subarray(0, this.position);
1883
1885
  }
@@ -1921,23 +1923,25 @@ var BinaryWriter = class {
1921
1923
  if (!Number.isInteger(value) || value < 0 || value > 4294967295) {
1922
1924
  throw new CodecError(`UInt32 value ${value} out of range [0, 4294967295]`);
1923
1925
  }
1924
- this.buffer.writeUInt32LE(value, offset);
1926
+ this.view.setUint32(offset, value, true);
1925
1927
  }
1926
1928
  /**
1927
1929
  * Ensure buffer has enough capacity, growing if necessary.
1928
1930
  */
1929
1931
  ensureCapacity(additionalBytes) {
1930
- const required = this.position + additionalBytes;
1931
- if (required > this.buffer.length) {
1932
- const newSize = Math.max(required, this.buffer.length * 2);
1933
- const newBuffer = Buffer.allocUnsafe(newSize);
1934
- this.buffer.copy(newBuffer, 0, 0, this.position);
1932
+ const requiredSize = this.position + additionalBytes;
1933
+ if (requiredSize > this.buffer.length) {
1934
+ const newSize = Math.max(this.buffer.length * this.growthFactor, requiredSize);
1935
+ const newBuffer = new Uint8Array(newSize);
1936
+ newBuffer.set(this.buffer.subarray(0, this.position), 0);
1935
1937
  this.buffer = newBuffer;
1938
+ this.view = new DataView(newBuffer.buffer, newBuffer.byteOffset, newBuffer.byteLength);
1939
+ console.log(`BufferWriter: resized buffer to ${newSize} bytes`);
1936
1940
  }
1937
1941
  }
1938
1942
  writeBoolean(value) {
1939
1943
  this.ensureCapacity(1);
1940
- this.buffer.writeUInt8(value ? 1 : 0, this.position);
1944
+ this.view.setUint8(this.position, value ? 1 : 0);
1941
1945
  this.position += 1;
1942
1946
  }
1943
1947
  writeByte(value) {
@@ -1945,7 +1949,7 @@ var BinaryWriter = class {
1945
1949
  throw new CodecError(`Byte value ${value} out of range [0, 255]`);
1946
1950
  }
1947
1951
  this.ensureCapacity(1);
1948
- this.buffer.writeUInt8(value, this.position);
1952
+ this.view.setUint8(this.position, value);
1949
1953
  this.position += 1;
1950
1954
  }
1951
1955
  writeSByte(value) {
@@ -1953,7 +1957,7 @@ var BinaryWriter = class {
1953
1957
  throw new CodecError(`SByte value ${value} out of range [-128, 127]`);
1954
1958
  }
1955
1959
  this.ensureCapacity(1);
1956
- this.buffer.writeInt8(value, this.position);
1960
+ this.view.setInt8(this.position, value);
1957
1961
  this.position += 1;
1958
1962
  }
1959
1963
  writeInt16(value) {
@@ -1961,7 +1965,7 @@ var BinaryWriter = class {
1961
1965
  throw new CodecError(`Int16 value ${value} out of range [-32768, 32767]`);
1962
1966
  }
1963
1967
  this.ensureCapacity(2);
1964
- this.buffer.writeInt16LE(value, this.position);
1968
+ this.view.setInt16(this.position, value, true);
1965
1969
  this.position += 2;
1966
1970
  }
1967
1971
  writeUInt16(value) {
@@ -1969,7 +1973,7 @@ var BinaryWriter = class {
1969
1973
  throw new CodecError(`UInt16 value ${value} out of range [0, 65535]`);
1970
1974
  }
1971
1975
  this.ensureCapacity(2);
1972
- this.buffer.writeUInt16LE(value, this.position);
1976
+ this.view.setUint16(this.position, value, true);
1973
1977
  this.position += 2;
1974
1978
  }
1975
1979
  writeInt32(value) {
@@ -1977,7 +1981,7 @@ var BinaryWriter = class {
1977
1981
  throw new CodecError(`Int32 value ${value} out of range [-2147483648, 2147483647]`);
1978
1982
  }
1979
1983
  this.ensureCapacity(4);
1980
- this.buffer.writeInt32LE(value, this.position);
1984
+ this.view.setInt32(this.position, value, true);
1981
1985
  this.position += 4;
1982
1986
  }
1983
1987
  writeUInt32(value) {
@@ -1985,46 +1989,35 @@ var BinaryWriter = class {
1985
1989
  throw new CodecError(`UInt32 value ${value} out of range [0, 4294967295]`);
1986
1990
  }
1987
1991
  this.ensureCapacity(4);
1988
- this.buffer.writeUInt32LE(value, this.position);
1992
+ this.view.setUint32(this.position, value, true);
1989
1993
  this.position += 4;
1990
1994
  }
1991
1995
  writeInt64(value) {
1992
1996
  this.ensureCapacity(8);
1993
- this.buffer.writeBigInt64LE(value, this.position);
1997
+ this.view.setBigInt64(this.position, value, true);
1994
1998
  this.position += 8;
1995
1999
  }
1996
2000
  writeUInt64(value) {
1997
2001
  this.ensureCapacity(8);
1998
- this.buffer.writeBigUInt64LE(value, this.position);
2002
+ this.view.setBigUint64(this.position, value, true);
1999
2003
  this.position += 8;
2000
2004
  }
2001
2005
  writeFloat(value) {
2002
2006
  this.ensureCapacity(4);
2003
- this.buffer.writeFloatLE(value, this.position);
2007
+ this.view.setFloat32(this.position, value, true);
2004
2008
  this.position += 4;
2005
2009
  }
2006
2010
  writeDouble(value) {
2007
2011
  this.ensureCapacity(8);
2008
- this.buffer.writeDoubleLE(value, this.position);
2012
+ this.view.setFloat64(this.position, value, true);
2009
2013
  this.position += 8;
2010
2014
  }
2011
2015
  writeString(value) {
2012
- if (value == null) {
2013
- this.writeInt32(-1);
2014
- return;
2015
- }
2016
- const utf8Bytes = Buffer.from(value, "utf8");
2017
- const length = utf8Bytes.length;
2018
- if (length > 16777216) {
2019
- throw new CodecError(
2020
- `String length ${length} exceeds maximum allowed length of 16,777,216 bytes`,
2021
- { format: "Binary", suggestedAction: "Reduce string length" }
2022
- );
2016
+ let encoded = void 0;
2017
+ if (value && value !== "") {
2018
+ encoded = new TextEncoder().encode(value);
2023
2019
  }
2024
- this.writeInt32(length);
2025
- this.ensureCapacity(length);
2026
- utf8Bytes.copy(this.buffer, this.position);
2027
- this.position += length;
2020
+ this.writeByteString(encoded);
2028
2021
  }
2029
2022
  writeDateTime(value) {
2030
2023
  const jsTimestamp = BigInt(value.getTime());
@@ -2038,19 +2031,19 @@ var BinaryWriter = class {
2038
2031
  }
2039
2032
  this.ensureCapacity(16);
2040
2033
  const data1 = parseInt(hex.substr(0, 8), 16);
2041
- this.buffer.writeUInt32LE(data1, this.position);
2034
+ this.view.setUint32(this.position, data1, true);
2042
2035
  const data2 = parseInt(hex.substr(8, 4), 16);
2043
- this.buffer.writeUInt16LE(data2, this.position + 4);
2036
+ this.view.setUint16(this.position + 4, data2, true);
2044
2037
  const data3 = parseInt(hex.substr(12, 4), 16);
2045
- this.buffer.writeUInt16LE(data3, this.position + 6);
2038
+ this.view.setUint16(this.position + 6, data3, true);
2046
2039
  for (let i = 0; i < 8; i++) {
2047
2040
  const byte = parseInt(hex.substr(16 + i * 2, 2), 16);
2048
- this.buffer.writeUInt8(byte, this.position + 8 + i);
2041
+ this.view.setUint8(this.position + 8 + i, byte);
2049
2042
  }
2050
2043
  this.position += 16;
2051
2044
  }
2052
2045
  writeByteString(value) {
2053
- if (value == null) {
2046
+ if (!value) {
2054
2047
  this.writeInt32(-1);
2055
2048
  return;
2056
2049
  }
@@ -2061,14 +2054,8 @@ var BinaryWriter = class {
2061
2054
  { format: "Binary", suggestedAction: "Reduce ByteString length" }
2062
2055
  );
2063
2056
  }
2064
- this.writeInt32(length);
2065
- this.ensureCapacity(length);
2066
- if (Buffer.isBuffer(value)) {
2067
- value.copy(this.buffer, this.position);
2068
- } else {
2069
- this.buffer.set(value, this.position);
2070
- }
2071
- this.position += length;
2057
+ this.writeInt32(value.length);
2058
+ this.writeBytes(value);
2072
2059
  }
2073
2060
  writeXmlElement(value) {
2074
2061
  this.writeString(value);
@@ -2136,8 +2123,7 @@ var BinaryWriter = class {
2136
2123
  this.writeByte(5 /* ByteString */);
2137
2124
  this.writeUInt16(value.namespace);
2138
2125
  const ident = value.identifier;
2139
- const buf = ident instanceof Uint8Array && !(ident instanceof Buffer) ? Buffer.from(ident) : ident;
2140
- this.writeByteString(buf);
2126
+ this.writeByteString(ident);
2141
2127
  break;
2142
2128
  }
2143
2129
  default:
@@ -2216,20 +2202,22 @@ var BinaryWriter = class {
2216
2202
  switch (value.encoding) {
2217
2203
  case 0 /* None */:
2218
2204
  break;
2219
- case 1 /* Binary */:
2205
+ case 1 /* Binary */: {
2220
2206
  if (!value.data) {
2221
2207
  throw new CodecError("ExtensionObject with Binary encoding must have data");
2222
2208
  }
2223
2209
  const binaryData = encoder.encodeWithoutId(value.data, "binary");
2224
2210
  this.writeByteString(binaryData);
2225
2211
  break;
2226
- case 2 /* Xml */:
2212
+ }
2213
+ case 2 /* Xml */: {
2227
2214
  if (!value.data) {
2228
2215
  throw new CodecError("ExtensionObject with Xml encoding must have data");
2229
2216
  }
2230
2217
  const xmlString = encoder.encodeWithoutId(value.data, "xml");
2231
2218
  this.writeXmlElement(xmlString);
2232
2219
  break;
2220
+ }
2233
2221
  default:
2234
2222
  throw new CodecError(`Invalid ExtensionObject encoding: ${value.encoding}`);
2235
2223
  }
@@ -2447,7 +2435,9 @@ var BinaryWriter = class {
2447
2435
  }
2448
2436
  }
2449
2437
  constructor(initialSize = 1024) {
2450
- this.buffer = Buffer.allocUnsafe(initialSize);
2438
+ const data = new Uint8Array(initialSize);
2439
+ this.buffer = data;
2440
+ this.view = new DataView(data.buffer, data.byteOffset, data.byteLength);
2451
2441
  this.position = 0;
2452
2442
  }
2453
2443
  };