-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Improve Deno compatibility: config-first and safe env access #3547
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
base: master
Are you sure you want to change the base?
Changes from all commits
150f210
39ea164
546e1fd
0ba62cd
b0c5778
43f2c27
8d7ace8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,31 +34,40 @@ const PG = function (clientConstructor) { | |
| this.utils = utils | ||
| } | ||
|
|
||
| if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') { | ||
| module.exports = new PG(require('./native')) | ||
| } else { | ||
| module.exports = new PG(Client) | ||
| let clientConstructor = Client | ||
|
|
||
| // lazy require native module...the native module may not have installed | ||
| Object.defineProperty(module.exports, 'native', { | ||
| configurable: true, | ||
| enumerable: false, | ||
| get() { | ||
| let native = null | ||
| try { | ||
| native = new PG(require('./native')) | ||
| } catch (err) { | ||
| if (err.code !== 'MODULE_NOT_FOUND') { | ||
| throw err | ||
| } | ||
| let forceNative = false | ||
| try { | ||
| forceNative = !!process.env.NODE_PG_FORCE_NATIVE | ||
| } catch { | ||
| // ignore, e.g., Deno without --allow-env | ||
Jobians marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (forceNative) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I am reading this right, if you are in a deno environment with no --allow-env, then the above will always error, and forceNative will always remain in its default of false, and so it will be impossible to use the native driver. I'm not sure how much of a dealbreaker this actually is (eg does the native driver actually run in deno???). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the environment variable was always a mistake (same with the property), and breaking it in a way that’s backwards-compatible is fine. If a future API of the pg package even includes pg-native at all, I suggest it should be imported from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I also agree that the best API would be to import from const PG = ... // same as before
function makeClient(config) {
let useNative = false;
try {
useNative = typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined'
} catch {
// ignore, eg in Deno sandbox without --allow-env
}
const actualClient = useNative ? require('./native') : Client
return actualClient(config)
}
module.exports = new PG(makeClient)
// lazy require native module...the native module may not have installed
Object.defineProperty(module.exports, 'native', {
...Is there anything else you need to merge this PR? With that change I'm happy. Honestly, the current suggested implementation is similar enough, I would be fine merging that as well. |
||
| clientConstructor = require('./native') | ||
| } | ||
|
|
||
| module.exports = new PG(clientConstructor) | ||
|
|
||
| // lazy require native module...the native module may not have installed | ||
| Object.defineProperty(module.exports, 'native', { | ||
| configurable: true, | ||
| enumerable: false, | ||
| get() { | ||
| let native = null | ||
| try { | ||
| native = new PG(require('./native')) | ||
| } catch (err) { | ||
| if (err.code !== 'MODULE_NOT_FOUND') { | ||
| throw err | ||
| } | ||
| } | ||
|
|
||
| // overwrite module.exports.native so that getter is never called again | ||
| Object.defineProperty(module.exports, 'native', { | ||
| value: native, | ||
| }) | ||
| // overwrite module.exports.native so that getter is never called again | ||
| Object.defineProperty(module.exports, 'native', { | ||
| value: native, | ||
| }) | ||
|
|
||
| return native | ||
| }, | ||
| }) | ||
| } | ||
| return native | ||
| }, | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe say explicitly
let user = undefined? I know they behave the same, but semantically this barelet user(incorrectly) implies to me that we expect to always set it to a value.