@@ -135,9 +135,20 @@ const noop = () => {};
135
135
136
136
const kPerfHooksNetConnectContext = Symbol ( 'kPerfHooksNetConnectContext' ) ;
137
137
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
+ }
141
152
142
153
const {
143
154
hasObserver,
@@ -210,6 +221,7 @@ function connect(...args) {
210
221
const options = normalized [ 0 ] ;
211
222
debug ( 'createConnection' , normalized ) ;
212
223
const socket = new Socket ( options ) ;
224
+ lazyChannels ( ) ;
213
225
if ( netClientSocketChannel . hasSubscribers ) {
214
226
netClientSocketChannel . publish ( {
215
227
socket,
@@ -1756,6 +1768,7 @@ function onconnection(err, clientHandle) {
1756
1768
1757
1769
DTRACE_NET_SERVER_CONNECTION ( socket ) ;
1758
1770
self . emit ( 'connection' , socket ) ;
1771
+ lazyChannels ( ) ;
1759
1772
if ( netServerSocketChannel . hasSubscribers ) {
1760
1773
netServerSocketChannel . publish ( {
1761
1774
socket,
0 commit comments