/
connect.js
160 lines (149 loc) · 4.99 KB
/
connect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (c) 2015-present, Vitaly Tomilov
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Removal or modification of this copyright notice is prohibited.
*/
'use strict';
const npm = {
con: require('manakin').local,
utils: require('./utils'),
events: require('./events'),
text: require('./text'),
formatting: require('./formatting')
};
function poolConnect(ctx, db, config) {
return config.promise((resolve, reject) => {
const p = db.$pool;
if (p.ending) {
db.$destroy();
const err = new Error(npm.text.poolDestroyed);
npm.events.error(ctx.options, err, {
dc: ctx.dc
});
reject(err);
return;
}
p.connect((err, client, done) => {
if (err) {
npm.events.error(ctx.options, err, {
cn: npm.utils.getSafeConnection(ctx.cn),
dc: ctx.dc
});
reject(err);
} else {
const isFresh = !client.$used;
if (isFresh) {
npm.utils.addReadProp(client, '$used', true, true);
hidePassword(client); // See: https://github.com/brianc/node-postgres/issues/1568
setSchema(client, ctx);
}
setCtx(client, ctx);
const end = lockClientEnd(client);
client.on('error', onError);
resolve({
isFresh, client,
done() {
client.end = end;
done();
npm.events.disconnect(ctx, client);
client.removeListener('error', onError);
}
});
npm.events.connect(ctx, client, isFresh);
}
});
});
}
function directConnect(ctx, config) {
return config.promise((resolve, reject) => {
const client = new config.pgp.pg.Client(ctx.cn);
client.connect(err => {
if (err) {
npm.events.error(ctx.options, err, {
cn: npm.utils.getSafeConnection(ctx.cn),
dc: ctx.dc
});
reject(err);
} else {
hidePassword(client); // See: https://github.com/brianc/node-postgres/issues/1568
setSchema(client, ctx);
setCtx(client, ctx);
const end = lockClientEnd(client);
client.on('error', onError);
resolve({
isFresh: true,
client,
done() {
client.end = end;
client.end();
npm.events.disconnect(ctx, client);
client.removeListener('error', onError);
}
});
npm.events.connect(ctx, client, true);
}
});
});
}
// this event only happens when the connection is lost physically,
// which cannot be tested automatically; removing from coverage:
// istanbul ignore next
function onError(err) {
const ctx = this.$ctx;
const cn = npm.utils.getSafeConnection(ctx.cn);
npm.events.error(ctx.options, err, {cn, dc: ctx.dc});
ctx.disconnect();
if (ctx.cnOptions && typeof ctx.cnOptions.onLost === 'function' && !ctx.notified) {
try {
ctx.cnOptions.onLost.call(this, err, {
cn,
dc: ctx.dc,
start: ctx.start,
client: this
});
} catch (e) {
npm.con.error(e && e.stack || e);
}
ctx.notified = true;
}
}
function lockClientEnd(client) {
const end = client.end;
client.end = doNotCall => {
// This call can happen only in the following two cases:
// 1. the client made the call directly, against the library's documentation (invalid code)
// 2. connection with the server broke, and the pool is terminating all clients forcefully.
npm.con.error(npm.text.clientEnd + '\n%s\n', npm.utils.getLocalStack(3));
if (!doNotCall) {
end.call(client);
}
};
return end;
}
function setCtx(client, ctx) {
Object.defineProperty(client, '$ctx', {
value: ctx,
writable: true
});
}
// See: https://github.com/brianc/node-postgres/issues/1568
function hidePassword(client) {
hideProperty(client, 'password');
hideProperty(client.connectionParameters, 'password');
}
function hideProperty(obj, prop) {
Object.defineProperty(obj, prop, {value: obj[prop], enumerable: false});
}
function setSchema(client, ctx) {
const s = ctx.options.schema;
if (s) {
client.query(npm.formatting.as.format('SET search_path TO $1:name', [s]));
}
}
module.exports = config => ({
pool: (ctx, db) => poolConnect(ctx, db, config),
direct: ctx => directConnect(ctx, config)
});