Skip to content

Commit c3513a2

Browse files
committed
Make native bindings an optional install
1 parent 89d0938 commit c3513a2

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

Makefile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ params := $(connectionString)
77
node-command := xargs -n 1 -I file node file $(params)
88

99
.PHONY : test test-connection test-integration bench test-native \
10-
build/default/binding.node jshint upgrade-pg publish
10+
build/default/binding.node jshint upgrade-pg publish test-missing-native
1111

1212
all:
1313
npm install
@@ -21,7 +21,7 @@ help:
2121

2222
test: test-unit
2323

24-
test-all: jshint test-unit test-integration test-native test-binary
24+
test-all: jshint test-missing-native test-unit test-integration test-native test-binary
2525

2626
test-travis: test-all upgrade-pg
2727
#@make test-all connectionString=postgres://postgres@localhost:5433/postgres
@@ -44,7 +44,18 @@ test-connection-binary:
4444
@echo "***Testing binary connection***"
4545
@node script/test-connection.js $(params) binary
4646

47-
test-native:
47+
test-missing-native:
48+
@echo "***Testing optional native install***"
49+
@rm -rf node_modules/pg-native
50+
@node test/native/missing-native.js
51+
@npm install pg-native@1.4.0
52+
@node test/native/missing-native.js
53+
@rm -rf node_modules/pg-native
54+
55+
node_modules/pg-native/index.js:
56+
@npm i pg-native
57+
58+
test-native: node_modules/pg-native/index.js
4859
@echo "***Testing native bindings***"
4960
@find test/native -name "*-tests.js" | $(node-command)
5061
@find test/integration -name "*-tests.js" | $(node-command) native

lib/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,15 @@ PG.prototype.cancel = function(config, client, query) {
6767
cancellingClient.cancel(client, query);
6868
};
6969

70-
var forceNative = Object.prototype.hasOwnProperty.call(process.env, 'NODE_PG_FORCE_NATIVE');
71-
if (forceNative) {
72-
module.exports = new PG(require(__dirname + '/native'));
70+
if(typeof process.env.NODE_PG_FORCE_NATIVE != 'undefined') {
71+
module.exports = new PG(require('./native'));
7372
} else {
7473
module.exports = new PG(Client);
7574

7675
//lazy require native module...the native module may not have installed
7776
module.exports.__defineGetter__("native", function() {
7877
delete module.exports.native;
79-
module.exports.native = new PG(require(__dirname + '/native'));
78+
module.exports.native = new PG(require('./native'));
8079
return module.exports.native;
8180
});
8281
}

lib/native/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
var Native = require('pg-native');
2+
var semver = require('semver');
3+
var pkg = require('../../package.json');
4+
var assert = require('assert');
25
var EventEmitter = require('events').EventEmitter;
36
var util = require('util');
47
var ConnectionParameters = require(__dirname + '/../connection-parameters');
58

9+
assert(semver.gte(Native.version, pkg.minNativeVersion));
10+
611
var NativeQuery = require('./query');
712

813
var Client = module.exports = function(config) {

lib/native/require.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
var pgNative = require('pg-native');
3+
assert(semver.gte(pgNative.version, pkg.minNativeVersion));
4+
module.exports = require('./index');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"nan": "1.3.0",
2424
"packet-reader": "0.2.0",
2525
"pg-connection-string": "0.1.3",
26-
"pg-native": "1.4.3",
2726
"pg-types": "1.6.0",
2827
"pgpass": "0.0.3"
2928
},
@@ -32,6 +31,7 @@
3231
"jshint": "2.5.2",
3332
"semver": "~3.0.1"
3433
},
34+
"minNativeVersion": "1.5.0",
3535
"scripts": {
3636
"changelog": "npm i github-changes && ./node_modules/.bin/github-changes -o brianc -r node-postgres -d pulls -a -v",
3737
"test": "make test-travis connectionString=postgres://postgres@localhost:5432/postgres"

test/native/missing-native.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//this test assumes it has been run from the Makefile
2+
//and that node_modules/pg-native has been deleted
3+
4+
var assert = require('assert');
5+
6+
assert.throws(function() {
7+
require('../../lib').native;
8+
});

0 commit comments

Comments
 (0)