@@ -135,9 +135,20 @@ const noop = () => {};
135135
136136const kPerfHooksNetConnectContext = Symbol ( 'kPerfHooksNetConnectContext' ) ;
137137
138- const dc = require ( 'diagnostics_channel' ) ;
139- const netClientSocketChannel = dc . channel ( 'net.client.socket' ) ;
140- const netServerSocketChannel = dc . channel ( 'net.server.socket' ) ;
138+ let netClientSocketChannel ;
139+ let netServerSocketChannel ;
140+ function lazyChannels ( ) {
141+ // TODO(joyeecheung): support diagnostics channels in the snapshot.
142+ // For now it is fine to create them lazily when there isn't a snapshot to
143+ // build. If users need the channels they would have to create them first
144+ // before invoking any built-ins that would publish to these channels
145+ // anyway.
146+ if ( netClientSocketChannel === undefined ) {
147+ const dc = require ( 'diagnostics_channel' ) ;
148+ netClientSocketChannel = dc . channel ( 'net.client.socket' ) ;
149+ netServerSocketChannel = dc . channel ( 'net.server.socket' ) ;
150+ }
151+ }
141152
142153const {
143154 hasObserver,
@@ -210,6 +221,7 @@ function connect(...args) {
210221 const options = normalized [ 0 ] ;
211222 debug ( 'createConnection' , normalized ) ;
212223 const socket = new Socket ( options ) ;
224+ lazyChannels ( ) ;
213225 if ( netClientSocketChannel . hasSubscribers ) {
214226 netClientSocketChannel . publish ( {
215227 socket,
@@ -1756,6 +1768,7 @@ function onconnection(err, clientHandle) {
17561768
17571769 DTRACE_NET_SERVER_CONNECTION ( socket ) ;
17581770 self . emit ( 'connection' , socket ) ;
1771+ lazyChannels ( ) ;
17591772 if ( netServerSocketChannel . hasSubscribers ) {
17601773 netServerSocketChannel . publish ( {
17611774 socket,
0 commit comments