Skip to content

Commit 8027b13

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`. This fix is applied to both the `Node` and `Node-1st-gen` versions of the `taskqueues-backup-images` function.
1 parent 87609d3 commit 8027b13

File tree

1 file changed

+6
-6
lines changed
  • Node-1st-gen/quickstarts/taskqueues-backup-images/functions

1 file changed

+6
-6
lines changed

Node-1st-gen/quickstarts/taskqueues-backup-images/functions/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
const path = require("path");
1818
const functions = require('firebase-functions/v1');
1919
const {initializeApp} = require("firebase-admin/app");
20+
const {Readable} = require("stream");
21+
const {pipeline} = require("stream/promises");
2022
const {getFunctions} = require("firebase-admin/functions");
2123
const {getStorage} = require("firebase-admin/storage");
2224
const logger = functions.logger;
@@ -83,12 +85,10 @@ exports.backupApod = functions
8385
.bucket(BACKUP_BUCKET)
8486
.file(`apod/${date}${path.extname(picUrl)}`);
8587
try {
86-
await new Promise((resolve, reject) => {
87-
const stream = dest.createWriteStream();
88-
picResp.body.pipe(stream);
89-
picResp.body.on("end", resolve);
90-
stream.on("error", reject);
91-
});
88+
await pipeline(
89+
Readable.fromWeb(picResp.body),
90+
dest.createWriteStream()
91+
);
9292
} catch (err) {
9393
logger.error(`Failed to upload ${picUrl} to ${dest.name}`, err);
9494
throw new HttpsError("internal", "Uh-oh. Something broke.");

0 commit comments

Comments
 (0)