-
Notifications
You must be signed in to change notification settings - Fork 94
/
connection-options.js
232 lines (222 loc) · 8.55 KB
/
connection-options.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
'use strict';
const Collations = require('../const/collations.js');
const urlFormat = /mariadb:\/\/(([^/@:]+)?(:([^/]+))?@)?(([^/:]+)(:([0-9]+))?)\/([^?]+)(\?(.*))?$/;
const moment = require('moment-timezone');
const Errors = require('../misc/errors');
/**
* Default option similar to mysql driver.
* known differences
* - no queryFormat option. Permitting client to parse is a security risk. Best is to give SQL + parameters
* Only possible Objects are :
* - Buffer
* - Date
* - Object that implement toSqlString function
* - JSON object
* + rowsAsArray (in mysql2) permit to have rows by index, not by name. Avoiding to parsing metadata string => faster
*/
class ConnectionOptions {
constructor(opts) {
if (typeof opts === 'string') {
opts = ConnectionOptions.parse(opts);
}
if (!opts) opts = {};
this.bigNumberStrings = opts.bigNumberStrings || false;
this.bulk = opts.bulk === undefined || opts.bulk;
if (opts.charset && typeof opts.charset === 'string') {
this.collation = Collations.fromName(opts.charset.toUpperCase());
if (this.collation === undefined)
throw new RangeError("Unknown charset '" + opts.charset + "'");
} else {
this.collation =
Collations.fromIndex(opts.charsetNumber) || Collations.fromIndex(224); //UTF8MB4_UNICODE_CI;
}
this.compress = opts.compress || false;
this.logPackets = opts.logPackets || false;
this.connectAttributes = opts.connectAttributes || false;
this.connectTimeout =
opts.connectTimeout === undefined ? 10000 : opts.connectTimeout;
this.socketTimeout =
opts.socketTimeout === undefined ? 0 : opts.socketTimeout;
this.database = opts.database;
this.dateStrings = opts.dateStrings || false;
this.debug = opts.debug || false;
this.debugCompress = opts.debugCompress || false;
this.debugLen = opts.debugLen || 256;
this.foundRows = opts.foundRows === undefined || opts.foundRows;
this.host = opts.host || 'localhost';
this.initSql = opts.initSql;
this.maxAllowedPacket = opts.maxAllowedPacket;
this.metaAsArray = opts.metaAsArray || false;
this.multipleStatements = opts.multipleStatements || false;
this.namedPlaceholders = opts.namedPlaceholders || false;
this.nestTables =
opts.nestTables === undefined ? undefined : opts.nestTables;
this.password = opts.password;
this.permitSetMultiParamEntries = opts.permitSetMultiParamEntries || false;
this.pipelining = opts.pipelining === undefined || opts.pipelining;
if (opts.pipelining === undefined) {
this.permitLocalInfile = opts.permitLocalInfile || false;
this.pipelining = !this.permitLocalInfile;
} else {
this.pipelining = opts.pipelining;
if (opts.permitLocalInfile === true && this.pipelining) {
throw new Error(
'enabling options `permitLocalInfile` and ' +
'`pipelining` is not possible, options are incompatible.'
);
}
this.permitLocalInfile = this.pipelining
? false
: opts.permitLocalInfile || false;
}
this.port = opts.port || 3306;
this.rowsAsArray = opts.rowsAsArray || false;
this.socketPath = opts.socketPath;
this.sessionVariables = opts.sessionVariables;
this.ssl = opts.ssl;
if (opts.ssl) {
if (typeof opts.ssl !== 'boolean' && typeof opts.ssl !== 'string') {
this.ssl.rejectUnauthorized = opts.ssl.rejectUnauthorized !== false;
}
}
this.supportBigNumbers = opts.supportBigNumbers || false;
this.timezone = opts.timezone || 'local';
if (
this.timezone &&
this.timezone !== 'local' &&
this.timezone !== 'auto'
) {
let tzName = this.timezone;
if (this.timezone === 'Z') {
tzName = 'Etc/UTC';
} else {
const matched = this.timezone.match(/([+\-\s])(\d\d):?(\d\d)?/);
if (matched) {
const hour =
(matched[1] === '-' ? 1 : -1) * Number.parseInt(matched[2], 10);
const minutes =
matched.length > 2 && matched[3]
? Number.parseInt(matched[3], 10)
: 0;
if (minutes > 0) {
throw new RangeError(
"timezone format incompatible with IANA standard timezone format was '" +
this.timezone +
"'"
);
}
tzName = 'Etc/GMT' + (matched[1] === '-' ? '+' : '') + hour;
console.log(
"warning: please use IANA standard timezone format ('Etc/GMT" +
(matched[1] === '-' ? '+' : '') +
hour +
"')"
);
}
}
this.localTz = moment.tz.guess();
if (tzName === this.localTz) {
this.tz = null;
} else {
this.tz = moment.tz.setDefault(tzName);
if (!this.tz.defaultZone) {
throw Errors.createError(
"Unknown IANA timezone '" + tzName + "'.",
true,
null,
'08S01',
Errors.ER_WRONG_IANA_TIMEZONE
);
}
}
}
this.trace = opts.trace || false;
this.typeCast = opts.typeCast;
if (this.typeCast !== undefined && typeof this.typeCast !== 'function') {
this.typeCast = undefined;
}
this.user = opts.user || process.env.USERNAME;
if (this.maxAllowedPacket && !Number.isInteger(this.maxAllowedPacket)) {
throw new RangeError(
"maxAllowedPacket must be an integer. was '" +
this.maxAllowedPacket +
"'"
);
}
}
/**
* When parsing from String, correcting type.
*
* @param opts options
* @return {opts}
*/
static parseOptionDataType(opts) {
if (opts.bigNumberStrings)
opts.bigNumberStrings = opts.bigNumberStrings == 'true';
if (opts.bulk) opts.bulk = opts.bulk == 'true';
if (opts.logPackets) opts.logPackets = opts.logPackets == 'true';
if (opts.charset && !isNaN(Number.parseInt(opts.charset)))
opts.charset = Number.parseInt(opts.charset);
if (opts.compress) opts.compress = opts.compress == 'true';
if (opts.connectAttributes)
opts.connectAttributes = opts.connectAttributes == 'true';
if (opts.connectTimeout)
opts.connectTimeout = parseInt(opts.connectTimeout);
if (opts.socketTimeout) opts.socketTimeout = parseInt(opts.socketTimeout);
if (opts.dateStrings) opts.dateStrings = opts.dateStrings == 'true';
if (opts.debug) opts.debug = opts.debug == 'true';
if (opts.debugCompress) opts.debugCompress = opts.debugCompress == 'true';
if (opts.debugLen) opts.debugLen = parseInt(opts.debugLen);
if (opts.foundRows) opts.foundRows = opts.foundRows == 'true';
if (opts.maxAllowedPacket && !isNaN(Number.parseInt(opts.maxAllowedPacket)))
opts.maxAllowedPacket = parseInt(opts.maxAllowedPacket);
if (opts.metaAsArray) opts.metaAsArray = opts.metaAsArray == 'true';
if (opts.multipleStatements)
opts.multipleStatements = opts.multipleStatements == 'true';
if (opts.namedPlaceholders)
opts.namedPlaceholders = opts.namedPlaceholders == 'true';
if (opts.nestTables) opts.nestTables = opts.nestTables == 'true';
if (opts.permitSetMultiParamEntries)
opts.permitSetMultiParamEntries =
opts.permitSetMultiParamEntries == 'true';
if (opts.pipelining) opts.pipelining = opts.pipelining == 'true';
if (opts.rowsAsArray) opts.rowsAsArray = opts.rowsAsArray == 'true';
if (opts.supportBigNumbers)
opts.supportBigNumbers = opts.supportBigNumbers == 'true';
if (opts.trace) opts.trace = opts.trace == 'true';
if (opts.ssl && (opts.ssl == 'true' || opts.ssl == 'false'))
opts.ssl = opts.ssl == 'true';
return opts;
}
static parse(opts) {
const matchResults = opts.match(urlFormat);
if (!matchResults) {
throw new Error(
"error parsing connection string '" +
opts +
"'. format must be 'mariadb://[<user>[:<password>]@]<host>[:<port>]/[<db>[?<opt1>=<value1>[&<opt2>=<value2>]]]'"
);
}
const options = {
user: matchResults[2],
password: matchResults[4],
host: matchResults[6],
port: matchResults[8] ? parseInt(matchResults[8]) : undefined,
database: matchResults[9]
};
const variousOptsString = matchResults[11];
if (variousOptsString) {
const keyVals = variousOptsString.split('&');
keyVals.forEach(function(keyVal) {
const equalIdx = keyVal.indexOf('=');
if (equalIdx !== 1) {
options[keyVal.substring(0, equalIdx)] = keyVal.substring(
equalIdx + 1
);
}
});
}
return this.parseOptionDataType(options);
}
}
module.exports = ConnectionOptions;