I have a proxy server that needs to make a request to an external API server to synthesize a voice from some text. According to the API docs, I will first receive a response with headers and then stream binary data, as the response body contains 'Transfer-Encoding: chunked'.
To handle this, I am using responseType: 'stream'
in my code:
export const synthesizeVoice = async (contentType: ContentType,
dataForSynthesize: string,
format: string = 'wav16',
voice: string = 'Nec_24000') => {
// Code implementation here...
};
My main issue is that my method returns the file path before receiving the full binary data, resulting in an incomplete audio file on the front end. How can I ensure that I get the complete binary data before sending it, or should I implement streaming requests on the front end as well?
Perhaps there is a better way to approach this problem?
UPDATE: Revised code snippet
axios({
// Unchanged original code
})
.then(resp => {
resp.data.pipe(file);
resp.data.on('end', () => {
file.close();
res.set({
'Content-Type': 'audio/wav16',
});
const resFile = fs.createReadStream(file.path).pipe(res);
});
})
.catch(err => {
console.log(err.message)
});