Make native bindings an optional install

This commit is contained in:
Brian M. Carlson 2014-10-18 00:20:18 -04:00
parent 89d0938655
commit c3513a27fa
6 changed files with 35 additions and 8 deletions

View File

@ -7,7 +7,7 @@ params := $(connectionString)
node-command := xargs -n 1 -I file node file $(params)
.PHONY : test test-connection test-integration bench test-native \
build/default/binding.node jshint upgrade-pg publish
build/default/binding.node jshint upgrade-pg publish test-missing-native
all:
npm install
@ -21,7 +21,7 @@ help:
test: test-unit
test-all: jshint test-unit test-integration test-native test-binary
test-all: jshint test-missing-native test-unit test-integration test-native test-binary
test-travis: test-all upgrade-pg
#@make test-all connectionString=postgres://postgres@localhost:5433/postgres
@ -44,7 +44,18 @@ test-connection-binary:
@echo "***Testing binary connection***"
@node script/test-connection.js $(params) binary
test-native:
test-missing-native:
@echo "***Testing optional native install***"
@rm -rf node_modules/pg-native
@node test/native/missing-native.js
@npm install pg-native@1.4.0
@node test/native/missing-native.js
@rm -rf node_modules/pg-native
node_modules/pg-native/index.js:
@npm i pg-native
test-native: node_modules/pg-native/index.js
@echo "***Testing native bindings***"
@find test/native -name "*-tests.js" | $(node-command)
@find test/integration -name "*-tests.js" | $(node-command) native

View File

@ -67,16 +67,15 @@ PG.prototype.cancel = function(config, client, query) {
cancellingClient.cancel(client, query);
};
var forceNative = Object.prototype.hasOwnProperty.call(process.env, 'NODE_PG_FORCE_NATIVE');
if (forceNative) {
module.exports = new PG(require(__dirname + '/native'));
if(typeof process.env.NODE_PG_FORCE_NATIVE != 'undefined') {
module.exports = new PG(require('./native'));
} else {
module.exports = new PG(Client);
//lazy require native module...the native module may not have installed
module.exports.__defineGetter__("native", function() {
delete module.exports.native;
module.exports.native = new PG(require(__dirname + '/native'));
module.exports.native = new PG(require('./native'));
return module.exports.native;
});
}

View File

@ -1,8 +1,13 @@
var Native = require('pg-native');
var semver = require('semver');
var pkg = require('../../package.json');
var assert = require('assert');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var ConnectionParameters = require(__dirname + '/../connection-parameters');
assert(semver.gte(Native.version, pkg.minNativeVersion));
var NativeQuery = require('./query');
var Client = module.exports = function(config) {

4
lib/native/require.js Normal file
View File

@ -0,0 +1,4 @@
var pgNative = require('pg-native');
assert(semver.gte(pgNative.version, pkg.minNativeVersion));
module.exports = require('./index');

View File

@ -23,7 +23,6 @@
"nan": "1.3.0",
"packet-reader": "0.2.0",
"pg-connection-string": "0.1.3",
"pg-native": "1.4.3",
"pg-types": "1.6.0",
"pgpass": "0.0.3"
},
@ -32,6 +31,7 @@
"jshint": "2.5.2",
"semver": "~3.0.1"
},
"minNativeVersion": "1.5.0",
"scripts": {
"changelog": "npm i github-changes && ./node_modules/.bin/github-changes -o brianc -r node-postgres -d pulls -a -v",
"test": "make test-travis connectionString=postgres://postgres@localhost:5432/postgres"

View File

@ -0,0 +1,8 @@
//this test assumes it has been run from the Makefile
//and that node_modules/pg-native has been deleted
var assert = require('assert');
assert.throws(function() {
require('../../lib').native;
});