Здравствуйте. А не подскажите?
*stream.pipeline* всегда возвращает
Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close. Хотя стрим выбросил исключение
new Error('destroy').
В реальном приложении идет закачка большого файла, иногда сервер по таймауту скидывает соеднинение и при использовании stream.pipeline непонятно какая произошла ошибка, всегда возращается ERR_STREAM_PREMATURE_CLOSE.
Node.js: 15.5.0
Platform: Ubuntu 20.10 x64
/* eslint-disable */
import http from 'https';
import { createWriteStream } from 'fs';
import { pipeline } from 'stream/promises';
const httpRequest = url => new Promise(resolve => http.get(url, res => resolve(res)));
const FILENAME_TMP = './1.tmp'
const linkFile = 'https://nodejs.org/dist/v15.5.0/node-v15.5.0-linux-x64.tar.xz';
const downloadStream = await httpRequest(linkFile, { responseType: 'stream' });
// view error
downloadStream.on('error', err => console.log('onError', err));
// break download
setTimeout(() => downloadStream.destroy(new Error('destroy')), 1000);
try {
await pipeline(
downloadStream,
createWriteStream(FILENAME_TMP)
);
console.log('Done');
} catch (err) {
console.log('onPipeline', err);
}
onError Error: destroy
at Timeout._onTimeout (file:///home/user/Project/ufopParser/3.js:17:41)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7)
onPipeline Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
at new NodeError (node:internal/errors:278:15)
at IncomingMessage.onclose (node:internal/streams/end-of-stream:117:38)
at IncomingMessage.emit (node:events:376:20)
at IncomingMessage._destroy (node:_http_incoming:169:10)
at _destroy (node:internal/streams/destroy:67:23)
at IncomingMessage.destroy (node:internal/streams/destroy:59:5)
at Timeout._onTimeout (file:///home/user/Project/ufopParser/3.js:17:33)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7) {
code: 'ERR_STREAM_PREMATURE_CLOSE'
}
Спасибо