monsqlize 1.3.0 → 2.0.0
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.
- package/CHANGELOG.md +56 -60
- package/LICENSE +201 -21
- package/README.md +537 -1828
- package/changelogs/README.md +160 -0
- package/changelogs/v2.0.0.md +222 -0
- package/dist/cjs/index.cjs +10600 -0
- package/dist/cjs/mongodb/common/transaction-aware.cjs +10 -0
- package/dist/cjs/transaction/CacheLockManager.cjs +100 -0
- package/dist/cjs/transaction/Transaction.cjs +158 -0
- package/dist/cjs/transaction/TransactionManager.cjs +298 -0
- package/dist/esm/index.mjs +10650 -0
- package/dist/types/base.d.ts +81 -0
- package/dist/types/collection.d.ts +1031 -0
- package/dist/types/expression.d.ts +115 -0
- package/dist/types/index.d.ts +23 -0
- package/dist/types/lock.d.ts +74 -0
- package/dist/types/model.d.ts +526 -0
- package/dist/types/mongodb.d.ts +49 -0
- package/dist/types/monsqlize.d.ts +491 -0
- package/dist/types/pool.d.ts +84 -0
- package/dist/types/runtime.d.ts +362 -0
- package/dist/types/saga.d.ts +143 -0
- package/dist/types/slow-query-log.d.ts +126 -0
- package/dist/types/sync.d.ts +103 -0
- package/dist/types/transaction.d.ts +132 -0
- package/package.json +67 -69
- package/index.d.ts +0 -206
- package/index.mjs +0 -52
- package/lib/cache-invalidation.js +0 -279
- package/lib/cache.js +0 -530
- package/lib/common/cursor.js +0 -59
- package/lib/common/docs-urls.js +0 -73
- package/lib/common/index-options.js +0 -223
- package/lib/common/log.js +0 -61
- package/lib/common/namespace.js +0 -22
- package/lib/common/normalize.js +0 -34
- package/lib/common/page-result.js +0 -43
- package/lib/common/runner.js +0 -57
- package/lib/common/server-features.js +0 -232
- package/lib/common/shape-builders.js +0 -27
- package/lib/common/validation.js +0 -113
- package/lib/connect.js +0 -99
- package/lib/constants.js +0 -55
- package/lib/count-queue.js +0 -188
- package/lib/distributed-cache-invalidator.js +0 -260
- package/lib/errors.js +0 -168
- package/lib/expression/cache/ExpressionCache.js +0 -114
- package/lib/expression/compiler/ExpressionCompiler.js +0 -1090
- package/lib/expression/compiler/ExpressionCompilerExtensions.js +0 -531
- package/lib/expression/detector.js +0 -84
- package/lib/expression/factory.js +0 -29
- package/lib/expression/index.js +0 -19
- package/lib/function-cache.js +0 -533
- package/lib/index.js +0 -1251
- package/lib/infrastructure/ConnectionPoolManager.js +0 -464
- package/lib/infrastructure/HealthChecker.js +0 -281
- package/lib/infrastructure/PoolConfig.js +0 -199
- package/lib/infrastructure/PoolSelector.js +0 -225
- package/lib/infrastructure/PoolStats.js +0 -244
- package/lib/infrastructure/ssh-tunnel-ssh2.js +0 -212
- package/lib/infrastructure/ssh-tunnel.js +0 -41
- package/lib/infrastructure/uri-parser.js +0 -36
- package/lib/lock/Lock.js +0 -67
- package/lib/lock/errors.js +0 -28
- package/lib/lock/index.js +0 -13
- package/lib/logger.js +0 -225
- package/lib/model/examples/test.js +0 -311
- package/lib/model/features/defaults.js +0 -161
- package/lib/model/features/populate.js +0 -568
- package/lib/model/features/relations.js +0 -120
- package/lib/model/features/soft-delete.js +0 -349
- package/lib/model/features/version.js +0 -157
- package/lib/model/features/virtuals.js +0 -219
- package/lib/model/index.js +0 -1265
- package/lib/mongodb/common/accessor-helpers.js +0 -59
- package/lib/mongodb/common/agg-pipeline.js +0 -36
- package/lib/mongodb/common/aggregation-validator.js +0 -127
- package/lib/mongodb/common/iid.js +0 -28
- package/lib/mongodb/common/lexicographic-expr.js +0 -53
- package/lib/mongodb/common/shape.js +0 -32
- package/lib/mongodb/common/sort.js +0 -39
- package/lib/mongodb/common/transaction-aware.js +0 -25
- package/lib/mongodb/connect.js +0 -234
- package/lib/mongodb/index.js +0 -639
- package/lib/mongodb/management/admin-ops.js +0 -200
- package/lib/mongodb/management/bookmark-ops.js +0 -167
- package/lib/mongodb/management/cache-ops.js +0 -50
- package/lib/mongodb/management/collection-ops.js +0 -387
- package/lib/mongodb/management/database-ops.js +0 -202
- package/lib/mongodb/management/index-ops.js +0 -475
- package/lib/mongodb/management/index.js +0 -17
- package/lib/mongodb/management/namespace.js +0 -31
- package/lib/mongodb/management/validation-ops.js +0 -268
- package/lib/mongodb/queries/aggregate.js +0 -172
- package/lib/mongodb/queries/chain.js +0 -631
- package/lib/mongodb/queries/count.js +0 -99
- package/lib/mongodb/queries/distinct.js +0 -78
- package/lib/mongodb/queries/find-and-count.js +0 -193
- package/lib/mongodb/queries/find-by-ids.js +0 -236
- package/lib/mongodb/queries/find-one-by-id.js +0 -171
- package/lib/mongodb/queries/find-one.js +0 -71
- package/lib/mongodb/queries/find-page.js +0 -618
- package/lib/mongodb/queries/find.js +0 -171
- package/lib/mongodb/queries/index.js +0 -51
- package/lib/mongodb/queries/watch.js +0 -538
- package/lib/mongodb/writes/common/batch-retry.js +0 -65
- package/lib/mongodb/writes/delete-batch.js +0 -323
- package/lib/mongodb/writes/delete-many.js +0 -181
- package/lib/mongodb/writes/delete-one.js +0 -173
- package/lib/mongodb/writes/find-one-and-delete.js +0 -203
- package/lib/mongodb/writes/find-one-and-replace.js +0 -239
- package/lib/mongodb/writes/find-one-and-update.js +0 -240
- package/lib/mongodb/writes/increment-one.js +0 -259
- package/lib/mongodb/writes/index.js +0 -46
- package/lib/mongodb/writes/insert-batch.js +0 -508
- package/lib/mongodb/writes/insert-many.js +0 -223
- package/lib/mongodb/writes/insert-one.js +0 -169
- package/lib/mongodb/writes/replace-one.js +0 -226
- package/lib/mongodb/writes/result-handler.js +0 -237
- package/lib/mongodb/writes/update-batch.js +0 -416
- package/lib/mongodb/writes/update-many.js +0 -275
- package/lib/mongodb/writes/update-one.js +0 -273
- package/lib/mongodb/writes/upsert-one.js +0 -203
- package/lib/multi-level-cache.js +0 -244
- package/lib/operators.js +0 -330
- package/lib/redis-cache-adapter.js +0 -267
- package/lib/saga/SagaContext.js +0 -67
- package/lib/saga/SagaDefinition.js +0 -32
- package/lib/saga/SagaExecutor.js +0 -201
- package/lib/saga/SagaOrchestrator.js +0 -186
- package/lib/saga/index.js +0 -11
- package/lib/slow-query-log/base-storage.js +0 -70
- package/lib/slow-query-log/batch-queue.js +0 -97
- package/lib/slow-query-log/config-manager.js +0 -196
- package/lib/slow-query-log/index.js +0 -238
- package/lib/slow-query-log/mongodb-storage.js +0 -324
- package/lib/slow-query-log/query-hash.js +0 -39
- package/lib/sync/ChangeStreamSyncManager.js +0 -405
- package/lib/sync/ResumeTokenStore.js +0 -192
- package/lib/sync/SyncConfig.js +0 -127
- package/lib/sync/SyncTarget.js +0 -240
- package/lib/sync/index.js +0 -20
- package/lib/transaction/CacheLockManager.js +0 -162
- package/lib/transaction/DistributedCacheLockManager.js +0 -475
- package/lib/transaction/Transaction.js +0 -315
- package/lib/transaction/TransactionManager.js +0 -267
- package/lib/transaction/index.js +0 -11
- package/lib/utils/objectid-converter.js +0 -632
- package/types/README.md +0 -122
- package/types/base.ts +0 -94
- package/types/batch.ts +0 -187
- package/types/cache.ts +0 -71
- package/types/chain.ts +0 -254
- package/types/collection.ts +0 -357
- package/types/expression.ts +0 -109
- package/types/function-cache.d.ts +0 -135
- package/types/lock.ts +0 -95
- package/types/model/definition.ts +0 -152
- package/types/model/index.ts +0 -10
- package/types/model/instance.ts +0 -121
- package/types/model/relations.ts +0 -121
- package/types/model/virtuals.ts +0 -32
- package/types/monsqlize.ts +0 -245
- package/types/options.ts +0 -192
- package/types/pagination.ts +0 -154
- package/types/pool.ts +0 -125
- package/types/query.ts +0 -71
- package/types/saga.ts +0 -125
- package/types/stream.ts +0 -64
- package/types/sync.ts +0 -79
- package/types/transaction.ts +0 -79
- package/types/write.ts +0 -77
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PoolStats - 连接池统计收集器
|
|
3
|
-
*
|
|
4
|
-
* 收集连接池的统计信息(连接数、响应时间、请求数等)
|
|
5
|
-
* 🔴 修复问题6:使用异步批量更新提升性能
|
|
6
|
-
*
|
|
7
|
-
* @module lib/infrastructure/PoolStats
|
|
8
|
-
* @since v1.0.8
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 统计收集器
|
|
13
|
-
*/
|
|
14
|
-
class PoolStats {
|
|
15
|
-
/**
|
|
16
|
-
* 构造函数
|
|
17
|
-
*
|
|
18
|
-
* @param {Object} options - 配置选项
|
|
19
|
-
* @param {Object} options.logger - 日志记录器
|
|
20
|
-
*/
|
|
21
|
-
constructor(options = {}) {
|
|
22
|
-
this._logger = options.logger || console;
|
|
23
|
-
|
|
24
|
-
this._stats = new Map(); // Map<poolName, StatsData>
|
|
25
|
-
this._buffer = []; // 缓冲区(修复问题6)
|
|
26
|
-
|
|
27
|
-
// 启动批量更新(每100ms)
|
|
28
|
-
this._batchInterval = setInterval(() => {
|
|
29
|
-
this._flush();
|
|
30
|
-
}, 100);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 记录选择事件
|
|
35
|
-
*
|
|
36
|
-
* @param {string} poolName - 连接池名称
|
|
37
|
-
* @param {string} operation - 操作类型
|
|
38
|
-
*/
|
|
39
|
-
recordSelection(poolName, operation) {
|
|
40
|
-
this._buffer.push({
|
|
41
|
-
poolName,
|
|
42
|
-
type: 'selection',
|
|
43
|
-
operation,
|
|
44
|
-
timestamp: Date.now()
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 记录查询(测试兼容方法)
|
|
50
|
-
*
|
|
51
|
-
* @param {string} poolName - 连接池名称
|
|
52
|
-
* @param {number} responseTime - 响应时间(毫秒)
|
|
53
|
-
* @param {Error|null} error - 错误对象
|
|
54
|
-
*/
|
|
55
|
-
async recordQuery(poolName, responseTime, error) {
|
|
56
|
-
this.recordRequest(poolName, responseTime, !error);
|
|
57
|
-
// 立即刷新以确保测试能获取最新统计
|
|
58
|
-
this._flush();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* 记录连接数(测试兼容方法)
|
|
63
|
-
*
|
|
64
|
-
* @param {string} poolName - 连接池名称
|
|
65
|
-
* @param {number} count - 连接数
|
|
66
|
-
*/
|
|
67
|
-
recordConnections(poolName, count) {
|
|
68
|
-
let stats = this._stats.get(poolName);
|
|
69
|
-
if (!stats) {
|
|
70
|
-
stats = {
|
|
71
|
-
connections: count,
|
|
72
|
-
available: 0,
|
|
73
|
-
waiting: 0,
|
|
74
|
-
totalRequests: 0,
|
|
75
|
-
successRequests: 0,
|
|
76
|
-
failedRequests: 0,
|
|
77
|
-
totalResponseTime: 0,
|
|
78
|
-
avgResponseTime: 0,
|
|
79
|
-
errorRate: 0
|
|
80
|
-
};
|
|
81
|
-
this._stats.set(poolName, stats);
|
|
82
|
-
} else {
|
|
83
|
-
stats.connections = count;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* 记录请求完成
|
|
89
|
-
*
|
|
90
|
-
* @param {string} poolName - 连接池名称
|
|
91
|
-
* @param {number} responseTime - 响应时间(毫秒)
|
|
92
|
-
* @param {boolean} success - 是否成功
|
|
93
|
-
*/
|
|
94
|
-
recordRequest(poolName, responseTime, success) {
|
|
95
|
-
this._buffer.push({
|
|
96
|
-
poolName,
|
|
97
|
-
type: 'request',
|
|
98
|
-
responseTime,
|
|
99
|
-
success,
|
|
100
|
-
timestamp: Date.now()
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* 批量更新统计(修复问题6)
|
|
106
|
-
*
|
|
107
|
-
* @private
|
|
108
|
-
*/
|
|
109
|
-
_flush() {
|
|
110
|
-
if (this._buffer.length === 0) {
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// 取出缓冲区数据
|
|
115
|
-
const batch = this._buffer.splice(0);
|
|
116
|
-
|
|
117
|
-
// 批量更新
|
|
118
|
-
for (const item of batch) {
|
|
119
|
-
this._updateStats(item);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* 更新统计
|
|
125
|
-
*
|
|
126
|
-
* @private
|
|
127
|
-
*/
|
|
128
|
-
_updateStats(item) {
|
|
129
|
-
const { poolName } = item;
|
|
130
|
-
|
|
131
|
-
// 获取或创建统计对象
|
|
132
|
-
let stats = this._stats.get(poolName);
|
|
133
|
-
if (!stats) {
|
|
134
|
-
stats = {
|
|
135
|
-
connections: 0,
|
|
136
|
-
available: 0,
|
|
137
|
-
waiting: 0,
|
|
138
|
-
totalRequests: 0,
|
|
139
|
-
successRequests: 0,
|
|
140
|
-
failedRequests: 0,
|
|
141
|
-
totalResponseTime: 0,
|
|
142
|
-
avgResponseTime: 0,
|
|
143
|
-
errorRate: 0
|
|
144
|
-
};
|
|
145
|
-
this._stats.set(poolName, stats);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// 根据类型更新
|
|
149
|
-
if (item.type === 'selection') {
|
|
150
|
-
// 选择事件:暂时不需要更新
|
|
151
|
-
} else if (item.type === 'request') {
|
|
152
|
-
// 请求事件
|
|
153
|
-
stats.totalRequests++;
|
|
154
|
-
|
|
155
|
-
if (item.success) {
|
|
156
|
-
stats.successRequests++;
|
|
157
|
-
} else {
|
|
158
|
-
stats.failedRequests++;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
stats.totalResponseTime += item.responseTime;
|
|
162
|
-
stats.avgResponseTime = stats.totalResponseTime / stats.totalRequests;
|
|
163
|
-
stats.errorRate = stats.failedRequests / stats.totalRequests;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* 获取连接池统计
|
|
169
|
-
*
|
|
170
|
-
* @param {string} poolName - 连接池名称
|
|
171
|
-
* @returns {Object} 统计对象(总是返回,不存在则返回初始值)
|
|
172
|
-
*/
|
|
173
|
-
getStats(poolName) {
|
|
174
|
-
let stats = this._stats.get(poolName);
|
|
175
|
-
if (!stats) {
|
|
176
|
-
// 返回初��统计对象
|
|
177
|
-
stats = {
|
|
178
|
-
connections: 0,
|
|
179
|
-
available: 0,
|
|
180
|
-
waiting: 0,
|
|
181
|
-
totalRequests: 0,
|
|
182
|
-
successRequests: 0,
|
|
183
|
-
failedRequests: 0,
|
|
184
|
-
totalResponseTime: 0,
|
|
185
|
-
avgResponseTime: 0,
|
|
186
|
-
errorRate: 0
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
return { ...stats }; // 返回副本
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* 获取所有统计
|
|
194
|
-
*
|
|
195
|
-
* @returns {Object}
|
|
196
|
-
*/
|
|
197
|
-
getAllStats() {
|
|
198
|
-
const result = {};
|
|
199
|
-
|
|
200
|
-
for (const [poolName, stats] of this._stats.entries()) {
|
|
201
|
-
result[poolName] = { ...stats };
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return result;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* 重置统计
|
|
209
|
-
*
|
|
210
|
-
* @param {string} [poolName] - 连接池名称(不传则重置所有)
|
|
211
|
-
*/
|
|
212
|
-
reset(poolName) {
|
|
213
|
-
if (poolName) {
|
|
214
|
-
this._stats.delete(poolName);
|
|
215
|
-
} else {
|
|
216
|
-
this._stats.clear();
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* 重置所有统计
|
|
222
|
-
*/
|
|
223
|
-
resetAll() {
|
|
224
|
-
this._stats.clear();
|
|
225
|
-
this._buffer = [];
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* 关闭统计收集
|
|
230
|
-
*/
|
|
231
|
-
close() {
|
|
232
|
-
if (this._batchInterval) {
|
|
233
|
-
clearInterval(this._batchInterval);
|
|
234
|
-
this._batchInterval = null;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// 刷新剩余缓冲
|
|
238
|
-
this._flush();
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
module.exports = PoolStats;
|
|
243
|
-
|
|
244
|
-
|
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SSH隧道管理器 - ssh2实现
|
|
3
|
-
* 支持密码认证和私钥认证
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const { Client } = require('ssh2');
|
|
7
|
-
const net = require('net');
|
|
8
|
-
const fs = require('fs');
|
|
9
|
-
const path = require('path');
|
|
10
|
-
const os = require('os');
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 基于ssh2的SSH隧道管理器
|
|
14
|
-
* 支持密码认证和私钥认证
|
|
15
|
-
*/
|
|
16
|
-
class SSHTunnelSSH2 {
|
|
17
|
-
/**
|
|
18
|
-
* @param {Object} sshConfig - SSH配置
|
|
19
|
-
* @param {string} sshConfig.host - SSH服务器地址
|
|
20
|
-
* @param {number} [sshConfig.port=22] - SSH端口
|
|
21
|
-
* @param {string} sshConfig.username - SSH用户名
|
|
22
|
-
* @param {string} [sshConfig.password] - SSH密码
|
|
23
|
-
* @param {string} [sshConfig.privateKey] - 私钥内容
|
|
24
|
-
* @param {string} [sshConfig.privateKeyPath] - 私钥路径
|
|
25
|
-
* @param {string} [sshConfig.passphrase] - 私钥密码
|
|
26
|
-
* @param {number} [sshConfig.localPort] - 本地监听端口(可选,默认随机)
|
|
27
|
-
* @param {number} [sshConfig.readyTimeout=20000] - 连接超时(毫秒)
|
|
28
|
-
* @param {number} [sshConfig.keepaliveInterval=30000] - 心跳间隔(毫秒)
|
|
29
|
-
* @param {string} remoteHost - 远程主机(数据库服务器地址)
|
|
30
|
-
* @param {number} remotePort - 远程端口(数据库端口)
|
|
31
|
-
* @param {Object} options - 可选配置
|
|
32
|
-
* @param {Object} options.logger - 日志记录器
|
|
33
|
-
* @param {string} options.name - 隧道名称(用于日志)
|
|
34
|
-
*/
|
|
35
|
-
constructor(sshConfig, remoteHost, remotePort, options = {}) {
|
|
36
|
-
this.sshConfig = sshConfig;
|
|
37
|
-
this.remoteHost = remoteHost;
|
|
38
|
-
this.remotePort = remotePort;
|
|
39
|
-
this.logger = options.logger;
|
|
40
|
-
this.name = options.name || `${remoteHost}:${remotePort}`;
|
|
41
|
-
|
|
42
|
-
this.sshClient = null;
|
|
43
|
-
this.server = null;
|
|
44
|
-
this.localPort = null;
|
|
45
|
-
this.isConnected = false;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 建立SSH隧道
|
|
50
|
-
* @returns {Promise<number>} 本地监听端口
|
|
51
|
-
*/
|
|
52
|
-
async connect() {
|
|
53
|
-
if (this.isConnected) {
|
|
54
|
-
this.logger?.warn?.(`SSH tunnel [${this.name}] already connected`);
|
|
55
|
-
return this.localPort;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return new Promise((resolve, reject) => {
|
|
59
|
-
this.sshClient = new Client();
|
|
60
|
-
|
|
61
|
-
this.sshClient.on('ready', () => {
|
|
62
|
-
this.logger?.info?.(`✅ SSH connection established [${this.name}]`);
|
|
63
|
-
|
|
64
|
-
// 创建本地TCP服务器(端口转发)
|
|
65
|
-
this.server = net.createServer((sock) => {
|
|
66
|
-
this.sshClient.forwardOut(
|
|
67
|
-
sock.remoteAddress,
|
|
68
|
-
sock.remotePort,
|
|
69
|
-
this.remoteHost,
|
|
70
|
-
this.remotePort,
|
|
71
|
-
(err, stream) => {
|
|
72
|
-
if (err) {
|
|
73
|
-
this.logger?.error?.(`SSH forward error [${this.name}]`, err);
|
|
74
|
-
return sock.end();
|
|
75
|
-
}
|
|
76
|
-
sock.pipe(stream).pipe(sock);
|
|
77
|
-
}
|
|
78
|
-
);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// 监听本地端口(0 = 随机端口)
|
|
82
|
-
const port = this.sshConfig.localPort || 0;
|
|
83
|
-
this.server.listen(port, 'localhost', () => {
|
|
84
|
-
this.localPort = this.server.address().port;
|
|
85
|
-
this.isConnected = true;
|
|
86
|
-
|
|
87
|
-
this.logger?.info?.(`✅ SSH tunnel ready [${this.name}]`, {
|
|
88
|
-
localPort: this.localPort,
|
|
89
|
-
remote: `${this.remoteHost}:${this.remotePort}`
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
resolve(this.localPort);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
this.server.on('error', (err) => {
|
|
96
|
-
this.logger?.error?.(`Local server error [${this.name}]`, err);
|
|
97
|
-
reject(err);
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
this.sshClient.on('error', (err) => {
|
|
102
|
-
this.logger?.error?.(`SSH connection error [${this.name}]`, err);
|
|
103
|
-
reject(err);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
this.sshClient.on('end', () => {
|
|
107
|
-
this.logger?.info?.(`SSH connection ended [${this.name}]`);
|
|
108
|
-
this.isConnected = false;
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
// 构建认证配置并连接
|
|
112
|
-
try {
|
|
113
|
-
const authConfig = this._buildAuthConfig();
|
|
114
|
-
this.sshClient.connect(authConfig);
|
|
115
|
-
} catch (err) {
|
|
116
|
-
reject(err);
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* 关闭SSH隧道
|
|
123
|
-
*/
|
|
124
|
-
async close() {
|
|
125
|
-
if (this.server) {
|
|
126
|
-
this.server.close();
|
|
127
|
-
this.server = null;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (this.sshClient) {
|
|
131
|
-
this.sshClient.end();
|
|
132
|
-
this.sshClient = null;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
this.isConnected = false;
|
|
136
|
-
this.localPort = null;
|
|
137
|
-
|
|
138
|
-
this.logger?.info?.(`✅ SSH tunnel closed [${this.name}]`);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* 获取隧道连接URI
|
|
143
|
-
* @param {string} protocol - 数据库协议(mongodb/postgresql/mysql/redis)
|
|
144
|
-
* @param {string} originalUri - 原始URI
|
|
145
|
-
* @returns {string} 替换后的本地URI
|
|
146
|
-
*/
|
|
147
|
-
getTunnelUri(protocol, originalUri) {
|
|
148
|
-
if (!this.isConnected) {
|
|
149
|
-
throw new Error(`SSH tunnel [${this.name}] not connected`);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// 替换主机:端口为 localhost:本地端口
|
|
153
|
-
const pattern = new RegExp(`${protocol}://([^@]*@)?([^:/]+):(\\d+)`);
|
|
154
|
-
const replacement = `${protocol}://$1localhost:${this.localPort}`;
|
|
155
|
-
|
|
156
|
-
return originalUri.replace(pattern, replacement);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* 获取本地连接地址
|
|
161
|
-
* @returns {string} localhost:端口
|
|
162
|
-
*/
|
|
163
|
-
getLocalAddress() {
|
|
164
|
-
if (!this.isConnected) {
|
|
165
|
-
throw new Error(`SSH tunnel [${this.name}] not connected`);
|
|
166
|
-
}
|
|
167
|
-
return `localhost:${this.localPort}`;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* 构建SSH认证配置
|
|
172
|
-
* @private
|
|
173
|
-
*/
|
|
174
|
-
_buildAuthConfig() {
|
|
175
|
-
const { host, port, username, password, privateKey, privateKeyPath, passphrase } = this.sshConfig;
|
|
176
|
-
|
|
177
|
-
if (!host || !username) {
|
|
178
|
-
throw new Error('SSH config requires: host, username');
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const config = {
|
|
182
|
-
host,
|
|
183
|
-
port: port || 22,
|
|
184
|
-
username,
|
|
185
|
-
readyTimeout: this.sshConfig.readyTimeout || 20000,
|
|
186
|
-
keepaliveInterval: this.sshConfig.keepaliveInterval || 30000,
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
// 优先使用私钥认证
|
|
190
|
-
if (privateKey) {
|
|
191
|
-
config.privateKey = privateKey;
|
|
192
|
-
if (passphrase) config.passphrase = passphrase;
|
|
193
|
-
} else if (privateKeyPath) {
|
|
194
|
-
const resolvedPath = privateKeyPath.startsWith('~')
|
|
195
|
-
? path.join(os.homedir(), privateKeyPath.slice(1))
|
|
196
|
-
: privateKeyPath;
|
|
197
|
-
config.privateKey = fs.readFileSync(resolvedPath);
|
|
198
|
-
if (passphrase) config.passphrase = passphrase;
|
|
199
|
-
} else if (password) {
|
|
200
|
-
// 密码认证
|
|
201
|
-
config.password = password;
|
|
202
|
-
} else {
|
|
203
|
-
throw new Error('SSH authentication required: privateKey, privateKeyPath, or password');
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return config;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
module.exports = { SSHTunnelSSH2 };
|
|
211
|
-
|
|
212
|
-
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SSH隧道管理器 - 统一入口
|
|
3
|
-
* 使用ssh2库实现,支持密码认证和私钥认证
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const { SSHTunnelSSH2 } = require('./ssh-tunnel-ssh2');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* SSH隧道管理器
|
|
10
|
-
* 直接使用ssh2实现
|
|
11
|
-
*/
|
|
12
|
-
class SSHTunnelManager {
|
|
13
|
-
/**
|
|
14
|
-
* 创建SSH隧道实例
|
|
15
|
-
* @param {Object} sshConfig - SSH配置
|
|
16
|
-
* @param {string} remoteHost - 远程主机
|
|
17
|
-
* @param {number} remotePort - 远程端口
|
|
18
|
-
* @param {Object} options - 可选配置
|
|
19
|
-
* @returns {Object} SSH隧道实例
|
|
20
|
-
*/
|
|
21
|
-
static create(sshConfig, remoteHost, remotePort, options = {}) {
|
|
22
|
-
return new SSHTunnelSSH2(sshConfig, remoteHost, remotePort, options);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 获取当前实现信息
|
|
27
|
-
* @returns {Object}
|
|
28
|
-
*/
|
|
29
|
-
static getInfo() {
|
|
30
|
-
return {
|
|
31
|
-
implementation: 'ssh2',
|
|
32
|
-
supportsPassword: true,
|
|
33
|
-
supportsPrivateKey: true,
|
|
34
|
-
dependencies: ['ssh2']
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
module.exports = { SSHTunnelManager };
|
|
40
|
-
|
|
41
|
-
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 通用URI解析器
|
|
3
|
-
* 支持:mongodb://、postgresql://、mysql://、redis://
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 解析数据库连接URI
|
|
8
|
-
* @param {string} uri - 数据库连接URI
|
|
9
|
-
* @returns {{protocol: string, auth: string|null, host: string, port: number}} 解析结果
|
|
10
|
-
*/
|
|
11
|
-
function parseUri(uri) {
|
|
12
|
-
const patterns = {
|
|
13
|
-
mongodb: /mongodb:\/\/([^@]*@)?([^:/]+):(\d+)/,
|
|
14
|
-
postgresql: /postgresql:\/\/([^@]*@)?([^:/]+):(\d+)/,
|
|
15
|
-
mysql: /mysql:\/\/([^@]*@)?([^:/]+):(\d+)/,
|
|
16
|
-
redis: /redis:\/\/([^@]*@)?([^:/]+):(\d+)/
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
for (const [protocol, pattern] of Object.entries(patterns)) {
|
|
20
|
-
const match = uri.match(pattern);
|
|
21
|
-
if (match) {
|
|
22
|
-
return {
|
|
23
|
-
protocol,
|
|
24
|
-
auth: match[1] ? match[1].slice(0, -1) : null, // 去掉末尾的@
|
|
25
|
-
host: match[2],
|
|
26
|
-
port: parseInt(match[3], 10)
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
throw new Error(`Unsupported URI format: ${uri}`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
module.exports = { parseUri };
|
|
35
|
-
|
|
36
|
-
|
package/lib/lock/Lock.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 业务锁对象
|
|
3
|
-
* 表示一个已获取的锁,提供释放和续期方法
|
|
4
|
-
*/
|
|
5
|
-
class Lock {
|
|
6
|
-
/**
|
|
7
|
-
* @param {string} key - 锁的标识
|
|
8
|
-
* @param {string} lockId - 锁的唯一ID
|
|
9
|
-
* @param {Object} manager - 锁管理器实例
|
|
10
|
-
* @param {number} ttl - 锁的过期时间(毫秒)
|
|
11
|
-
*/
|
|
12
|
-
constructor(key, lockId, manager, ttl) {
|
|
13
|
-
this.key = key;
|
|
14
|
-
this.lockId = lockId;
|
|
15
|
-
this.manager = manager;
|
|
16
|
-
this.ttl = ttl;
|
|
17
|
-
this.released = false;
|
|
18
|
-
this.acquiredAt = Date.now();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 释放锁
|
|
23
|
-
* @returns {Promise<boolean>}
|
|
24
|
-
*/
|
|
25
|
-
async release() {
|
|
26
|
-
if (this.released) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const result = await this.manager.releaseLock(this.key, this.lockId);
|
|
31
|
-
this.released = true;
|
|
32
|
-
return result;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 续期(延长锁的过期时间)
|
|
37
|
-
* @param {number} [ttl] - 新的过期时间,默认使用原TTL
|
|
38
|
-
* @returns {Promise<boolean>}
|
|
39
|
-
*/
|
|
40
|
-
async renew(ttl) {
|
|
41
|
-
if (this.released) {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return this.manager.renewLock(this.key, this.lockId, ttl || this.ttl);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 检查锁是否仍被持有
|
|
50
|
-
* @returns {boolean}
|
|
51
|
-
*/
|
|
52
|
-
isHeld() {
|
|
53
|
-
return !this.released;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 获取锁持有时间
|
|
58
|
-
* @returns {number} 毫秒
|
|
59
|
-
*/
|
|
60
|
-
getHoldTime() {
|
|
61
|
-
return Date.now() - this.acquiredAt;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
module.exports = Lock;
|
|
66
|
-
|
|
67
|
-
|
package/lib/lock/errors.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 锁获取失败错误
|
|
3
|
-
*/
|
|
4
|
-
class LockAcquireError extends Error {
|
|
5
|
-
constructor(message) {
|
|
6
|
-
super(message);
|
|
7
|
-
this.name = 'LockAcquireError';
|
|
8
|
-
this.code = 'LOCK_ACQUIRE_FAILED';
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 锁超时错误
|
|
14
|
-
*/
|
|
15
|
-
class LockTimeoutError extends Error {
|
|
16
|
-
constructor(message) {
|
|
17
|
-
super(message);
|
|
18
|
-
this.name = 'LockTimeoutError';
|
|
19
|
-
this.code = 'LOCK_TIMEOUT';
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
module.exports = {
|
|
24
|
-
LockAcquireError,
|
|
25
|
-
LockTimeoutError
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
|