Skip to content

Do not throw from connection.send in Node.js #5578

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 4 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-peaches-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/storage": patch
---

Do not throw from `connection.send` to enable retries in Node.js.
4 changes: 4 additions & 0 deletions packages/storage/src/implementation/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export type Headers = Record<string, string>;
* goog.net.XhrIo-like interface.
*/
export interface Connection {
/**
* This method should never reject. In case of encountering an error, set an error code internally which can be accessed
* by calling getErrorCode() by callers.
*/
send(
url: string,
method: string,
Expand Down
1 change: 1 addition & 0 deletions packages/storage/src/implementation/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class NetworkRequest<T> implements Request<T> {
connection.addUploadProgressListener(progressListener);
}

// connection.send() never rejects, so we don't need to have a error handler or use catch on the returned promise.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
connection
.send(this.url_, this.method_, this.body_, this.headers_)
Expand Down
13 changes: 8 additions & 5 deletions packages/storage/src/platform/node/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { ErrorCode, Connection } from '../../implementation/connection';
import { internalError } from '../../implementation/error';
import nodeFetch from 'node-fetch';
import nodeFetch, { FetchError } from 'node-fetch';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const fetch: typeof window.fetch = nodeFetch as any;
Expand Down Expand Up @@ -58,10 +58,13 @@ export class FetchConnection implements Connection {
.then(resp => {
this.headers_ = resp.headers;
this.statusCode_ = resp.status;
return resp.text();
})
.then(body => {
this.body_ = body;
return resp.text().then(body => {
this.body_ = body;
});
}, (_err: FetchError) => {
this.errorCode_ = ErrorCode.NETWORK_ERROR;
// emulate XHR which sets status to 0 when encountering a network error
this.statusCode_ = 0;
});
}

Expand Down