Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert: mercury plugin changes of cc websocket #3971

Open
wants to merge 1 commit into
base: wxcc
Choose a base branch
from
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
21 changes: 7 additions & 14 deletions packages/@webex/internal-plugin-mercury/src/mercury.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ const Mercury = WebexPlugin.extend({
token: token.toString(),
trackingId: `${this.webex.sessionId}_${Date.now()}`,
logger: this.logger,
authorizationRequired: this.config.authorizationRequired ?? true,
acknowledgementRequired: this.config.acknowledgementRequired ?? true,
};

// if the consumer has supplied request options use them
Expand Down Expand Up @@ -401,11 +399,8 @@ const Mercury = WebexPlugin.extend({
},

_getEventHandlers(eventType) {
const handlers = [];
if (!eventType) {
return handlers;
}
const [namespace, name] = eventType.split('.');
const handlers = [];

if (!this.webex[namespace] && !this.webex.internal[namespace]) {
return handlers;
Expand Down Expand Up @@ -516,15 +511,13 @@ const Mercury = WebexPlugin.extend({
)
.then(() => {
this._emit('event', event.data);
if (data.eventType) {
const [namespace] = data.eventType.split('.');
const [namespace] = data.eventType.split('.');

if (namespace === data.eventType) {
this._emit(`event:${namespace}`, envelope);
} else {
this._emit(`event:${namespace}`, envelope);
this._emit(`event:${data.eventType}`, envelope);
}
if (namespace === data.eventType) {
this._emit(`event:${namespace}`, envelope);
} else {
this._emit(`event:${namespace}`, envelope);
this._emit(`event:${data.eventType}`, envelope);
}
})
.catch((reason) => {
Expand Down
44 changes: 11 additions & 33 deletions packages/@webex/internal-plugin-mercury/src/socket/socket-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ export default class Socket extends EventEmitter {
* @param {string} options.token (required)
* @param {string} options.trackingId (required)
* @param {Logger} options.logger (required)
* @param {boolean} options.authorizationRequired
* @param {boolean} options.acknowledgementRequired
* @param {string} options.logLevelToken
* @returns {Promise}
*/
Expand Down Expand Up @@ -212,18 +210,7 @@ export default class Socket extends EventEmitter {
options
);

// Destructure and set default values for authorizationRequired and acknowledgementRequired
const {
authorizationRequired = true,
acknowledgementRequired = true,
...remainingOptions
} = options;

this.authorizationRequired = authorizationRequired;
this.acknowledgementRequired = acknowledgementRequired;

// Assign the remaining options to the instance
Object.keys(remainingOptions).forEach((key) => {
Object.keys(options).forEach((key) => {
Reflect.defineProperty(this, key, {
enumerable: false,
value: options[key],
Expand Down Expand Up @@ -263,18 +250,13 @@ export default class Socket extends EventEmitter {

socket.onopen = () => {
this.logger.info(`socket,${this._domain}: connected`);
// Added the "authorizationRequired" condition to bypass the "_authorize" in case of the contact center context, in which case it is configured as false.
if (this.authorizationRequired) {
this._authorize()
.then(() => {
this.logger.info(`socket,${this._domain}: authorized`);
socket.onclose = this.onclose;
resolve();
})
.catch(reject);
} else {
this._ping();
}
this._authorize()
.then(() => {
this.logger.info(`socket,${this._domain}: authorized`);
socket.onclose = this.onclose;
resolve();
})
.catch(reject);
};

socket.onerror = (event) => {
Expand Down Expand Up @@ -328,13 +310,9 @@ export default class Socket extends EventEmitter {
// modified and we don't actually care about anything but the data property
const processedEvent = {data};

// Added the "acknowledgementRequired" condition to bypass the "_acknowledge" in case of the contact center context, in which case it is configured as false.
if (this.acknowledgementRequired) {
this._acknowledge(processedEvent);
}
if (data.type === 'pong' || data.type === 'ping') {
// added above ping condition to handle pong messages of contact center where type is 'ping' instead of 'pong'
this.emit('pong', {...processedEvent, type: 'pong'});
this._acknowledge(processedEvent);
if (data.type === 'pong') {
this.emit('pong', processedEvent);
} else {
this.emit('message', processedEvent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,21 +713,6 @@ describe('plugin-mercury', () => {
return res;
});
});

it('_onmessage without eventType', () => {
sinon.spy(mercury, '_getEventHandlers');
sinon.spy(mercury, '_emit');
const event = {data: {data: {eventType: undefined, mydata: 'some data'}}};
mercury.logger.error.restore();
sinon.stub(mercury.logger, 'error');
return Promise.resolve(mercury._onmessage(event)).then(() => {
assert.calledWith(mercury._getEventHandlers, undefined);
assert.calledWith(mercury._emit, 'event', event.data);
assert.notCalled(mercury.logger.error);
mercury._emit.restore();
mercury._getEventHandlers.restore();
});
});
});

describe('#_applyOverrides()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ describe('plugin-mercury', () => {
));

it('accepts a logLevelToken option', () => {
const acknowledgeSpy = sinon.spy(socket, '_acknowledge');
const promise = socket.open('ws://example.com', {
forceCloseDelay: mockoptions.forceCloseDelay,
pingInterval: mockoptions.pingInterval,
Expand All @@ -148,7 +147,6 @@ describe('plugin-mercury', () => {
token: 'mocktoken',
trackingId: 'mocktrackingid',
logLevelToken: 'mocklogleveltoken',
acknowledgementRequired: true,
});

mockWebSocket.readyState = 1;
Expand All @@ -164,83 +162,9 @@ describe('plugin-mercury', () => {
});

return promise.then(() => {
assert.called(acknowledgeSpy);
assert.equal(socket.logLevelToken, 'mocklogleveltoken');
});
});

it('accepts acknowledgementRequired option as false and skip acknowledge', () => {
const acknowledgeSpy = sinon.spy(socket, '_acknowledge');
const promise = socket.open('ws://example.com', {
forceCloseDelay: mockoptions.forceCloseDelay,
pingInterval: mockoptions.pingInterval,
pongTimeout: mockoptions.pongTimeout,
logger: console,
token: 'mocktoken',
trackingId: 'mocktrackingid',
logLevelToken: 'mocklogleveltoken',
acknowledgementRequired: false,
});

mockWebSocket.readyState = 1;
mockWebSocket.emit('open');

mockWebSocket.emit('message', {
data: JSON.stringify({
id: uuid.v4(),
data: {
eventType: 'mercury.buffer_state',
},
}),
});

return promise.then(() => {
assert.notCalled(acknowledgeSpy);
assert.equal(socket.logLevelToken, 'mocklogleveltoken');
});
});

it('accepts authorizationRequired option as false and skip authorize', () => {
const s = new Socket();
const authorizeSpy = sinon.spy(socket, '_authorize');
socket.open('ws://example.com', {
forceCloseDelay: mockoptions.forceCloseDelay,
pingInterval: mockoptions.pingInterval,
pongTimeout: mockoptions.pongTimeout,
logger: console,
token: 'mocktoken',
trackingId: 'mocktrackingid',
logLevelToken: 'mocklogleveltoken',
authorizationRequired: false,
});

mockWebSocket.readyState = 1;
mockWebSocket.emit('open');

assert.notCalled(authorizeSpy);
assert.called(socket._ping);
});

it('accepts authorizationRequired option as true and calles authorize', () => {
const s = new Socket();
const authorizeSpy = sinon.spy(socket, '_authorize');
socket.open('ws://example.com', {
forceCloseDelay: mockoptions.forceCloseDelay,
pingInterval: mockoptions.pingInterval,
pongTimeout: mockoptions.pongTimeout,
logger: console,
token: 'mocktoken',
trackingId: 'mocktrackingid',
logLevelToken: 'mocklogleveltoken',
authorizationRequired: true,
});

mockWebSocket.readyState = 1;
mockWebSocket.emit('open');

assert.called(authorizeSpy);
assert.called(socket._ping);
});
});

describe('#binaryType', () => {
Expand Down
Loading