Federer survives Tiafoe scare to advance at the US Open