utils: fix pipeRequest

This commit is contained in:
undefined committed 2024-03-07 17:26:36 +08:00
1 parent c7f8d3c340
commit 1b8f209429
1 file changed
+17 -14
+17 -14
View File
@@ -325,20 +325,23 @@ export async function extractZip(zip: AdmZip, dest: string, overwrite = false, s
export async function pipeRequest(req: superagent.Request, w: fs.WriteStream, timeout?: number, name?: string) {
try {
await req.buffer(false).timeout({
response: Math.min(10000, timeout),
deadline: timeout,
}).parse((resp, cb) => {
if (resp.statusCode !== 200) throw new Error(`${resp.statusCode}`);
else {
resp.pipe(w);
resp.on('end', () => {
cb(null, undefined);
});
resp.on('error', (err) => {
cb(err, undefined);
});
}
await new Promise((resolve, reject) => {
req.buffer(false).timeout({
response: Math.min(10000, timeout),
deadline: timeout,
}).parse((resp, cb) => {
if (resp.statusCode !== 200) throw new Error(`${resp.statusCode}`);
else {
resp.pipe(w);
resp.on('end', () => {
cb(null, undefined);
resolve(null);
});
resp.on('error', (err) => {
cb(err, undefined);
});
}
}).catch(reject);
});
} catch (e) {
throw new Error(`Download${e.errno === 'ETIMEDOUT' ? 'Timedout' : 'Error'}(${name ? `${name}, ` : ''}${e.message})`);