@nocobase/plugin-multi-app-manager 2.1.0-alpha.1 → 2.1.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/LICENSE +201 -661
  2. package/README.md +79 -10
  3. package/dist/client/Migrate.d.ts +10 -0
  4. package/dist/client/e9551d3b66931c0a.js +10 -0
  5. package/dist/client/index.js +1 -1
  6. package/dist/externalVersion.js +7 -7
  7. package/dist/locale/en-US.json +3 -2
  8. package/dist/locale/zh-CN.json +3 -2
  9. package/dist/node_modules/p-queue/dist/index.d.ts +101 -0
  10. package/dist/node_modules/p-queue/dist/index.js +1 -0
  11. package/dist/node_modules/p-queue/dist/lower-bound.d.ts +1 -0
  12. package/dist/node_modules/p-queue/dist/lower-bound.js +21 -0
  13. package/dist/node_modules/p-queue/dist/options.d.ts +64 -0
  14. package/dist/node_modules/p-queue/dist/options.js +2 -0
  15. package/dist/node_modules/p-queue/dist/priority-queue.d.ts +12 -0
  16. package/dist/node_modules/p-queue/dist/priority-queue.js +32 -0
  17. package/dist/node_modules/p-queue/dist/queue.d.ts +7 -0
  18. package/dist/node_modules/p-queue/dist/queue.js +2 -0
  19. package/dist/node_modules/p-queue/license +9 -0
  20. package/dist/node_modules/p-queue/node_modules/eventemitter3/index.d.ts +134 -0
  21. package/dist/node_modules/p-queue/node_modules/eventemitter3/index.js +336 -0
  22. package/dist/node_modules/p-queue/node_modules/eventemitter3/package.json +56 -0
  23. package/dist/node_modules/p-queue/node_modules/eventemitter3/umd/eventemitter3.js +340 -0
  24. package/dist/node_modules/p-queue/node_modules/eventemitter3/umd/eventemitter3.min.js +1 -0
  25. package/dist/node_modules/p-queue/package.json +1 -0
  26. package/dist/server/adapters/legacy-adapter.d.ts +43 -0
  27. package/dist/server/adapters/legacy-adapter.js +207 -0
  28. package/dist/server/server.d.ts +4 -6
  29. package/dist/server/server.js +38 -191
  30. package/package.json +3 -3
  31. package/dist/client/db3a142b120d6553.js +0 -10
  32. package/dist/node_modules/mariadb/LICENSE +0 -502
  33. package/dist/node_modules/mariadb/callback.js +0 -41
  34. package/dist/node_modules/mariadb/lib/cmd/batch-bulk.js +0 -278
  35. package/dist/node_modules/mariadb/lib/cmd/batch-rewrite.js +0 -372
  36. package/dist/node_modules/mariadb/lib/cmd/change-user.js +0 -149
  37. package/dist/node_modules/mariadb/lib/cmd/class/ok-packet.js +0 -17
  38. package/dist/node_modules/mariadb/lib/cmd/column-definition.js +0 -102
  39. package/dist/node_modules/mariadb/lib/cmd/command.js +0 -168
  40. package/dist/node_modules/mariadb/lib/cmd/common-binary-cmd.js +0 -327
  41. package/dist/node_modules/mariadb/lib/cmd/common-text-cmd.js +0 -427
  42. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/caching-sha2-password-auth.js +0 -168
  43. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/clear-password-auth.js +0 -23
  44. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/ed25519-password-auth.js +0 -761
  45. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/native-password-auth.js +0 -55
  46. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/pam-password-auth.js +0 -58
  47. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/plugin-auth.js +0 -19
  48. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/sha256-password-auth.js +0 -142
  49. package/dist/node_modules/mariadb/lib/cmd/handshake/client-capabilities.js +0 -74
  50. package/dist/node_modules/mariadb/lib/cmd/handshake/client-handshake-response.js +0 -126
  51. package/dist/node_modules/mariadb/lib/cmd/handshake/handshake.js +0 -292
  52. package/dist/node_modules/mariadb/lib/cmd/handshake/initial-handshake.js +0 -74
  53. package/dist/node_modules/mariadb/lib/cmd/handshake/ssl-request.js +0 -29
  54. package/dist/node_modules/mariadb/lib/cmd/ping.js +0 -52
  55. package/dist/node_modules/mariadb/lib/cmd/query.js +0 -255
  56. package/dist/node_modules/mariadb/lib/cmd/quit.js +0 -28
  57. package/dist/node_modules/mariadb/lib/cmd/reset.js +0 -54
  58. package/dist/node_modules/mariadb/lib/cmd/resultset.js +0 -607
  59. package/dist/node_modules/mariadb/lib/cmd/stream.js +0 -45
  60. package/dist/node_modules/mariadb/lib/config/connection-options.js +0 -258
  61. package/dist/node_modules/mariadb/lib/config/pool-cluster-options.js +0 -19
  62. package/dist/node_modules/mariadb/lib/config/pool-options.js +0 -47
  63. package/dist/node_modules/mariadb/lib/connection-callback.js +0 -160
  64. package/dist/node_modules/mariadb/lib/connection.js +0 -1460
  65. package/dist/node_modules/mariadb/lib/const/capabilities.js +0 -64
  66. package/dist/node_modules/mariadb/lib/const/collations.js +0 -473
  67. package/dist/node_modules/mariadb/lib/const/connection_status.js +0 -13
  68. package/dist/node_modules/mariadb/lib/const/error-code.js +0 -1282
  69. package/dist/node_modules/mariadb/lib/const/field-detail.js +0 -35
  70. package/dist/node_modules/mariadb/lib/const/field-type.js +0 -71
  71. package/dist/node_modules/mariadb/lib/const/server-status.js +0 -30
  72. package/dist/node_modules/mariadb/lib/const/state-change.js +0 -12
  73. package/dist/node_modules/mariadb/lib/filtered-pool-cluster.js +0 -81
  74. package/dist/node_modules/mariadb/lib/io/bulk-packet.js +0 -590
  75. package/dist/node_modules/mariadb/lib/io/compression-input-stream.js +0 -141
  76. package/dist/node_modules/mariadb/lib/io/compression-output-stream.js +0 -171
  77. package/dist/node_modules/mariadb/lib/io/packet-input-stream.js +0 -193
  78. package/dist/node_modules/mariadb/lib/io/packet-node-encoded.js +0 -36
  79. package/dist/node_modules/mariadb/lib/io/packet-node-iconv.js +0 -37
  80. package/dist/node_modules/mariadb/lib/io/packet-output-stream.js +0 -502
  81. package/dist/node_modules/mariadb/lib/io/packet.js +0 -515
  82. package/dist/node_modules/mariadb/lib/io/rewrite-packet.js +0 -481
  83. package/dist/node_modules/mariadb/lib/misc/connection-information.js +0 -96
  84. package/dist/node_modules/mariadb/lib/misc/errors.js +0 -123
  85. package/dist/node_modules/mariadb/lib/misc/parse.js +0 -1033
  86. package/dist/node_modules/mariadb/lib/misc/utils.js +0 -298
  87. package/dist/node_modules/mariadb/lib/pool-base.js +0 -611
  88. package/dist/node_modules/mariadb/lib/pool-callback.js +0 -202
  89. package/dist/node_modules/mariadb/lib/pool-cluster-callback.js +0 -66
  90. package/dist/node_modules/mariadb/lib/pool-cluster.js +0 -407
  91. package/dist/node_modules/mariadb/lib/pool-promise.js +0 -108
  92. package/dist/node_modules/mariadb/node_modules/@types/node/assert/strict.d.ts +0 -8
  93. package/dist/node_modules/mariadb/node_modules/@types/node/assert.d.ts +0 -912
  94. package/dist/node_modules/mariadb/node_modules/@types/node/async_hooks.d.ts +0 -501
  95. package/dist/node_modules/mariadb/node_modules/@types/node/buffer.d.ts +0 -2232
  96. package/dist/node_modules/mariadb/node_modules/@types/node/child_process.d.ts +0 -1366
  97. package/dist/node_modules/mariadb/node_modules/@types/node/cluster.d.ts +0 -414
  98. package/dist/node_modules/mariadb/node_modules/@types/node/console.d.ts +0 -412
  99. package/dist/node_modules/mariadb/node_modules/@types/node/constants.d.ts +0 -18
  100. package/dist/node_modules/mariadb/node_modules/@types/node/crypto.d.ts +0 -3338
  101. package/dist/node_modules/mariadb/node_modules/@types/node/dgram.d.ts +0 -545
  102. package/dist/node_modules/mariadb/node_modules/@types/node/diagnostics_channel.d.ts +0 -152
  103. package/dist/node_modules/mariadb/node_modules/@types/node/dns/promises.d.ts +0 -370
  104. package/dist/node_modules/mariadb/node_modules/@types/node/dns.d.ts +0 -659
  105. package/dist/node_modules/mariadb/node_modules/@types/node/domain.d.ts +0 -169
  106. package/dist/node_modules/mariadb/node_modules/@types/node/events.d.ts +0 -651
  107. package/dist/node_modules/mariadb/node_modules/@types/node/fs/promises.d.ts +0 -1091
  108. package/dist/node_modules/mariadb/node_modules/@types/node/fs.d.ts +0 -3869
  109. package/dist/node_modules/mariadb/node_modules/@types/node/globals.d.ts +0 -294
  110. package/dist/node_modules/mariadb/node_modules/@types/node/globals.global.d.ts +0 -1
  111. package/dist/node_modules/mariadb/node_modules/@types/node/http.d.ts +0 -1396
  112. package/dist/node_modules/mariadb/node_modules/@types/node/http2.d.ts +0 -2101
  113. package/dist/node_modules/mariadb/node_modules/@types/node/https.d.ts +0 -391
  114. package/dist/node_modules/mariadb/node_modules/@types/node/index.d.ts +0 -129
  115. package/dist/node_modules/mariadb/node_modules/@types/node/inspector.d.ts +0 -2744
  116. package/dist/node_modules/mariadb/node_modules/@types/node/module.d.ts +0 -114
  117. package/dist/node_modules/mariadb/node_modules/@types/node/net.d.ts +0 -791
  118. package/dist/node_modules/mariadb/node_modules/@types/node/os.d.ts +0 -455
  119. package/dist/node_modules/mariadb/node_modules/@types/node/package.json +0 -220
  120. package/dist/node_modules/mariadb/node_modules/@types/node/path.d.ts +0 -180
  121. package/dist/node_modules/mariadb/node_modules/@types/node/perf_hooks.d.ts +0 -557
  122. package/dist/node_modules/mariadb/node_modules/@types/node/process.d.ts +0 -1481
  123. package/dist/node_modules/mariadb/node_modules/@types/node/punycode.d.ts +0 -117
  124. package/dist/node_modules/mariadb/node_modules/@types/node/querystring.d.ts +0 -131
  125. package/dist/node_modules/mariadb/node_modules/@types/node/readline.d.ts +0 -650
  126. package/dist/node_modules/mariadb/node_modules/@types/node/repl.d.ts +0 -424
  127. package/dist/node_modules/mariadb/node_modules/@types/node/stream/consumers.d.ts +0 -24
  128. package/dist/node_modules/mariadb/node_modules/@types/node/stream/promises.d.ts +0 -42
  129. package/dist/node_modules/mariadb/node_modules/@types/node/stream/web.d.ts +0 -330
  130. package/dist/node_modules/mariadb/node_modules/@types/node/stream.d.ts +0 -1249
  131. package/dist/node_modules/mariadb/node_modules/@types/node/string_decoder.d.ts +0 -67
  132. package/dist/node_modules/mariadb/node_modules/@types/node/timers/promises.d.ts +0 -68
  133. package/dist/node_modules/mariadb/node_modules/@types/node/timers.d.ts +0 -94
  134. package/dist/node_modules/mariadb/node_modules/@types/node/tls.d.ts +0 -1020
  135. package/dist/node_modules/mariadb/node_modules/@types/node/trace_events.d.ts +0 -161
  136. package/dist/node_modules/mariadb/node_modules/@types/node/tty.d.ts +0 -204
  137. package/dist/node_modules/mariadb/node_modules/@types/node/url.d.ts +0 -891
  138. package/dist/node_modules/mariadb/node_modules/@types/node/util.d.ts +0 -1594
  139. package/dist/node_modules/mariadb/node_modules/@types/node/v8.d.ts +0 -378
  140. package/dist/node_modules/mariadb/node_modules/@types/node/vm.d.ts +0 -507
  141. package/dist/node_modules/mariadb/node_modules/@types/node/wasi.d.ts +0 -158
  142. package/dist/node_modules/mariadb/node_modules/@types/node/worker_threads.d.ts +0 -649
  143. package/dist/node_modules/mariadb/node_modules/@types/node/zlib.d.ts +0 -517
  144. package/dist/node_modules/mariadb/package.json +0 -1
  145. package/dist/node_modules/mariadb/promise.js +0 -34
  146. package/dist/node_modules/mariadb/types/index.d.ts +0 -870
