@serverless-plugin-sqs-local/elasticmq-localhost
Version:
ElasticMQ runner for development and testing
193 lines (182 loc) • 6.21 kB
JavaScript
;
var fs = require('fs');
var fetch = require('node-fetch');
var path = require('path');
var ProgressBar = require('progress');
var mergeWith = require('lodash.mergewith');
var child_process = require('child_process');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
var path__namespace = /*#__PURE__*/_interopNamespace(path);
var ProgressBar__default = /*#__PURE__*/_interopDefaultLegacy(ProgressBar);
var mergeWith__default = /*#__PURE__*/_interopDefaultLegacy(mergeWith);
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__namespace.isAbsolute(relPath) ? relPath : path__namespace.resolve(relPath);
};
const buildConfig = (options = {}) => {
return mergeWith__default["default"]({}, 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__default["default"](downloadUrl);
const len = parseInt(res.headers.get('content-length') || '0', 10);
const bar = new ProgressBar__default["default"]('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__namespace.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__namespace.join(absInstallPath, jar);
if (fs__namespace.existsSync(filePath)) {
console.log(`ElasticMQ is already installed on ${filePath} !`);
return {
status: 'already-installed',
filePath,
};
}
else {
if (!fs__namespace.existsSync(absInstallPath)) {
fs__namespace.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__namespace.writeFileSync(path__namespace.join(absInstallPath, conf), customConf(port));
preArgs.push(`-Dconfig.file=${conf}`);
}
let args = ['-jar', jar];
args = preArgs.concat(args);
const child = child_process.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();
module.exports = elasticmq;
//# sourceMappingURL=index.js.map