Skip to content

Update remaining test targets to use undici #7761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 10, 2023
2 changes: 1 addition & 1 deletion integration/messaging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"express": "4.18.2",
"geckodriver": "2.0.4",
"mocha": "9.2.2",
"node-fetch": "2.6.7",
"undici": "5.26.5",
"selenium-assistant": "6.1.1"
}
}
4 changes: 2 additions & 2 deletions integration/messaging/test/utils/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

const fetch = require('node-fetch');
const undici = require('undici');
const FCM_SEND_ENDPOINT = 'https://fcm.googleapis.com/fcm/send';
// Rotatable fcm server key. It's generally a bad idea to expose server keys. The reason is to
// simplify testing process (no need to implement server side decryption of git secret). The
Expand All @@ -28,7 +28,7 @@ module.exports = async payload => {
'Requesting to send an FCM message with payload: ' + JSON.stringify(payload)
);

const response = await fetch(FCM_SEND_ENDPOINT, {
const response = await undici.fetch(FCM_SEND_ENDPOINT, {
method: 'POST',
body: JSON.stringify(payload),
headers: {
Expand Down
1 change: 1 addition & 0 deletions packages/rules-unit-testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"url": "https://github.com/firebase/firebase-js-sdk/issues"
},
"dependencies": {
"undici": "5.26.5",
"node-fetch": "2.6.7",
"@types/node-fetch": "2.6.4"
}
Expand Down
8 changes: 4 additions & 4 deletions packages/rules-unit-testing/src/impl/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { HostAndPort } from '../public_types';
import { makeUrl } from './url';
import fetch from 'node-fetch';
import { fetch as undiciFetch } from 'undici';

/**
* @private
Expand All @@ -29,7 +29,7 @@ export async function loadDatabaseRules(
): Promise<void> {
const url = makeUrl(hostAndPort, '/.settings/rules.json');
url.searchParams.append('ns', databaseName);
const resp = await fetch(url, {
const resp = await undiciFetch(url, {
method: 'PUT',
headers: { Authorization: 'Bearer owner' },
body: rules
Expand All @@ -48,7 +48,7 @@ export async function loadFirestoreRules(
projectId: string,
rules: string
): Promise<void> {
const resp = await fetch(
const resp = await undiciFetch(
makeUrl(hostAndPort, `/emulator/v1/projects/${projectId}:securityRules`),
{
method: 'PUT',
Expand All @@ -72,7 +72,7 @@ export async function loadStorageRules(
hostAndPort: HostAndPort,
rules: string
): Promise<void> {
const resp = await fetch(makeUrl(hostAndPort, '/internal/setRules'), {
const resp = await undiciFetch(makeUrl(hostAndPort, '/internal/setRules'), {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
Expand Down
4 changes: 2 additions & 2 deletions packages/rules-unit-testing/src/impl/test_environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import fetch from 'node-fetch';
import { fetch as undiciFetch } from 'undici';
import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';
import 'firebase/compat/database';
Expand Down Expand Up @@ -106,7 +106,7 @@ export class RulesTestEnvironmentImpl implements RulesTestEnvironment {
this.checkNotDestroyed();
assertEmulatorRunning(this.emulators, 'firestore');

const resp = await fetch(
const resp = await undiciFetch(
makeUrl(
this.emulators.firestore,
`/emulator/v1/projects/${this.projectId}/databases/(default)/documents`
Expand Down
6 changes: 3 additions & 3 deletions packages/rules-unit-testing/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from './impl/discovery';
import { fixHostname, makeUrl } from './impl/url';
import { HostAndPort } from './public_types';
import fetch from 'node-fetch';
import { fetch as undiciFetch } from 'undici';

/**
* Run a setup function with background Cloud Functions triggers disabled. This can be used to
Expand Down Expand Up @@ -79,7 +79,7 @@ export async function withFunctionTriggersDisabled<TResult>(
hub.host = fixHostname(hub.host);
makeUrl(hub, '/functions/disableBackgroundTriggers');
// Disable background triggers
const disableRes = await fetch(
const disableRes = await undiciFetch(
makeUrl(hub, '/functions/disableBackgroundTriggers'),
{
method: 'PUT'
Expand All @@ -98,7 +98,7 @@ export async function withFunctionTriggersDisabled<TResult>(
result = await maybeFn();
} finally {
// Re-enable background triggers
const enableRes = await fetch(
const enableRes = await undiciFetch(
makeUrl(hub, '/functions/enableBackgroundTriggers'),
{
method: 'PUT'
Expand Down
4 changes: 2 additions & 2 deletions packages/rules-unit-testing/test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('assertFails()', () => {

describe('withFunctionTriggersDisabled()', () => {
it('disabling function triggers does not throw, returns value', async function () {
const fetchSpy = sinon.spy(require('node-fetch'), 'default');
const fetchSpy = sinon.spy(require('undici'), 'fetch');

const res = await withFunctionTriggersDisabled(() => {
return Promise.resolve(1234);
Expand All @@ -176,7 +176,7 @@ describe('withFunctionTriggersDisabled()', () => {
});

it('disabling function triggers always re-enables, event when the function throws', async function () {
const fetchSpy = sinon.spy(require('node-fetch'), 'default');
const fetchSpy = sinon.spy(require('undici'), 'fetch');

const res = withFunctionTriggersDisabled(() => {
throw new Error('I throw!');
Expand Down
5 changes: 1 addition & 4 deletions packages/storage/src/implementation/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* limitations under the License.
*/

import { isNode } from '@firebase/util';
import { invalidArgument } from './error';

export function isJustDef<T>(p: T | null | undefined): p is T | null {
Expand All @@ -40,9 +39,7 @@ export function isNativeBlob(p: unknown): p is Blob {
}

export function isNativeBlobDefined(): boolean {
// Note: The `isNode()` check can be removed when `node-fetch` adds native Blob support
// PR: https://github.com/node-fetch/node-fetch/pull/1664
return typeof Blob !== 'undefined' && !isNode();
return typeof Blob !== 'undefined';
}

export function validateNumber(
Expand Down