-
-
Notifications
You must be signed in to change notification settings - Fork 667
Expand file tree
/
Copy pathcommon.js
More file actions
196 lines (172 loc) · 5.89 KB
/
common.js
File metadata and controls
196 lines (172 loc) · 5.89 KB
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
'use strict';
const fs = require('fs');
const path = require('path');
const config = {
host: process.env.MYSQL_HOST || 'localhost',
user: process.env.MYSQL_USER || 'root',
password: (process.env.CI ? process.env.MYSQL_PASSWORD : '') || '',
database: process.env.MYSQL_DATABASE || 'test',
compress: process.env.MYSQL_USE_COMPRESSION,
port: process.env.MYSQL_PORT || 3306
};
if (process.env.MYSQL_USE_TLS === '1') {
config.ssl = {
rejectUnauthorized: false,
ca: fs.readFileSync(
path.join(__dirname, '../examples/ssl/certs/ca.pem'),
'utf-8'
)
};
}
const configURI = `mysql://${config.user}:${config.password}@${config.host}:${config.port}/${config.database}`;
exports.SqlString = require('sqlstring');
exports.config = config;
exports.waitDatabaseReady = function(callback) {
const start = Date.now();
const tryConnect = function() {
const conn = exports.createConnection({ database: 'mysql', password: process.env.MYSQL_PASSWORD });
conn.once('error', err => {
if (err.code !== 'PROTOCOL_CONNECTION_LOST' && err.code !== 'ETIMEDOUT' && err.code !== 'ECONNREFUSED') {
console.log('Unexpected error waiting for connection', err);
process.exit(-1);
}
try {
conn.close();
} catch (err) {
console.log(err);
}
console.log('not ready');
setTimeout(tryConnect, 1000);
});
conn.once('connect', () => {
console.log(`ready after ${Date.now() - start}ms!`);
conn.close();
callback();
});
};
tryConnect();
};
exports.createConnection = function(args) {
const driver = require('../index.js');
if (!args?.port && process.env.MYSQL_CONNECTION_URL) {
return driver.createConnection({ ...args, uri: process.env.MYSQL_CONNECTION_URL })
}
if (!args) {
args = {};
}
const params = {
host: args.host || config.host,
rowsAsArray: args.rowsAsArray,
user: (args && args.user) || config.user,
password: (args && args.password) || config.password,
database: (args && args.database) || config.database,
multipleStatements: args ? args.multipleStatements : false,
port: (args && args.port) || config.port,
debug: process.env.DEBUG || (args && args.debug),
supportBigNumbers: args && args.supportBigNumbers,
bigNumberStrings: args && args.bigNumberStrings,
compress: (args && args.compress) || config.compress,
decimalNumbers: args && args.decimalNumbers,
charset: args && args.charset,
timezone: args && args.timezone,
dateStrings: args && args.dateStrings,
authSwitchHandler: args && args.authSwitchHandler,
typeCast: args && args.typeCast,
namedPlaceholders: args && args.namedPlaceholders,
connectTimeout: args && args.connectTimeout,
ssl: (args && args.ssl) ?? config.ssl,
decimalStringTrimTrailingZero: (args && args.decimalStringTrimTrailingZero) ?? config.decimalStringTrimTrailingZero,
};
const conn = driver.createConnection(params);
return conn;
};
exports.getConfig = function(input) {
const args = input || {};
const params = {
host: args.host || config.host,
rowsAsArray: args.rowsAsArray,
user: (args && args.user) || config.user,
password: (args && args.password) || config.password,
database: (args && args.database) || config.database,
multipleStatements: args ? args.multipleStatements : false,
port: (args && args.port) || config.port,
debug: process.env.DEBUG || (args && args.debug),
supportBigNumbers: args && args.supportBigNumbers,
bigNumberStrings: args && args.bigNumberStrings,
compress: (args && args.compress) || config.compress,
decimalNumbers: args && args.decimalNumbers,
charset: args && args.charset,
timezone: args && args.timezone,
dateStrings: args && args.dateStrings,
authSwitchHandler: args && args.authSwitchHandler,
typeCast: args && args.typeCast,
connectionLimit: args && args.connectionLimit,
maxIdle: args && args.maxIdle,
idleTimeout: args && args.idleTimeout,
};
return params;
};
exports.createPool = function(args) {
let driver = require('../index.js');
if (!args?.port && process.env.MYSQL_CONNECTION_URL) {
return driver.createPool({ ...args, uri: process.env.MYSQL_CONNECTION_URL })
}
if (!args) {
args = {};
}
if (process.env.BENCHMARK_MYSQL1) {
driver = require('mysql');
}
return driver.createPool(exports.getConfig(args));
};
exports.createPoolCluster = function(args = {}) {
const driver = require('../index.js');
if (!args?.port && process.env.MYSQL_CONNECTION_URL) {
return driver.createPoolCluster({ ...args, uri: process.env.MYSQL_CONNECTION_URL })
}
return driver.createPoolCluster(args)
}
exports.createConnectionWithURI = function() {
const driver = require('../index.js');
return driver.createConnection({ uri: configURI });
};
exports.createTemplate = function() {
const jade = require('jade');
const template = require('fs').readFileSync(
`${__dirname}/template.jade`,
'ascii'
);
return jade.compile(template);
};
const ClientFlags = require('../lib/constants/client.js');
const portfinder = require('portfinder');
exports.createServer = function(onListening, handler) {
const server = require('../index.js').createServer();
server.on('connection', conn => {
conn.on('error', () => {
// server side of the connection
// ignore disconnects
});
// remove ssl bit from the flags
let flags = 0xffffff;
flags = flags ^ (ClientFlags.COMPRESS | ClientFlags.SSL);
conn.serverHandshake({
protocolVersion: 10,
serverVersion: 'node.js rocks',
connectionId: 1234,
statusFlags: 2,
characterSet: 8,
capabilityFlags: flags
});
if (handler) {
handler(conn);
}
});
portfinder.getPort((err, port) => {
server.listen(port, onListening);
});
return server;
};
exports.useTestDb = function() {
// no-op in my setup, need it for compatibility with node-mysql tests
};