@@ -141,6 +141,14 @@ function validateOpcUaClientOptions(opts: OpcUaClientOptions): void {
141141 throw new Error ( 'OPC UA browseFilter array must contain only strings' ) ;
142142 }
143143 }
144+ if ( opts . browseMaxDepth !== undefined ) {
145+ if (
146+ typeof opts . browseMaxDepth !== 'number' ||
147+ ! Number . isInteger ( opts . browseMaxDepth )
148+ ) {
149+ throw new Error ( 'OPC UA browseMaxDepth must be an integer' ) ;
150+ }
151+ }
144152 const stringFields : ( keyof OpcUaClientOptions ) [ ] = [
145153 'username' ,
146154 'password' ,
@@ -185,7 +193,12 @@ export class OpcUaClient {
185193 > &
186194 Pick <
187195 OpcUaClientOptions ,
188- 'username' | 'password' | 'applicationUri' | 'pkiFolder' | 'certificateSubject'
196+ | 'username'
197+ | 'password'
198+ | 'applicationUri'
199+ | 'pkiFolder'
200+ | 'certificateSubject'
201+ | 'browseMaxDepth'
189202 > ;
190203
191204 constructor (
@@ -206,6 +219,7 @@ export class OpcUaClient {
206219 applicationUri : opts . applicationUri ,
207220 pkiFolder : opts . pkiFolder ,
208221 certificateSubject : opts . certificateSubject ,
222+ browseMaxDepth : opts . browseMaxDepth ?? 25 ,
209223 } ;
210224 }
211225
@@ -504,8 +518,8 @@ export class OpcUaClient {
504518 const visited = new Set < string > ( ) ;
505519 let totalTx = 0 ;
506520
507- let frontier : Array < { nodeId : string ; parentId : string | null } > = [
508- { nodeId : seedNodeId , parentId : null } ,
521+ let frontier : Array < { nodeId : string ; parentId : string | null ; depth : number } > = [
522+ { nodeId : seedNodeId , parentId : null , depth : 0 } ,
509523 ] ;
510524
511525 const useParallel = this . _opts . browseStrategy !== 'browseAll' ;
@@ -555,10 +569,19 @@ export class OpcUaClient {
555569 if ( m !== null ) output . push ( m ) ;
556570 }
557571
558- if ( ! shouldRecurse || shouldRecurse ( ref , item . nodeId ) ) {
572+ const maxDepth = this . _opts . browseMaxDepth ;
573+ const nextDepth = item . depth + 1 ;
574+ const canRecurse =
575+ maxDepth === undefined || maxDepth < 0 || nextDepth <= maxDepth ;
576+
577+ if ( canRecurse && ( ! shouldRecurse || shouldRecurse ( ref , item . nodeId ) ) ) {
559578 if ( ! queued . has ( childId ) ) {
560579 queued . add ( childId ) ;
561- nextFrontier . push ( { nodeId : childId , parentId : item . nodeId } ) ;
580+ nextFrontier . push ( {
581+ nodeId : childId ,
582+ parentId : item . nodeId ,
583+ depth : nextDepth ,
584+ } ) ;
562585 }
563586 }
564587 }
0 commit comments