From d046afdffd0967bb5f06d94d08c4db3213c3033b Mon Sep 17 00:00:00 2001 From: Andrea Gueugnaut Date: Wed, 15 Jan 2025 00:36:44 +0100 Subject: [PATCH] feat(database): stream backup download --- src/commands/database.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/commands/database.js b/src/commands/database.js index 1af910fa..e7f41802 100644 --- a/src/commands/database.js +++ b/src/commands/database.js @@ -4,6 +4,7 @@ import { Logger } from '../logger.js'; import { formatTable as initFormatTable } from '../format-table.js'; import superagent from 'superagent'; import fs from 'node:fs'; +import { Writable } from 'node:stream'; import { findOwnerId } from '../models/addon.js'; import { resolveRealId, resolveAddonId } from '../models/ids-resolver.js'; @@ -80,14 +81,14 @@ export async function downloadBackups (params) { throw new Error('no backup with this ID'); } - const res = await superagent - .get(backup.download_url) - .responseType('blob'); - - if (output) { - fs.writeFileSync(output, res.body); - return; + const response = await fetch(backup.download_url); + if (!response.ok) { + throw new Error('Failed to download backup'); } - process.stdout.write(res.body); + await response + .body + .pipeTo( + Writable.toWeb(output ? fs.createWriteStream(output) : process.stdout), + ); }