Skip to content

Commit d327c9e

Browse files
Remove ConnectionPool
1 parent 66d4a1e commit d327c9e

File tree

18 files changed

+144
-191
lines changed

18 files changed

+144
-191
lines changed

common/api-review/storage.api.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class _FirebaseStorageImpl implements FirebaseStorage {
5858
constructor(
5959
app: FirebaseApp, _authProvider: Provider<FirebaseAuthInternalName>,
6060
_appCheckProvider: Provider<AppCheckInternalComponentName>,
61-
_pool: ConnectionPool, _url?: string | undefined, _firebaseVersion?: string | undefined);
61+
_url?: string | undefined, _firebaseVersion?: string | undefined);
6262
readonly app: FirebaseApp;
6363
// (undocumented)
6464
readonly _appCheckProvider: Provider<AppCheckInternalComponentName>;
@@ -78,23 +78,20 @@ export class _FirebaseStorageImpl implements FirebaseStorage {
7878
get host(): string;
7979
set host(host: string);
8080
// Warning: (ae-forgotten-export) The symbol "RequestInfo" needs to be exported by the entry point index.d.ts
81+
// Warning: (ae-forgotten-export) The symbol "Connection" needs to be exported by the entry point index.d.ts
8182
// Warning: (ae-forgotten-export) The symbol "Request" needs to be exported by the entry point index.d.ts
8283
//
8384
// (undocumented)
84-
_makeRequest<T>(requestInfo: RequestInfo_2<T>, authToken: string | null, appCheckToken: string | null): Request_2<T>;
85+
_makeRequest<T>(requestInfo: RequestInfo_2<T>, requestFactory: () => Connection, authToken: string | null, appCheckToken: string | null): Request_2<T>;
8586
// (undocumented)
86-
makeRequestWithTokens<T>(requestInfo: RequestInfo_2<T>): Promise<Request_2<T>>;
87+
makeRequestWithTokens<T>(requestInfo: RequestInfo_2<T>, requestFactory: () => Connection): Promise<Request_2<T>>;
8788
_makeStorageReference(loc: _Location): _Reference;
8889
get maxOperationRetryTime(): number;
8990
set maxOperationRetryTime(time: number);
9091
get maxUploadRetryTime(): number;
9192
set maxUploadRetryTime(time: number);
9293
// (undocumented)
9394
_overrideAuthToken?: string;
94-
// Warning: (ae-forgotten-export) The symbol "ConnectionPool" needs to be exported by the entry point index.d.ts
95-
//
96-
// (undocumented)
97-
readonly _pool: ConnectionPool;
9895
// (undocumented)
9996
_protocol: string;
10097
// (undocumented)

packages/storage/src/implementation/connectionPool.ts

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

packages/storage/src/implementation/request.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import { RequestInfo } from './requestinfo';
3232
import { isJustDef } from './type';
3333
import { makeQueryString } from './url';
3434
import { Headers, Connection, ErrorCode } from './connection';
35-
import { ConnectionPool } from './connectionPool';
3635

3736
export interface Request<T> {
3837
getPromise(): Promise<T>;
@@ -67,7 +66,6 @@ class NetworkRequest<T> implements Request<T> {
6766
| null;
6867
private progressCallback_: ((p1: number, p2: number) => void) | null;
6968
private timeout_: number;
70-
private pool_: ConnectionPool;
7169
promise_: Promise<T>;
7270

7371
constructor(
@@ -78,12 +76,10 @@ class NetworkRequest<T> implements Request<T> {
7876
successCodes: number[],
7977
additionalRetryCodes: number[],
8078
callback: (p1: Connection, p2: string) => T,
81-
errorCallback:
82-
| ((p1: Connection, p2: StorageError) => StorageError)
83-
| null,
79+
errorCallback: ((p1: Connection, p2: StorageError) => StorageError) | null,
8480
timeout: number,
8581
progressCallback: ((p1: number, p2: number) => void) | null,
86-
pool: ConnectionPool
82+
private connectionFactory_: () => Connection
8783
) {
8884
this.url_ = url;
8985
this.method_ = method;
@@ -95,7 +91,6 @@ class NetworkRequest<T> implements Request<T> {
9591
this.errorCallback_ = errorCallback;
9692
this.progressCallback_ = progressCallback;
9793
this.timeout_ = timeout;
98-
this.pool_ = pool;
9994
this.promise_ = new Promise((resolve, reject) => {
10095
this.resolve_ = resolve as (value?: T | PromiseLike<T>) => void;
10196
this.reject_ = reject;
@@ -117,7 +112,7 @@ class NetworkRequest<T> implements Request<T> {
117112
backoffCallback(false, new RequestEndStatus(false, null, true));
118113
return;
119114
}
120-
const connection = self.pool_.createConnection();
115+
const connection = self.connectionFactory_();
121116
self.pendingConnection_ = connection;
122117

123118
function progressListener(progressEvent: ProgressEvent): void {
@@ -296,7 +291,7 @@ export function makeRequest<T>(
296291
appId: string | null,
297292
authToken: string | null,
298293
appCheckToken: string | null,
299-
pool: ConnectionPool,
294+
requestFactory: () => Connection,
300295
firebaseVersion?: string
301296
): Request<T> {
302297
const queryPart = makeQueryString(requestInfo.urlParams);
@@ -317,6 +312,6 @@ export function makeRequest<T>(
317312
requestInfo.errorHandler,
318313
requestInfo.timeout,
319314
requestInfo.progressCallback,
320-
pool
315+
requestFactory
321316
);
322317
}

packages/storage/src/implementation/requestmaker.ts

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

packages/storage/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
SDK_VERSION
2828
} from '@firebase/app';
2929

30-
import { ConnectionPool } from '../src/implementation/connectionPool';
3130
import { FirebaseStorageImpl } from '../src/service';
3231
import {
3332
Component,
@@ -56,7 +55,6 @@ function factory(
5655
app,
5756
authProvider,
5857
appCheckProvider,
59-
new ConnectionPool(),
6058
url,
6159
SDK_VERSION
6260
);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import {
2222
} from '../../implementation/connection';
2323
import { internalError } from '../../implementation/error';
2424

25+
/** An override for the text-based Connection. Used in tests. */
26+
let connectionFactoryOverride: (() => Connection) | null = null;
27+
2528
/**
2629
* Network layer for browsers. We use this instead of goog.net.XhrIo because
2730
* goog.net.XhrIo is hyuuuuge and doesn't work in React Native on Android.
@@ -148,5 +151,11 @@ export class XhrConnection implements Connection {
148151
}
149152

150153
export function newConnection(): Connection {
151-
return new XhrConnection();
154+
return connectionFactoryOverride
155+
? connectionFactoryOverride()
156+
: new XhrConnection();
157+
}
158+
159+
export function injectTestConnection(factory: (() => Connection) | null): void {
160+
connectionFactoryOverride = factory;
152161
}

packages/storage/src/platform/connection.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@
1515
* limitations under the License.
1616
*/
1717
import { Connection } from '../implementation/connection';
18-
import { newConnection as nodeNewConnection } from './node/connection';
18+
import {
19+
newConnection as nodeNewConnection,
20+
injectTestConnection as nodeInjectTestConnection
21+
} from './node/connection';
1922

2023
export function newConnection(): Connection {
2124
// This file is only used under ts-node.
2225
return nodeNewConnection();
2326
}
27+
28+
export function injectTestConnection(factory: (() => Connection) | null): void {
29+
// This file is only used under ts-node.
30+
nodeInjectTestConnection(factory);
31+
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import nodeFetch from 'node-fetch';
2222
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2323
const fetch: typeof window.fetch = nodeFetch as any;
2424

25+
/** An override for the text-based Connection. Used in tests. */
26+
let connectionFactoryOverride: (() => Connection) | null = null;
27+
2528
/**
2629
* Network layer that works in Node.
2730
*
@@ -114,5 +117,11 @@ export class FetchConnection implements Connection {
114117
}
115118

116119
export function newConnection(): Connection {
117-
return new FetchConnection();
120+
return connectionFactoryOverride
121+
? connectionFactoryOverride()
122+
: new FetchConnection();
123+
}
124+
125+
export function injectTestConnection(factory: (() => Connection) | null): void {
126+
connectionFactoryOverride = factory;
118127
}

packages/storage/src/reference.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { ListResult } from './list';
3939
import { UploadTask } from './task';
4040
import { invalidRootOperation, noDownloadURL } from './implementation/error';
4141
import { validateNumber } from './implementation/type';
42+
import { newConnection } from './platform/connection';
4243

4344
/**
4445
* Provides methods to interact with a bucket in the Firebase Storage service.
@@ -165,7 +166,7 @@ export function uploadBytes(
165166
metadata
166167
);
167168
return ref.storage
168-
.makeRequestWithTokens(requestInfo)
169+
.makeRequestWithTokens(requestInfo, newConnection)
169170
.then(request => request.getPromise())
170171
.then(finalMetadata => {
171172
return {
@@ -312,7 +313,9 @@ export async function list(
312313
op.pageToken,
313314
op.maxResults
314315
);
315-
return (await ref.storage.makeRequestWithTokens(requestInfo)).getPromise();
316+
return (
317+
await ref.storage.makeRequestWithTokens(requestInfo, newConnection)
318+
).getPromise();
316319
}
317320

318321
/**
@@ -329,7 +332,9 @@ export async function getMetadata(ref: Reference): Promise<Metadata> {
329332
ref._location,
330333
getMappings()
331334
);
332-
return (await ref.storage.makeRequestWithTokens(requestInfo)).getPromise();
335+
return (
336+
await ref.storage.makeRequestWithTokens(requestInfo, newConnection)
337+
).getPromise();
333338
}
334339

335340
/**
@@ -354,7 +359,9 @@ export async function updateMetadata(
354359
metadata,
355360
getMappings()
356361
);
357-
return (await ref.storage.makeRequestWithTokens(requestInfo)).getPromise();
362+
return (
363+
await ref.storage.makeRequestWithTokens(requestInfo, newConnection)
364+
).getPromise();
358365
}
359366

360367
/**
@@ -370,7 +377,7 @@ export async function getDownloadURL(ref: Reference): Promise<string> {
370377
ref._location,
371378
getMappings()
372379
);
373-
return (await ref.storage.makeRequestWithTokens(requestInfo))
380+
return (await ref.storage.makeRequestWithTokens(requestInfo, newConnection))
374381
.getPromise()
375382
.then(url => {
376383
if (url === null) {
@@ -389,7 +396,9 @@ export async function getDownloadURL(ref: Reference): Promise<string> {
389396
export async function deleteObject(ref: Reference): Promise<void> {
390397
ref._throwIfRoot('deleteObject');
391398
const requestInfo = requestsDeleteObject(ref.storage, ref._location);
392-
return (await ref.storage.makeRequestWithTokens(requestInfo)).getPromise();
399+
return (
400+
await ref.storage.makeRequestWithTokens(requestInfo, newConnection)
401+
).getPromise();
393402
}
394403

395404
/**

packages/storage/src/service.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { Location } from './implementation/location';
1919
import { FailRequest } from './implementation/failrequest';
2020
import { Request, makeRequest } from './implementation/request';
2121
import { RequestInfo } from './implementation/requestinfo';
22-
import { ConnectionPool } from './implementation/connectionPool';
2322
import { Reference, _getChild } from './reference';
2423
import { Provider } from '@firebase/component';
2524
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
@@ -31,7 +30,7 @@ import {
3130
DEFAULT_HOST,
3231
DEFAULT_MAX_OPERATION_RETRY_TIME,
3332
DEFAULT_MAX_UPLOAD_RETRY_TIME
34-
} from '../src/implementation/constants';
33+
} from './implementation/constants';
3534
import {
3635
invalidArgument,
3736
appDeleted,
@@ -40,6 +39,7 @@ import {
4039
import { validateNumber } from './implementation/type';
4140
import { FirebaseStorage } from './public-types';
4241
import { createMockUserToken, EmulatorMockTokenOptions } from '@firebase/util';
42+
import { Connection } from './implementation/connection';
4343

4444
export function isUrl(path?: string): boolean {
4545
return /^[A-Za-z]+:\/\//.test(path as string);
@@ -182,7 +182,6 @@ export class FirebaseStorageImpl implements FirebaseStorage {
182182
/**
183183
* @internal
184184
*/
185-
readonly _pool: ConnectionPool,
186185
readonly _url?: string,
187186
readonly _firebaseVersion?: string
188187
) {
@@ -301,6 +300,7 @@ export class FirebaseStorageImpl implements FirebaseStorage {
301300
*/
302301
_makeRequest<T>(
303302
requestInfo: RequestInfo<T>,
303+
requestFactory: () => Connection,
304304
authToken: string | null,
305305
appCheckToken: string | null
306306
): Request<T> {
@@ -310,7 +310,7 @@ export class FirebaseStorageImpl implements FirebaseStorage {
310310
this._appId,
311311
authToken,
312312
appCheckToken,
313-
this._pool,
313+
requestFactory,
314314
this._firebaseVersion
315315
);
316316
this._requests.add(request);
@@ -326,13 +326,19 @@ export class FirebaseStorageImpl implements FirebaseStorage {
326326
}
327327

328328
async makeRequestWithTokens<T>(
329-
requestInfo: RequestInfo<T>
329+
requestInfo: RequestInfo<T>,
330+
requestFactory: () => Connection
330331
): Promise<Request<T>> {
331332
const [authToken, appCheckToken] = await Promise.all([
332333
this._getAuthToken(),
333334
this._getAppCheckToken()
334335
]);
335336

336-
return this._makeRequest(requestInfo, authToken, appCheckToken);
337+
return this._makeRequest(
338+
requestInfo,
339+
requestFactory,
340+
authToken,
341+
appCheckToken
342+
);
337343
}
338344
}

0 commit comments

Comments
 (0)