Skip to content

Commit 87609d3

Browse files
fix(taskqueues-backup-images): Use Readable.fromWeb to handle web stream
The native fetch API returns a web stream, which is not compatible with the Node.js stream API used by Firebase Storage. This commit fixes the issue by converting the web stream to a Node.js stream using `Readable.fromWeb` and then piping it to the destination using `pipeline`.
1 parent f3f8f61 commit 87609d3

File tree

1 file changed

+6
-6
lines changed
  • Node/taskqueues-backup-images/functions

1 file changed

+6
-6
lines changed

Node/taskqueues-backup-images/functions/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const path = require("path");
2626
const {initializeApp} = require("firebase-admin/app");
2727
const {getStorage} = require("firebase-admin/storage");
2828
const {GoogleAuth} = require("google-auth-library");
29+
const {Readable} = require("stream");
30+
const {pipeline} = require("stream/promises");
2931
// [END imports]
3032
initializeApp();
3133

@@ -88,12 +90,10 @@ exports.backupapod = onTaskDispatched(
8890
.bucket(BACKUP_BUCKET)
8991
.file(`apod/${date}${path.extname(picUrl)}`);
9092
try {
91-
await new Promise((resolve, reject) => {
92-
const stream = dest.createWriteStream();
93-
picResp.body.pipe(stream);
94-
picResp.body.on("end", resolve);
95-
stream.on("error", reject);
96-
});
93+
await pipeline(
94+
Readable.fromWeb(picResp.body),
95+
dest.createWriteStream()
96+
);
9797
} catch (err) {
9898
logger.error(`Failed to upload ${picUrl} to ${dest.name}`, err);
9999
throw new HttpsError("internal", "Uh-oh. Something broke.");

0 commit comments

Comments
 (0)