Skip to content

Commit de302d7

Browse files
Misc source cleanup for Firebase Storage
1 parent 66d4a1e commit de302d7

File tree

9 files changed

+75
-242
lines changed

9 files changed

+75
-242
lines changed

packages/storage/src/implementation/connection.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18-
/**
19-
* Network headers
20-
*/
21-
export interface Headers {
22-
[name: string]: string;
23-
}
18+
/** Network headers */
19+
export type Headers = Record<string, string>;
2420

2521
/**
2622
* A lightweight wrapper around XMLHttpRequest with a

packages/storage/src/implementation/error.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,7 @@ export function invalidRootOperation(name: string): StorageError {
312312
* @param format - The format that was not valid.
313313
* @param message - A message describing the format violation.
314314
*/
315-
export function invalidFormat(
316-
format: string,
317-
message: string
318-
): StorageError {
315+
export function invalidFormat(format: string, message: string): StorageError {
319316
return new StorageError(
320317
StorageErrorCode.INVALID_FORMAT,
321318
"String does not match format '" + format + "': " + message
@@ -326,10 +323,7 @@ export function invalidFormat(
326323
* @param message - A message describing the internal error.
327324
*/
328325
export function unsupportedEnvironment(message: string): StorageError {
329-
throw new StorageError(
330-
StorageErrorCode.UNSUPPORTED_ENVIRONMENT,
331-
message
332-
);
326+
throw new StorageError(StorageErrorCode.UNSUPPORTED_ENVIRONMENT, message);
333327
}
334328

335329
/**

packages/storage/src/implementation/request.ts

Lines changed: 43 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
canceled,
2929
retryLimitExceeded
3030
} from './error';
31-
import { RequestInfo } from './requestinfo';
31+
import { RequestHandler, RequestInfo } from './requestinfo';
3232
import { isJustDef } from './type';
3333
import { makeQueryString } from './url';
3434
import { Headers, Connection, ErrorCode } from './connection';
@@ -48,54 +48,28 @@ export interface Request<T> {
4848
}
4949

5050
class NetworkRequest<T> implements Request<T> {
51-
private url_: string;
52-
private method_: string;
53-
private headers_: Headers;
54-
private body_: string | Blob | Uint8Array | null;
55-
private successCodes_: number[];
56-
private additionalRetryCodes_: number[];
5751
private pendingConnection_: Connection | null = null;
5852
private backoffId_: backoffId | null = null;
5953
private resolve_!: (value?: T | PromiseLike<T>) => void;
6054
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6155
private reject_!: (reason?: any) => void;
6256
private canceled_: boolean = false;
6357
private appDelete_: boolean = false;
64-
private callback_: (p1: Connection, p2: string) => T;
65-
private errorCallback_:
66-
| ((p1: Connection, p2: StorageError) => StorageError)
67-
| null;
68-
private progressCallback_: ((p1: number, p2: number) => void) | null;
69-
private timeout_: number;
70-
private pool_: ConnectionPool;
7158
promise_: Promise<T>;
7259

7360
constructor(
74-
url: string,
75-
method: string,
76-
headers: Headers,
77-
body: string | Blob | Uint8Array | null,
78-
successCodes: number[],
79-
additionalRetryCodes: number[],
80-
callback: (p1: Connection, p2: string) => T,
81-
errorCallback:
82-
| ((p1: Connection, p2: StorageError) => StorageError)
83-
| null,
84-
timeout: number,
85-
progressCallback: ((p1: number, p2: number) => void) | null,
86-
pool: ConnectionPool
61+
private url_: string,
62+
private method_: string,
63+
private headers_: Headers,
64+
private body_: string | Blob | Uint8Array | null,
65+
private successCodes_: number[],
66+
private additionalRetryCodes_: number[],
67+
private callback_: RequestHandler<string, T>,
68+
private errorCallback_: RequestHandler<StorageError, StorageError> | null,
69+
private timeout_: number,
70+
private progressCallback_: ((p1: number, p2: number) => void) | null,
71+
private pool_: ConnectionPool
8772
) {
88-
this.url_ = url;
89-
this.method_ = method;
90-
this.headers_ = headers;
91-
this.body_ = body;
92-
this.successCodes_ = successCodes.slice();
93-
this.additionalRetryCodes_ = additionalRetryCodes.slice();
94-
this.callback_ = callback;
95-
this.errorCallback_ = errorCallback;
96-
this.progressCallback_ = progressCallback;
97-
this.timeout_ = timeout;
98-
this.pool_ = pool;
9973
this.promise_ = new Promise((resolve, reject) => {
10074
this.resolve_ = resolve as (value?: T | PromiseLike<T>) => void;
10175
this.reject_ = reject;
@@ -107,67 +81,68 @@ class NetworkRequest<T> implements Request<T> {
10781
* Actually starts the retry loop.
10882
*/
10983
private start_(): void {
110-
const self = this;
111-
112-
function doTheRequest(
113-
backoffCallback: (p1: boolean, ...p2: unknown[]) => void,
84+
const doTheRequest: (
85+
backoffCallback: (success: boolean, ...p2: unknown[]) => void,
11486
canceled: boolean
115-
): void {
87+
) => void = (backoffCallback, canceled) => {
11688
if (canceled) {
11789
backoffCallback(false, new RequestEndStatus(false, null, true));
11890
return;
11991
}
120-
const connection = self.pool_.createConnection();
121-
self.pendingConnection_ = connection;
92+
const connection = this.pool_.createConnection();
93+
this.pendingConnection_ = connection;
12294

123-
function progressListener(progressEvent: ProgressEvent): void {
124-
const loaded = progressEvent.loaded;
125-
const total = progressEvent.lengthComputable ? progressEvent.total : -1;
126-
if (self.progressCallback_ !== null) {
127-
self.progressCallback_(loaded, total);
128-
}
129-
}
130-
if (self.progressCallback_ !== null) {
95+
const progressListener: (progressEvent: ProgressEvent) => void =
96+
progressEvent => {
97+
const loaded = progressEvent.loaded;
98+
const total = progressEvent.lengthComputable
99+
? progressEvent.total
100+
: -1;
101+
if (this.progressCallback_ !== null) {
102+
this.progressCallback_(loaded, total);
103+
}
104+
};
105+
if (this.progressCallback_ !== null) {
131106
connection.addUploadProgressListener(progressListener);
132107
}
133108

134109
// eslint-disable-next-line @typescript-eslint/no-floating-promises
135110
connection
136-
.send(self.url_, self.method_, self.body_, self.headers_)
111+
.send(this.url_, this.method_, this.body_, this.headers_)
137112
.then(() => {
138-
if (self.progressCallback_ !== null) {
113+
if (this.progressCallback_ !== null) {
139114
connection.removeUploadProgressListener(progressListener);
140115
}
141-
self.pendingConnection_ = null;
116+
this.pendingConnection_ = null;
142117
const hitServer = connection.getErrorCode() === ErrorCode.NO_ERROR;
143118
const status = connection.getStatus();
144-
if (!hitServer || self.isRetryStatusCode_(status)) {
119+
if (!hitServer || this.isRetryStatusCode_(status)) {
145120
const wasCanceled = connection.getErrorCode() === ErrorCode.ABORT;
146121
backoffCallback(
147122
false,
148123
new RequestEndStatus(false, null, wasCanceled)
149124
);
150125
return;
151126
}
152-
const successCode = self.successCodes_.indexOf(status) !== -1;
127+
const successCode = this.successCodes_.indexOf(status) !== -1;
153128
backoffCallback(true, new RequestEndStatus(successCode, connection));
154129
});
155-
}
130+
};
156131

157132
/**
158133
* @param requestWentThrough - True if the request eventually went
159134
* through, false if it hit the retry limit or was canceled.
160135
*/
161-
function backoffDone(
136+
const backoffDone: (
162137
requestWentThrough: boolean,
163138
status: RequestEndStatus
164-
): void {
165-
const resolve = self.resolve_;
166-
const reject = self.reject_;
139+
) => void = (requestWentThrough, status) => {
140+
const resolve = this.resolve_;
141+
const reject = this.reject_;
167142
const connection = status.connection as Connection;
168143
if (status.wasSuccessCode) {
169144
try {
170-
const result = self.callback_(
145+
const result = this.callback_(
171146
connection,
172147
connection.getResponseText()
173148
);
@@ -183,22 +158,22 @@ class NetworkRequest<T> implements Request<T> {
183158
if (connection !== null) {
184159
const err = unknown();
185160
err.serverResponse = connection.getResponseText();
186-
if (self.errorCallback_) {
187-
reject(self.errorCallback_(connection, err));
161+
if (this.errorCallback_) {
162+
reject(this.errorCallback_(connection, err));
188163
} else {
189164
reject(err);
190165
}
191166
} else {
192167
if (status.canceled) {
193-
const err = self.appDelete_ ? appDeleted() : canceled();
168+
const err = this.appDelete_ ? appDeleted() : canceled();
194169
reject(err);
195170
} else {
196171
const err = retryLimitExceeded();
197172
reject(err);
198173
}
199174
}
200175
}
201-
}
176+
};
202177
if (this.canceled_) {
203178
backoffDone(false, new RequestEndStatus(false, null, true));
204179
} else {

packages/storage/src/implementation/requestinfo.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,20 @@ export interface UrlParams {
2424
[name: string]: string | number;
2525
}
2626

27+
/**
28+
* A function that converts a server response to the API type expected by the
29+
* SDK.
30+
*
31+
* @param I - the type of the backend's network response
32+
* @param O - the output response type used by the rest of the SDK.
33+
*/
34+
export type RequestHandler<I, O> = (connection: Connection, response: I) => O;
35+
2736
export class RequestInfo<T> {
2837
urlParams: UrlParams = {};
2938
headers: Headers = {};
3039
body: Blob | string | Uint8Array | null = null;
31-
32-
errorHandler:
33-
| ((p1: Connection, p2: StorageError) => StorageError)
34-
| null = null;
40+
errorHandler: RequestHandler<StorageError, StorageError> | null = null;
3541

3642
/**
3743
* Called with the current number of bytes uploaded and total size (-1 if not
@@ -51,7 +57,7 @@ export class RequestInfo<T> {
5157
* Note: The XhrIo passed to this function may be reused after this callback
5258
* returns. Do not keep a reference to it in any way.
5359
*/
54-
public handler: (p1: Connection, p2: string) => T,
60+
public handler: RequestHandler<string, T>,
5561
public timeout: number
5662
) {}
5763
}

packages/storage/src/platform/browser/connection.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ export class XhrConnection implements Connection {
5050
});
5151
}
5252

53-
/**
54-
* @override
55-
*/
5653
send(
5754
url: string,
5855
method: string,
@@ -79,19 +76,13 @@ export class XhrConnection implements Connection {
7976
return this.sendPromise_;
8077
}
8178

82-
/**
83-
* @override
84-
*/
8579
getErrorCode(): ErrorCode {
8680
if (!this.sent_) {
8781
throw internalError('cannot .getErrorCode() before sending');
8882
}
8983
return this.errorCode_;
9084
}
9185

92-
/**
93-
* @override
94-
*/
9586
getStatus(): number {
9687
if (!this.sent_) {
9788
throw internalError('cannot .getStatus() before sending');
@@ -103,43 +94,28 @@ export class XhrConnection implements Connection {
10394
}
10495
}
10596

106-
/**
107-
* @override
108-
*/
10997
getResponseText(): string {
11098
if (!this.sent_) {
11199
throw internalError('cannot .getResponseText() before sending');
112100
}
113101
return this.xhr_.responseText;
114102
}
115103

116-
/**
117-
* Aborts the request.
118-
* @override
119-
*/
104+
/** Aborts the request. */
120105
abort(): void {
121106
this.xhr_.abort();
122107
}
123108

124-
/**
125-
* @override
126-
*/
127109
getResponseHeader(header: string): string | null {
128110
return this.xhr_.getResponseHeader(header);
129111
}
130112

131-
/**
132-
* @override
133-
*/
134113
addUploadProgressListener(listener: (p1: ProgressEvent) => void): void {
135114
if (this.xhr_.upload != null) {
136115
this.xhr_.upload.addEventListener('progress', listener);
137116
}
138117
}
139118

140-
/**
141-
* @override
142-
*/
143119
removeUploadProgressListener(listener: (p1: ProgressEvent) => void): void {
144120
if (this.xhr_.upload != null) {
145121
this.xhr_.upload.removeEventListener('progress', listener);

packages/storage/src/task.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,7 @@ export class UploadTask {
521521
/**
522522
* Equivalent to calling `then(null, onRejected)`.
523523
*/
524-
catch<T>(
525-
onRejected: (p1: StorageError) => T | Promise<T>
526-
): Promise<T> {
524+
catch<T>(onRejected: (p1: StorageError) => T | Promise<T>): Promise<T> {
527525
return this.then(null, onRejected);
528526
}
529527

packages/storage/test/unit/connection.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ import {
1919
Headers,
2020
Connection
2121
} from '../../src/implementation/connection';
22-
import {
23-
StorageError,
24-
StorageErrorCode
25-
} from '../../src/implementation/error';
22+
import { StorageError, StorageErrorCode } from '../../src/implementation/error';
2623

2724
export type SendHook = (
2825
connection: TestingConnection,
@@ -67,10 +64,7 @@ export class TestingConnection implements Connection {
6764
headers?: Headers
6865
): Promise<void> {
6966
if (this.state !== State.START) {
70-
throw new StorageError(
71-
StorageErrorCode.UNKNOWN,
72-
"Can't send again"
73-
);
67+
throw new StorageError(StorageErrorCode.UNKNOWN, "Can't send again");
7468
}
7569

7670
this.state = State.SENT;

0 commit comments

Comments
 (0)