@@ -6,12 +6,14 @@ const pair = require('pull-pair')
66const Reader = require ( 'pull-reader' )
77const cat = require ( 'pull-cat' )
88const pull = require ( 'pull-stream' )
9+ const deferred = require ( 'pull-defer' )
910
1011const cryptoStreams = require ( './crypto' )
1112const NONCE_LENGTH = require ( './key-generator' ) . NONCE_LENGTH
1213
1314const log = debug ( 'libp2p:pnet' )
1415log . err = debug ( 'libp2p:pnet:err' )
16+ log . trace = debug ( 'libp2p:pnet:trace' )
1517
1618/**
1719 * Keeps track of the state of a given connection, such as the local psk
@@ -67,19 +69,21 @@ class State {
6769 // The outer stream needs to be returned before we setup the
6870 // rest of the streams, so we're delaying the execution
6971 setTimeout ( ( ) => {
70- // Read the nonce first, this queues up the read when data
71- // is available , so we will have the nonce before any
72- // decryption occurs
72+ // Read the nonce first, once we have it resolve the
73+ // deferred source , so we keep reading
74+ const deferredSource = deferred . source ( )
7375 this . rawReader . read ( NONCE_LENGTH , ( err , data ) => {
7476 if ( err ) {
7577 log . err ( 'There was an error attempting to read the nonce' , err )
7678 }
79+ log . trace ( 'remote nonce received' )
7780 this . remote . nonce = data
81+ deferredSource . resolve ( this . rawReader . read ( ) )
7882 } )
7983
8084 this . innerRawStream = {
8185 sink : this . rawPairStream . sink ,
82- source : this . rawReader . read ( )
86+ source : deferredSource
8387 }
8488
8589 // Create the pull exchange between the two inner streams
0 commit comments