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

Merge branch 'sergey12313-master'

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