Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit c75b8b8

Browse files
fix: correct STORAGE_EMULATOR_HOST handling (#2069, #1314) (#2070)
* fix: correct STORAGE_EMULATOR_HOST handling (#2069, #1314) credit to @jpambrun for identifying the fix * fix: normalize baseUrl Co-authored-by: Daniel Bankhead <dan@danielbankhead.com> * fix: adjust URL normalization & tests for consistency Co-authored-by: Daniel Bankhead <dan@danielbankhead.com>
1 parent 9741a7a commit c75b8b8

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

src/storage.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,12 @@ export class Storage extends Service {
664664

665665
options = Object.assign({}, options, {apiEndpoint});
666666

667-
// Note: EMULATOR_HOST is an experimental configuration variable. Use apiEndpoint instead.
668-
const baseUrl = EMULATOR_HOST || `${options.apiEndpoint}/storage/v1`;
667+
// Note: EMULATOR_HOST, if present and not overridden, has been applied to
668+
// `options` at this point. Also, this uses string concatenation because the
669+
// endpoint may contain a base path, and any trailing slash on that will
670+
// have been removed, so using the two-arg URL constructor for relative path
671+
// resolution won't work.
672+
const baseUrl = new URL(options.apiEndpoint + '/storage/v1').href;
669673

670674
const config = {
671675
apiEndpoint: options.apiEndpoint!,

test/index.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,27 +435,33 @@ describe('Storage', () => {
435435
delete process.env.STORAGE_EMULATOR_HOST;
436436
});
437437

438-
it('should set baseUrl to env var STORAGE_EMULATOR_HOST', () => {
438+
it('should set baseUrl to env var STORAGE_EMULATOR_HOST plus standard path', () => {
439439
const storage = new Storage({
440440
projectId: PROJECT_ID,
441441
});
442442

443443
const calledWith = storage.calledWith_[0];
444-
assert.strictEqual(calledWith.baseUrl, EMULATOR_HOST);
444+
assert.strictEqual(calledWith.baseUrl, EMULATOR_HOST + '/storage/v1');
445445
assert.strictEqual(
446446
calledWith.apiEndpoint,
447447
'https://internal.benchmark.com/path'
448448
);
449449
});
450450

451-
it('should be overriden by apiEndpoint', () => {
451+
it('should be overridden by apiEndpoint', () => {
452452
const storage = new Storage({
453453
projectId: PROJECT_ID,
454454
apiEndpoint: 'https://some.api.com',
455455
});
456456

457457
const calledWith = storage.calledWith_[0];
458-
assert.strictEqual(calledWith.baseUrl, EMULATOR_HOST);
458+
// NOTE: this used to assert partially the opposite of what the test
459+
// says: it checked that baseUrl was _not_ overridden, but apiEndpoint
460+
// was.
461+
assert.strictEqual(
462+
calledWith.baseUrl,
463+
'https://some.api.com/storage/v1'
464+
);
459465
assert.strictEqual(calledWith.apiEndpoint, 'https://some.api.com');
460466
});
461467

@@ -468,7 +474,13 @@ describe('Storage', () => {
468474
});
469475

470476
const calledWith = storage.calledWith_[0];
471-
assert.strictEqual(calledWith.baseUrl, EMULATOR_HOST);
477+
// NOTE: this used to assert partially the opposite of what the test
478+
// says: it checked that baseUrl was _not_ overridden, but apiEndpoint
479+
// was.
480+
assert.strictEqual(
481+
calledWith.baseUrl,
482+
'https://internal.benchmark.com/path/storage/v1'
483+
);
472484
assert.strictEqual(
473485
calledWith.apiEndpoint,
474486
'https://internal.benchmark.com/path'

0 commit comments

Comments
 (0)