Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
validate(attrs) {
const errObj = {};
const addError = (fieldName, error) => {
errObj[fieldName] = errObj[fieldName] || [];
errObj[fieldName].push(error);
};
if (attrs.notifications) {
if (is.not.url(attrs.serverAddress)) {
addError('serverAddress', app.polyglot.t('smtpModelErrors.serverAddress'));
}
if (is.empty(attrs.username)) {
addError('username', app.polyglot.t('smtpModelErrors.username.caseEmpty'));
} else if (/\s/.test(attrs.username)) {
addError('username', app.polyglot.t('smtpModelErrors.username.noWhitespace'));
}
if (is.empty(attrs.password)) {
addError('password', app.polyglot.t('smtpModelErrors.password'));
}
if (is.not.email(attrs.senderEmail)) {
addError('senderEmail', app.polyglot.t('smtpModelErrors.senderEmail'));
}
if (is.not.email(attrs.recipientEmail)) {
addError('recipientEmail', app.polyglot.t('smtpModelErrors.recipientEmail'));
}
} else {
if (is.not.url(attrs.serverAddress) && is.not.empty(attrs.serverAddress)) {
addError('serverAddress', app.polyglot.t('smtpModelErrors.serverAddress'));
_serviceFound(ad) {
if (!('advertisement' in ad) || is.not.object(ad.advertisement)) return;
else if (is.not.number(ad.advertisement.port)) return;
else if (is.not.string(ad.address) || is.not.ip(ad.address)) return;
else if (is.not.array(ad.advertisement.services) || is.empty(ad.advertisement.services)) return;
const address = `${ ad.address }:${ ad.advertisement.port }`;
if (this.options.debug) console.log(`service found: ${ JSON.stringify(ad) }`);
this._instances.push(address);
const socket = axon.socket('req');
socket.connect(ad.advertisement.port, ad.address);
for (let service of ad.advertisement.services) {
if (!(service in this._sockets)) this._sockets[service] = {};
if (!(ad.id in this._sockets[service])) this._sockets[service][ad.id] = socket;
}
}
hasService(service) {
if (is.not.string(service) || is.empty(service)) return false;
return this._services.hasOwnProperty(service);
}
} else {
if (!is.within(attrs.api_socket_port, -1, 65536)) {
this._addError(err, 'api_socket_port', 'Please provide a number between 0 and 65535.');
}
}
if (!is.number(attrs.heartbeat_socket_port)) {
this._addError(err, 'heartbeat_socket_port', 'Please provide a number.');
} else {
if (!is.within(attrs.heartbeat_socket_port, -1, 65536)) {
this._addError(err, 'heartbeat_socket_port', 'Please provide a number between 0 and 65535.');
}
}
if (!this.isLocalServer()) {
if (!is.existy(attrs.username) || is.empty(attrs.username)) {
this._addError(err, 'username', 'Please provide a value.');
}
if (!is.existy(attrs.password) || is.empty(attrs.password)) {
this._addError(err, 'password', 'Please provide a value.');
}
}
if (Object.keys(err).length > 0) {
return err;
}
},
isEmpty() {
return is.empty(this._services);
}
async forward(service, method, data, options) {
if (is.not.object(options) || is.array(options)) options = this.options.job || {};
if (is.not.string(service) || is.empty(service)) throw new Error('invalid service');
else if (is.not.string(method) || is.empty(method)) throw new Error('invalid method');
else if (is.not.object(data) || is.array(data)) throw new Error('invalid data');
else if (is.not.object(options) || is.array(options))
throw new Error('invalid options');
data._ = method;
const job = this.services[service].createJob(data);
for (let config in options)
if (jobOptions.includes(config) && is.function(job[config]))
if (is.array(options[config])) job[config](...options[config]);
else job[config](options[config]);
await job.save();
this.logger.info(`${ service }|${ method }|C|${ JSON.stringify(data) }`);
return job;
}
const required = value => {
return !is.empty(value) && !is.undefined(value) && !is.null(value)
}
start(publisher, subscriber, services) {
if (is.not.array(services) || is.empty(services)) throw new Error('there is no service');
for (let name of services) {
const options = Object.assign({}, this._options.plugin || {});
options.prefix = this._prefix(name);
this._services[name] = { s: new this._ClientPlugin(publisher, subscriber, options), m: {} };
this._services[name].s.start(async m => this._onMessage(m));
}
}
acceptService(service, options) {
if (is.not.object(options) || is.array(options)) options = this.options.bee;
if (is.not.string(service) || is.empty(service))
throw new Error('invalid service');
else if (is.not.object(options)) throw new Error('invalid options');
if (is.not.existy(this.services[service])) {
this.services[service] = new Queue(service, options);
this.logger.info(`${ service }|ready(C)`);
} else this.logger.warn(`${ service }|exists`);
}
return new Promise((resolve, reject) => {
try {
if (is.not.string(service) || is.not.string(method) || method.startsWith('_'))
throw new Error('invalid service or method');
else if (!this._services[service]) throw new Error(`unknown service(${ service })`);
else if (is.empty(this._services[service].s._pair)) throw new Error('no service found');
const msg = { id: shortid.generate(), d: data, s: service, m: method };
const instances = Object.keys(this._services[service].s._push);
if (is.empty(instances)) throw new Error(`no pair found for service(${ service })`);
const address = instances[Math.floor(Math.random() * instances.length)];
this._services[service].s.send(address, msg);
this._services[service].m[ msg.id ] = [ resolve, reject ];
if (is.not.number(timeout)) timeout = this._options.timeout;
if (timeout) this._services[service].m[ msg.id ].push(setTimeout(() => {
clearTimeout(this._services[service].m[ msg.id ][2]);
this._services[service].m[ msg.id ][1](new Error('request timed out'));
delete this._services[service].m[ msg.id ];
}, timeout));
} catch (e) {
reject(e);
}