Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,20 @@ export function execute(
};

const handleError = (err: Error) => {
reject(err);
if (timer) clearTimeout(timer);
reject(err);
};

const req = https.get(options, handleResponse).on("error", handleError);

if (timeout > 0) {
timer = setTimeout(() => {
reject(new RequestTimeoutError());
if (timer) clearTimeout(timer);
req.off("error", handleError);
req.on("error", () => {});
const timeoutError = new RequestTimeoutError();
req.destroy();
reject(timeoutError);
}, timeout);
}
});
Expand Down Expand Up @@ -163,6 +167,11 @@ export function uploadImage(

return new Promise((resolve, reject) => {
let timer: ReturnType<typeof setTimeout>;
const handleError = (error: Error) => {
if (timer) clearTimeout(timer);
reject(error);
};

const req = https.request(options, (resp) => {
resp.setEncoding("utf8");
let data = "";
Expand All @@ -173,14 +182,16 @@ export function uploadImage(
else reject(data);
});
});
req.on("error", (error) => {
if (timer) clearTimeout(timer);
reject(error);
});
req.on("error", handleError);

if (timeout > 0) {
timer = setTimeout(() => {
reject(new RequestTimeoutError());
if (timer) clearTimeout(timer);
req.off("error", handleError);
req.on("error", () => {});
const timeoutError = new RequestTimeoutError();
req.destroy();
reject(timeoutError);
}, timeout);
}
req.end(multipart.body);
Expand Down