Skip to content

Commit

Permalink
build - 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian-Webster committed Aug 12, 2024
1 parent 203f21b commit 16ccba6
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 106 deletions.
31 changes: 18 additions & 13 deletions dist/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,24 @@ const AbortSignal_1 = __importDefault(require("./libraries/AbortSignal"));
const Version_1 = __importDefault(require("./libraries/Version"));
const versions_json_1 = __importDefault(require("./versions.json"));
const Downloader_1 = require("./libraries/Downloader");
const defaultOptions = {
dbName: 'dbdata',
logLevel: 'ERROR',
portRetries: 10,
downloadBinaryOnce: true,
lockRetries: 1000,
lockRetryWait: 1000,
username: 'root'
};
const crypto_1 = require("crypto");
const path_1 = require("path");
process.on('exit', () => {
AbortSignal_1.default.abort('Process is exiting');
});
async function createDB(opts = defaultOptions) {
async function createDB(opts) {
const defaultOptions = {
dbName: 'dbdata',
logLevel: 'ERROR',
portRetries: 10,
downloadBinaryOnce: true,
lockRetries: 1000,
lockRetryWait: 1000,
username: 'root',
deleteDBAfterStopped: true,
//mysqlmsn = MySQL Memory Server Node.js
dbPath: (0, path_1.normalize)(`${os.tmpdir()}/mysqlmsn/dbs/${(0, crypto_1.randomUUID)().replace(/-/g, '')}`)
};
const options = { ...defaultOptions, ...opts };
const logger = new Logger_1.default(options.logLevel);
const executor = new Executor_1.default(logger);
Expand All @@ -63,16 +68,16 @@ async function createDB(opts = defaultOptions) {
catch (e) {
logger.error(e);
if (options.version) {
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!`;
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!`;
}
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!`;
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!`;
}
try {
binaryFilepath = await (0, Downloader_1.downloadBinary)(binaryInfo, options, logger);
}
catch (error) {
logger.error('Failed to download binary');
throw error;
throw `Failed to download binary. The error was: "${error}"`;
}
logger.log('Running downloaded binary');
return await executor.startMySQL(options, binaryFilepath);
Expand Down
4 changes: 2 additions & 2 deletions dist/src/libraries/Downloader.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Logger from './Logger';
import { BinaryInfo, ServerOptions } from '../../types';
import { BinaryInfo, InternalServerOptions } from '../../types';
export declare function downloadVersions(): Promise<string>;
export declare function downloadBinary(binaryInfo: BinaryInfo, options: ServerOptions, logger: Logger): Promise<string>;
export declare function downloadBinary(binaryInfo: BinaryInfo, options: InternalServerOptions, logger: Logger): Promise<string>;
19 changes: 14 additions & 5 deletions dist/src/libraries/Downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,31 @@ function downloadVersions() {
function downloadFromCDN(url, downloadLocation, logger) {
return new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream(downloadLocation);
let error;
fileStream.on('open', () => {
const request = https.get(url, (response) => {
response.pipe(fileStream);
});
request.on('error', (err) => {
error = err;
logger.error(err);
fileStream.close();
fs.unlink(downloadLocation, (err) => {
reject(err);
fs.unlink(downloadLocation, () => {
reject(err.message);
});
});
});
fileStream.on('finish', () => {
resolve();
if (!error) {
resolve();
}
});
fileStream.on('error', (err) => {
error = err;
logger.error(err);
fileStream.end();
fs.unlink(downloadLocation, () => {
reject(err);
reject(err.message);
});
});
});
Expand All @@ -106,7 +111,11 @@ function extractBinary(url, archiveLocation, extractedLocation) {
const lastDashIndex = url.lastIndexOf('-');
const fileExtension = url.slice(lastDashIndex).split('.').splice(1).join('.');
await fsPromises.mkdir(extractedLocation, { recursive: true });
const folderName = url.split('/').at(-1).replace(`.${fileExtension}`, '');
const mySQLFolderName = url.split('/').at(-1);
if (!mySQLFolderName) {
return reject(`Folder name is undefined for url: ${url}`);
}
const folderName = mySQLFolderName.replace(`.${fileExtension}`, '');
if (fileExtension === 'zip') {
//Only Windows MySQL files use the .zip extension
const zip = new adm_zip_1.default(archiveLocation);
Expand Down
1 change: 1 addition & 0 deletions dist/src/libraries/Executor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare class Executor {
#private;
logger: Logger;
constructor(logger: Logger);
deleteDatabaseDirectory(path: string): Promise<void>;
getMySQLVersion(preferredVersion?: string): Promise<InstalledMySQLVersion | null>;
startMySQL(options: InternalServerOptions, binaryFilepath: string): Promise<MySQLDB>;
}
Expand Down
Loading

0 comments on commit 16ccba6

Please sign in to comment.