ipull
Version:
The only file downloader you'll ever need. For node.js and the browser, CLI and library for fast and reliable file downloads.
31 lines • 1.07 kB
JavaScript
import { promiseWithResolvers } from "./retry-async-statement.js";
export default async function streamResponse(stream, downloadEngine, smartSplit, onProgress) {
const { promise, resolve, reject } = promiseWithResolvers();
stream.on("data", (chunk) => {
smartSplit.addChunk(chunk);
onProgress?.(smartSplit.savedLength);
});
stream.on("close", () => {
smartSplit.closeAndSendLeftoversIfLengthIsUnknown();
resolve();
});
stream.on("error", (error) => {
reject(error);
});
const pause = stream.pause.bind(stream);
const resume = stream.resume.bind(stream);
const close = stream.destroy.bind(stream);
downloadEngine.on("paused", pause);
downloadEngine.on("resumed", resume);
downloadEngine.on("aborted", close);
try {
await promise;
}
finally {
downloadEngine.off("paused", pause);
downloadEngine.off("resumed", resume);
downloadEngine.off("aborted", close);
stream.destroy();
}
}
//# sourceMappingURL=stream-response.js.map