Skip to content

Commit 0fe7877

Browse files
Feedback
1 parent f1f71fa commit 0fe7877

21 files changed

+199
-183
lines changed

packages/storage/exp/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
// eslint-disable-next-line import/no-extraneous-dependencies
2929
} from '@firebase/app-exp';
3030

31-
import { XhrIoPool } from '../src/implementation/xhriopool';
31+
import { ConnectionPool } from '../src/implementation/connectionPool';
3232
import {
3333
StorageService as StorageServiceInternal,
3434
useStorageEmulator as useEmulatorInternal
@@ -76,7 +76,7 @@ function factory(
7676
app,
7777
authProvider,
7878
appCheckProvider,
79-
new XhrIoPool(),
79+
new ConnectionPool(),
8080
url,
8181
SDK_VERSION
8282
);

packages/storage/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { _FirebaseNamespace } from '@firebase/app-types/private';
2020
import { StringFormat } from './src/implementation/string';
2121
import { TaskEvent, TaskState } from './src/implementation/taskenums';
2222

23-
import { XhrIoPool } from './src/implementation/xhriopool';
23+
import { ConnectionPool } from './src/implementation/connectionPool';
2424
import { ReferenceCompat } from './compat/reference';
2525
import { StorageServiceCompat } from './compat/service';
2626
import { StorageService } from './src/service';
@@ -59,7 +59,7 @@ function factory(
5959
app,
6060
authProvider,
6161
appCheckProvider,
62-
new XhrIoPool(),
62+
new ConnectionPool(),
6363
url,
6464
firebase.SDK_VERSION
6565
)

packages/storage/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = function (config) {
3131
};
3232

3333
function getTestFiles(argv) {
34+
process.env.TEST_PLATFORM = 'browser';
3435
let unitTestFiles = ['test/unit/*'];
3536
let integrationTestFiles = [];
3637
if (argv.exp) {

packages/storage/src/implementation/xhrio.ts renamed to packages/storage/src/implementation/connection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
/**
19-
* XHR headers
19+
* Network headers
2020
*/
2121
export interface Headers {
2222
[name: string]: string;
@@ -26,12 +26,12 @@ export interface Headers {
2626
* A lightweight wrapper around XMLHttpRequest with a
2727
* goog.net.XhrIo-like interface.
2828
*/
29-
export interface XhrIo {
29+
export interface Connection {
3030
send(
3131
url: string,
3232
method: string,
3333
body?: ArrayBufferView | Blob | string | null,
34-
headers?: Record<string, string>
34+
headers?: Headers
3535
): Promise<void>;
3636

3737
getErrorCode(): ErrorCode;

packages/storage/src/implementation/xhriopool.ts renamed to packages/storage/src/implementation/connectionPool.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
/**
1919
* @fileoverview Replacement for goog.net.XhrIoPool that works with fbs.XhrIo.
2020
*/
21-
import { XhrIo } from './xhrio';
21+
import { Connection } from './connection';
2222
import { newConnection } from '../platform/connection';
2323

2424
/**
2525
* Factory-like class for creating XhrIo instances.
2626
*/
27-
export class XhrIoPool {
28-
createXhrIo(): XhrIo {
27+
export class ConnectionPool {
28+
createConnection(): Connection {
2929
return newConnection();
3030
}
3131
}

packages/storage/src/implementation/request.ts

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import {
3131
import { RequestInfo } from './requestinfo';
3232
import { isJustDef } from './type';
3333
import { makeQueryString } from './url';
34-
import { Headers, XhrIo, ErrorCode } from './xhrio';
35-
import { XhrIoPool } from './xhriopool';
34+
import { Headers, Connection, ErrorCode } from './connection';
35+
import { ConnectionPool } from './connectionPool';
3636

3737
export interface Request<T> {
3838
getPromise(): Promise<T>;
@@ -54,20 +54,20 @@ class NetworkRequest<T> implements Request<T> {
5454
private body_: string | Blob | Uint8Array | null;
5555
private successCodes_: number[];
5656
private additionalRetryCodes_: number[];
57-
private pendingXhr_: XhrIo | null = null;
57+
private pendingConnection_: Connection | null = null;
5858
private backoffId_: backoffId | null = null;
5959
private resolve_!: (value?: T | PromiseLike<T>) => void;
6060
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6161
private reject_!: (reason?: any) => void;
6262
private canceled_: boolean = false;
6363
private appDelete_: boolean = false;
64-
private callback_: (p1: XhrIo, p2: string) => T;
64+
private callback_: (p1: Connection, p2: string) => T;
6565
private errorCallback_:
66-
| ((p1: XhrIo, p2: FirebaseStorageError) => FirebaseStorageError)
66+
| ((p1: Connection, p2: FirebaseStorageError) => FirebaseStorageError)
6767
| null;
6868
private progressCallback_: ((p1: number, p2: number) => void) | null;
6969
private timeout_: number;
70-
private pool_: XhrIoPool;
70+
private pool_: ConnectionPool;
7171
promise_: Promise<T>;
7272

7373
constructor(
@@ -77,13 +77,13 @@ class NetworkRequest<T> implements Request<T> {
7777
body: string | Blob | Uint8Array | null,
7878
successCodes: number[],
7979
additionalRetryCodes: number[],
80-
callback: (p1: XhrIo, p2: string) => T,
80+
callback: (p1: Connection, p2: string) => T,
8181
errorCallback:
82-
| ((p1: XhrIo, p2: FirebaseStorageError) => FirebaseStorageError)
82+
| ((p1: Connection, p2: FirebaseStorageError) => FirebaseStorageError)
8383
| null,
8484
timeout: number,
8585
progressCallback: ((p1: number, p2: number) => void) | null,
86-
pool: XhrIoPool
86+
pool: ConnectionPool
8787
) {
8888
this.url_ = url;
8989
this.method_ = method;
@@ -117,8 +117,8 @@ class NetworkRequest<T> implements Request<T> {
117117
backoffCallback(false, new RequestEndStatus(false, null, true));
118118
return;
119119
}
120-
const xhr = self.pool_.createXhrIo();
121-
self.pendingXhr_ = xhr;
120+
const connection = self.pool_.createConnection();
121+
self.pendingConnection_ = connection;
122122

123123
function progressListener(progressEvent: ProgressEvent): void {
124124
const loaded = progressEvent.loaded;
@@ -128,28 +128,30 @@ class NetworkRequest<T> implements Request<T> {
128128
}
129129
}
130130
if (self.progressCallback_ !== null) {
131-
xhr.addUploadProgressListener(progressListener);
131+
connection.addUploadProgressListener(progressListener);
132132
}
133133

134134
// eslint-disable-next-line @typescript-eslint/no-floating-promises
135-
xhr.send(self.url_, self.method_, self.body_, self.headers_).then(() => {
136-
if (self.progressCallback_ !== null) {
137-
xhr.removeUploadProgressListener(progressListener);
138-
}
139-
self.pendingXhr_ = null;
140-
const hitServer = xhr.getErrorCode() === ErrorCode.NO_ERROR;
141-
const status = xhr.getStatus();
142-
if (!hitServer || self.isRetryStatusCode_(status)) {
143-
const wasCanceled = xhr.getErrorCode() === ErrorCode.ABORT;
144-
backoffCallback(
145-
false,
146-
new RequestEndStatus(false, null, wasCanceled)
147-
);
148-
return;
149-
}
150-
const successCode = self.successCodes_.indexOf(status) !== -1;
151-
backoffCallback(true, new RequestEndStatus(successCode, xhr));
152-
});
135+
connection
136+
.send(self.url_, self.method_, self.body_, self.headers_)
137+
.then(() => {
138+
if (self.progressCallback_ !== null) {
139+
connection.removeUploadProgressListener(progressListener);
140+
}
141+
self.pendingConnection_ = null;
142+
const hitServer = connection.getErrorCode() === ErrorCode.NO_ERROR;
143+
const status = connection.getStatus();
144+
if (!hitServer || self.isRetryStatusCode_(status)) {
145+
const wasCanceled = connection.getErrorCode() === ErrorCode.ABORT;
146+
backoffCallback(
147+
false,
148+
new RequestEndStatus(false, null, wasCanceled)
149+
);
150+
return;
151+
}
152+
const successCode = self.successCodes_.indexOf(status) !== -1;
153+
backoffCallback(true, new RequestEndStatus(successCode, connection));
154+
});
153155
}
154156

155157
/**
@@ -162,10 +164,13 @@ class NetworkRequest<T> implements Request<T> {
162164
): void {
163165
const resolve = self.resolve_;
164166
const reject = self.reject_;
165-
const xhr = status.xhr as XhrIo;
167+
const connection = status.connection as Connection;
166168
if (status.wasSuccessCode) {
167169
try {
168-
const result = self.callback_(xhr, xhr.getResponseText());
170+
const result = self.callback_(
171+
connection,
172+
connection.getResponseText()
173+
);
169174
if (isJustDef(result)) {
170175
resolve(result);
171176
} else {
@@ -175,11 +180,11 @@ class NetworkRequest<T> implements Request<T> {
175180
reject(e);
176181
}
177182
} else {
178-
if (xhr !== null) {
183+
if (connection !== null) {
179184
const err = unknown();
180-
err.serverResponse = xhr.getResponseText();
185+
err.serverResponse = connection.getResponseText();
181186
if (self.errorCallback_) {
182-
reject(self.errorCallback_(xhr, err));
187+
reject(self.errorCallback_(connection, err));
183188
} else {
184189
reject(err);
185190
}
@@ -213,8 +218,8 @@ class NetworkRequest<T> implements Request<T> {
213218
if (this.backoffId_ !== null) {
214219
stop(this.backoffId_);
215220
}
216-
if (this.pendingXhr_ !== null) {
217-
this.pendingXhr_.abort();
221+
if (this.pendingConnection_ !== null) {
222+
this.pendingConnection_.abort();
218223
}
219224
}
220225

@@ -247,7 +252,7 @@ export class RequestEndStatus {
247252

248253
constructor(
249254
public wasSuccessCode: boolean,
250-
public xhr: XhrIo | null,
255+
public connection: Connection | null,
251256
canceled?: boolean
252257
) {
253258
this.canceled = !!canceled;
@@ -291,7 +296,7 @@ export function makeRequest<T>(
291296
appId: string | null,
292297
authToken: string | null,
293298
appCheckToken: string | null,
294-
pool: XhrIoPool,
299+
pool: ConnectionPool,
295300
firebaseVersion?: string
296301
): Request<T> {
297302
const queryPart = makeQueryString(requestInfo.urlParams);

packages/storage/src/implementation/requestinfo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717
import { FirebaseStorageError } from './error';
18-
import { Headers, XhrIo } from './xhrio';
18+
import { Headers, Connection } from './connection';
1919

2020
/**
2121
* Type for url params stored in RequestInfo.
@@ -30,7 +30,7 @@ export class RequestInfo<T> {
3030
body: Blob | string | Uint8Array | null = null;
3131

3232
errorHandler:
33-
| ((p1: XhrIo, p2: FirebaseStorageError) => FirebaseStorageError)
33+
| ((p1: Connection, p2: FirebaseStorageError) => FirebaseStorageError)
3434
| null = null;
3535

3636
/**
@@ -51,7 +51,7 @@ export class RequestInfo<T> {
5151
* Note: The XhrIo passed to this function may be reused after this callback
5252
* returns. Do not keep a reference to it in any way.
5353
*/
54-
public handler: (p1: XhrIo, p2: string) => T,
54+
public handler: (p1: Connection, p2: string) => T,
5555
public timeout: number
5656
) {}
5757
}

packages/storage/src/implementation/requestmaker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
*/
1717
import { Request } from './request';
1818
import { RequestInfo } from './requestinfo';
19-
import { XhrIoPool } from './xhriopool';
19+
import { ConnectionPool } from './connectionPool';
2020

2121
type requestMaker = <T>(
2222
requestInfo: RequestInfo<T>,
2323
appId: string | null,
2424
authToken: string | null,
25-
pool: XhrIoPool
25+
pool: ConnectionPool
2626
) => Request<T>;
2727

2828
export { requestMaker };

0 commit comments

Comments
 (0)