Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/esl/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export enum ConnectionEvent
Ready = 'esl::ready',
}

export type ExecuteArg = {
'execute-app-arg'?: string,
[key: string]: string | undefined };

/**
* ESLconnection
*
Expand Down Expand Up @@ -513,11 +517,11 @@ export class Connection extends EventEmitter2
* or "-ERR [Error Message]" on failure.
*/
execute(app: string, cb?: IEventCallback): string;
execute(app: string, arg: string, cb?: IEventCallback): string;
execute(app: string, arg: string, uuid: string, cb?: IEventCallback): string;
execute(app: string, argOrCallback?: string | IEventCallback, uuidOrCallback?: string | IEventCallback, cb?: IEventCallback): string
execute(app: string, arg: ExecuteArg, cb?: IEventCallback): string;
execute(app: string, arg: ExecuteArg, uuid: string, cb?: IEventCallback): string;
execute(app: string, argOrCallback?: ExecuteArg | IEventCallback, uuidOrCallback?: string | IEventCallback, cb?: IEventCallback): string
{
let arg = '';
let arg: ExecuteArg = {};
let uniqueId = uuid.v4();

if (typeof argOrCallback === 'function')
Expand All @@ -539,12 +543,15 @@ export class Connection extends EventEmitter2
}
}

const { 'execute-app-arg': executeAppArg, ...headers }: ExecuteArg = arg;

const options: IDictionary<string> = {
'execute-app-name': app,
...headers
};

if (typeof arg !== 'undefined' && arg.toString().length > 0)
options['execute-app-arg'] = arg.toString();
if (typeof executeAppArg !== 'undefined' && executeAppArg.toString().length > 0)
options['execute-app-arg'] = executeAppArg.toString();

if (this.type === ConnectionType.Inbound)
{
Expand All @@ -568,14 +575,14 @@ export class Connection extends EventEmitter2
* "async: true" header in the message sent to the channel.
*/
executeAsync(app: string, cb?: IEventCallback): string;
executeAsync(app: string, arg: string, cb?: IEventCallback): string;
executeAsync(app: string, arg: string, uuid: string, cb?: IEventCallback): string;
executeAsync(app: string, argOrCallback?: string | IEventCallback, uuidOrCallback?: string | IEventCallback, cb?: IEventCallback): string
executeAsync(app: string, arg: ExecuteArg, cb?: IEventCallback): string;
executeAsync(app: string, arg: ExecuteArg, uuid: string, cb?: IEventCallback): string;
executeAsync(app: string, argOrCallback?: ExecuteArg | IEventCallback, uuidOrCallback?: string | IEventCallback, cb?: IEventCallback): string
{
const oldAsyncValue = this.execAsync;
this.execAsync = true;

const eventUuid = this.execute(app, argOrCallback as string, uuidOrCallback as string, cb);
const eventUuid = this.execute(app, argOrCallback as ExecuteArg, uuidOrCallback as string, cb);

this.execAsync = oldAsyncValue;

Expand Down Expand Up @@ -792,7 +799,7 @@ export class Connection extends EventEmitter2

// this method of event tracking is based on:
// http://lists.freeswitch.org/pipermail/freeswitch-users/2013-May/095329.html
const eventUuid = uuid.v4();
const eventUuid = args['Event-UUID'] ?? uuid.v4();
args['Event-UUID'] = eventUuid;

const eventName = `esl::event::CHANNEL_EXECUTE_COMPLETE::${uniqueId}`;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* ESL Object
* @see https://freeswitch.org/confluence/display/FREESWITCH/Event+Socket+Library#EventSocketLibrary-ESLObject
*/
export { Connection } from './esl/Connection';
export { Connection, ExecuteArg, IEventCallback } from './esl/Connection';
export { Event } from './esl/Event';
export { Parser } from './esl/Parser';
export { Server } from './esl/Server';
Expand Down
22 changes: 15 additions & 7 deletions test/spec/esl/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as sinon from 'sinon';
import { expect } from 'chai';
import { getEchoServerAndSocket, getInboundConnection, ITestSendArgs } from '../../fixtures/helpers';
import { setupSinonChai } from '../../fixtures/setup';
import { Connection } from '../../../src/esl/Connection';
import { Connection, ExecuteArg } from '../../../src/esl/Connection';
import { ICallback } from '../../../src/utils';
import { Event } from '../../../src/esl/Event';
import { cmdReply } from '../../fixtures/data';
Expand Down Expand Up @@ -129,7 +129,9 @@ describe('esl.Connection', function ()

it('Invokes the callback', function (done)
{
testChannelExecute(testConnection, 'playback', 'foo', id0, function (evt)
testChannelExecute(testConnection, 'playback', {
'execute-app-arg': 'foo'
}, id0, function (evt)
{
expect(evt.getHeader('Application')).to.equal('playback');
done();
Expand All @@ -138,7 +140,9 @@ describe('esl.Connection', function ()

it('Invokes the callback only once for the same session', function (done)
{
testChannelExecute(testConnection, 'hangup', '', id0, function (evt)
testChannelExecute(testConnection, 'hangup', {
'execute-app-arg': ''
}, id0, function (evt)
{
expect(evt.getHeader('Application')).to.equal('hangup');
done();
Expand All @@ -147,7 +151,9 @@ describe('esl.Connection', function ()

it('Invokes the callback again for a new session', function (done)
{
testChannelExecute(testConnection, 'hangup', '', id1, function (evt)
testChannelExecute(testConnection, 'hangup', {
'execute-app-arg': '',
}, id1, function (evt)
{
expect(evt.getHeader('Application')).to.equal('hangup');
done();
Expand Down Expand Up @@ -456,7 +462,7 @@ function sendChannelExecuteResponse(conn: Connection, appUuid: string, appName:
conn.socket.write(resp);
}

function testChannelExecute(conn: Connection, appName: string, appArg: string, requestId: string, cb: ICallback<Event>)
function testChannelExecute(conn: Connection, appName: string, appArg: ExecuteArg, requestId: string, cb: ICallback<Event>)
{
conn.socket.once('data', function (data)
{
Expand All @@ -468,9 +474,11 @@ function testChannelExecute(conn: Connection, appName: string, appArg: string, r
expect(lines).to.contain(`execute-app-name: ${appName}`);
expect(lines.some(x => x.includes('Event-UUID: '))).to.be.true;

if (appArg)
const arg: string | undefined = appArg['execute-app-arg'];

if (arg)
{
expect(lines).to.contain(`execute-app-arg: ${appArg}`);
expect(lines).to.contain(`execute-app-arg: ${arg}`);
}
else
{
Expand Down