Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow Settings.host to be used when Settings.servicePath is set #1275

Merged
merged 8 commits into from
Jul 31, 2020

Conversation

thebrianchen
Copy link

@thebrianchen thebrianchen commented Jul 27, 2020

Fixes #1265.

The existing behavior prioritized FIRESTORE_EMULATOR_HOST over any user-specified host when set, and prohibits setting the host more than once. This PR allows Settings.host to take precedence over Settings.servicePath and Settings.apiEndpoint if FIRESTORE_EMULATOR_HOST is not set and logs a warning when the host is overridden. Otherwise, FIRESTORE_EMULATOR_HOST is used as the host, regardless of any values that are passed via Settings.

@thebrianchen thebrianchen requested a review from a team as a code owner July 27, 2020 22:32
@thebrianchen thebrianchen self-assigned this Jul 27, 2020
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Jul 27, 2020
@codecov
Copy link

codecov bot commented Jul 27, 2020

Codecov Report

Merging #1275 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1275   +/-   ##
=======================================
  Coverage   98.50%   98.50%           
=======================================
  Files          30       30           
  Lines       18612    18617    +5     
  Branches     1431     1431           
=======================================
+ Hits        18333    18338    +5     
  Misses        276      276           
  Partials        3        3           
Impacted Files Coverage Δ
dev/src/index.ts 97.51% <100.00%> (+<0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c34970d...be70f6b. Read the comment docs.

dev/src/index.ts Outdated Show resolved Hide resolved
@schmidt-sebastian
Copy link
Contributor

@thebrianchen Did you look at why this failed? I don't see how this can fail:

delete emulatorSettings.servicePath;

unsets the servicePath if FIRESTORE_EMULATOR_HOST is set.
this.validateAndApplySettings(mergedSettings);
doesn't seem to change it again.

@thebrianchen thebrianchen changed the title fix: allow 'host' to override existing settings feat: Allow Settings.host to override existing host when calling settings(). Jul 28, 2020
@thebrianchen thebrianchen changed the title feat: Allow Settings.host to override existing host when calling settings(). feat: allow Settings.host to override existing host when calling settings(). Jul 28, 2020
@thebrianchen thebrianchen changed the title feat: allow Settings.host to override existing host when calling settings(). feat: allow Settings.host to override existing host when calling settings() Jul 28, 2020
@thebrianchen thebrianchen added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jul 28, 2020
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jul 28, 2020
dev/src/index.ts Outdated
Comment on lines 517 to 519
settings.servicePath !== undefined
? settings.servicePath
: settings.apiEndpoint
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
settings.servicePath !== undefined
? settings.servicePath
: settings.apiEndpoint
settings.servicePath ?? settings.apiEndpoint

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, done.

dev/src/index.ts Outdated
Comment on lines 508 to 511
(settings.servicePath !== undefined &&
settings.servicePath !== settings.host) ||
(settings.apiEndpoint !== undefined &&
settings.apiEndpoint !== settings.host)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Host is "hostname:port". At least servicePath is just hostname. Can you verify that this works as expected?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed logic to compare hostnames.

delete settings.host;
delete settings.apiEndpoint;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can now delete the code that also does this in the constructor.

By the way, I am still a little uneasy about the order of operations here:

process.env.FIRESTORE_EMULATOR_HOST = 'chicago';
const firestore = new Firestore({host: 'sanfrancisco'});
// Now talking to 'chicago'

vs:

process.env.FIRESTORE_EMULATOR_HOST = 'chicago';
const firestore = new Firestore()
firstore.setting({host: 'sanfrancisco'});
// Now talking to 'sanfrancisco'

Maybe all the constructor code should be moved to validateAndApplySettings, where we can then use a unified and sane behavior?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved the deletion code over to validateAndApplySettings. However, we need to keep the env var check in the constructor in order to preserve existing behavior where the env var overrides anything passed into the constructor.

@thebrianchen thebrianchen added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jul 30, 2020
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jul 30, 2020
dev/src/index.ts Outdated
// calls `settings()`, which will compare the the provided `host` to the
// existing hostname stored on `servicePath`.
delete settings.host;
delete settings.apiEndpoint;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved into the if-statement above, shouldn't it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, done.

let url: URL | null = null;

// If the environment variable is set, it should always take precedence
// over any user passed in settings.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we feel strongly that this is the wrong thing to do, should add a TODO to fix this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how we would fix it without creating an inconvenient breaking change for developers. Since this is only used for emulators and 1p testing, I think the current behavior isn't "wrong". I mainly wanted to document the priority order to make the code easier to understand.

settings.apiEndpoint !== url.hostname)
) {
// eslint-disable-next-line no-console
console.warn(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am debating if it makes sense to log this warning even when FIRESTORE_EMULATOR_HOST is used. Most of the time, this shouldn't warn since servicePath and apiEndpoint are rarely set.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that makes more sense. If anything, it will make it easier for developers to figure out what host is actually being used.

dev/src/index.ts Outdated
}

// We need to remove the `host` and `apiEndpoint` setting, in case a user
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment no longer applicable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this still applicable if the user does not provide the env var? In that case, creating a new Firestore with a host passed in and then calling settings() with host specified would cause this situation.

@thebrianchen thebrianchen removed their assignment Jul 30, 2020
Copy link
Contributor

@schmidt-sebastian schmidt-sebastian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Please update PR title to match new behavior.

@thebrianchen thebrianchen changed the title feat: allow Settings.host to override existing host when calling settings() feat: allow Settings.host to be used when Settings.servicePath is set Jul 30, 2020
@thebrianchen thebrianchen merged commit 34d6728 into master Jul 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Firestore] firestore.settings() cannot have 'host' when FIRESTORE_EMULATOR_HOST is set
4 participants