Commit 6962e9c5 authored by sergey's avatar sergey

используем линтер для красоты кода ))

parent 508c005f
{
"extends": [
"google"
],
"env": {
"node": true
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"new-cap": ["error", { "properties": true }]
}
}
\ No newline at end of file
language: node_js
node_js:
- "7"
- "8"
- "10"
- "11"
- "12"
\ No newline at end of file
module.exports = function(grunt){
grunt.initConfig({
mocha_istanbul: {
coverage: {
src: 'test',
options: {
'ui': 'tdd',
'recursive': true,
'reporter': 'mocha-istanbul'
}
}
}
});
grunt.loadNpmTasks('grunt-mocha-istanbul');
grunt.registerTask('coverage', ['mocha_istanbul:coverage']);
};
\ No newline at end of file
var AGIServer = require('./../lib/index');
const AGIServer = require('./../lib/index');
var handler = function (context) {
const handler = function(context) {
context.onEvent('variables')
.then(function (vars) {
.then(function(vars) {
console.log('vars', vars);
return context.streamFile('beep');
})
.then(function (result) {
return context.setVariable('RECOGNITION_RESULT', 'I\'m your father, Luc');
.then(function(result) {
return context.setVariable(
'RECOGNITION_RESULT', 'I\'m your father, Luc');
})
.then(function (result) {
.then(function(result) {
return context.end();
})
.fail(console.log);
};
var agi = new AGIServer(handler, {debug: true});
const agi = new AGIServer(handler, {debug: true});
agi.start(3007);
This diff is collapsed.
var commands = require('./command');
console.log(commands);
var q = {};
commands.map(function (command) {
q[command.name] = function () {
var args = [].slice.call(arguments, 0, command.params);
return command.command + " " + args.join(" ");
};
});
console.log(q.databaseDel("1", "2", "3", "4"));
console.log(q.databaseDelTree("1", "2", "3", "4"));
console.log(commands.length)
\ No newline at end of file
var Readable = require('readable-stream');
var EventEmitter = require('events').EventEmitter;
var state = require('./state');
var commands = require('./command');
const Readable = require('readable-stream');
const EventEmitter = require('events').EventEmitter;
const state = require('./state');
const commands = require('./command');
//base context
// base context
var Context = function (stream, loggerOptions = {}) {
const Context = function(stream, loggerOptions = {}) {
EventEmitter.call(this);
function consoleDecorator(arrow, data){
return console.log(arrow, JSON.stringify (data));
}
const consoleDecorator = function(arrow, data) {
return console.log(arrow, JSON.stringify(data));
};
this.log = (loggerOptions.logger) ?
loggerOptions.logger :
consoleDecorator;
this.debug = loggerOptions.debug
this.debug = loggerOptions.debug;
this.stream = new Readable();
this.stream.setEncoding('utf8');
this.stream.wrap(stream);
this.state = state.init;
this.msg = "";
this.msg = '';
this.variables = {};
this.pending = null;
var self = this;
this.stream.on('readable', function () {
//always keep the 'leftover' part of the message
const self = this;
this.stream.on('readable', function() {
// always keep the 'leftover' part of the message
self.msg = self.read();
});
......@@ -38,30 +38,32 @@ var Context = function (stream, loggerOptions = {}) {
require('util').inherits(Context, EventEmitter);
Context.prototype.read = function () {
var buffer = this.stream.read();
Context.prototype.read = function() {
const buffer = this.stream.read();
if (!buffer) return this.msg;
this.msg += buffer;
if (this.state === state.init) {
if (this.msg.indexOf('\n\n') < 0) return this.msg; //we don't have whole message
// we don't have whole message
if (this.msg.indexOf('\n\n') < 0) return this.msg;
this.readVariables(this.msg);
} else if (this.state === state.waiting) {
if (this.msg.indexOf('\n') < 0) return this.msg; //we don't have whole message
// we don't have whole message
if (this.msg.indexOf('\n') < 0) return this.msg;
this.readResponse(this.msg);
}
return '';
};
Context.prototype.readVariables = function (msg) {
var lines = msg.split('\n');
Context.prototype.readVariables = function(msg) {
const lines = msg.split('\n');
lines.map(function (line) {
var split = line.split(':');
var name = split[0];
var value = split[1];
lines.map(function(line) {
const split = line.split(':');
const name = split[0];
const value = split[1];
this.variables[name] = (value || '').trim();
}, this);
......@@ -69,26 +71,26 @@ Context.prototype.readVariables = function (msg) {
this.setState(state.waiting);
};
Context.prototype.readResponse = function (msg) {
var lines = msg.split('\n');
Context.prototype.readResponse = function(msg) {
const lines = msg.split('\n');
lines.map(function (line) {
lines.map(function(line) {
this.readResponseLine(line);
}, this);
};
Context.prototype.readResponseLine = function (line) {
Context.prototype.readResponseLine = function(line) {
if (!line) return;
//var parsed = /^(\d{3})(?: result=)(.*)/.exec(line);
var parsed = /^(\d{3})(?: result=)([^(]*)(?:\((.*)\))?/.exec(line);
// var parsed = /^(\d{3})(?: result=)(.*)/.exec(line);
const parsed = /^(\d{3})(?: result=)([^(]*)(?:\((.*)\))?/.exec(line);
if (!parsed) {
return this.emit('hangup');
}
var response = {
const response = {
code: parseInt(parsed[1]),
result: parsed[2].trim(),
};
......@@ -96,63 +98,62 @@ Context.prototype.readResponseLine = function (line) {
response.value = parsed[3];
}
//our last command had a pending callback
// our last command had a pending callback
if (this.pending) {
var pending = this.pending;
const pending = this.pending;
this.pending = null;
pending(null, response);
}
this.emit('response', response);
}
};
Context.prototype.setState = function (state) {
Context.prototype.setState = function(state) {
this.state = state;
};
Context.prototype.send = function (msg, cb) {
Context.prototype.send = function(msg, cb) {
this.pending = cb;
this.stream.write(msg);
};
Context.prototype.end = function () {
Context.prototype.end = function() {
this.stream.end();
return Promise.resolve();
};
Context.prototype.sendCommand = function (command) {
if (this.debug) this.log('------->', { command: command });
var self = this;
return new Promise(function (resolve, reject) {
self.send(command + '\n', function (err, result) {
if (self.debug) self.log('<-------', { err: err, result: result });
Context.prototype.sendCommand = function(command) {
if (this.debug) this.log('------->', {command: command});
const self = this;
return new Promise(function(resolve, reject) {
self.send(command + '\n', function(err, result) {
if (self.debug) self.log('<-------', {err: err, result: result});
if (err) {
reject(err);
} else {
resolve(result);
}
});
})
});
};
Context.prototype.onEvent = function (event) {
var self = this
return new Promise(function (resolve) {
self.on(event, function (data) {
Context.prototype.onEvent = function(event) {
const self = this;
return new Promise(function(resolve) {
self.on(event, function(data) {
resolve(data);
})
})
});
});
};
//additional agi commands
// additional agi commands
commands.forEach(function (command) {
var str = '';
Context.prototype[command.name] = function () {
commands.forEach(function(command) {
let str = '';
Context.prototype[command.name] = function(...args) {
if (command.params > 0) {
var args = [].slice.call(arguments, 0, command.params);
str = command.command + " " + prepareArgs(args, command.paramRules, command.params).join(" ");
// const args = [].slice.call(arguments, 0, command.params);
str = command.command + ' ' +
prepareArgs(args, command.paramRules, command.params).join(' ');
} else {
str = command.command;
}
......@@ -160,22 +161,28 @@ commands.forEach(function (command) {
};
});
var prepareArgs = function (args, argsRules, count) {
const prepareArgs = function(args, argsRules, count) {
if (!argsRules || !count) {
return args;
}
return Array.apply(null, new Array(count)) // old node.js versions don't support Array.fill()
.map(function (arg, i) {
arg = args[i] !== undefined && args[i] !== null ? args[i] : argsRules[i] && argsRules[i].default || '';
var prepare = argsRules[i] && argsRules[i].prepare || function (x) { return x; };
return (new Array(count)).fill(null)
.map(function(arg, i) {
arg = args[i] !== undefined && args[i] !== null ?
args[i] :
argsRules[i] && argsRules[i].default || '';
const prepare = argsRules[i] && argsRules[i].prepare ||
function(x) {
return x;
};
return prepare(String(arg));
});
};
//sugar commands
// sugar commands
Context.prototype.dial = function (target, timeout, params) {
Context.prototype.dial = function(target, timeout, params) {
return this.exec('Dial', target + ',' + timeout + ',' + params);
};
......
var Context = require('./context');
const Context = require('./context');
var agi = function (handler, optionsIn) {
const agi = function(handler, optionsIn) {
const options = optionsIn || {};
var server;
var options = optionsIn || {};
var settings = {
const settings = {
port: options.port || 3000,
debug: options.debug || false,
logger: options.logger || false,
host: options.host,
};
var handle = function (stream) {
var context = new Context(stream, { debug: settings.debug, logger: options.logger});
const handle = function(stream) {
const context = new Context(stream, {
debug: settings.debug,
logger: options.logger,
});
handler(context);
};
var start = function (portIn, hostIn) {
var port = portIn || settings.port;
var host = hostIn || settings.host;
const start = function(portIn, hostIn) {
const port = portIn || settings.port;
const host = hostIn || settings.host;
return require('net').createServer(handle).listen(port, host);
};
return {
start: start
start: start,
};
};
......
//states for context
var state = {
// states for context
const state = {
init: 0,
waiting: 2
waiting: 2,
};
module.exports = state;
......@@ -9,21 +9,17 @@
},
"main": "lib/",
"scripts": {
"test": "mocha -R tap --exit"
},
"engines": {
"node": ">=6"
"test": "mocha -R tap --exit",
"lint-fix": "eslint --fix ."
},
"dependencies": {
"expect.js": "^0.3.1",
"readable-stream": "^2.3.0"
},
"devDependencies": {
"expect.js": "*",
"grunt": "^0.4.5",
"grunt-mocha-istanbul": "^2.2.0",
"istanbul": "^0.3.5",
"eslint": "^5.16.0",
"eslint-config-google": "^0.12.0",
"memorystream": "^0.3.0",
"mocha": "*",
"mocha-istanbul": "^0.2.0"
"mocha": "6.1.4"
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment