Skip to content

Commit 27557ba

Browse files
committed
Address PR comments
1 parent 9748078 commit 27557ba

File tree

8 files changed

+26
-25
lines changed

8 files changed

+26
-25
lines changed

.changeset/tricky-seahorses-look.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
'@firebase/storage': minor
44
---
55

6-
Add `storage().useEmulator()` method allowing users to set a storage emulator host and port.
6+
Add `storage().useEmulator()` method to enable emulator mode for storage, allowing users
7+
to set a storage emulator host and port.

packages/storage/compat/service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class StorageServiceCompat implements types.FirebaseStorage {
7575
);
7676
}
7777
try {
78-
Location.makeFromUrl(url, this._delegate.emulatorOrigin);
78+
Location.makeFromUrl(url, this._delegate.host);
7979
} catch (e) {
8080
throw invalidArgument(
8181
'refFromUrl() expected a valid full URL but got an invalid one.'

packages/storage/src/implementation/metadata.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export function fromResourceString(
156156
export function downloadUrlFromResourceString(
157157
metadata: Metadata,
158158
resourceString: string,
159-
emulatorOrigin?: string
159+
host?: string
160160
): string | null {
161161
const obj = jsonObjectOrNull(resourceString);
162162
if (obj === null) {
@@ -177,7 +177,7 @@ export function downloadUrlFromResourceString(
177177
const bucket: string = metadata['bucket'] as string;
178178
const path: string = metadata['fullPath'] as string;
179179
const urlPart = '/b/' + encode(bucket) + '/o/' + encode(path);
180-
const base = makeUrl(urlPart, emulatorOrigin);
180+
const base = makeUrl(urlPart, host);
181181
const queryString = makeQueryString({
182182
alt: 'media',
183183
token

packages/storage/src/implementation/requests.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function downloadUrlHandler(
8989
return downloadUrlFromResourceString(
9090
metadata as Metadata,
9191
text,
92-
service.emulatorOrigin
92+
service.host
9393
);
9494
}
9595
return handler;
@@ -147,7 +147,7 @@ export function getMetadata(
147147
mappings: Mappings
148148
): RequestInfo<Metadata> {
149149
const urlPart = location.fullServerUrl();
150-
const url = makeUrl(urlPart, service.emulatorOrigin);
150+
const url = makeUrl(urlPart, service.host);
151151
const method = 'GET';
152152
const timeout = service.maxOperationRetryTime;
153153
const requestInfo = new RequestInfo(
@@ -183,7 +183,7 @@ export function list(
183183
urlParams['maxResults'] = maxResults;
184184
}
185185
const urlPart = location.bucketOnlyServerUrl();
186-
const url = makeUrl(urlPart, service.emulatorOrigin);
186+
const url = makeUrl(urlPart, service.host);
187187
const method = 'GET';
188188
const timeout = service.maxOperationRetryTime;
189189
const requestInfo = new RequestInfo(
@@ -203,7 +203,7 @@ export function getDownloadUrl(
203203
mappings: Mappings
204204
): RequestInfo<string | null> {
205205
const urlPart = location.fullServerUrl();
206-
const url = makeUrl(urlPart, service.emulatorOrigin);
206+
const url = makeUrl(urlPart, service.host);
207207
const method = 'GET';
208208
const timeout = service.maxOperationRetryTime;
209209
const requestInfo = new RequestInfo(
@@ -223,7 +223,7 @@ export function updateMetadata(
223223
mappings: Mappings
224224
): RequestInfo<Metadata> {
225225
const urlPart = location.fullServerUrl();
226-
const url = makeUrl(urlPart, service.emulatorOrigin);
226+
const url = makeUrl(urlPart, service.host);
227227
const method = 'PATCH';
228228
const body = toResourceString(metadata, mappings);
229229
const headers = { 'Content-Type': 'application/json; charset=utf-8' };
@@ -245,7 +245,7 @@ export function deleteObject(
245245
location: Location
246246
): RequestInfo<void> {
247247
const urlPart = location.fullServerUrl();
248-
const url = makeUrl(urlPart, service.emulatorOrigin);
248+
const url = makeUrl(urlPart, service.host);
249249
const method = 'DELETE';
250250
const timeout = service.maxOperationRetryTime;
251251

@@ -325,7 +325,7 @@ export function multipartUpload(
325325
throw cannotSliceBlob();
326326
}
327327
const urlParams: UrlParams = { name: metadata_['fullPath']! };
328-
const url = makeUrl(urlPart, service.emulatorOrigin);
328+
const url = makeUrl(urlPart, service.host);
329329
const method = 'POST';
330330
const timeout = service.maxUploadRetryTime;
331331
const requestInfo = new RequestInfo(
@@ -385,7 +385,7 @@ export function createResumableUpload(
385385
const urlPart = location.bucketOnlyServerUrl();
386386
const metadataForUpload = metadataForUpload_(location, blob, metadata);
387387
const urlParams: UrlParams = { name: metadataForUpload['fullPath']! };
388-
const url = makeUrl(urlPart, service.emulatorOrigin);
388+
const url = makeUrl(urlPart, service.host);
389389
const method = 'POST';
390390
const headers = {
391391
'X-Goog-Upload-Protocol': 'resumable',

packages/storage/src/reference.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class Reference {
6060
if (location instanceof Location) {
6161
this._location = location;
6262
} else {
63-
this._location = Location.makeFromUrl(location, _service.emulatorOrigin);
63+
this._location = Location.makeFromUrl(location, _service.host);
6464
}
6565
}
6666

packages/storage/src/service.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from '@firebase/app-types-exp';
3131
import {
3232
CONFIG_STORAGE_BUCKET_KEY,
33+
DEFAULT_HOST,
3334
DEFAULT_MAX_OPERATION_RETRY_TIME,
3435
DEFAULT_MAX_UPLOAD_RETRY_TIME
3536
} from '../src/implementation/constants';
@@ -41,6 +42,7 @@ import {
4142
import { validateNumber } from './implementation/type';
4243

4344
export function isUrl(path?: string): boolean {
45+
console.log(path);
4446
return /^[A-Za-z]+:\/\//.test(path as string);
4547
}
4648

@@ -135,7 +137,7 @@ export function useStorageEmulator(
135137
host: string,
136138
port: number
137139
): void {
138-
storage.emulatorOrigin = `http://${host}:${port}`;
140+
storage.host = `http://${host}:${port}`;
139141
}
140142

141143
/**
@@ -145,7 +147,7 @@ export function useStorageEmulator(
145147
*/
146148
export class StorageService implements _FirebaseService {
147149
_bucket: Location | null = null;
148-
private _emulatorOrigin?: string;
150+
private _host: string = DEFAULT_HOST;
149151
protected readonly _appId: string | null = null;
150152
private readonly _requests: Set<Request<unknown>>;
151153
private _deleted: boolean = false;
@@ -172,16 +174,16 @@ export class StorageService implements _FirebaseService {
172174
}
173175
}
174176

175-
get emulatorOrigin(): string | undefined {
176-
return this._emulatorOrigin;
177+
get host(): string {
178+
return this._host;
177179
}
178180

179181
/**
180-
* Set emulator origin string for this service.
181-
* @param origin Origin string in the form of http://[host]:[port]
182+
* Set host:port string for this service.
183+
* @param origin - Origin string in the form of http://[host]:[port]
182184
*/
183-
set emulatorOrigin(origin: string | undefined) {
184-
this._emulatorOrigin = origin;
185+
set host(origin: string) {
186+
this._host = origin;
185187
if (this._url != null) {
186188
this._bucket = Location.makeFromBucketSpec(this._url, origin);
187189
} else {

packages/storage/test/unit/service.compat.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ describe('Firebase Storage > Service', () => {
191191
testShared.makePool(newSend)
192192
);
193193
service.useEmulator('test.host.org', 1234);
194-
expect(service._delegate.emulatorOrigin).to.equal(
195-
'http://test.host.org:1234'
196-
);
194+
expect(service._delegate.host).to.equal('http://test.host.org:1234');
197195
void service.ref('test.png').getDownloadURL();
198196
});
199197
});

packages/storage/test/unit/service.exp.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ GOOG4-RSA-SHA256`
240240
testShared.makePool(newSend)
241241
);
242242
useStorageEmulator(service, 'test.host.org', 1234);
243-
expect(service.emulatorOrigin).to.equal('http://test.host.org:1234');
243+
expect(service.host).to.equal('http://test.host.org:1234');
244244
void getDownloadURL(ref(service, 'test.png'));
245245
});
246246
});

0 commit comments

Comments
 (0)