Skip to content

Commit d5a76e6

Browse files
tpraxlmknjwellwelwel
authored
fix: circular dependencies (sidorares#3081)
* fix(lib): circular dependency index.js <-> lib pool.js and pool_connection.js required index.js, and index.js required pool.js and pool_connection.js. Circular dependency can cause all sorts of problems. This fix aims to provide a step towards fixing a problem with @opentelemetry/instrumentation-mysql2, which looses several exports when using its patched require. For instance, `format` is lost and can cause an instrumented application to crash. * fix: circular dependency index.js <-> promise.js index.js and promise.js require each other. Circular dependency can cause all sorts of problems. This commit aims to fix a problem with @opentelemetry/instrumentation-mysql2, which looses several exports when using its patched require. For instance, `format` is lost and can cause an instrumented application to crash. * refactor: split common.js into lib modules The proposal to put common.js into lib was good, but it felt weird to arbitrarily stuff just some exported functions in lib/common.js. This made sense when common.js was meant to provide commons for index.js and promise.js. But in the lib, this felt like a weird scope. I therefore split common.js into - lib/create_connection.js - lib/create_pool.js - lib/create_pool_cluster.js Also made `require` more consistent in all affected files: all `require` files now have a js suffix when they refer to a single local file. * fix: circular dependency to promise.js promise.js was required by lib sources, and promise.js required the same lib sources eventually. Extracted the respective classes to lib. Also extracted functions that are shared between several of the new files. Decided to put each exported class / function into its own file. This may bloat the lib folder a bit, but it provides clarity (where to find what). * fix: missing patched functions The extraction of classes was performed without extracting the patch of methods like `escape`, etc. The patching is now performed in the files that define the respective classes, guaranteeing patched versions when these files are required. * chore: remove unused require * style: add missing semicolons and file name suffixes for require Co-authored-by: mknj <[email protected]> * chore: resolve all remaining circular dependencies * chore: better organize new files --------- Co-authored-by: mknj <[email protected]> Co-authored-by: wellwelwel <[email protected]>
1 parent 5615273 commit d5a76e6

17 files changed

+1782
-1689
lines changed

Diff for: index.js

+16-22
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,32 @@
22

33
const SqlString = require('sqlstring');
44

5-
const Connection = require('./lib/connection.js');
65
const ConnectionConfig = require('./lib/connection_config.js');
7-
const parserCache = require('./lib/parsers/parser_cache');
6+
const parserCache = require('./lib/parsers/parser_cache.js');
87

9-
exports.createConnection = function(opts) {
10-
return new Connection({ config: new ConnectionConfig(opts) });
11-
};
8+
const Connection = require('./lib/connection.js');
129

10+
exports.createConnection = require('./lib/create_connection.js');
1311
exports.connect = exports.createConnection;
1412
exports.Connection = Connection;
1513
exports.ConnectionConfig = ConnectionConfig;
1614

1715
const Pool = require('./lib/pool.js');
1816
const PoolCluster = require('./lib/pool_cluster.js');
17+
const createPool = require('./lib/create_pool.js');
18+
const createPoolCluster = require('./lib/create_pool_cluster.js');
1919

20-
exports.createPool = function(config) {
21-
const PoolConfig = require('./lib/pool_config.js');
22-
return new Pool({ config: new PoolConfig(config) });
23-
};
20+
exports.createPool = createPool;
2421

25-
exports.createPoolCluster = function(config) {
26-
const PoolCluster = require('./lib/pool_cluster.js');
27-
return new PoolCluster(config);
28-
};
22+
exports.createPoolCluster = createPoolCluster;
2923

3024
exports.createQuery = Connection.createQuery;
3125

3226
exports.Pool = Pool;
3327

3428
exports.PoolCluster = PoolCluster;
3529

36-
exports.createServer = function(handler) {
30+
exports.createServer = function (handler) {
3731
const Server = require('./lib/server.js');
3832
const s = new Server();
3933
if (handler) {
@@ -42,7 +36,7 @@ exports.createServer = function(handler) {
4236
return s;
4337
};
4438

45-
exports.PoolConnection = require('./lib/pool_connection');
39+
exports.PoolConnection = require('./lib/pool_connection.js');
4640
exports.authPlugins = require('./lib/auth_plugins');
4741
exports.escape = SqlString.escape;
4842
exports.escapeId = SqlString.escapeId;
@@ -51,33 +45,33 @@ exports.raw = SqlString.raw;
5145

5246
exports.__defineGetter__(
5347
'createConnectionPromise',
54-
() => require('./promise.js').createConnection
48+
() => require('./promise.js').createConnection,
5549
);
5650

5751
exports.__defineGetter__(
5852
'createPoolPromise',
59-
() => require('./promise.js').createPool
53+
() => require('./promise.js').createPool,
6054
);
6155

6256
exports.__defineGetter__(
6357
'createPoolClusterPromise',
64-
() => require('./promise.js').createPoolCluster
58+
() => require('./promise.js').createPoolCluster,
6559
);
6660

6761
exports.__defineGetter__('Types', () => require('./lib/constants/types.js'));
6862

6963
exports.__defineGetter__('Charsets', () =>
70-
require('./lib/constants/charsets.js')
64+
require('./lib/constants/charsets.js'),
7165
);
7266

7367
exports.__defineGetter__('CharsetToEncoding', () =>
74-
require('./lib/constants/charset_encodings.js')
68+
require('./lib/constants/charset_encodings.js'),
7569
);
7670

77-
exports.setMaxParserCache = function(max) {
71+
exports.setMaxParserCache = function (max) {
7872
parserCache.setMaxCache(max);
7973
};
8074

81-
exports.clearParserCache = function() {
75+
exports.clearParserCache = function () {
8276
parserCache.clearCache();
8377
};

0 commit comments

Comments
 (0)