@zintrust/db-mysql 1.8.7 → 1.8.8
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/dist/index.js +75 -28
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4,6 +4,66 @@ const getInjectedMysqlModule = () => {
|
|
|
4
4
|
const globalAny = globalThis;
|
|
5
5
|
return globalAny.__zintrustMysqlModule;
|
|
6
6
|
};
|
|
7
|
+
const getNodeMysqlSslConfig = (tlsEnabled) => {
|
|
8
|
+
if (!tlsEnabled) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
rejectUnauthorized: false,
|
|
13
|
+
minVersion: 'TLSv1.2',
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
const getSocketTimeoutMs = (config) => {
|
|
17
|
+
if (typeof config.socketTimeoutMs === 'number' && config.socketTimeoutMs > 0) {
|
|
18
|
+
return config.socketTimeoutMs;
|
|
19
|
+
}
|
|
20
|
+
return 30000;
|
|
21
|
+
};
|
|
22
|
+
const createWorkersPool = async ({ mysql, host, port, database, user, password, tlsEnabled, timeoutMs, }) => {
|
|
23
|
+
if (!Cloudflare.isCloudflareSocketsEnabled()) {
|
|
24
|
+
throw ErrorFactory.createConfigError('Cloudflare sockets are disabled. Set ENABLE_CLOUDFLARE_SOCKETS=true to use MySQL sockets on Workers.');
|
|
25
|
+
}
|
|
26
|
+
const createSocket = await loadCloudflareSocketFactory();
|
|
27
|
+
return mysql.createPool({
|
|
28
|
+
host,
|
|
29
|
+
port,
|
|
30
|
+
database,
|
|
31
|
+
user,
|
|
32
|
+
password,
|
|
33
|
+
waitForConnections: true,
|
|
34
|
+
connectionLimit: 10,
|
|
35
|
+
namedPlaceholders: false,
|
|
36
|
+
disableEval: true,
|
|
37
|
+
stream: () => createSocket({ host, port, tls: tlsEnabled, timeoutMs }),
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
const createNodePool = (mysql, host, port, database, user, password, nodeMysqlSslConfig) => {
|
|
41
|
+
return mysql.createPool({
|
|
42
|
+
host,
|
|
43
|
+
port,
|
|
44
|
+
database,
|
|
45
|
+
user,
|
|
46
|
+
password,
|
|
47
|
+
ssl: nodeMysqlSslConfig,
|
|
48
|
+
waitForConnections: true,
|
|
49
|
+
connectionLimit: 10,
|
|
50
|
+
namedPlaceholders: false,
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
const describeDriverError = (error) => {
|
|
54
|
+
if (error === null || typeof error !== 'object') {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const details = error;
|
|
58
|
+
const parts = [
|
|
59
|
+
typeof details.code === 'string' ? `code=${details.code}` : undefined,
|
|
60
|
+
typeof details.errno === 'number' ? `errno=${details.errno}` : undefined,
|
|
61
|
+
typeof details.sqlState === 'string' ? `sqlState=${details.sqlState}` : undefined,
|
|
62
|
+
typeof details.sqlMessage === 'string' ? `sqlMessage=${details.sqlMessage}` : undefined,
|
|
63
|
+
typeof details.fatal === 'boolean' ? `fatal=${details.fatal}` : undefined,
|
|
64
|
+
].filter((part) => part !== undefined);
|
|
65
|
+
return parts.length > 0 ? parts.join(', ') : undefined;
|
|
66
|
+
};
|
|
7
67
|
function isMissingEsmPackage(error, packageName) {
|
|
8
68
|
if (error === null || typeof error !== 'object')
|
|
9
69
|
return false;
|
|
@@ -97,43 +157,23 @@ async function connect(state, config) {
|
|
|
97
157
|
const { host, port, database, user, password } = getConnectionParams(config);
|
|
98
158
|
const isWorkersRuntime = Cloudflare.getWorkersEnv() !== null;
|
|
99
159
|
const tlsEnabled = Boolean(config.ssl);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
timeoutMs = 30000; // default 30s
|
|
106
|
-
}
|
|
160
|
+
const nodeMysqlSslConfig = getNodeMysqlSslConfig(tlsEnabled);
|
|
161
|
+
Logger.info(`[db-mysql] Effective connection params: host=${host}, port=${port}, database=${database}, user=${user}, password_len=${password.length}, ssl=${tlsEnabled}`);
|
|
162
|
+
const timeoutMs = getSocketTimeoutMs(config);
|
|
107
163
|
if (isWorkersRuntime) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
const createSocket = await loadCloudflareSocketFactory();
|
|
112
|
-
state.pool = mysql.createPool({
|
|
164
|
+
state.pool = await createWorkersPool({
|
|
165
|
+
mysql,
|
|
113
166
|
host,
|
|
114
167
|
port,
|
|
115
168
|
database,
|
|
116
169
|
user,
|
|
117
170
|
password,
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
namedPlaceholders: false,
|
|
121
|
-
disableEval: true,
|
|
122
|
-
stream: () => createSocket({ host, port, tls: tlsEnabled, timeoutMs }),
|
|
171
|
+
tlsEnabled,
|
|
172
|
+
timeoutMs,
|
|
123
173
|
});
|
|
124
174
|
}
|
|
125
175
|
else {
|
|
126
|
-
state.pool = mysql
|
|
127
|
-
host,
|
|
128
|
-
port,
|
|
129
|
-
database,
|
|
130
|
-
user,
|
|
131
|
-
password,
|
|
132
|
-
ssl: tlsEnabled ? {} : undefined,
|
|
133
|
-
waitForConnections: true,
|
|
134
|
-
connectionLimit: 10,
|
|
135
|
-
namedPlaceholders: false,
|
|
136
|
-
});
|
|
176
|
+
state.pool = createNodePool(mysql, host, port, database, user, password, nodeMysqlSslConfig);
|
|
137
177
|
}
|
|
138
178
|
// Probe.
|
|
139
179
|
await state.pool.execute('SELECT 1');
|
|
@@ -144,6 +184,13 @@ async function connect(state, config) {
|
|
|
144
184
|
if (isMissingEsmPackage(error, 'mysql2')) {
|
|
145
185
|
throw ErrorFactory.createConfigError("MySQL adapter requires the 'mysql2' package (run `npm install mysql2` or `zin add db:mysql`).");
|
|
146
186
|
}
|
|
187
|
+
const driverError = describeDriverError(error);
|
|
188
|
+
if (driverError !== undefined) {
|
|
189
|
+
Logger.error(`[db-mysql] MySQL driver error: ${driverError}`);
|
|
190
|
+
}
|
|
191
|
+
if (error instanceof Error && typeof error.stack === 'string' && error.stack.trim() !== '') {
|
|
192
|
+
Logger.error(`[db-mysql] MySQL driver stack: ${error.stack}`);
|
|
193
|
+
}
|
|
147
194
|
throw ErrorFactory.createTryCatchError('Failed to connect to MySQL', error);
|
|
148
195
|
}
|
|
149
196
|
}
|