@@ -1,611 +0,0 @@
1
- 'use strict';
2
-
3
- const EventEmitter = require('events');
4
- const util = require('util');
5
- const Queue = require('denque');
6
- const Errors = require('./misc/errors');
7
- const Utils = require('./misc/utils');
8
-
9
- function PoolBase(options, processTask, createConnectionPool, pingPromise) {
10
- //*****************************************************************
11
- // public methods
12
- //*****************************************************************
13
-
14
- /**
15
- * Retrieve a connection from pool.
16
- * Create a new one, if limit is not reached.
17
- * wait until acquireTimeout.
18
- *
19
- * @return {Promise}
20
- */
21
- this.getConnection = function () {
22
- return addRequest(this);
23
- };
24
-
25
- /**
26
- * Execute a query on one connection from pool.
27
- *
28
- * @param sql sql command
29
- * @param value parameter value of sql command (not mandatory)
30
- * @return {Promise}
31
- */
32
- this.query = function (sql, value) {
33
- return addRequest(this, sql, value, false);
34
- };
35
-
36
- /**
37
- * Execute a batch on one connection from pool.
38
- *
39
- * @param sql sql command
40
- * @param value parameter value of sql command (not mandatory)
41
- * @return {Promise}
42
- */
43
- this.batch = function (sql, value) {
44
- return addRequest(this, sql, value, true);
45
- };
46
-
47
- /**
48
- * Close all connection in pool
49
- *
50
- * @return Promise
51
- */
52
- this.end = function () {
53
- if (closed) {
54
- return Promise.reject(
55
- Errors.createError(
56
- 'pool is already closed',
57
- null,
58
- false,
59
- null,
60
- 'HY000',
61
- Errors.ER_POOL_ALREADY_CLOSED,
62
- undefined,
63
- false
64
- )
65
- );
66
- }
67
- closed = true;
68
- clearInterval(idleMaintainingTask);
69
-
70
- //close unused connections
71
- const idleConnectionsEndings = [];
72
- let conn;
73
- while ((conn = idleConnections.shift())) {
74
- idleConnectionsEndings.push(conn.forceEnd());
75
- }
76
-
77
- firstTaskTimeout = clearTimeout(firstTaskTimeout);
78
-
79
- //reject all waiting task
80
- if (taskQueue.size() > 0) {
81
- let task;
82
- const err = Errors.createError(
83
- 'pool is ending, connection request aborted',
84
- null,
85
- false,
86
- null,
87
- 'HY000',
88
- Errors.ER_CLOSING_POOL,
89
- undefined,
90
- false
91
- );
92
- while ((task = taskQueue.shift())) {
93
- process.nextTick(task.reject, err);
94
- }
95
- }
96
-
97
- return Promise.all(idleConnectionsEndings);
98
- };
99
-
100
- /**
101
- * Get current active connections.
102
- * @return {number}
103
- */
104
- this.activeConnections = function () {
105
- return Object.keys(activeConnections).length;
106
- };
107
-
108
- /**
109
- * Get current total connection number.
110
- * @return {number}
111
- */
112
- this.totalConnections = function () {
113
- return this.activeConnections() + this.idleConnections();
114
- };
115
-
116
- /**
117
- * Get current idle connection number.
118
- * @return {number}
119
- */
120
- this.idleConnections = function () {
121
- return idleConnections.size();
122
- };
123
-
124
- /**
125
- * Get current stacked connection request.
126
- * @return {number}
127
- */
128
- this.taskQueueSize = function () {
129
- return taskQueue.size();
130
- };
131
-
132
- /**
133
- * First connection creation.
134
- * activation is slightly different than pooling grow : If connection fails, there is many retries for 30s
135
- * (option initializationTimeout).
136
- * If connection fails, error will be thrown to request / console if no request, to ensure that error is thrown.
137
- */
138
- this.initialize = function () {
139
- connectionInCreation = true;
140
- const self = this;
141
- const timeoutEnd = Date.now() + opts.initializationTimeout;
142
- connectionCreationLoop(self, 0, timeoutEnd)
143
- .then((conn) => {
144
- //add to pool
145
- if (closed) {
146
- conn.forceEnd().catch((err) => {});
147
- } else {
148
- addPoolConnection(self, conn);
149
- if (opts.idleTimeout > 0) {
150
- idleMaintainingTask = setInterval(idleMaintainer, 500, self);
151
- }
152
- }
153
- })
154
- .catch((err) => {
155
- connectionInCreation = false;
156
- const task = taskQueue.shift();
157
- if (task) {
158
- firstTaskTimeout = clearTimeout(firstTaskTimeout);
159
- process.nextTick(task.reject, err);
160
- resetTimeoutToNextTask();
161
- } else if (!closed) {
162
- console.error(err);
163
- }
164
- })
165
- .finally(() => {
166
- ensurePoolSize(self);
167
- });
168
- };
169
-
170
- this.escape = (value) => {
171
- return Utils.escape(options.connOptions, searchInfo(), value);
172
- };
173
-
174
- this.escapeId = (value) => {
175
- return Utils.escapeId(options.connOptions, searchInfo(), value);
176
- };
177
-
178
- //*****************************************************************
179
- // internal methods
180
- //*****************************************************************
181
-
182
- /**
183
- * Search info object of an existing connection. to know server type and version.
184
- * @returns information object if connection available.
185
- */
186
- const searchInfo = () => {
187
- let info = null;
188
- let conn = idleConnections.get(0);
189
-
190
- if (conn == null) {
191
- conn = Object.keys(activeConnections)[0];
192
- }
193
-
194
- if (conn != null) {
195
- info = conn.info;
196
- }
197
- return info;
198
- };
199
-
200
- /**
201
- * Get a connection from pool / execute query
202
- *
203
- * @param pool current pool
204
- * @param sql sql value (not mandatory)
205
- * @param values sql parameter (not mandatory)
206
- * @param isBatch is batch request
207
- * @return {*}
208
- */
209
- const addRequest = function (pool, sql, values, isBatch) {
210
- if (isBatch != undefined && !sql) {
211
- // request for query/batch without sql
212
- return Promise.reject(
213
- Errors.createError(
214
- 'sql parameter is mandatory',
215
- null,
216
- false,
217
- null,
218
- 'HY000',
219
- Errors.ER_POOL_UNDEFINED_SQL,
220
- undefined,
221
- false
222
- )
223
- );
224
- }
225
-
226
- if (closed) {
227
- return Promise.reject(
228
- Errors.createError(
229
- 'pool is closed',
230
- null,
231
- false,
232
- null,
233
- 'HY000',
234
- Errors.ER_POOL_ALREADY_CLOSED,
235
- undefined,
236
- false
237
- )
238
- );
239
- }
240
-
241
- return getIdleValidConnection(pool).then(
242
- (conn) => {
243
- pool.emit('acquire', conn);
244
- return processTask(conn, sql, values, isBatch);
245
- },
246
- () => {
247
- process.nextTick(() => pool.emit('enqueue'));
248
-
249
- //no idle connection available
250
- //create a new connection if limit is not reached
251
- ensurePoolSize(pool);
252
-
253
- //connections are all used, stack demand.
254
- return new Promise((resolve, reject) => {
255
- const task = {
256
- timeout: Date.now() + opts.acquireTimeout,
257
- reject: reject,
258
- resolve: resolve,
259
- sql: sql,
260
- values: values,
261
- isBatch: isBatch
262
- };
263
- if (!firstTaskTimeout) {
264
- firstTaskTimeout = setTimeout(rejectAndResetTimeout, opts.acquireTimeout, task);
265
- }
266
- taskQueue.push(task);
267
- });
268
- }
269
- );
270
- };
271
-
272
- /**
273
- * Return an idle Connection.
274
- * If connection has not been used for some time ( minDelayValidation), validate connection status.
275
- *
276
- * @param pool pool
277
- * @returns {Promise<Connection|null>)} connection of null of no valid idle connection.
278
- */
279
- const getIdleValidConnection = function (pool) {
280
- if (idleConnections.isEmpty()) {
281
- return Promise.reject(null);
282
- }
283
- const conn = idleConnections.shift();
284
- activeConnections[conn.threadId] = conn;
285
- if (opts.minDelayValidation <= 0 || Date.now() - conn.lastUse > opts.minDelayValidation) {
286
- return pingPromise(conn)
287
- .then(() => {
288
- initLeakProcess(conn);
289
- return Promise.resolve(conn);
290
- })
291
- .catch((err) => {
292
- delete activeConnections[conn.threadId];
293
- pool.emit('_remove-conn');
294
- return getIdleValidConnection(pool);
295
- });
296
- } else {
297
- //just check connection state
298
- if (conn.isValid()) {
299
- initLeakProcess(conn);
300
- return Promise.resolve(conn);
301
- } else {
302
- delete activeConnections[conn.threadId];
303
- pool.emit('_remove-conn');
304
- return getIdleValidConnection(pool);
305
- }
306
- }
307
- };
308
-
309
- /**
310
- * Task request timeout handler
311
- * @param task
312
- */
313
- const timeoutTask = (task) => {
314
- firstTaskTimeout = null;
315
- if (task === taskQueue.peekFront()) {
316
- taskQueue.shift();
317
- process.nextTick(
318
- task.reject,
319
- Errors.createError(
320
- 'retrieve connection from pool timeout after ' +
321
- Math.abs(Date.now() - (task.timeout - opts.acquireTimeout)) +
322
- 'ms',
323
- null,
324
- false,
325
- null,
326
- 'HY000',
327
- Errors.ER_GET_CONNECTION_TIMEOUT,
328
- undefined,
329
- false
330
- )
331
- );
332
- } else {
333
- throw new Error('Rejection by timeout without task !!!');
334
- }
335
- };
336
-
337
- /**
338
- * Reject task, and reset timeout to next waiting task if any.
339
- * @param task
340
- */
341
- const rejectAndResetTimeout = (task) => {
342
- timeoutTask(task);
343
- resetTimeoutToNextTask();
344
- };
345
-
346
- /**
347
- * Loop for connection creation.
348
- * This permits to wait before next try after a connection fail.
349
- *
350
- * @param pool current pool
351
- * @param iteration current iteration
352
- * @param timeoutEnd ending timeout
353
- * @returns {Promise<any>} Connection if found, error if not
354
- */
355
- const connectionCreationLoop = function (pool, iteration, timeoutEnd) {
356
- return new Promise(function (resolve, reject) {
357
- const creationTryout = function (resolve, reject) {
358
- if (closed) {
359
- reject(
360
- Errors.createError(
361
- 'Cannot create new connection to pool, pool closed',
362
- null,
363
- true,
364
- null,
365
- '08S01',
366
- Errors.ER_ADD_CONNECTION_CLOSED_POOL,
367
- null
368
- )
369
- );
370
- return;
371
- }
372
- iteration++;
373
- createConnectionPool(pool)
374
- .then((conn) => {
375
- resolve(conn);
376
- })
377
- .catch((err) => {
378
- //if timeout is reached or authentication fail return error
379
- if (
380
- closed ||
381
- (err.errno && (err.errno === 1524 || err.errno === 1045 || err.errno === 1698)) ||
382
- timeoutEnd < Date.now()
383
- ) {
384
- reject(err);
385
- return;
386
- }
387
-
388
- setTimeout(creationTryout.bind(null, resolve, reject), 500);
389
- });
390
- };
391
- //initial without timeout
392
- creationTryout(resolve, reject);
393
- });
394
- };
395
-
396
- const addPoolConnection = function (pool, conn) {
397
- conn.lastUse = Date.now();
398
- const initialDestroyFct = conn.destroy;
399
- conn.destroy = () => {
400
- removeLeakProcess(conn);
401
- delete activeConnections[conn.threadId];
402
- initialDestroyFct();
403
- pool.emit('_remove-conn');
404
- };
405
-
406
- //Connection error
407
- // -> evict connection from pool
408
- conn.on('error', (err) => {
409
- let idx = 0;
410
- let currConn;
411
- removeLeakProcess(conn);
412
- delete activeConnections[conn.threadId];
413
- while ((currConn = idleConnections.peekAt(idx))) {
414
- if (currConn === conn) {
415
- idleConnections.removeOne(idx);
416
- break;
417
- } else {
418
- //since connection did have an error, other waiting connection might too
419
- //forcing validation when borrowed next time, even if "minDelayValidation" is not reached.
420
- currConn.lastUse = Math.min(Date.now() - opts.minDelayValidation, currConn.lastUse);
421
- }
422
- idx++;
423
- }
424
- pool.emit('_remove-conn');
425
- });
426
- connectionInCreation = false;
427
- idleConnections.push(conn);
428
- pool.emit('_idle-conn');
429
- process.nextTick(() => pool.emit('connection', conn));
430
- };
431
-
432
- this._releaseConnection = function (conn) {
433
- removeLeakProcess(conn);
434
- conn.lastUse = Date.now();
435
- delete activeConnections[conn.threadId];
436
- const pool = this;
437
- if (closed) {
438
- return conn.forceEnd().catch(() => {
439
- return Promise.resolve();
440
- });
441
- } else if (conn.isValid()) {
442
- pool.emit('release', conn);
443
- idleConnections.push(conn);
444
- process.nextTick(() => pool.emit('_idle-conn'));
445
- } else {
446
- ensurePoolSize(pool);
447
- }
448
- };
449
-
450
- /**
451
- * Grow pool connections until reaching connection limit.
452
- */
453
- const ensurePoolSize = function (pool) {
454
- if (
455
- !connectionInCreation &&
456
- pool.idleConnections() < opts.minimumIdle &&
457
- pool.totalConnections() < opts.connectionLimit &&
458
- !closed
459
- ) {
460
- connectionInCreation = true;
461
- process.nextTick(() => {
462
- const timeoutEnd = Date.now() + opts.initializationTimeout;
463
- if (!closed) {
464
- connectionCreationLoop(pool, 0, timeoutEnd)
465
- .then((conn) => {
466
- if (closed) {
467
- return conn.forceEnd().catch((err) => {});
468
- }
469
- addPoolConnection(pool, conn);
470
- })
471
- .catch((err) => {
472
- if (pool.totalConnections() === 0) {
473
- const task = taskQueue.shift();
474
- if (task) {
475
- firstTaskTimeout = clearTimeout(firstTaskTimeout);
476
- process.nextTick(task.reject, err);
477
- resetTimeoutToNextTask();
478
- }
479
- } else if (!closed) {
480
- console.error(`pool fail to create connection (${err.message})`);
481
- }
482
-
483
- //delay next try
484
- setTimeout(() => {
485
- connectionInCreation = false;
486
- if (taskQueue.size() > 0) {
487
- ensurePoolSize(pool);
488
- }
489
- }, 500);
490
- });
491
- }
492
- });
493
- }
494
- };
495
-
496
- const resetTimeoutToNextTask = () => {
497
- //handle next Timer
498
- const currTime = Date.now();
499
- let nextTask;
500
- while ((nextTask = taskQueue.peekFront())) {
501
- if (nextTask.timeout < currTime) {
502
- timeoutTask(nextTask);
503
- } else {
504
- firstTaskTimeout = setTimeout(rejectAndResetTimeout, nextTask.timeout - currTime, nextTask);
505
- return;
506
- }
507
- }
508
- };
509
-
510
- /**
511
- * Permit to remove idle connection if unused for some time.
512
- * @param pool current pool
513
- */
514
- const idleMaintainer = function (pool) {
515
- let toRemove = Math.max(1, pool.idleConnections() - opts.minimumIdle);
516
- while (toRemove > 0) {
517
- const conn = idleConnections.peek();
518
- --toRemove;
519
- if (conn && conn.lastUse + opts.idleTimeout * 1000 < Date.now()) {
520
- idleConnections.shift();
521
- conn.forceEnd().catch((err) => {});
522
- continue;
523
- }
524
- break;
525
- }
526
- ensurePoolSize(pool);
527
- };
528
-
529
- this._discardConnection = (conn) => {
530
- removeLeakProcess(conn);
531
- delete activeConnections[conn.threadId];
532
- conn.forceEnd().catch((err) => {});
533
- this.emit('_remove-conn');
534
- };
535
-
536
- const logLeak = (conn) => {
537
- console.log(
538
- 'Possible connection leak on thread ' +
539
- conn.info.threadId +
540
- ' (connection not returned to pool since ' +
541
- (Date.now() - conn.lastUse) +
542
- 'ms. Did connection.released() been implemented'
543
- );
544
- conn.leaked = true;
545
- };
546
-
547
- const _initLeakProcess = (conn) => {
548
- conn.lastUse = Date.now();
549
- conn.leaked = false;
550
- conn.leakProcess = setTimeout(logLeak, opts.leakDetectionTimeout, conn);
551
- };
552
-
553
- const _removeLeakProcess = (conn) => {
554
- conn.leakProcess = clearTimeout(conn.leakProcess);
555
- if (conn.leaked) {
556
- console.log(
557
- 'Previous possible leak connection with thread ' +
558
- conn.info.threadId +
559
- ' was returned to pool'
560
- );
561
- }
562
- };
563
-
564
- /**
565
- * Launch next waiting task request if available connections.
566
- */
567
- const handleTaskQueue = function () {
568
- firstTaskTimeout = clearTimeout(firstTaskTimeout);
569
- const task = taskQueue.shift();
570
-
571
- if (task) {
572
- const conn = idleConnections.shift();
573
- if (conn) {
574
- initLeakProcess(conn);
575
- this.emit('acquire', conn);
576
- activeConnections[conn.threadId] = conn;
577
- resetTimeoutToNextTask();
578
- processTask(conn, task.sql, task.values, task.isBatch)
579
- .then(task.resolve)
580
- .catch(task.reject);
581
- } else {
582
- taskQueue.unshift(task);
583
- }
584
- }
585
- };
586
-
587
- const opts = options;
588
- let closed = false;
589
- let connectionInCreation = false;
590
- const initLeakProcess = opts.leakDetectionTimeout > 0 ? _initLeakProcess : () => {};
591
- const removeLeakProcess = opts.leakDetectionTimeout > 0 ? _removeLeakProcess : () => {};
592
- const idleConnections = new Queue();
593
- const activeConnections = {};
594
- const taskQueue = new Queue();
595
- let idleMaintainingTask;
596
- let firstTaskTimeout;
597
- Object.defineProperty(this, 'closed', {
598
- get() {
599
- return closed;
600
- }
601
- });
602
-
603
- EventEmitter.call(this);
604
-
605
- this.on('_idle-conn', handleTaskQueue.bind(this));
606
- this.on('_remove-conn', ensurePoolSize.bind(this, this));
607
- this.on('connection', ensurePoolSize.bind(this, this));
608
- }
609
-
610
- util.inherits(PoolBase, EventEmitter);
611
- module.exports = PoolBase;