opcjs-base 0.1.34-alpha → 0.1.35-alpha

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
@@ -1864,6 +1864,25 @@ var DiagnosticInfo = class _DiagnosticInfo {
1864
1864
  };
1865
1865
 
1866
1866
  // src/types/variant.ts
1867
+ function inferBuiltInType(element) {
1868
+ if (typeof element === "boolean") return { type: 1 /* Boolean */, rawValue: element };
1869
+ if (typeof element === "string") return { type: 12 /* String */, rawValue: element };
1870
+ if (element instanceof Uint8Array) return { type: 15 /* ByteString */, rawValue: element };
1871
+ if (element instanceof Date) return { type: 13 /* DateTime */, rawValue: element };
1872
+ if (element instanceof ExpandedNodeId) return { type: 18 /* ExpandedNodeId */, rawValue: element };
1873
+ if (element instanceof NodeId) return { type: 17 /* NodeId */, rawValue: element };
1874
+ if (element instanceof QualifiedName) return { type: 20 /* QualifiedName */, rawValue: element };
1875
+ if (element instanceof LocalizedText) return { type: 21 /* LocalizedText */, rawValue: element };
1876
+ if (element instanceof XmlElement) return { type: 16 /* XmlElement */, rawValue: element };
1877
+ if (element instanceof ExtensionObject) return { type: 22 /* ExtensionObject */, rawValue: element };
1878
+ if (element instanceof DataValue) return { type: 23 /* DataValue */, rawValue: element };
1879
+ if (element instanceof DiagnosticInfo) return { type: 25 /* DiagnosticInfo */, rawValue: element };
1880
+ if (typeof element === "object" && element !== null && "type" in element) {
1881
+ const tagged = element;
1882
+ return { type: tagged.type, rawValue: tagged.value };
1883
+ }
1884
+ throw new Error(`newFrom: unhandled array element: ${JSON.stringify(element)}`);
1885
+ }
1867
1886
  var Variant = class _Variant {
1868
1887
  /**
1869
1888
  * The variant type identifier.
@@ -1956,14 +1975,55 @@ var Variant = class _Variant {
1956
1975
  return `Variant(${typeName}: ${String(this.value)})`;
1957
1976
  }
1958
1977
  /**
1959
- * Union of all OPC UA built-in types accepted by {@link Variant.newFrom}.
1978
+ * Creates a {@link Variant} from any OPC UA built-in scalar value or a
1979
+ * homogeneous array of such values.
1980
+ *
1981
+ * **Scalar usage** — pass any concrete OPC UA type and the BuiltInType is
1982
+ * inferred automatically:
1983
+ * ```ts
1984
+ * Variant.newFrom('hello') // String
1985
+ * Variant.newFrom(uaDouble(3.14)) // Double
1986
+ * Variant.newFrom(new LocalizedText('en', 'Hi'))
1987
+ * ```
1960
1988
  *
1961
- * Includes both the tagged numeric primitives from `UaPrimitive` (which carry
1962
- * a `BuiltInType` discriminant so the exact numeric encoding is known) and the
1963
- * class-based built-in types that are identified unambiguously at runtime via
1964
- * `instanceof`.
1989
+ * **Array usage** pass a plain JS array of a single homogeneous element type:
1990
+ * ```ts
1991
+ * Variant.newFrom([uaDouble(1.0), uaDouble(2.0)]) // Double[]
1992
+ * Variant.newFrom([new LocalizedText('en', 'A'), ...]) // LocalizedText[]
1993
+ * ```
1994
+ *
1995
+ * Throws if the array is empty (element type cannot be inferred), if the
1996
+ * first element is null/undefined, or if elements have mixed types.
1997
+ *
1998
+ * Note: `Variant` itself is excluded from the accepted types intentionally —
1999
+ * callers should work with concrete OPC UA types.
1965
2000
  */
1966
2001
  static newFrom(value) {
2002
+ if (Array.isArray(value)) {
2003
+ if (value.length === 0) {
2004
+ throw new Error(
2005
+ "newFrom: cannot create an array Variant from an empty array \u2014 the element type cannot be inferred. Use new Variant(type, []) directly."
2006
+ );
2007
+ }
2008
+ const firstElement = value[0];
2009
+ if (firstElement === null || firstElement === void 0) {
2010
+ throw new Error(
2011
+ "newFrom: the first array element must not be null or undefined \u2014 the element type cannot be inferred."
2012
+ );
2013
+ }
2014
+ const { type } = inferBuiltInType(firstElement);
2015
+ const rawValues = value.map((el, index) => {
2016
+ if (el === null || el === void 0) return el;
2017
+ const { type: elType, rawValue } = inferBuiltInType(el);
2018
+ if (elType !== type) {
2019
+ throw new Error(
2020
+ `newFrom: mixed-type arrays are not supported. Expected ${BuiltInType[type]} but got ${BuiltInType[elType]} at index ${index}.`
2021
+ );
2022
+ }
2023
+ return rawValue;
2024
+ });
2025
+ return new _Variant(type, rawValues);
2026
+ }
1967
2027
  if (value === null || value === void 0) {
1968
2028
  return _Variant.newNull();
1969
2029
  }
@@ -2003,9 +2063,6 @@ var Variant = class _Variant {
2003
2063
  if (value instanceof DiagnosticInfo) {
2004
2064
  return new _Variant(25 /* DiagnosticInfo */, value);
2005
2065
  }
2006
- if (value instanceof _Variant) {
2007
- return new _Variant(24 /* Variant */, value);
2008
- }
2009
2066
  if (typeof value === "object" && "type" in value) {
2010
2067
  const tagged = value;
2011
2068
  return new _Variant(tagged.type, tagged.value);