Repository navigation
pg: read the default user from the environment on first access, not at module load - #3864
Open
proggeramlug wants to merge 1 commit into
Open
proggeramlug wants to merge 1 commit into
proggeramlug wants to merge 1 commit into
Conversation
…dule load defaults.user becomes a lazy accessor that turns into a plain data property on first read or write, so requiring pg no longer reads process.env.USER.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
lib/defaults.jscopiesprocess.env.USER(USERNAMEon Windows) into the exported defaults object while the module loads. Requiringpgtherefore reads the environment even when no connection is ever made, and the value is fixed at require time. Some tools prefer libraries that do no environment reads at module load (bundlers that evaluate module bodies ahead of time, startup snapshots, sandboxed runtimes that gate environment access).Change
useron the defaults object becomes a getter/setter pair in the same position of the object literal. The first read runs the same expression as before (inside the same try/catch) and then replaces itself with a plain data property{ writable: true, enumerable: true, configurable: true }holding that value; an assignment replaces it with a data property holding the assigned value. So:defaults.user,pg.defaults.userandConnectionParameters(user, anddatabasedefaulting touser) see the same values as before.defaults.user = 'x'anddelete defaults.userbehave as before.Object.keys(defaults)order,{ ...defaults }andJSON.stringify(defaults)are unchanged.The only difference: the environment is read when
useris first needed rather than whenpgis required. A program that changesprocess.env.USERafter requiringpgand before any connection now gets the changed value. Before the first access,Object.getOwnPropertyDescriptor(defaults, 'user')reports an accessor.Tests
Adds
packages/pg/test/unit/defaults-tests.js: loadinglib/defaults.jswith a recordingprocess.envreads nothing untildefaults.useris accessed; the value and descriptor after first access; key order; assignment anddeletebefore the first read; an unreadable environment yieldsundefined. The first test fails on the old code and passes with the change. The existing unit tests pass (connection/error-tests.jsfails locally on a busy port, with or without this change).