Commit 32f97890 authored by Sergey Dmitriev's avatar Sergey Dmitriev

Merge branch 'sergey12313-master'

parents dddca25e 2c0f8cc2
node_modules/ node_modules/
coverage coverage
html-report html-report
package-lock.json
language: node_js language: node_js
node_js: node_js:
- "0.11" - "7"
- "0.10" - "8"
- "4.0" \ No newline at end of file
- "4.1"
- "4.2"
- "5.0"
- "5.1"
- "4"
- "5"
- "iojs"
\ No newline at end of file
var Readable = require('readable-stream'); var Readable = require('readable-stream');
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var state = require('./state'); var state = require('./state');
var Q = require('q');
var commands = require('./command'); var commands = require('./command');
//base context //base context
var Context = function (stream, debug) { var Context = function (stream, loggerOptions = {}) {
EventEmitter.call(this); EventEmitter.call(this);
this.debug = debug; function consoleDecorator(arrow, data){
return console.log(arrow, JSON.stringify (data));
}
this.log = (loggerOptions.logger) ?
loggerOptions.logger :
consoleDecorator;
this.debug = loggerOptions.debug
this.stream = new Readable(); this.stream = new Readable();
this.stream.setEncoding('utf8'); this.stream.setEncoding('utf8');
this.stream.wrap(stream); this.stream.wrap(stream);
...@@ -110,30 +116,33 @@ Context.prototype.send = function (msg, cb) { ...@@ -110,30 +116,33 @@ Context.prototype.send = function (msg, cb) {
Context.prototype.end = function () { Context.prototype.end = function () {
this.stream.end(); this.stream.end();
return Q.resolve(); return Promise.resolve();
}; };
Context.prototype.sendCommand = function (command) { Context.prototype.sendCommand = function (command) {
var defer = new Q.defer(); if (this.debug) this.log('------->', { command: command });
if (this.debug) console.log('command', command);
var self = this; var self = this;
this.send(command + '\n', function (err, result) { return new Promise(function (resolve, reject) {
if (self.debug) console.log('err:', err, 'result:', result); self.send(command + '\n', function (err, result) {
if (self.debug) self.log('<-------', { err: err, result: result });
if (err) { if (err) {
defer.reject(err); reject(err);
} else { } else {
defer.resolve(result); resolve(result);
} }
}); });
return defer.promise; })
}; };
Context.prototype.onEvent = function (event) { Context.prototype.onEvent = function (event) {
var defer = new Q.defer(); var self = this
this.on(event, function (data) { return new Promise(function (resolve) {
defer.resolve(data); self.on(event, function (data) {
}); resolve(data);
return defer.promise; })
})
}; };
//additional agi commands //additional agi commands
......
...@@ -8,11 +8,12 @@ var agi = function (handler, optionsIn) { ...@@ -8,11 +8,12 @@ var agi = function (handler, optionsIn) {
var settings = { var settings = {
port: options.port || 3000, port: options.port || 3000,
debug: options.debug || false, debug: options.debug || false,
logger: options.logger || false,
host: options.host, host: options.host,
}; };
var handle = function (stream) { var handle = function (stream) {
var context = new Context(stream, settings.debug); var context = new Context(stream, { debug: settings.debug, logger: options.logger});
handler(context); handler(context);
}; };
......
...@@ -2,20 +2,19 @@ ...@@ -2,20 +2,19 @@
"author": "Sergey Dmitriev <serge.dmitriev@gmail.com>", "author": "Sergey Dmitriev <serge.dmitriev@gmail.com>",
"name": "ding-dong", "name": "ding-dong",
"description": "Write AGI-server quickly! (AGI - Asterisk Gateway Interface)", "description": "Write AGI-server quickly! (AGI - Asterisk Gateway Interface)",
"version": "0.1.5", "version": "0.1.6",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/antirek/ding-dong.git" "url": "git://github.com/antirek/ding-dong.git"
}, },
"main": "lib/", "main": "lib/",
"scripts": { "scripts": {
"test": "mocha -R tap" "test": "mocha -R tap --exit"
}, },
"engines": { "engines": {
"node": ">=0.8.0" "node": ">=0.8.0"
}, },
"dependencies": { "dependencies": {
"q": "^1.4.1",
"readable-stream": "*" "readable-stream": "*"
}, },
"devDependencies": { "devDependencies": {
......
...@@ -135,17 +135,15 @@ describe('Context', function() { ...@@ -135,17 +135,15 @@ describe('Context', function() {
context.exec('test') context.exec('test')
.then(function (res) { .then(function (res) {
expect(res.result).to.eql('0'); expect(res.result).to.eql('0');
process.nextTick(function() {
context.stream.write('200 result=1\n');
});
return context.exec('test 2'); return context.exec('test 2');
}) })
.then(function (res) { .then(function (res) {
expect(res.result).to.eql('1'); expect(res.result).to.eql('1');
done(); done();
}); });
process.nextTick(function() {
context.stream.write('200 result=1\n');
});
}); });
}); });
}); });
......
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