End stream when done and emit error when error callback is called by requirejs