-
-
Notifications
You must be signed in to change notification settings - Fork 668
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (123 loc) · 4.08 KB
/
index.js
File metadata and controls
150 lines (123 loc) · 4.08 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
'use strict';
const SqlString = require('sql-escaper');
const ConnectionConfig = require('./lib/connection_config.js');
const parserCache = require('./lib/parsers/parser_cache.js');
const Connection = require('./lib/connection.js');
exports.createConnection = require('./lib/create_connection.js');
exports.connect = exports.createConnection;
exports.Connection = Connection;
exports.ConnectionConfig = ConnectionConfig;
const Pool = require('./lib/pool.js');
const PoolCluster = require('./lib/pool_cluster.js');
const createPool = require('./lib/create_pool.js');
const createPoolCluster = require('./lib/create_pool_cluster.js');
exports.createPool = createPool;
exports.createPoolCluster = createPoolCluster;
exports.createQuery = Connection.createQuery;
exports.Pool = Pool;
exports.PoolCluster = PoolCluster;
const _serverHandlerKeys = ['query', 'ping', 'quit', 'init_db', 'auth'];
function _hasHandlerKeys(obj) {
return _serverHandlerKeys.some((k) => typeof obj[k] === 'function');
}
function _wrapAuth(authHandler) {
return function (params, cb) {
Promise.resolve()
.then(() => authHandler(params))
.then(() => cb(null))
.catch((err) =>
cb(null, { message: err.message, code: err.code || 1045 })
);
};
}
function _buildHandshakeArgs(handlers) {
const args = {
protocolVersion: 10,
serverVersion: handlers.serverVersion || 'mysql2-server',
connectionId: Math.floor(Math.random() * 1000000),
statusFlags: 2,
characterSet: 8,
capabilityFlags: 0xffffff,
};
if (handlers.auth) {
args.authCallback = _wrapAuth(handlers.auth);
}
return args;
}
exports.createServer = function (opts = {}) {
const Server = require('./lib/server.js');
const Commands = require('./lib/commands/index.js');
const { buildHandleCommand } = require('./lib/commands/server/index.js');
if (typeof opts === 'function') {
const fn = opts;
const s = new Server({ encoding: 'cesu8' });
s.on('connection', (conn) => {
conn.on('error', () => {});
const result = fn(conn);
if (!result || typeof result !== 'object' || !_hasHandlerKeys(result)) {
return;
}
const handlers = result;
const encoding = handlers.encoding || 'cesu8';
conn.serverConfig = { encoding };
conn.config.serverOptions = Object.assign({}, conn.config.serverOptions, {
handleCommand: buildHandleCommand(handlers),
encoding,
});
conn.addCommand(
new Commands.ServerHandshake(_buildHandshakeArgs(handlers))
);
});
return s;
}
if (_hasHandlerKeys(opts)) {
const handleCommand = buildHandleCommand(opts);
const encoding = opts.encoding || 'cesu8';
const s = new Server({ handleCommand, encoding });
s.on('connection', (conn) => {
conn.on('error', () => {});
conn.serverConfig = { encoding };
conn.addCommand(new Commands.ServerHandshake(_buildHandshakeArgs(opts)));
});
return s;
}
const s = new Server({
handleCommand: opts.handleCommand,
encoding: opts.encoding || 'cesu8',
});
if (opts.onConnection) {
s.on('connection', opts.onConnection);
}
return s;
};
exports.PoolConnection = require('./lib/pool_connection.js');
exports.authPlugins = require('./lib/auth_plugins');
exports.escape = SqlString.escape;
exports.escapeId = SqlString.escapeId;
exports.format = SqlString.format;
exports.raw = SqlString.raw;
exports.__defineGetter__(
'createConnectionPromise',
() => require('./promise.js').createConnection
);
exports.__defineGetter__(
'createPoolPromise',
() => require('./promise.js').createPool
);
exports.__defineGetter__(
'createPoolClusterPromise',
() => require('./promise.js').createPoolCluster
);
exports.__defineGetter__('Types', () => require('./lib/constants/types.js'));
exports.__defineGetter__('Charsets', () =>
require('./lib/constants/charsets.js')
);
exports.__defineGetter__('CharsetToEncoding', () =>
require('./lib/constants/charset_encodings.js')
);
exports.setMaxParserCache = function (max) {
parserCache.setMaxCache(max);
};
exports.clearParserCache = function () {
parserCache.clearCache();
};