I have a small nodejs script and I want to download a zip file:
const fs = requrie("fs");
const request = require("requestretry");
export class FileFetcher {
public async fetchFile(url: string): Promise<string> {
const fileName = "./download.zip";
return new Promise<string>(resolve => {
request(url)
.pipe(fs.createWriteStream(fileName))
.on("close", function () {
resolve(fileName);
});
});
}
}
I am using the requestretry
library, a wrapper around request
, as the file may only exists in the future and hence will most likely fail for the first couple times.
Yet I have to adapt to my strangely behaving external endpoint. It always returns a 200 OK
instead of an expected 404 Not Found
.
Hence, the only way to determine is the Content-Length
header in the response. If Content-Length: 0
then there is no file.
There seems to be different retry strategies in requestretry
, yet they all seem to assume a well-beaved api, e.g. request.RetryStrategies.HTTPOrNetworkError
. Since it gets a 200 it will never retry and "successfully" download an empty zip file.
I am not fixed on using requestretry
, I am wondering about how to retry a request based on the response headers.