@@ -131,9 +131,20 @@ const noop = () => {};
131
131
132
132
const kPerfHooksNetConnectContext = Symbol ( 'kPerfHooksNetConnectContext' ) ;
133
133
134
- const dc = require ( 'diagnostics_channel' ) ;
135
- const netClientSocketChannel = dc . channel ( 'net.client.socket' ) ;
136
- const netServerSocketChannel = dc . channel ( 'net.server.socket' ) ;
134
+ let netClientSocketChannel ;
135
+ let netServerSocketChannel ;
136
+ function lazyChannels ( ) {
137
+ // TODO(joyeecheung): support diagnostics channels in the snapshot.
138
+ // For now it is fine to create them lazily when there isn't a snapshot to
139
+ // build. If users need the channels they would have to create them first
140
+ // before invoking any built-ins that would publish to these channels
141
+ // anyway.
142
+ if ( netClientSocketChannel === undefined ) {
143
+ const dc = require ( 'diagnostics_channel' ) ;
144
+ netClientSocketChannel = dc . channel ( 'net.client.socket' ) ;
145
+ netServerSocketChannel = dc . channel ( 'net.server.socket' ) ;
146
+ }
147
+ }
137
148
138
149
const {
139
150
hasObserver,
@@ -206,6 +217,7 @@ function connect(...args) {
206
217
const options = normalized [ 0 ] ;
207
218
debug ( 'createConnection' , normalized ) ;
208
219
const socket = new Socket ( options ) ;
220
+ lazyChannels ( ) ;
209
221
if ( netClientSocketChannel . hasSubscribers ) {
210
222
netClientSocketChannel . publish ( {
211
223
socket,
@@ -1739,6 +1751,7 @@ function onconnection(err, clientHandle) {
1739
1751
socket . server = self ;
1740
1752
socket . _server = self ;
1741
1753
self . emit ( 'connection' , socket ) ;
1754
+ lazyChannels ( ) ;
1742
1755
if ( netServerSocketChannel . hasSubscribers ) {
1743
1756
netServerSocketChannel . publish ( {
1744
1757
socket,
0 commit comments