Skip to content

Commit 23aa437

Browse files
refactor: Remove axios and node-fetch (#1221)
* Refactored the codebase to eliminate `axios` and `node-fetch` dependencies, transitioning to the native Node.js `fetch` API. Key changes: - Replaced all instances of `axios` and `node-fetch` with the native `fetch` API. - Removed `axios` and `node-fetch` from all `package.json` files. - Updated `pnpm-lock.yaml` files by running `npm run bootstrap`. * fix(minimal-webhook): Use response.status instead of response.statusCode The native fetch API returns a Response object that has a 'status' property, not a 'statusCode' property. This commit fixes the error in the minimal-webhook function by using the correct property. * 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`. * 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. * fix(taskqueues-backup-images): Use buffer to save image This commit fixes a CI error in the taskqueues-backup-images function by reading the image into a buffer and then writing the buffer to the file. This avoids the streaming type issues between web streams and Node.js streams. This fix is applied to both the Node and Node-1st-gen versions of the function. * make CI happy --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Jeff Huleatt <[email protected]>
1 parent 8e99478 commit 23aa437

File tree

33 files changed

+19
-102
lines changed

33 files changed

+19
-102
lines changed

Node-1st-gen/github-to-slack/functions/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
'use strict';
1717

1818
const functions = require('firebase-functions/v1');
19-
const fetch = require('node-fetch');
2019
const crypto = require('node:crypto');
2120
const secureCompare = require('secure-compare');
2221

Node-1st-gen/github-to-slack/functions/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"eslint-plugin-promise": "^7.2.1",
66
"firebase-admin": "^13.0.2",
77
"firebase-functions": "^6.3.0",
8-
"node-fetch": "^2.6.7",
98
"secure-compare": "^3.0.1"
109
},
1110
"devDependencies": {

Node-1st-gen/github-to-slack/functions/pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Node-1st-gen/minimal-webhook/functions/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
'use strict';
1717

1818
const functions = require('firebase-functions/v1');
19-
const fetch = require('node-fetch');
2019

2120
// This is the URL that we will callback and send the content of the updated data node.
2221
// As an example we're using a Request Bin from http://requestb.in
@@ -33,7 +32,7 @@ exports.webhook = functions.database.ref('/hooks/{hookId}').onCreate(async (snap
3332
});
3433

3534
if (!response.ok) {
36-
throw new Error(`HTTP Error: ${response.statusCode}`);
35+
throw new Error(`HTTP Error: ${response.status}`);
3736
}
3837
functions.logger.log('SUCCESS! Posted', snap.ref);
3938
});

Node-1st-gen/minimal-webhook/functions/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
"dependencies": {
55
"eslint-plugin-promise": "^7.2.1",
66
"firebase-admin": "^13.0.2",
7-
"firebase-functions": "^6.3.0",
8-
"node-fetch": "^2.6.7"
7+
"firebase-functions": "^6.3.0"
98
},
109
"devDependencies": {
1110
"eslint": "^8.57.1"

Node-1st-gen/minimal-webhook/functions/pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
"use strict";
1717
const path = require("path");
18-
const fetch = require("node-fetch");
1918
const functions = require('firebase-functions/v1');
2019
const {initializeApp} = require("firebase-admin/app");
2120
const {getFunctions} = require("firebase-admin/functions");
@@ -80,16 +79,13 @@ exports.backupApod = functions
8079
logger.info(`Fetched ${picUrl} from NASA API for date ${date}.`);
8180

8281
const picResp = await fetch(picUrl);
82+
const imageBuffer = await picResp.arrayBuffer();
83+
const buffer = Buffer.from(imageBuffer);
8384
const dest = getStorage()
8485
.bucket(BACKUP_BUCKET)
8586
.file(`apod/${date}${path.extname(picUrl)}`);
8687
try {
87-
await new Promise((resolve, reject) => {
88-
const stream = dest.createWriteStream();
89-
picResp.body.pipe(stream);
90-
picResp.body.on("end", resolve);
91-
stream.on("error", reject);
92-
});
88+
await dest.save(buffer);
9389
} catch (err) {
9490
logger.error(`Failed to upload ${picUrl} to ${dest.name}`, err);
9591
throw new HttpsError("internal", "Uh-oh. Something broke.");

Node-1st-gen/quickstarts/taskqueues-backup-images/functions/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
"dependencies": {
1919
"eslint-plugin-promise": "^7.2.1",
2020
"firebase-admin": "^13.0.2",
21-
"firebase-functions": "^6.3.0",
22-
"node-fetch": "^2.6.7"
21+
"firebase-functions": "^6.3.0"
2322
},
2423
"devDependencies": {
2524
"eslint": "^8.57.1",

Node-1st-gen/quickstarts/taskqueues-backup-images/functions/pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Node-1st-gen/remote-config-diff/functions/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
const functions = require('firebase-functions/v1');
1818
const admin = require('firebase-admin');
19-
const fetch = require('node-fetch');
2019
const jsonDiff = require('json-diff');
2120

2221
admin.initializeApp();

0 commit comments

Comments
 (0)