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.cjs CHANGED
@@ -1551,12 +1551,12 @@ var BinaryReader = class _BinaryReader {
1551
1551
  }
1552
1552
  case 1 /* Binary */: {
1553
1553
  const reader = new _BinaryReader(this.readByteString());
1554
- data = decoder.decodeWithTypeId(typeId.identifier, reader);
1554
+ data = decoder.decodeWithEncodingId(typeId.identifier, reader);
1555
1555
  break;
1556
1556
  }
1557
1557
  case 2 /* Xml */: {
1558
1558
  const reader = new XmlReader(this.readString());
1559
- data = decoder.decodeWithTypeId(typeId.identifier, reader);
1559
+ data = decoder.decodeWithEncodingId(typeId.identifier, reader);
1560
1560
  break;
1561
1561
  }
1562
1562
  }
@@ -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,46 +1991,35 @@ 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) {
2014
- if (value == null) {
2015
- this.writeInt32(-1);
2016
- return;
2017
- }
2018
- const utf8Bytes = Buffer.from(value, "utf8");
2019
- const length = utf8Bytes.length;
2020
- if (length > 16777216) {
2021
- throw new CodecError(
2022
- `String length ${length} exceeds maximum allowed length of 16,777,216 bytes`,
2023
- { format: "Binary", suggestedAction: "Reduce string length" }
2024
- );
2018
+ let encoded = void 0;
2019
+ if (value && value !== "") {
2020
+ encoded = new TextEncoder().encode(value);
2025
2021
  }
2026
- this.writeInt32(length);
2027
- this.ensureCapacity(length);
2028
- utf8Bytes.copy(this.buffer, this.position);
2029
- this.position += length;
2022
+ this.writeByteString(encoded);
2030
2023
  }
2031
2024
  writeDateTime(value) {
2032
2025
  const jsTimestamp = BigInt(value.getTime());
@@ -2040,19 +2033,19 @@ var BinaryWriter = class {
2040
2033
  }
2041
2034
  this.ensureCapacity(16);
2042
2035
  const data1 = parseInt(hex.substr(0, 8), 16);
2043
- this.buffer.writeUInt32LE(data1, this.position);
2036
+ this.view.setUint32(this.position, data1, true);
2044
2037
  const data2 = parseInt(hex.substr(8, 4), 16);
2045
- this.buffer.writeUInt16LE(data2, this.position + 4);
2038
+ this.view.setUint16(this.position + 4, data2, true);
2046
2039
  const data3 = parseInt(hex.substr(12, 4), 16);
2047
- this.buffer.writeUInt16LE(data3, this.position + 6);
2040
+ this.view.setUint16(this.position + 6, data3, true);
2048
2041
  for (let i = 0; i < 8; i++) {
2049
2042
  const byte = parseInt(hex.substr(16 + i * 2, 2), 16);
2050
- this.buffer.writeUInt8(byte, this.position + 8 + i);
2043
+ this.view.setUint8(this.position + 8 + i, byte);
2051
2044
  }
2052
2045
  this.position += 16;
2053
2046
  }
2054
2047
  writeByteString(value) {
2055
- if (value == null) {
2048
+ if (!value) {
2056
2049
  this.writeInt32(-1);
2057
2050
  return;
2058
2051
  }
@@ -2063,14 +2056,8 @@ var BinaryWriter = class {
2063
2056
  { format: "Binary", suggestedAction: "Reduce ByteString length" }
2064
2057
  );
2065
2058
  }
2066
- this.writeInt32(length);
2067
- this.ensureCapacity(length);
2068
- if (Buffer.isBuffer(value)) {
2069
- value.copy(this.buffer, this.position);
2070
- } else {
2071
- this.buffer.set(value, this.position);
2072
- }
2073
- this.position += length;
2059
+ this.writeInt32(value.length);
2060
+ this.writeBytes(value);
2074
2061
  }
2075
2062
  writeXmlElement(value) {
2076
2063
  this.writeString(value);
@@ -2138,8 +2125,7 @@ var BinaryWriter = class {
2138
2125
  this.writeByte(5 /* ByteString */);
2139
2126
  this.writeUInt16(value.namespace);
2140
2127
  const ident = value.identifier;
2141
- const buf = ident instanceof Uint8Array && !(ident instanceof Buffer) ? Buffer.from(ident) : ident;
2142
- this.writeByteString(buf);
2128
+ this.writeByteString(ident);
2143
2129
  break;
2144
2130
  }
2145
2131
  default:
@@ -2218,20 +2204,22 @@ var BinaryWriter = class {
2218
2204
  switch (value.encoding) {
2219
2205
  case 0 /* None */:
2220
2206
  break;
2221
- case 1 /* Binary */:
2207
+ case 1 /* Binary */: {
2222
2208
  if (!value.data) {
2223
2209
  throw new CodecError("ExtensionObject with Binary encoding must have data");
2224
2210
  }
2225
2211
  const binaryData = encoder.encodeWithoutId(value.data, "binary");
2226
2212
  this.writeByteString(binaryData);
2227
2213
  break;
2228
- case 2 /* Xml */:
2214
+ }
2215
+ case 2 /* Xml */: {
2229
2216
  if (!value.data) {
2230
2217
  throw new CodecError("ExtensionObject with Xml encoding must have data");
2231
2218
  }
2232
2219
  const xmlString = encoder.encodeWithoutId(value.data, "xml");
2233
2220
  this.writeXmlElement(xmlString);
2234
2221
  break;
2222
+ }
2235
2223
  default:
2236
2224
  throw new CodecError(`Invalid ExtensionObject encoding: ${value.encoding}`);
2237
2225
  }
@@ -2449,7 +2437,9 @@ var BinaryWriter = class {
2449
2437
  }
2450
2438
  }
2451
2439
  constructor(initialSize = 1024) {
2452
- this.buffer = Buffer.allocUnsafe(initialSize);
2440
+ const data = new Uint8Array(initialSize);
2441
+ this.buffer = data;
2442
+ this.view = new DataView(data.buffer, data.byteOffset, data.byteLength);
2453
2443
  this.position = 0;
2454
2444
  }
2455
2445
  };