mysql-memory-server 1.1.0 → 1.2.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/README.md +20 -0
- package/dist/src/index.js +18 -13
- package/dist/src/libraries/Downloader.d.ts +2 -2
- package/dist/src/libraries/Downloader.js +14 -5
- package/dist/src/libraries/Executor.d.ts +1 -0
- package/dist/src/libraries/Executor.js +147 -81
- package/dist/src/libraries/Logger.js +3 -3
- package/dist/src/libraries/Version.js +6 -1
- package/dist/types/index.d.ts +4 -0
- package/package.json +13 -4
package/README.md
CHANGED
|
@@ -132,6 +132,26 @@ Default: root
|
|
|
132
132
|
|
|
133
133
|
Description: The username of the user that is used to login to the database.
|
|
134
134
|
|
|
135
|
+
- `deleteDBAfterStopped: boolean`
|
|
136
|
+
|
|
137
|
+
Required: No
|
|
138
|
+
|
|
139
|
+
Default: true
|
|
140
|
+
|
|
141
|
+
Gotchas: This option is intended to be for internal debugging purposes only and not meant for people to use. As such, this option will not follow Semantic Versioning.
|
|
142
|
+
|
|
143
|
+
Description: Changes whether or not the database will be deleted after it has been stopped. If set to `true`, the database WILL be deleted after it has been stopped.
|
|
144
|
+
|
|
145
|
+
- `dataPath: string`
|
|
146
|
+
|
|
147
|
+
Required: No
|
|
148
|
+
|
|
149
|
+
Default: `TMPDIR/mysqlmsn/dbs/UUID` (replacing TMPDIR with the OS temp directory and UUID with a UUIDv4 without seperating dashes)
|
|
150
|
+
|
|
151
|
+
Gotchas: This option is intended to be for internal debugging purposes only and not meant for people to use. As such, this option will not follow Semantic Versioning.
|
|
152
|
+
|
|
153
|
+
Description: The folder to store database-related data in
|
|
154
|
+
|
|
135
155
|
## If using Ubuntu 24.04 and newer
|
|
136
156
|
|
|
137
157
|
Selecting what MySQL version to use is not currently supported on Ubuntu 24.04 and newer. To use this package on Ubuntu 24.04 and newer you must have the `mysql-server` package installed on your system and `ServerOptions.version` must either be the version that is installed on the system or undefined.
|
package/dist/src/index.js
CHANGED
|
@@ -35,19 +35,24 @@ const AbortSignal_1 = __importDefault(require("./libraries/AbortSignal"));
|
|
|
35
35
|
const Version_1 = __importDefault(require("./libraries/Version"));
|
|
36
36
|
const versions_json_1 = __importDefault(require("./versions.json"));
|
|
37
37
|
const Downloader_1 = require("./libraries/Downloader");
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
logLevel: 'ERROR',
|
|
41
|
-
portRetries: 10,
|
|
42
|
-
downloadBinaryOnce: true,
|
|
43
|
-
lockRetries: 1000,
|
|
44
|
-
lockRetryWait: 1000,
|
|
45
|
-
username: 'root'
|
|
46
|
-
};
|
|
38
|
+
const crypto_1 = require("crypto");
|
|
39
|
+
const path_1 = require("path");
|
|
47
40
|
process.on('exit', () => {
|
|
48
41
|
AbortSignal_1.default.abort('Process is exiting');
|
|
49
42
|
});
|
|
50
|
-
async function createDB(opts
|
|
43
|
+
async function createDB(opts) {
|
|
44
|
+
const defaultOptions = {
|
|
45
|
+
dbName: 'dbdata',
|
|
46
|
+
logLevel: 'ERROR',
|
|
47
|
+
portRetries: 10,
|
|
48
|
+
downloadBinaryOnce: true,
|
|
49
|
+
lockRetries: 1000,
|
|
50
|
+
lockRetryWait: 1000,
|
|
51
|
+
username: 'root',
|
|
52
|
+
deleteDBAfterStopped: true,
|
|
53
|
+
//mysqlmsn = MySQL Memory Server Node.js
|
|
54
|
+
dbPath: (0, path_1.normalize)(`${os.tmpdir()}/mysqlmsn/dbs/${(0, crypto_1.randomUUID)().replace(/-/g, '')}`)
|
|
55
|
+
};
|
|
51
56
|
const options = { ...defaultOptions, ...opts };
|
|
52
57
|
const logger = new Logger_1.default(options.logLevel);
|
|
53
58
|
const executor = new Executor_1.default(logger);
|
|
@@ -63,16 +68,16 @@ async function createDB(opts = defaultOptions) {
|
|
|
63
68
|
catch (e) {
|
|
64
69
|
logger.error(e);
|
|
65
70
|
if (options.version) {
|
|
66
|
-
throw `A MySQL version ${options.version} binary could not be found that supports your OS (${os.platform()} | ${os.version()}) and CPU architecture (${os.arch()}). Please check you have the latest version of mysql-memory-server. If the latest version still doesn't support the version you want to use, feel free to make a pull request to add support!`;
|
|
71
|
+
throw `A MySQL version ${options.version} binary could not be found that supports your OS (${os.platform()} | ${os.version()} | ${os.release()}) and CPU architecture (${os.arch()}). Please check you have the latest version of mysql-memory-server. If the latest version still doesn't support the version you want to use, feel free to make a pull request to add support!`;
|
|
67
72
|
}
|
|
68
|
-
throw `A MySQL binary could not be found that supports your OS (${os.platform()} | ${os.version()}) and CPU architecture (${os.arch()}). Please check you have the latest version of mysql-memory-server. If the latest version still doesn't support your OS and CPU architecture, feel free to make a pull request to add support!`;
|
|
73
|
+
throw `A MySQL binary could not be found that supports your OS (${os.platform()} | ${os.version()} | ${os.release()}) and CPU architecture (${os.arch()}). Please check you have the latest version of mysql-memory-server. If the latest version still doesn't support your OS and CPU architecture, feel free to make a pull request to add support!`;
|
|
69
74
|
}
|
|
70
75
|
try {
|
|
71
76
|
binaryFilepath = await (0, Downloader_1.downloadBinary)(binaryInfo, options, logger);
|
|
72
77
|
}
|
|
73
78
|
catch (error) {
|
|
74
79
|
logger.error('Failed to download binary');
|
|
75
|
-
throw error
|
|
80
|
+
throw `Failed to download binary. The error was: "${error}"`;
|
|
76
81
|
}
|
|
77
82
|
logger.log('Running downloaded binary');
|
|
78
83
|
return await executor.startMySQL(options, binaryFilepath);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import Logger from './Logger';
|
|
2
|
-
import { BinaryInfo,
|
|
2
|
+
import { BinaryInfo, InternalServerOptions } from '../../types';
|
|
3
3
|
export declare function downloadVersions(): Promise<string>;
|
|
4
|
-
export declare function downloadBinary(binaryInfo: BinaryInfo, options:
|
|
4
|
+
export declare function downloadBinary(binaryInfo: BinaryInfo, options: InternalServerOptions, logger: Logger): Promise<string>;
|
|
@@ -77,26 +77,31 @@ function downloadVersions() {
|
|
|
77
77
|
function downloadFromCDN(url, downloadLocation, logger) {
|
|
78
78
|
return new Promise((resolve, reject) => {
|
|
79
79
|
const fileStream = fs.createWriteStream(downloadLocation);
|
|
80
|
+
let error;
|
|
80
81
|
fileStream.on('open', () => {
|
|
81
82
|
const request = https.get(url, (response) => {
|
|
82
83
|
response.pipe(fileStream);
|
|
83
84
|
});
|
|
84
85
|
request.on('error', (err) => {
|
|
86
|
+
error = err;
|
|
85
87
|
logger.error(err);
|
|
86
88
|
fileStream.close();
|
|
87
|
-
fs.unlink(downloadLocation, (
|
|
88
|
-
reject(err);
|
|
89
|
+
fs.unlink(downloadLocation, () => {
|
|
90
|
+
reject(err.message);
|
|
89
91
|
});
|
|
90
92
|
});
|
|
91
93
|
});
|
|
92
94
|
fileStream.on('finish', () => {
|
|
93
|
-
|
|
95
|
+
if (!error) {
|
|
96
|
+
resolve();
|
|
97
|
+
}
|
|
94
98
|
});
|
|
95
99
|
fileStream.on('error', (err) => {
|
|
100
|
+
error = err;
|
|
96
101
|
logger.error(err);
|
|
97
102
|
fileStream.end();
|
|
98
103
|
fs.unlink(downloadLocation, () => {
|
|
99
|
-
reject(err);
|
|
104
|
+
reject(err.message);
|
|
100
105
|
});
|
|
101
106
|
});
|
|
102
107
|
});
|
|
@@ -106,7 +111,11 @@ function extractBinary(url, archiveLocation, extractedLocation) {
|
|
|
106
111
|
const lastDashIndex = url.lastIndexOf('-');
|
|
107
112
|
const fileExtension = url.slice(lastDashIndex).split('.').splice(1).join('.');
|
|
108
113
|
await fsPromises.mkdir(extractedLocation, { recursive: true });
|
|
109
|
-
const
|
|
114
|
+
const mySQLFolderName = url.split('/').at(-1);
|
|
115
|
+
if (!mySQLFolderName) {
|
|
116
|
+
return reject(`Folder name is undefined for url: ${url}`);
|
|
117
|
+
}
|
|
118
|
+
const folderName = mySQLFolderName.replace(`.${fileExtension}`, '');
|
|
110
119
|
if (fileExtension === 'zip') {
|
|
111
120
|
//Only Windows MySQL files use the .zip extension
|
|
112
121
|
const zip = new adm_zip_1.default(archiveLocation);
|
|
@@ -4,6 +4,7 @@ declare class Executor {
|
|
|
4
4
|
#private;
|
|
5
5
|
logger: Logger;
|
|
6
6
|
constructor(logger: Logger);
|
|
7
|
+
deleteDatabaseDirectory(path: string): Promise<void>;
|
|
7
8
|
getMySQLVersion(preferredVersion?: string): Promise<InstalledMySQLVersion | null>;
|
|
8
9
|
startMySQL(options: InternalServerOptions, binaryFilepath: string): Promise<MySQLDB>;
|
|
9
10
|
}
|
|
@@ -30,7 +30,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
30
30
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
31
31
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
32
32
|
};
|
|
33
|
-
var _Executor_instances, _Executor_execute, _Executor_startMySQLProcess;
|
|
33
|
+
var _Executor_instances, _Executor_execute, _Executor_killProcess, _Executor_startMySQLProcess, _Executor_setupDataDirectories;
|
|
34
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
35
|
const child_process_1 = require("child_process");
|
|
36
36
|
const semver_1 = require("semver");
|
|
@@ -40,12 +40,34 @@ const fs = __importStar(require("fs"));
|
|
|
40
40
|
const Port_1 = require("./Port");
|
|
41
41
|
const AbortSignal_1 = __importDefault(require("./AbortSignal"));
|
|
42
42
|
const path_1 = require("path");
|
|
43
|
-
const crypto_1 = require("crypto");
|
|
44
43
|
class Executor {
|
|
45
44
|
constructor(logger) {
|
|
46
45
|
_Executor_instances.add(this);
|
|
47
46
|
this.logger = logger;
|
|
48
47
|
}
|
|
48
|
+
async deleteDatabaseDirectory(path) {
|
|
49
|
+
let retries = 0;
|
|
50
|
+
//Maximum wait of 10 seconds | 500ms * 20 retries = 10,000ms = 10 seconds
|
|
51
|
+
const waitTime = 500;
|
|
52
|
+
const maxRetries = 20;
|
|
53
|
+
//Since the database processes are killed instantly (SIGKILL) sometimes the database file handles may still be open
|
|
54
|
+
//This would cause an EBUSY error. Retrying the deletions for 10 seconds should give the OS enough time to close
|
|
55
|
+
//the file handles.
|
|
56
|
+
while (retries <= maxRetries) {
|
|
57
|
+
try {
|
|
58
|
+
await fsPromises.rm(path, { recursive: true, force: true });
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
if (retries === maxRetries) {
|
|
63
|
+
throw e;
|
|
64
|
+
}
|
|
65
|
+
await new Promise(resolve => setTimeout(resolve, waitTime));
|
|
66
|
+
retries++;
|
|
67
|
+
this.logger.log('DB data directory deletion failed. Now on retry', retries);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
49
71
|
getMySQLVersion(preferredVersion) {
|
|
50
72
|
return new Promise(async (resolve, reject) => {
|
|
51
73
|
if (process.platform === 'win32') {
|
|
@@ -55,6 +77,7 @@ class Executor {
|
|
|
55
77
|
if (servers.length === 0) {
|
|
56
78
|
return resolve(null);
|
|
57
79
|
}
|
|
80
|
+
this.logger.log(servers);
|
|
58
81
|
const versions = [];
|
|
59
82
|
for (const dir of servers) {
|
|
60
83
|
const path = `${process.env.PROGRAMFILES}\\MySQL\\${dir}\\bin\\mysqld`;
|
|
@@ -104,51 +127,35 @@ class Executor {
|
|
|
104
127
|
}
|
|
105
128
|
});
|
|
106
129
|
}
|
|
107
|
-
startMySQL(options, binaryFilepath) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (process.platform === 'linux' && err.message.includes('libaio.so')) {
|
|
121
|
-
this.logger.error(err || stderr);
|
|
122
|
-
return reject('The mysqld command failed to run. MySQL needs the libaio package installed on Linux systems to run. Do you have this installed? Learn more at https://dev.mysql.com/doc/refman/en/binary-installation.html');
|
|
123
|
-
}
|
|
124
|
-
return reject(err || stderr);
|
|
125
|
-
}
|
|
126
|
-
let initText = `CREATE DATABASE ${options.dbName};`;
|
|
127
|
-
if (options.username !== 'root') {
|
|
128
|
-
initText += `RENAME USER 'root'@'localhost' TO '${options.username}'@'localhost';`;
|
|
130
|
+
async startMySQL(options, binaryFilepath) {
|
|
131
|
+
let retries = 0;
|
|
132
|
+
const datadir = (0, path_1.normalize)(`${options.dbPath}/data`);
|
|
133
|
+
do {
|
|
134
|
+
await __classPrivateFieldGet(this, _Executor_instances, "m", _Executor_setupDataDirectories).call(this, options, binaryFilepath, datadir);
|
|
135
|
+
const port = (0, Port_1.GenerateRandomPort)();
|
|
136
|
+
const mySQLXPort = (0, Port_1.GenerateRandomPort)();
|
|
137
|
+
this.logger.log('Using port:', port, 'and MySQLX port:', mySQLXPort, 'on retry:', retries);
|
|
138
|
+
try {
|
|
139
|
+
this.logger.log('Starting MySQL process');
|
|
140
|
+
const resolved = await __classPrivateFieldGet(this, _Executor_instances, "m", _Executor_startMySQLProcess).call(this, options, port, mySQLXPort, datadir, options.dbPath, binaryFilepath);
|
|
141
|
+
this.logger.log('Starting process was successful');
|
|
142
|
+
return resolved;
|
|
129
143
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
this.logger.log('Using port:', port, 'on retry:', retries);
|
|
136
|
-
try {
|
|
137
|
-
const resolved = await __classPrivateFieldGet(this, _Executor_instances, "m", _Executor_startMySQLProcess).call(this, options, port, mySQLXPort, datadir, dbPath, binaryFilepath);
|
|
138
|
-
return resolve(resolved);
|
|
144
|
+
catch (e) {
|
|
145
|
+
this.logger.warn('Caught error:', e, `\nRetries: ${retries} | options.portRetries: ${options.portRetries}`);
|
|
146
|
+
if (e !== 'Port is already in use') {
|
|
147
|
+
this.logger.error('Error:', e);
|
|
148
|
+
throw e;
|
|
139
149
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
retries++;
|
|
145
|
-
if (retries < options.portRetries) {
|
|
146
|
-
this.logger.warn(`One or both of these ports are already in use: ${port} or ${mySQLXPort}. Now retrying... ${retries}/${options.portRetries} possible retries.`);
|
|
147
|
-
}
|
|
150
|
+
retries++;
|
|
151
|
+
if (retries <= options.portRetries) {
|
|
152
|
+
this.logger.warn(`One or both of these ports are already in use: ${port} or ${mySQLXPort}. Now retrying... ${retries}/${options.portRetries} possible retries.`);
|
|
148
153
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
154
|
+
else {
|
|
155
|
+
throw `The port has been retried ${options.portRetries} times and a free port could not be found.\nEither try again, or if this is a common issue, increase options.portRetries.`;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
} while (retries <= options.portRetries);
|
|
152
159
|
}
|
|
153
160
|
}
|
|
154
161
|
_Executor_instances = new WeakSet(), _Executor_execute = function _Executor_execute(command) {
|
|
@@ -157,43 +164,84 @@ _Executor_instances = new WeakSet(), _Executor_execute = function _Executor_exec
|
|
|
157
164
|
resolve({ error, stdout, stderr });
|
|
158
165
|
});
|
|
159
166
|
});
|
|
167
|
+
}, _Executor_killProcess = async function _Executor_killProcess(process) {
|
|
168
|
+
let killed = false;
|
|
169
|
+
if (os.platform() === 'win32') {
|
|
170
|
+
const { error, stderr } = await __classPrivateFieldGet(this, _Executor_instances, "m", _Executor_execute).call(this, `taskkill /pid ${process.pid} /t /f`);
|
|
171
|
+
if (!error && !stderr) {
|
|
172
|
+
killed = true;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
this.logger.error(error || stderr);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
killed = process.kill();
|
|
180
|
+
}
|
|
181
|
+
return killed;
|
|
160
182
|
}, _Executor_startMySQLProcess = function _Executor_startMySQLProcess(options, port, mySQLXPort, datadir, dbPath, binaryFilepath) {
|
|
161
183
|
const errors = [];
|
|
162
184
|
const logFile = `${dbPath}/log.log`;
|
|
185
|
+
const errorLogFile = `${datadir}/errorlog.err`;
|
|
163
186
|
return new Promise(async (resolve, reject) => {
|
|
164
187
|
await fsPromises.rm(logFile, { force: true });
|
|
165
|
-
const process = (0, child_process_1.spawn)(binaryFilepath, ['--no-defaults', `--port=${port}`, `--datadir=${datadir}`, `--mysqlx-port=${mySQLXPort}`, `--mysqlx-socket=${dbPath}/x.sock`, `--socket=${dbPath}/m.sock`, `--general-log-file=${logFile}`, '--general-log=1', `--init-file=${dbPath}/init.sql`, '--bind-address=127.0.0.1', '--innodb-doublewrite=OFF'], { signal: AbortSignal_1.default.signal, killSignal: 'SIGKILL' });
|
|
188
|
+
const process = (0, child_process_1.spawn)(binaryFilepath, ['--no-defaults', `--port=${port}`, `--datadir=${datadir}`, `--mysqlx-port=${mySQLXPort}`, `--mysqlx-socket=${dbPath}/x.sock`, `--socket=${dbPath}/m.sock`, `--general-log-file=${logFile}`, '--general-log=1', `--init-file=${dbPath}/init.sql`, '--bind-address=127.0.0.1', '--innodb-doublewrite=OFF', '--mysqlx=FORCE', `--log-error=${errorLogFile}`], { signal: AbortSignal_1.default.signal, killSignal: 'SIGKILL' });
|
|
166
189
|
//resolveFunction is the function that will be called to resolve the promise that stops the database.
|
|
167
190
|
//If resolveFunction is not undefined, the database has received a kill signal and data cleanup procedures should run.
|
|
168
191
|
//Once ran, resolveFunction will be called.
|
|
169
192
|
let resolveFunction;
|
|
170
193
|
process.on('close', async (code, signal) => {
|
|
171
|
-
|
|
172
|
-
|
|
194
|
+
let errorLog;
|
|
195
|
+
try {
|
|
196
|
+
errorLog = await fsPromises.readFile(errorLogFile, { encoding: 'utf-8' });
|
|
197
|
+
}
|
|
198
|
+
catch (e) {
|
|
199
|
+
errorLog = `ERROR WHILE READING LOG: ${e}`;
|
|
200
|
+
}
|
|
201
|
+
const portIssue = errorLog.includes("Do you already have another mysqld server running");
|
|
202
|
+
const xPortIssue = errorLog.includes("X Plugin can't bind to it");
|
|
203
|
+
this.logger.log('Exiting because of a port issue:', portIssue, '. MySQL X Plugin failed to bind:', xPortIssue);
|
|
204
|
+
if (portIssue || xPortIssue) {
|
|
205
|
+
this.logger.log('Error log when exiting for port in use error:', errorLog);
|
|
206
|
+
try {
|
|
207
|
+
await this.deleteDatabaseDirectory(options.dbPath);
|
|
208
|
+
}
|
|
209
|
+
catch (e) {
|
|
210
|
+
this.logger.error(e);
|
|
211
|
+
return reject(`MySQL failed to listen on a certain port. To restart MySQL with a different port, the database directory needed to be deleted. An error occurred while deleting the database directory. Aborting. The error was: ${e}`);
|
|
212
|
+
}
|
|
173
213
|
return reject('Port is already in use');
|
|
174
214
|
}
|
|
175
215
|
try {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const splitPath = binaryFilepath.split(os.platform() === 'win32' ? '\\' : '/');
|
|
179
|
-
const binariesIndex = splitPath.indexOf('binaries');
|
|
180
|
-
//The path will be the directory path for the binary download
|
|
181
|
-
splitPath.splice(binariesIndex + 2);
|
|
182
|
-
//Delete the binary folder
|
|
183
|
-
await fsPromises.rm(splitPath.join('/'), { force: true, recursive: true });
|
|
216
|
+
if (options.deleteDBAfterStopped) {
|
|
217
|
+
await this.deleteDatabaseDirectory(dbPath);
|
|
184
218
|
}
|
|
185
219
|
}
|
|
186
220
|
finally {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
221
|
+
try {
|
|
222
|
+
if (binaryFilepath.includes(os.tmpdir()) && !options.downloadBinaryOnce) {
|
|
223
|
+
const splitPath = binaryFilepath.split(os.platform() === 'win32' ? '\\' : '/');
|
|
224
|
+
const binariesIndex = splitPath.indexOf('binaries');
|
|
225
|
+
//The path will be the directory path for the binary download
|
|
226
|
+
splitPath.splice(binariesIndex + 2);
|
|
227
|
+
//Delete the binary folder
|
|
228
|
+
await fsPromises.rm(splitPath.join('/'), { force: true, recursive: true });
|
|
229
|
+
}
|
|
193
230
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
231
|
+
finally {
|
|
232
|
+
if (resolveFunction) {
|
|
233
|
+
resolveFunction();
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const errorString = errors.join('\n');
|
|
237
|
+
if (code === 0) {
|
|
238
|
+
return reject(`Database exited early.\nError log: ${errorLog}\nError string: "${errorString}`);
|
|
239
|
+
}
|
|
240
|
+
if (code) {
|
|
241
|
+
const errorMessage = `The database exited early with code ${code}. The error log was:\n${errorLog}\nThe error string was: "${errorString}".`;
|
|
242
|
+
this.logger.error(errorMessage);
|
|
243
|
+
return reject(errorMessage);
|
|
244
|
+
}
|
|
197
245
|
}
|
|
198
246
|
}
|
|
199
247
|
});
|
|
@@ -207,12 +255,22 @@ _Executor_instances = new WeakSet(), _Executor_execute = function _Executor_exec
|
|
|
207
255
|
}
|
|
208
256
|
}
|
|
209
257
|
});
|
|
210
|
-
fs.watchFile(
|
|
258
|
+
fs.watchFile(errorLogFile, async (curr) => {
|
|
211
259
|
if (curr.dev !== 0) {
|
|
212
260
|
//File exists
|
|
213
|
-
const file = await fsPromises.readFile(
|
|
214
|
-
if (file.includes('
|
|
215
|
-
|
|
261
|
+
const file = await fsPromises.readFile(errorLogFile, { encoding: 'utf8' });
|
|
262
|
+
if (file.includes("X Plugin can't bind to it")) {
|
|
263
|
+
//As stated in the MySQL X Plugin documentation at https://dev.mysql.com/doc/refman/8.4/en/x-plugin-options-system-variables.html#sysvar_mysqlx_bind_address
|
|
264
|
+
//when the MySQL X Plugin fails to bind to an address, it does not prevent the MySQL server startup because MySQL X is not a mandatory plugin.
|
|
265
|
+
//It doesn't seem like there is a way to prevent server startup when that happens. The workaround to that is to shutdown the MySQL server ourselves when the X plugin
|
|
266
|
+
//cannot bind to an address. If there is a way to prevent server startup when binding fails, this workaround can be removed.
|
|
267
|
+
const killed = await __classPrivateFieldGet(this, _Executor_instances, "m", _Executor_killProcess).call(this, process);
|
|
268
|
+
if (!killed) {
|
|
269
|
+
reject('Failed to kill MySQL process to retry listening on a free port.');
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
else if (file.includes('ready for connections. Version:')) {
|
|
273
|
+
fs.unwatchFile(errorLogFile);
|
|
216
274
|
resolve({
|
|
217
275
|
port,
|
|
218
276
|
xPort: mySQLXPort,
|
|
@@ -221,19 +279,7 @@ _Executor_instances = new WeakSet(), _Executor_execute = function _Executor_exec
|
|
|
221
279
|
stop: () => {
|
|
222
280
|
return new Promise(async (resolve, reject) => {
|
|
223
281
|
resolveFunction = resolve;
|
|
224
|
-
|
|
225
|
-
if (os.platform() === 'win32') {
|
|
226
|
-
const { error, stderr } = await __classPrivateFieldGet(this, _Executor_instances, "m", _Executor_execute).call(this, `taskkill /pid ${process.pid} /t /f`);
|
|
227
|
-
if (!error && !stderr) {
|
|
228
|
-
killed = true;
|
|
229
|
-
}
|
|
230
|
-
else {
|
|
231
|
-
this.logger.error(error || stderr);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
else {
|
|
235
|
-
killed = process.kill();
|
|
236
|
-
}
|
|
282
|
+
const killed = await __classPrivateFieldGet(this, _Executor_instances, "m", _Executor_killProcess).call(this, process);
|
|
237
283
|
if (!killed) {
|
|
238
284
|
reject();
|
|
239
285
|
}
|
|
@@ -244,5 +290,25 @@ _Executor_instances = new WeakSet(), _Executor_execute = function _Executor_exec
|
|
|
244
290
|
}
|
|
245
291
|
});
|
|
246
292
|
});
|
|
293
|
+
}, _Executor_setupDataDirectories = async function _Executor_setupDataDirectories(options, binaryFilepath, datadir) {
|
|
294
|
+
this.logger.log('Created data directory for database at:', datadir);
|
|
295
|
+
await fsPromises.mkdir(datadir, { recursive: true });
|
|
296
|
+
const { error: err, stderr } = await __classPrivateFieldGet(this, _Executor_instances, "m", _Executor_execute).call(this, `"${binaryFilepath}" --no-defaults --datadir=${datadir} --initialize-insecure`);
|
|
297
|
+
if (err || (stderr && !stderr.includes('InnoDB initialization has ended'))) {
|
|
298
|
+
if (process.platform === 'win32' && ((err === null || err === void 0 ? void 0 : err.message.includes('Command failed')) || stderr.includes('Command failed'))) {
|
|
299
|
+
this.logger.error(err || stderr);
|
|
300
|
+
throw 'The mysqld command failed to run. A possible cause is that the Microsoft Visual C++ Redistributable Package is not installed. MySQL 5.7.40 and newer requires Microsoft Visual C++ Redistributable Package 2019 to be installed. Check the MySQL docs for Microsoft Visual C++ requirements for other MySQL versions. If you are sure you have this installed, check the error message in the console for more details.';
|
|
301
|
+
}
|
|
302
|
+
if (process.platform === 'linux' && ((err === null || err === void 0 ? void 0 : err.message.includes('libaio.so')) || stderr.includes('libaio.so'))) {
|
|
303
|
+
this.logger.error(err || stderr);
|
|
304
|
+
throw 'The mysqld command failed to run. MySQL needs the libaio package installed on Linux systems to run. Do you have this installed? Learn more at https://dev.mysql.com/doc/refman/en/binary-installation.html';
|
|
305
|
+
}
|
|
306
|
+
throw err || stderr;
|
|
307
|
+
}
|
|
308
|
+
let initText = `CREATE DATABASE ${options.dbName};`;
|
|
309
|
+
if (options.username !== 'root') {
|
|
310
|
+
initText += `\nRENAME USER 'root'@'localhost' TO '${options.username}'@'localhost';`;
|
|
311
|
+
}
|
|
312
|
+
await fsPromises.writeFile(`${options.dbPath}/init.sql`, initText, { encoding: 'utf8' });
|
|
247
313
|
};
|
|
248
314
|
exports.default = Executor;
|
|
@@ -11,17 +11,17 @@ class Logger {
|
|
|
11
11
|
}
|
|
12
12
|
log(...args) {
|
|
13
13
|
if (this.LOG_LEVEL === 0) {
|
|
14
|
-
console.log
|
|
14
|
+
console.log(...args);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
warn(...args) {
|
|
18
18
|
if (this.LOG_LEVEL <= 1) {
|
|
19
|
-
console.warn
|
|
19
|
+
console.warn(...args);
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
error(...args) {
|
|
23
23
|
if (this.LOG_LEVEL <= 2) {
|
|
24
|
-
console.error
|
|
24
|
+
console.error(...args);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -34,7 +34,12 @@ function getBinaryURL(versions, versionToGet = "9.x") {
|
|
|
34
34
|
availableVersions = availableVersions.filter(v => v.os === process.platform);
|
|
35
35
|
if (availableVersions.length === 0)
|
|
36
36
|
throw `No MySQL binary could be found for your OS: ${process.platform}`;
|
|
37
|
-
availableVersions = availableVersions.filter(v =>
|
|
37
|
+
availableVersions = availableVersions.filter(v => {
|
|
38
|
+
const release = (0, semver_1.coerce)(os.release());
|
|
39
|
+
if (!release)
|
|
40
|
+
return false;
|
|
41
|
+
return (0, semver_1.satisfies)(release.version, v.osKernelVersionsSupported);
|
|
42
|
+
});
|
|
38
43
|
if (availableVersions.length === 0)
|
|
39
44
|
throw `No MySQL binary could be found that supports your OS version: ${os.release()} | ${os.version()}`;
|
|
40
45
|
const wantedVersions = availableVersions.filter(v => (0, semver_1.satisfies)(v.version, versionToGet));
|
package/dist/types/index.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ export type ServerOptions = {
|
|
|
9
9
|
lockRetries?: number;
|
|
10
10
|
lockRetryWait?: number;
|
|
11
11
|
username?: string;
|
|
12
|
+
deleteDBAfterStopped?: boolean;
|
|
13
|
+
dbPath?: string;
|
|
12
14
|
};
|
|
13
15
|
export type InternalServerOptions = {
|
|
14
16
|
version?: string;
|
|
@@ -19,6 +21,8 @@ export type InternalServerOptions = {
|
|
|
19
21
|
lockRetries: number;
|
|
20
22
|
lockRetryWait: number;
|
|
21
23
|
username: string;
|
|
24
|
+
deleteDBAfterStopped: boolean;
|
|
25
|
+
dbPath: string;
|
|
22
26
|
};
|
|
23
27
|
export type ExecutorOptions = {
|
|
24
28
|
logLevel: LOG_LEVEL;
|
package/package.json
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mysql-memory-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Spin up an ephemeral MySQL database from your JavaScript code",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"types": "dist/src/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
8
|
"/dist"
|
|
9
9
|
],
|
|
10
|
-
"keywords": [
|
|
10
|
+
"keywords": [
|
|
11
|
+
"MySQL",
|
|
12
|
+
"ephemeral database",
|
|
13
|
+
"continuous integration",
|
|
14
|
+
"CI",
|
|
15
|
+
"CI/CD",
|
|
16
|
+
"memory database"
|
|
17
|
+
],
|
|
11
18
|
"scripts": {
|
|
12
|
-
"test": "jest"
|
|
19
|
+
"test": "jest --testPathIgnorePatterns=/stress-tests/",
|
|
20
|
+
"test:ci": "jest --testPathIgnorePatterns=/stress-tests/ --setupFilesAfterEnv ./ciSetup.js",
|
|
21
|
+
"stress": "jest --runTestsByPath stress-tests/stress.test.ts --setupFilesAfterEnv ./ciSetup.js"
|
|
13
22
|
},
|
|
14
23
|
"author": "Sebastian-Webster",
|
|
15
24
|
"license": "MIT",
|
|
@@ -27,7 +36,7 @@
|
|
|
27
36
|
"typescript": "^5.5.4"
|
|
28
37
|
},
|
|
29
38
|
"dependencies": {
|
|
30
|
-
"adm-zip": "^0.5.
|
|
39
|
+
"adm-zip": "^0.5.15",
|
|
31
40
|
"proper-lockfile": "^4.1.2",
|
|
32
41
|
"semver": "^7.6.3"
|
|
33
42
|
},
|