Skip to content

Commit

Permalink
chore: use IBasicSessionAsync whenever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Nov 20, 2023
1 parent 3e2be22 commit 9d7eeb3
Show file tree
Hide file tree
Showing 46 changed files with 467 additions and 459 deletions.
8 changes: 7 additions & 1 deletion packages/node-opcua-address-space/source/pseudo_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,13 @@ export class PseudoSession implements IBasicSession {
public getArgumentDefinition(methodId: MethodId): Promise<ArgumentDefinition>;
public getArgumentDefinition(methodId: MethodId, callback: ResponseCallback<ArgumentDefinition>): void;
public getArgumentDefinition(methodId: MethodId, callback?: ResponseCallback<ArgumentDefinition>): any {
return getArgumentDefinitionHelper(this, methodId, callback!);
getArgumentDefinitionHelper(this, methodId)
.then((result) => {
callback!(null, result);
})
.catch((err: Error) => {
callback!(err);
});
}

public translateBrowsePath(browsePaths: BrowsePath[], callback: ResponseCallback<BrowsePathResult[]>): void;
Expand Down
6 changes: 3 additions & 3 deletions packages/node-opcua-address-space/test/test_load_nodeset2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { checkDebugFlag, make_debugLog } from "node-opcua-debug";

import { BinaryStream } from "node-opcua-binary-stream";
import { ExtensionObject } from "node-opcua-extension-object";
import { IBasicSession } from "node-opcua-pseudo-session";
import { IBasicSessionAsync } from "node-opcua-pseudo-session";
import { ExtraDataTypeManager, resolveOpaqueStructureInExtentionObject } from "node-opcua-client-dynamic-extension-object";

import { AddressSpace, ensureDatatypeExtracted, PseudoSession, UADataType, UAVariable } from "..";
Expand Down Expand Up @@ -463,7 +463,7 @@ describe("testing NodeSet XML file loading", function (this: any) {
a.setValueFromSource({ dataType: DataType.ExtensionObject, value: object });
});

async function testEncodeDecode(object: ExtensionObject, constructor: any, session: IBasicSession) {
async function testEncodeDecode(object: ExtensionObject, constructor: any, session: IBasicSessionAsync) {
doDebug && console.log("------------ Before");
doDebug && console.log(object.toString());

Expand All @@ -488,7 +488,7 @@ describe("testing NodeSet XML file loading", function (this: any) {
}

describe("VVA", () => {
let session: IBasicSession;
let session: IBasicSessionAsync;
let nsIndex: number;
beforeEach(async () => {
addressSpace.registerNamespace("PRIVATE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as async from "async";

import { UAReferenceType } from "node-opcua-address-space";
import { assert } from "node-opcua-assert";
import { browseAll, BrowseDescriptionLike, IBasicSession, ReadValueIdOptions, ResponseCallback } from "node-opcua-client";
import { browseAll, BrowseDescriptionLike, IBasicSessionAsync2, ReadValueIdOptions } from "node-opcua-client";
import { DataTypeDefinition } from "node-opcua-types";
import { ReferenceTypeIds, VariableIds } from "node-opcua-constants";
import {
Expand Down Expand Up @@ -551,7 +551,7 @@ export class NodeCrawlerBase extends EventEmitter implements NodeCrawlerEvents {
this.browseCounter += nodesToBrowse.length;
this.transactionCounter++;

browseAll(this.session as IBasicSession, nodesToBrowse)
browseAll(this.session as IBasicSessionAsync2, nodesToBrowse)
.then((browseResults?: BrowseResult[]) => {
assert(browseResults!.length === nodesToBrowse.length);
browseResults = browseResults || [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { assert } from "node-opcua-assert";
import { AttributeIds, BrowseDirection, makeResultMask, NodeClassMask } from "node-opcua-data-model";
import { DataValue } from "node-opcua-data-value";
import { make_debugLog, make_errorLog, make_warningLog } from "node-opcua-debug";
import { INodeId, NodeIdType, sameNodeId } from "node-opcua-nodeid";
//import { DataTypeIds } from "node-opcua-constant";
import {
BitField,
ConstructorFuncWithSchema,
DataTypeFactory,
EnumerationDefinitionSchema,
extractAllPossibleFields,
Expand All @@ -18,8 +16,7 @@ import {
TypeDefinition
} from "node-opcua-factory";
import { NodeId, makeExpandedNodeId, resolveNodeId, coerceNodeId } from "node-opcua-nodeid";
import { browseAll, BrowseDescriptionLike, findBasicDataType, getBuiltInDataType, IBasicSession } from "node-opcua-pseudo-session";
import { StatusCodes } from "node-opcua-status-code";
import { browseAll, BrowseDescriptionLike, findBasicDataType, IBasicSessionAsync, IBasicSessionAsync2, IBasicSessionBrowseNextAsync } from "node-opcua-pseudo-session";
import {
EnumDefinition,
DataTypeDefinition,
Expand All @@ -37,7 +34,7 @@ const debugLog = make_debugLog(__filename);
const errorLog = make_errorLog(__filename);
const warningLog = make_warningLog(__filename);

async function findSuperType(session: IBasicSession, dataTypeNodeId: NodeId): Promise<NodeId> {
async function findSuperType(session: IBasicSessionAsync2, dataTypeNodeId: NodeId): Promise<NodeId> {
if (dataTypeNodeId.namespace === 0 && dataTypeNodeId.value === 24) {
// BaseDataType !
return coerceNodeId(0);
Expand Down Expand Up @@ -69,7 +66,7 @@ async function findSuperType(session: IBasicSession, dataTypeNodeId: NodeId): Pr
return result3.references[0].nodeId;
}
async function findDataTypeCategory(
session: IBasicSession,
session: IBasicSessionAsync2,
cache: { [key: string]: CacheForFieldResolution },
dataTypeNodeId: NodeId
): Promise<FieldCategory> {
Expand Down Expand Up @@ -101,7 +98,7 @@ async function findDataTypeCategory(
}

async function findDataTypeBasicType(
session: IBasicSession,
session: IBasicSessionAsync2,
cache: { [key: string]: CacheForFieldResolution },
dataTypeNodeId: NodeId
): Promise<TypeDefinition> {
Expand Down Expand Up @@ -141,7 +138,7 @@ export interface CacheForFieldResolution {
dataType?: NodeId;
}

async function readBrowseName(session: IBasicSession, nodeId: NodeId): Promise<string> {
async function readBrowseName(session: IBasicSessionAsync, nodeId: NodeId): Promise<string> {
const dataValue = await session.read({ nodeId, attributeId: AttributeIds.BrowseName });
if (dataValue.statusCode.isNotGood()) {
const message =
Expand All @@ -153,7 +150,7 @@ async function readBrowseName(session: IBasicSession, nodeId: NodeId): Promise<s
}

async function resolve2(
session: IBasicSession,
session: IBasicSessionAsync2,
dataTypeNodeId: NodeId,
dataTypeFactory: DataTypeFactory,
fieldTypeName: string,
Expand Down Expand Up @@ -221,7 +218,7 @@ async function resolve2(
return { schema, category };
}

const isExtensionObject = async (session: IBasicSession, dataTypeNodeId: NodeId): Promise<boolean> => {
const isExtensionObject = async (session: IBasicSessionAsync2, dataTypeNodeId: NodeId): Promise<boolean> => {
if (dataTypeNodeId.namespace === 0 && dataTypeNodeId.value === DataType.ExtensionObject) {
return true;
}
Expand All @@ -241,7 +238,7 @@ const isExtensionObject = async (session: IBasicSession, dataTypeNodeId: NodeId)

// eslint-disable-next-line max-statements
async function resolveFieldType(
session: IBasicSession,
session: IBasicSessionAsync2,
dataTypeNodeId: NodeId,
dataTypeFactory: DataTypeFactory,
cache: { [key: string]: CacheForFieldResolution }
Expand Down Expand Up @@ -344,7 +341,7 @@ async function resolveFieldType(
}

async function _setupEncodings(
session: IBasicSession,
session: IBasicSessionAsync & IBasicSessionBrowseNextAsync,
dataTypeNodeId: NodeId,
schema: IStructuredTypeSchema
): Promise<IStructuredTypeSchema> {
Expand All @@ -363,7 +360,7 @@ async function _setupEncodings(

// eslint-disable-next-line max-statements
export async function convertDataTypeDefinitionToStructureTypeSchema(
session: IBasicSession,
session: IBasicSessionAsync2,
dataTypeNodeId: NodeId,
name: string,
definition: DataTypeDefinition,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QualifiedName, AttributeIds } from "node-opcua-data-model";
import { NodeId } from "node-opcua-nodeid";
import { IBasicSession } from "node-opcua-pseudo-session";
import { IBasicSessionAsync2 } from "node-opcua-pseudo-session";
import { AnyConstructorFunc } from "node-opcua-schemas";
//
import { getExtraDataTypeManager } from "./get_extra_data_type_manager";
Expand All @@ -9,7 +9,7 @@ import { readDataTypeDefinitionAndBuildType } from "./private/populate_data_type
/**
*
*/
export async function getExtensionObjectConstructor(session: IBasicSession, dataTypeNodeId: NodeId): Promise<AnyConstructorFunc> {
export async function getExtensionObjectConstructor(session: IBasicSessionAsync2, dataTypeNodeId: NodeId): Promise<AnyConstructorFunc> {
const extraDataTypeManager = await getExtraDataTypeManager(session);

const dataTypeFactory = extraDataTypeManager.getDataTypeFactory(dataTypeNodeId.namespace);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { checkDebugFlag, make_debugLog, make_errorLog } from "node-opcua-debug";
import { DataTypeFactory, getStandardDataTypeFactory } from "node-opcua-factory";
import { IBasicSession, readNamespaceArray } from "node-opcua-pseudo-session";
import {
IBasicSessionAsync2,
readNamespaceArray
} from "node-opcua-pseudo-session";
//
import { ExtraDataTypeManager } from "./extra_data_type_manager";
import { populateDataTypeManager } from "./populate_data_type_manager";
Expand All @@ -10,19 +13,19 @@ const debugLog = make_debugLog(__filename);
const errorLog = make_errorLog(__filename);
const warningLog = errorLog;

interface IBasicSessionEx extends IBasicSession {
interface IBasicSessionEx extends IBasicSessionAsync2 {
$$extraDataTypeManager?: ExtraDataTypeManager;
$$extraDataTypeManagerToResolve?: [(a: ExtraDataTypeManager) => void, (err: Error) => void][];
}
export async function invalidateExtraDataTypeManager(session: IBasicSession): Promise<void> {
export async function invalidateExtraDataTypeManager(session: IBasicSessionAsync2): Promise<void> {
const sessionPriv: IBasicSessionEx = session as IBasicSessionEx;
sessionPriv.$$extraDataTypeManager = undefined;
if (sessionPriv.$$extraDataTypeManagerToResolve) {
warningLog("Warning: invalidateExtraDataTypeManager is called while getExtraDataTypeManager is in progress");
}
}

async function extractDataTypeManager(session: IBasicSession): Promise<ExtraDataTypeManager> {
async function extractDataTypeManager(session: IBasicSessionAsync2): Promise<ExtraDataTypeManager> {
const namespaceArray = await readNamespaceArray(session);
// istanbul ignore next
if (namespaceArray.length === 0) {
Expand All @@ -46,7 +49,7 @@ async function extractDataTypeManager(session: IBasicSession): Promise<ExtraData
return dataTypeManager;
}

export async function getExtraDataTypeManager(session: IBasicSession): Promise<ExtraDataTypeManager> {
export async function getExtraDataTypeManager(session: IBasicSessionAsync2): Promise<ExtraDataTypeManager> {
const sessionPriv: IBasicSessionEx = session as IBasicSessionEx;
if (sessionPriv.$$extraDataTypeManager) {
return sessionPriv.$$extraDataTypeManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { AttributeIds, BrowseDirection, NodeClassMask, ResultMask } from "node-opcua-data-model";
import { resolveNodeId } from "node-opcua-nodeid";
import { IBasicSession, browseAll } from "node-opcua-pseudo-session";
import { DataTypeIds, ObjectIds, ObjectTypeIds, VariableTypeIds } from "node-opcua-constants";
import {
IBasicSessionAsync2,
browseAll
} from "node-opcua-pseudo-session";
import { DataTypeIds, ObjectIds, VariableTypeIds } from "node-opcua-constants";
import { DataType } from "node-opcua-variant";
import { ReferenceDescription } from "node-opcua-types";
import { makeBrowsePath } from "node-opcua-service-translate-browse-path";
Expand All @@ -13,7 +16,9 @@ import { populateDataTypeManager104 } from "./private/populate_data_type_manager
/**
* @private
*/
export async function serverImplementsDataTypeDefinition(session: IBasicSession): Promise<boolean> {
export async function serverImplementsDataTypeDefinition(
session: IBasicSessionAsync2
): Promise<boolean> {
// One way to figure out is to check if the server provides DataTypeDefinition node
// ( see OPCUA 1.04 part 6 -)
// This is the preferred route, as we go along, more and more servers will implement this.
Expand Down Expand Up @@ -106,7 +111,7 @@ export async function serverImplementsDataTypeDefinition(session: IBasicSession)
return false;
}

export async function populateDataTypeManager(session: IBasicSession, dataTypeManager: ExtraDataTypeManager): Promise<void> {
export async function populateDataTypeManager(session: IBasicSessionAsync2, dataTypeManager: ExtraDataTypeManager): Promise<void> {
const force104 = await serverImplementsDataTypeDefinition(session);
if (force104) {
await populateDataTypeManager104(session, dataTypeManager);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BrowseDirection, makeNodeClassMask, makeResultMask } from "node-opcua-data-model";
import { NodeId, resolveNodeId } from "node-opcua-nodeid";
import { IBasicSession, BrowseDescriptionLike, browseAll } from "node-opcua-pseudo-session";
import { BrowseDescriptionLike, browseAll, ISessionForBrowseAll } from "node-opcua-pseudo-session";
import { DataTypeAndEncodingId } from "node-opcua-schemas";

export async function _findEncodings(session: IBasicSession, dataTypeNodeId: NodeId): Promise<DataTypeAndEncodingId> {
export async function _findEncodings(session: ISessionForBrowseAll, dataTypeNodeId: NodeId): Promise<DataTypeAndEncodingId> {
const nodeToBrowse: BrowseDescriptionLike = {
browseDirection: BrowseDirection.Forward,
includeSubtypes: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AttributeIds, makeNodeClassMask, makeResultMask, NodeClassMask, Qualifi
import { checkDebugFlag, make_debugLog, make_errorLog, make_warningLog } from "node-opcua-debug";
import { ConstructorFuncWithSchema, DataTypeFactory, getStandardDataTypeFactory } from "node-opcua-factory";
import { ExpandedNodeId, NodeId, resolveNodeId, sameNodeId } from "node-opcua-nodeid";
import { browseAll, BrowseDescriptionLike, IBasicSession } from "node-opcua-pseudo-session";
import { browseAll, BrowseDescriptionLike, IBasicSessionAsync, IBasicSessionAsync2 } from "node-opcua-pseudo-session";
import {
createDynamicObjectConstructor,
DataTypeAndEncodingId,
Expand Down Expand Up @@ -52,7 +52,7 @@ const warningLog = make_warningLog(__filename);
// keep the current method to access type definition from embedded xsd.
//

async function _readDeprecatedFlag(session: IBasicSession, dataTypeDictionary: NodeId): Promise<boolean> {
async function _readDeprecatedFlag(session: IBasicSessionAsync, dataTypeDictionary: NodeId): Promise<boolean> {
const browsePath = makeBrowsePath(dataTypeDictionary, ".Deprecated");
const a = await session.translateBrowsePath(browsePath);
/* istanbul ignore next */
Expand All @@ -66,7 +66,7 @@ async function _readDeprecatedFlag(session: IBasicSession, dataTypeDictionary: N
return dataValue.value.value;
}

async function _readNamespaceUriProperty(session: IBasicSession, dataTypeDictionary: NodeId): Promise<string> {
async function _readNamespaceUriProperty(session: IBasicSessionAsync, dataTypeDictionary: NodeId): Promise<string> {
const a = await session.translateBrowsePath(makeBrowsePath(dataTypeDictionary, ".NamespaceUri"));
/* istanbul ignore next */
if (!a.targets || a.targets.length === 0) {
Expand All @@ -84,7 +84,7 @@ interface IDataTypeDescription {
symbolicName?: string;
}

async function _getDataTypeDescriptions(session: IBasicSession, dataTypeDictionaryNodeId: NodeId): Promise<IDataTypeDescription[]> {
async function _getDataTypeDescriptions(session: IBasicSessionAsync2, dataTypeDictionaryNodeId: NodeId): Promise<IDataTypeDescription[]> {
const nodeToBrowse2: BrowseDescriptionLike = {
browseDirection: BrowseDirection.Forward,
includeSubtypes: false,
Expand All @@ -99,7 +99,7 @@ async function _getDataTypeDescriptions(session: IBasicSession, dataTypeDictiona
return result2.references.map((r) => ({ nodeId: r.nodeId, browseName: r.browseName }));
}

async function _enrichWithDescriptionOf(session: IBasicSession, dataTypeDescriptions: IDataTypeDescription[]): Promise<NodeId[]> {
async function _enrichWithDescriptionOf(session: IBasicSessionAsync2, dataTypeDescriptions: IDataTypeDescription[]): Promise<NodeId[]> {
const nodesToBrowse3: BrowseDescriptionOptions[] = [];
for (const ref of dataTypeDescriptions) {
ref.browseName.toString();
Expand Down Expand Up @@ -258,7 +258,7 @@ function sortStructure(dataTypeDefinitions: DataTypeDefinitions) {
}

async function _extractDataTypeDictionaryFromDefinition(
session: IBasicSession,
session: IBasicSessionAsync2,
dataTypeDictionaryNodeId: NodeId,
dataTypeFactory: DataTypeFactory
) {
Expand Down Expand Up @@ -345,7 +345,7 @@ async function _extractDataTypeDictionaryFromDefinition(
}

async function _extractNodeIds(
session: IBasicSession,
session: IBasicSessionAsync2,
dataTypeDictionaryNodeId: NodeId
): Promise<MapDataTypeAndEncodingIdProvider> {
const map: { [key: string]: DataTypeAndEncodingId } = {};
Expand Down Expand Up @@ -381,7 +381,7 @@ function _isOldDataTypeDictionary(d: TypeDictionaryInfo) {
return !isDictionaryDeprecated && rawSchema.length >= 0;
}
async function _extractDataTypeDictionary(
session: IBasicSession,
session: IBasicSessionAsync2,
d: TypeDictionaryInfo,
dataTypeManager: ExtraDataTypeManager
): Promise<void> {
Expand Down Expand Up @@ -423,7 +423,7 @@ async function _extractDataTypeDictionary(
}

async function _exploreDataTypeDefinition(
session: IBasicSession,
session: IBasicSessionAsync2,
dataTypeDictionaryTypeNode: NodeId,
dataTypeFactory: DataTypeFactory,
namespaces: string[]
Expand Down Expand Up @@ -520,7 +520,7 @@ function extraNamespaceRef(attribute: string): { xmlns: string; namespace: strin
* @param dataTypeManager
* @async
*/
export async function populateDataTypeManager103(session: IBasicSession, dataTypeManager: ExtraDataTypeManager): Promise<void> {
export async function populateDataTypeManager103(session: IBasicSessionAsync2, dataTypeManager: ExtraDataTypeManager): Promise<void> {
debugLog("in ... populateDataTypeManager");

// read namespace array
Expand Down

0 comments on commit 9d7eeb3

Please sign in to comment.