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) { export async function pipeRequest(req: superagent.Request, w: fs.WriteStream, timeout?: number, name?: string) {
try { try {
await req.buffer(false).timeout({ await new Promise((resolve, reject) => {
response: Math.min(10000, timeout), req.buffer(false).timeout({
deadline: timeout, response: Math.min(10000, timeout),
}).parse((resp, cb) => { deadline: timeout,
if (resp.statusCode !== 200) throw new Error(`${resp.statusCode}`); }).parse((resp, cb) => {
else { if (resp.statusCode !== 200) throw new Error(`${resp.statusCode}`);
resp.pipe(w); else {
resp.on('end', () => { resp.pipe(w);
cb(null, undefined); resp.on('end', () => {
}); cb(null, undefined);
resp.on('error', (err) => { resolve(null);
cb(err, undefined); });
}); resp.on('error', (err) => {
} cb(err, undefined);
});
}
}).catch(reject);
}); });
} catch (e) { } catch (e) {
throw new Error(`Download${e.errno === 'ETIMEDOUT' ? 'Timedout' : 'Error'}(${name ? `${name}, ` : ''}${e.message})`); throw new Error(`Download${e.errno === 'ETIMEDOUT' ? 'Timedout' : 'Error'}(${name ? `${name}, ` : ''}${e.message})`);