Skip to content

Commit fd30f0c

Browse files
Using node-fetch
1 parent f7aaab9 commit fd30f0c

File tree

16 files changed

+42
-115
lines changed

16 files changed

+42
-115
lines changed

packages/firestore/.idea/runConfigurations/firestore_lite_Tests__Emulator_.xml

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

packages/firestore/lite/test/integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ function genericMutationTests(
625625
// The Transaction tests use backoff for updates that fail with failed
626626
// preconditions. This leads to test timeouts.
627627
// eslint-disable-next-line no-restricted-properties
628-
(testRunnerMayUseBackoff ? it.skip : it)(
628+
(testRunnerMayUseBackoff ? it.skip : it.only)(
629629
'enforces that document exists',
630630
() => {
631631
return withTestDoc(async docRef => {

packages/firestore/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"@grpc/grpc-js": "^1.0.0",
6565
"@grpc/proto-loader": "^0.5.0",
6666
"axios": "^0.19.2",
67+
"node-fetch": "2.6.0",
6768
"tslib": "^1.11.1"
6869
},
6970
"peerDependencies": {

packages/firestore/src/platform/base64.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
// This file is only used under ts-node.
1919
// eslint-disable-next-line @typescript-eslint/no-require-imports
20-
const platform = require(`./${process.env.TEST_PLATFORM}/base64`);
20+
const platform = require(`./${process.env.TEST_PLATFORM ?? 'node'}/base64`);
2121

2222
/** Converts a Base64 encoded string to a binary string. */
2323
export function decodeBase64(encoded: string): string {

packages/firestore/src/platform/browser_lite/connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ export { newConnectivityMonitor } from '../browser/connection';
2323

2424
/** Initializes the HTTP connection for the REST API. */
2525
export function newConnection(databaseInfo: DatabaseInfo): Promise<Connection> {
26-
return Promise.resolve(new FetchConnection(databaseInfo));
26+
return Promise.resolve(new FetchConnection(databaseInfo, fetch));
2727
}

packages/firestore/src/platform/browser_lite/fetch_connection.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@
1313
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
16-
*/
16+
*/import * as nodeFetch from 'node-fetch';
1717

1818
import { Token } from '../../api/credentials';
1919
import { Stream } from '../../remote/connection';
2020
import { mapCodeFromHttpStatus } from '../../remote/rpc_error';
21-
import { FirestoreError } from '../../util/error';
21+
import { FirestoreError } from '../../util/error';
2222
import { StringMap } from '../../util/types';
2323
import { RestConnection } from '../../remote/rest_connection';
24+
import {DatabaseInfo} from "../../core/database_info";
2425

2526
export class FetchConnection extends RestConnection {
27+
constructor(databaseInfo: DatabaseInfo, private readonly fetchImpl: typeof fetch) {
28+
super(databaseInfo);
29+
}
30+
2631
openStream<Req, Resp>(
2732
rpcName: string,
2833
token: Token | null
@@ -40,7 +45,7 @@ export class FetchConnection extends RestConnection {
4045
let response: Response;
4146

4247
try {
43-
response = await fetch(url, {
48+
response = await this.fetchImpl(url, {
4449
method: 'POST',
4550
headers,
4651
body: requestJson
@@ -51,7 +56,14 @@ export class FetchConnection extends RestConnection {
5156
'Request failed with error: ' + err.statusText
5257
);
5358
}
54-
55-
return JSON.parse(await response.text());
59+
60+
if (!response.ok) {
61+
throw new FirestoreError(
62+
mapCodeFromHttpStatus(response.status),
63+
'Request failed with error: ' + response.statusText
64+
);
65+
}
66+
67+
return response.json();
5668
}
5769
}

packages/firestore/src/platform/connection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ import {Connection} from "../remote/connection";
2020

2121
// This file is only used under ts-node.
2222
// eslint-disable-next-line @typescript-eslint/no-require-imports
23-
const platform = require(`./${process.env.TEST_PLATFORM}/connection`);
23+
const platform = require(`./${process.env.TEST_PLATFORM ?? 'node'}/connection`);
2424

2525
export function newConnectivityMonitor(): ConnectivityMonitor {
2626
return platform.newConnectivityMonitor();
2727
}
2828

29+
// TODO(firestorexp): This doesn't need to return a Promise
2930
export function newConnection(databaseInfo: DatabaseInfo): Promise<Connection> {
3031
return platform.newConnection(databaseInfo);
3132
}

packages/firestore/src/platform/dom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
// This file is only used under ts-node.
1919
// eslint-disable-next-line @typescript-eslint/no-require-imports
20-
const platform = require(`./${process.env.TEST_PLATFORM}/dom`);
20+
const platform = require(`./${process.env.TEST_PLATFORM ?? 'node'}/dom`);
2121

2222
/** The Platform's 'window' implementation or null if not available. */
2323
export function getWindow(): Window | null {

packages/firestore/src/platform/format_json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
// This file is only used under ts-node.
1919
// eslint-disable-next-line @typescript-eslint/no-require-imports
20-
const platform = require(`./${process.env.TEST_PLATFORM}/format_json`);
20+
const platform = require(`./${process.env.TEST_PLATFORM ?? 'node'}/format_json`);
2121

2222
/** Formats an object as a JSON string, suitable for logging. */
2323
export function formatJSON(value: unknown): string {

packages/firestore/src/platform/node_lite/connection.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { DatabaseInfo } from '../../core/database_info';
19-
import { Connection } from '../../remote/connection';
20-
import { HttpConnection } from './http_connection';
18+
import * as nodeFetch from 'node-fetch';
19+
20+
import {FetchConnection} from "../browser_lite/fetch_connection";
21+
import {DatabaseInfo} from "../../core/database_info";
22+
import {Connection} from "../../remote/connection";
2123

2224
export { newConnectivityMonitor } from '../browser/connection';
2325

2426
/** Initializes the HTTP connection for the REST API. */
2527
export function newConnection(databaseInfo: DatabaseInfo): Promise<Connection> {
26-
return Promise.resolve(new HttpConnection(databaseInfo));
28+
// node-fetch is meant to be API compatible with `fetch`, but its type don't
29+
// match 100%.
30+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
31+
return Promise.resolve(new FetchConnection(databaseInfo, nodeFetch as any));
2732
}

packages/firestore/src/platform/node_lite/http_connection.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

packages/firestore/src/platform/random_bytes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
// This file is only used under ts-node.
1919
// eslint-disable-next-line @typescript-eslint/no-require-imports
20-
const platform = require(`./${process.env.TEST_PLATFORM}/random_bytes`);
20+
const platform = require(`./${process.env.TEST_PLATFORM ?? 'node'}/random_bytes`);
2121

2222
/**
2323
* Generates `nBytes` of random bytes.

packages/firestore/src/platform/serializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {JsonProtoSerializer} from "../remote/serializer";
1919

2020
// This file is only used under ts-node.
2121
// eslint-disable-next-line @typescript-eslint/no-require-imports
22-
const platform = require(`./${process.env.TEST_PLATFORM}/serializer`);
22+
const platform = require(`./${process.env.TEST_PLATFORM ?? 'node'}/serializer`);
2323

2424
export function newSerializer(databaseId: DatabaseId): JsonProtoSerializer {
2525
return platform.newSerializer(databaseId);

packages/firestore/src/remote/rest_connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export abstract class RestConnection implements Connection {
9292
return response;
9393
},
9494
(err: FirestoreError) => {
95-
logWarn(LOG_TAG, `${rpcName} failed with error: `, err.message, 'url : ', url, 'request :', req);
95+
logWarn(LOG_TAG, `${rpcName} failed with error: `, err.message, 'url: ', url, 'request:', req);
9696
throw err;
9797
}
9898
);

packages/firestore/test/unit/remote/rest_connection.test.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,9 @@ describe('RestConnection', () => {
125125
expect(response).to.deep.equal({ response: true });
126126
});
127127

128-
it('returns error', async () => {
129-
connection.nextResponse = Promise.reject(
130-
new FirestoreError(Code.UNKNOWN, 'Test exception')
131-
);
132-
try {
133-
await connection.invokeRPC('RunQuery', {}, null);
134-
fail();
135-
} catch (e) {
136-
expect(e.code).to.equal(Code.UNKNOWN);
137-
}
128+
it('returns error', () => {
129+
const error = new FirestoreError(Code.UNKNOWN, 'Test exception');
130+
connection.nextResponse = Promise.reject(error);
131+
return expect(connection.invokeRPC('RunQuery', {}, null)).to.be.eventually.rejectedWith(error);
138132
});
139133
});

yarn.lock

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,35 +1068,6 @@
10681068
unique-filename "^1.1.1"
10691069
which "^1.3.1"
10701070

1071-
"@firebase/[email protected]":
1072-
version "0.1.16"
1073-
resolved "https://registry.npmjs.org/@firebase/component/-/component-0.1.16.tgz#7a0dbdfff1485d45b8485db87a982f053e68761a"
1074-
integrity sha512-FvffvFN0LWgv1H/FIyruTECOL69Dhy+JfwoTq+mV39V8Mz9lNpo41etonL5AOr7KmXxYJVbNwkx0L9Ei88i7JA==
1075-
dependencies:
1076-
"@firebase/util" "0.2.50"
1077-
tslib "^1.11.1"
1078-
1079-
"@firebase/[email protected]":
1080-
version "1.16.1"
1081-
resolved "https://registry.npmjs.org/@firebase/firestore/-/firestore-1.16.1.tgz#ddf4e357b4d847abe6a57a89d3e2d5b950339176"
1082-
integrity sha512-TGtvNIGHMEFFEuOSsRswou576GPZY39vXIsenn0B1Dqz9ACpyDtvAT9YdbG38srlPq7ZKwsP5x04LB43zZ6eAg==
1083-
dependencies:
1084-
"@firebase/component" "0.1.16"
1085-
"@firebase/firestore-types" "1.12.0"
1086-
"@firebase/logger" "0.2.6"
1087-
"@firebase/util" "0.2.50"
1088-
"@firebase/webchannel-wrapper" "0.2.41"
1089-
"@grpc/grpc-js" "^1.0.0"
1090-
"@grpc/proto-loader" "^0.5.0"
1091-
tslib "^1.11.1"
1092-
1093-
"@firebase/[email protected]":
1094-
version "0.2.50"
1095-
resolved "https://registry.npmjs.org/@firebase/util/-/util-0.2.50.tgz#77666b845dcb49bc217650aa296a7a8986c06b44"
1096-
integrity sha512-vFE6+Jfc25u0ViSpFxxq0q5s+XmuJ/y7CL3ud79RQe+WLFFg+j0eH1t23k0yNSG9vZNM7h3uHRIXbV97sYLAyw==
1097-
dependencies:
1098-
tslib "^1.11.1"
1099-
11001071
"@google-cloud/paginator@^2.0.0":
11011072
version "2.0.3"
11021073
resolved "https://registry.npmjs.org/@google-cloud/paginator/-/paginator-2.0.3.tgz#c7987ad05d1c3ebcef554381be80e9e8da4e4882"

0 commit comments

Comments
 (0)