UNPKG

@serverless-plugin-sqs-local/elasticmq-localhost

Version:
165 lines (158 loc) 5.13 kB
import * as fs from 'fs'; import fetch from 'node-fetch'; import * as path from 'path'; import ProgressBar from 'progress'; import mergeWith from 'lodash.mergewith'; import { spawn } from 'child_process'; const defaultConfig = { setup: { downloadUrl: 'http://s3-eu-west-1.amazonaws.com/softwaremill-public/elasticmq-server-0.14.6.jar', installPath: './.elasticmq', jar: 'elasticmq-server.jar', }, start: { port: 9324, }, }; const absPath = (relPath) => { return path.isAbsolute(relPath) ? relPath : path.resolve(relPath); }; const buildConfig = (options = {}) => { return mergeWith({}, defaultConfig, options, (a, b) => b === null || b === undefined ? a : undefined); }; const download = async (downloadUrl, filePath) => { console.log(`Started downloading ElasticMQ from ${downloadUrl} to ${filePath} .`); console.log('Process may take few minutes.'); try { const res = await fetch(downloadUrl); const len = parseInt(res.headers.get('content-length') || '0', 10); const bar = new ProgressBar('Downloading ElasticMQ [:bar] :percent :etas', { complete: '=', incomplete: ' ', width: 40, total: len, }); if (res.status !== 200) { throw new Error(`Error getting ElasticMQ jar: ${res.status}`); } return await new Promise((resolve, reject) => { const fileStream = fs.createWriteStream(filePath); res.body .on('data', chunk => { bar.tick(chunk.length); }) .pipe(fileStream) .on('error', err => { reject(err); }) .on('finish', () => { console.log('\nElasticMQ installation complete!'); resolve({ status: 'success', filePath, }); }); }); } catch (error) { throw new Error(`Error in downloading ElasticMQ ${error}`); } }; var install = async (options = {}) => { const config = buildConfig(options); const { downloadUrl, installPath, jar } = config.setup; const absInstallPath = absPath(installPath); try { const filePath = path.join(absInstallPath, jar); if (fs.existsSync(filePath)) { console.log(`ElasticMQ is already installed on ${filePath} !`); return { status: 'already-installed', filePath, }; } else { if (!fs.existsSync(absInstallPath)) { fs.mkdirSync(absInstallPath); } return await download(downloadUrl, filePath); } } catch (err) { throw new Error('Error configuring or installing ElasticMQ local ' + err); } }; const customConf = (port) => `node-address { protocol = http host = localhost port = ${port} context-path = "" } rest-sqs { enabled = true bind-port = ${port} bind-hostname = "0.0.0.0" } `; var start = (options = {}) => { const config = buildConfig(options); const { setup, start } = config; const { installPath, jar } = setup; const { port } = start; const absInstallPath = absPath(installPath); const preArgs = []; if (port !== defaultConfig.start.port) { const conf = `${port}.conf`; fs.writeFileSync(path.join(absInstallPath, conf), customConf(port)); preArgs.push(`-Dconfig.file=${conf}`); } let args = ['-jar', jar]; args = preArgs.concat(args); const child = spawn('java', args, { cwd: absInstallPath, env: process.env, stdio: ['pipe', 'pipe', process.stderr], }); if (!child.pid) { throw new Error('Unable to start ElasticMQ process!'); } child .on('error', err => { throw err; }) .on('close', code => { if (code !== null && code !== 0) { throw new Error(`ElasticMQ failed to start with code: ${code}`); } }); return { proc: child, port, }; }; class ElasticMQ { constructor() { this.instances = {}; this.install = async (options = {}) => { const config = buildConfig(options); return await install(config); }; this.start = (options = {}) => { const config = buildConfig(options); const instance = start(config); this.instances[instance.port] = instance; console.log(`ElasticMQ Started, http://localhost:${instance.port}`); return instance.port; }; this.stop = (port) => { if (this.instances[port]) { this.instances[port].proc.kill('SIGKILL'); delete this.instances[port]; } }; // noop } } const elasticmq = new ElasticMQ(); export { elasticmq as default }; //# sourceMappingURL=index.mjs.map