@voicenter-team/opensips-js 1.0.16 → 1.0.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,8 @@
1
1
  import { UA } from 'jssip';
2
2
  import { CallOptionsExtended } from '@/types/rtc';
3
3
  import { RTCSession } from 'jssip/lib/RTCSession';
4
+ import { MSRPSession } from '@/lib/msrp/session';
4
5
  export default class UAExtended extends UA {
5
6
  call(target: string, options?: CallOptionsExtended): RTCSession;
7
+ sendMSRPMessage(target: string, options: string): MSRPSession;
6
8
  }
@@ -1,9 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const jssip_1 = require("jssip");
4
+ const session_1 = require("@/lib/msrp/session");
4
5
  class UAExtended extends jssip_1.UA {
5
6
  call(target, options) {
6
7
  return super.call(target, options);
7
8
  }
9
+ sendMSRPMessage(target, options) {
10
+ const session = new session_1.MSRPSession(this);
11
+ session.send(target, options);
12
+ return session;
13
+ }
8
14
  }
9
15
  exports.default = UAExtended;
package/build/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import JsSIP from 'jssip';
2
- import UA from './helpers/UA';
3
- import { TempTimeData, ITimeData } from './helpers/time.helper';
2
+ import UA from '@/helpers/UA';
3
+ import { TempTimeData, ITimeData } from '@/helpers/time.helper';
4
4
  import { WebrtcMetricsConfigType, MediaDeviceType } from '@/types/webrtcmetrics';
5
5
  import { ListenersKeyType, ListenerCallbackFnType } from '@/types/listeners';
6
6
  import { RTCSessionExtended, ICall, IntervalType, ListenerEventType, IRoom, IDoCallParam, ICallStatus, IRoomUpdate, IOpenSIPSJSOptions } from '@/types/rtc';
@@ -12,9 +12,6 @@ export interface InnerState {
12
12
  activeCalls: {
13
13
  [key: string]: ICall;
14
14
  };
15
- extendedCalls: {
16
- [key: string]: ICall;
17
- };
18
15
  activeRooms: {
19
16
  [key: number]: IRoom;
20
17
  };
@@ -41,14 +38,12 @@ export interface InnerState {
41
38
  [key: string]: Array<(call: RTCSessionExtended, event: ListenerEventType | undefined) => void>;
42
39
  };
43
40
  metricConfig: WebrtcMetricsConfigType;
44
- isAutoAnswer: boolean;
45
41
  }
46
42
  declare class OpenSIPSJS extends UA {
47
43
  private initialized;
48
44
  private readonly options;
49
45
  private readonly newRTCSessionEventName;
50
46
  private readonly activeCalls;
51
- private readonly extendedCalls;
52
47
  private _currentActiveRoomId;
53
48
  private _callAddingInProgress;
54
49
  private state;
@@ -72,8 +67,6 @@ declare class OpenSIPSJS extends UA {
72
67
  };
73
68
  get currentActiveRoomId(): number | undefined;
74
69
  private set currentActiveRoomId(value);
75
- get autoAnswer(): boolean;
76
- set autoAnswer(value: boolean);
77
70
  get callAddingInProgress(): string | undefined;
78
71
  private set callAddingInProgress(value);
79
72
  get muteWhenJoin(): boolean;
@@ -120,7 +113,7 @@ declare class OpenSIPSJS extends UA {
120
113
  setMetricsConfig(config: WebrtcMetricsConfigType): void;
121
114
  sendDTMF(callId: string, value: string): void;
122
115
  doMute(value: boolean): void;
123
- sendMessage(target: string | JsSIP.URI, body: string, options?: SendMessageOptions): import("jssip/lib/Message").Message;
116
+ sendMSRP(target: string | JsSIP.URI, body: string, options?: SendMessageOptions): import("jssip/lib/Message").Message;
124
117
  doCallHold({ callId, toHold, automatic }: {
125
118
  callId: string;
126
119
  toHold: boolean;
package/build/index.js CHANGED
@@ -13,19 +13,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const jssip_1 = __importDefault(require("jssip"));
16
- const UA_1 = __importDefault(require("./helpers/UA"));
16
+ const UA_1 = __importDefault(require("@/helpers/UA"));
17
17
  const p_iteration_1 = require("p-iteration");
18
- const time_helper_1 = require("./helpers/time.helper");
19
- const filter_helper_1 = require("./helpers/filter.helper");
20
- const audio_helper_1 = require("./helpers/audio.helper");
21
- const metrics_1 = __importDefault(require("./helpers/webrtcmetrics/metrics"));
22
- const metric_keys_to_include_1 = require("./enum/metric.keys.to.include");
23
- const call_event_listener_type_1 = require("./enum/call.event.listener.type");
18
+ const time_helper_1 = require("@/helpers/time.helper");
19
+ const filter_helper_1 = require("@/helpers/filter.helper");
20
+ const audio_helper_1 = require("@/helpers/audio.helper");
21
+ const metrics_1 = __importDefault(require("@/helpers/webrtcmetrics/metrics"));
22
+ const metric_keys_to_include_1 = require("@/enum/metric.keys.to.include");
23
+ const call_event_listener_type_1 = require("@/enum/call.event.listener.type");
24
24
  const CALL_STATUS_UNANSWERED = 0;
25
25
  const STORAGE_KEYS = {
26
26
  SELECTED_INPUT_DEVICE: 'selectedInputDevice',
27
27
  SELECTED_OUTPUT_DEVICE: 'selectedOutputDevice'
28
28
  };
29
+ const activeCalls = {};
29
30
  class OpenSIPSJS extends UA_1.default {
30
31
  constructor(options) {
31
32
  const configuration = Object.assign(Object.assign({}, options.configuration), { sockets: options.socketInterfaces.map(sock => new jssip_1.default.WebSocketInterface(sock)) });
@@ -33,12 +34,9 @@ class OpenSIPSJS extends UA_1.default {
33
34
  this.initialized = false;
34
35
  this.newRTCSessionEventName = 'newRTCSession';
35
36
  this.activeCalls = {};
36
- this.extendedCalls = {};
37
37
  this.state = {
38
38
  isMuted: false,
39
- isAutoAnswer: false,
40
39
  activeCalls: {},
41
- extendedCalls: {},
42
40
  availableMediaDevices: [],
43
41
  selectedMediaDevices: {
44
42
  input: 'default',
@@ -84,12 +82,6 @@ class OpenSIPSJS extends UA_1.default {
84
82
  this._currentActiveRoomId = roomId;
85
83
  this.emit('currentActiveRoomChanged', roomId);
86
84
  }
87
- get autoAnswer() {
88
- return this.state.isAutoAnswer;
89
- }
90
- set autoAnswer(value) {
91
- this.state.isAutoAnswer = value;
92
- }
93
85
  get callAddingInProgress() {
94
86
  return this._callAddingInProgress;
95
87
  }
@@ -116,7 +108,7 @@ class OpenSIPSJS extends UA_1.default {
116
108
  }
117
109
  set speakerVolume(value) {
118
110
  this.state.speakerVolume = value;
119
- Object.values(this.state.extendedCalls).forEach((call) => {
111
+ Object.values(activeCalls).forEach((call) => {
120
112
  if (call.audioTag) {
121
113
  call.audioTag.volume = value;
122
114
  }
@@ -249,7 +241,7 @@ class OpenSIPSJS extends UA_1.default {
249
241
  if (!validation_regex.test(value)) {
250
242
  throw new Error('Not allowed character in DTMF input');
251
243
  }
252
- const call = this.state.extendedCalls[callId];
244
+ const call = activeCalls[callId];
253
245
  call.sendDTMF(value);
254
246
  }
255
247
  doMute(value) {
@@ -257,11 +249,12 @@ class OpenSIPSJS extends UA_1.default {
257
249
  this.isMuted = value;
258
250
  this.roomReconfigure(activeRoomId);
259
251
  }
260
- sendMessage(target, body, options) {
252
+ sendMSRP(target, body, options) {
253
+ super.sendMSRPMessage(`sip:${target}@${this.sipDomain}`, body);
261
254
  return super.sendMessage(`sip:${target}@${this.sipDomain}`, body, options);
262
255
  }
263
256
  doCallHold({ callId, toHold, automatic }) {
264
- const call = this.state.extendedCalls[callId];
257
+ const call = activeCalls[callId];
265
258
  call._automaticHold = automatic !== null && automatic !== void 0 ? automatic : false;
266
259
  if (toHold) {
267
260
  call.hold();
@@ -277,7 +270,7 @@ class OpenSIPSJS extends UA_1.default {
277
270
  }).forEach(call => this.callTerminate(call._id));
278
271
  }
279
272
  callAnswer(callId) {
280
- const call = this.state.extendedCalls[callId];
273
+ const call = activeCalls[callId];
281
274
  this._cancelAllOutgoingUnanswered();
282
275
  call.answer(this.sipOptions);
283
276
  this.updateCall(call);
@@ -304,16 +297,10 @@ class OpenSIPSJS extends UA_1.default {
304
297
  this.state.activeRooms = Object.assign(Object.assign({}, this.state.activeRooms), { [value.roomId]: Object.assign({}, newRoomData) });
305
298
  this.emit('updateRoom', { room: newRoomData, roomList: this.state.activeRooms });
306
299
  }
307
- _addCall(value, emitEvent = true) {
300
+ _addCall(value) {
308
301
  this.state.activeCalls = Object.assign(Object.assign({}, this.state.activeCalls), { [value._id]: (0, audio_helper_1.simplifyCallObject)(value) });
309
- /*this.state.extendedCalls = {
310
- ...this.state.extendedCalls,
311
- [value._id]: value
312
- }*/
313
- this.state.extendedCalls[value._id] = value;
314
- if (emitEvent) {
315
- this.emit('changeActiveCalls', this.state.activeCalls);
316
- }
302
+ activeCalls[value._id] = value;
303
+ this.emit('changeActiveCalls', this.state.activeCalls);
317
304
  }
318
305
  _addCallStatus(callId) {
319
306
  this.state.callStatus = Object.assign(Object.assign({}, this.state.callStatus), { [callId]: {
@@ -321,7 +308,6 @@ class OpenSIPSJS extends UA_1.default {
321
308
  isTransferring: false,
322
309
  isMerging: false
323
310
  } });
324
- this.emit('changeCallStatus', this.state.callStatus);
325
311
  }
326
312
  _updateCallStatus(value) {
327
313
  const prevStatus = Object.assign({}, this.state.callStatus[value.callId]);
@@ -336,13 +322,11 @@ class OpenSIPSJS extends UA_1.default {
336
322
  newStatus.isMerging = value.isMerging;
337
323
  }
338
324
  this.state.callStatus = Object.assign(Object.assign({}, this.state.callStatus), { [value.callId]: Object.assign({}, newStatus) });
339
- this.emit('changeCallStatus', this.state.callStatus);
340
325
  }
341
326
  _removeCallStatus(callId) {
342
327
  const callStatusCopy = Object.assign({}, this.state.callStatus);
343
328
  delete callStatusCopy[callId];
344
329
  this.state.callStatus = Object.assign({}, callStatusCopy);
345
- this.emit('changeCallStatus', this.state.callStatus);
346
330
  }
347
331
  _addRoom(value) {
348
332
  this.state.activeRooms = Object.assign(Object.assign({}, this.state.activeRooms), { [value.roomId]: value });
@@ -364,7 +348,7 @@ class OpenSIPSJS extends UA_1.default {
364
348
  if (Object.keys(this.getActiveCalls).length === 0) {
365
349
  return;
366
350
  }
367
- const callsInCurrentRoom = Object.values(this.state.extendedCalls).filter(call => call.roomId === this.currentActiveRoomId);
351
+ const callsInCurrentRoom = Object.values(activeCalls).filter(call => call.roomId === this.currentActiveRoomId);
368
352
  if (callsInCurrentRoom.length === 1) {
369
353
  Object.values(callsInCurrentRoom).forEach(call => {
370
354
  const processedStream = (0, audio_helper_1.processAudioVolume)(stream, this.microphoneInputLevel);
@@ -389,7 +373,7 @@ class OpenSIPSJS extends UA_1.default {
389
373
  return;
390
374
  }
391
375
  this.selectedOutputDevice = dId;
392
- const activeCallList = Object.values(this.state.extendedCalls);
376
+ const activeCallList = Object.values(activeCalls);
393
377
  if (activeCallList.length === 0) {
394
378
  return;
395
379
  }
@@ -417,10 +401,10 @@ class OpenSIPSJS extends UA_1.default {
417
401
  if (roomId === undefined) {
418
402
  return;
419
403
  }
420
- if (Object.values(this.state.extendedCalls).filter(call => call.roomId === roomId).length === 0) {
404
+ if (Object.values(activeCalls).filter(call => call.roomId === roomId).length === 0) {
421
405
  this.removeRoom(roomId);
422
406
  if (this.currentActiveRoomId === roomId) {
423
- this.currentActiveRoomId = undefined;
407
+ this.currentActiveRoomId = roomId;
424
408
  }
425
409
  }
426
410
  }
@@ -442,7 +426,7 @@ class OpenSIPSJS extends UA_1.default {
442
426
  if (roomId === undefined) {
443
427
  return;
444
428
  }
445
- const callsInRoom = Object.values(this.state.extendedCalls).filter(call => call.roomId === roomId);
429
+ const callsInRoom = Object.values(activeCalls).filter(call => call.roomId === roomId);
446
430
  // Let`s take care on the audio output first and check if passed room is our selected room
447
431
  if (this.currentActiveRoomId === roomId) {
448
432
  callsInRoom.forEach(call => {
@@ -555,7 +539,7 @@ class OpenSIPSJS extends UA_1.default {
555
539
  }
556
540
  }
557
541
  muteCaller(callId, value) {
558
- const call = this.state.extendedCalls[callId];
542
+ const call = activeCalls[callId];
559
543
  if (call && call.connection.getReceivers().length) {
560
544
  call.localMuted = value;
561
545
  call.connection.getReceivers().forEach((receiver) => {
@@ -566,7 +550,7 @@ class OpenSIPSJS extends UA_1.default {
566
550
  }
567
551
  }
568
552
  callTerminate(callId) {
569
- const call = this.state.extendedCalls[callId];
553
+ const call = activeCalls[callId];
570
554
  if (call._status !== 8) {
571
555
  call.terminate();
572
556
  }
@@ -575,17 +559,13 @@ class OpenSIPSJS extends UA_1.default {
575
559
  if (target.toString().length === 0) {
576
560
  return console.error('Target must be passed');
577
561
  }
578
- const call = this.state.extendedCalls[callId];
579
- if (!call._is_confirmed && !call._is_canceled) {
580
- call.refer(`sip:${target}@${this.sipDomain}`);
581
- return;
582
- }
583
562
  this._updateCallStatus({ callId, isTransferring: true });
563
+ const call = activeCalls[callId];
584
564
  call.refer(`sip:${target}@${this.sipDomain}`);
585
565
  this.updateCall(call);
586
566
  }
587
567
  callMerge(roomId) {
588
- const callsInRoom = Object.values(this.state.extendedCalls).filter((call) => call.roomId === roomId);
568
+ const callsInRoom = Object.values(activeCalls).filter((call) => call.roomId === roomId);
589
569
  if (callsInRoom.length !== 2)
590
570
  return;
591
571
  const firstCall = callsInRoom[0];
@@ -676,7 +656,6 @@ class OpenSIPSJS extends UA_1.default {
676
656
  incomingInProgress: false,
677
657
  roomId
678
658
  });
679
- this.deleteRoomIfEmpty(roomId);
680
659
  }
681
660
  });
682
661
  }
@@ -686,19 +665,9 @@ class OpenSIPSJS extends UA_1.default {
686
665
  const call = session;
687
666
  call.roomId = roomId;
688
667
  call.localMuted = false;
689
- const doAutoAnswer = call.direction === 'incoming' && this.autoAnswer;
690
- if (doAutoAnswer) {
691
- this._addCall(call, false);
692
- }
693
- else {
694
- this._addCall(call);
695
- }
696
- // this._addCall(call)
668
+ this._addCall(call);
697
669
  this._addCallStatus(session.id);
698
670
  this._addRoom(newRoomInfo);
699
- if (doAutoAnswer) {
700
- this.callAnswer(call._id);
701
- }
702
671
  });
703
672
  }
704
673
  _triggerListener({ listenerType, session, event }) {
@@ -713,15 +682,12 @@ class OpenSIPSJS extends UA_1.default {
713
682
  _removeCall(value) {
714
683
  const stateActiveCallsCopy = Object.assign({}, this.state.activeCalls);
715
684
  delete stateActiveCallsCopy[value];
716
- // delete activeCalls[value]
685
+ delete activeCalls[value];
717
686
  this.state.activeCalls = Object.assign({}, stateActiveCallsCopy);
718
- const stateExtendedCallsCopy = Object.assign({}, this.state.extendedCalls);
719
- delete stateExtendedCallsCopy[value];
720
- this.state.extendedCalls = Object.assign({}, stateExtendedCallsCopy);
721
687
  this.emit('changeActiveCalls', this.state.activeCalls);
722
688
  }
723
689
  _activeCallListRemove(call) {
724
- const callRoomIdToConfigure = this.state.extendedCalls[call._id].roomId;
690
+ const callRoomIdToConfigure = activeCalls[call._id].roomId;
725
691
  this._removeCall(call._id);
726
692
  this.roomReconfigure(callRoomIdToConfigure);
727
693
  }
@@ -735,13 +701,11 @@ class OpenSIPSJS extends UA_1.default {
735
701
  session.on('ended', (event) => {
736
702
  this._triggerListener({ listenerType: call_event_listener_type_1.CALL_EVENT_LISTENER_TYPE.CALL_ENDED, session, event });
737
703
  const s = this.getActiveCalls[session.id];
738
- if (s) {
739
- this._activeCallListRemove(s);
740
- }
704
+ this._activeCallListRemove(s);
741
705
  this._stopCallTimer(session.id);
742
706
  this._removeCallStatus(session.id);
743
707
  this._removeCallMetrics(session.id);
744
- if (!Object.keys(this.state.extendedCalls).length) {
708
+ if (!Object.keys(activeCalls).length) {
745
709
  this.isMuted = false;
746
710
  }
747
711
  });
@@ -754,13 +718,11 @@ class OpenSIPSJS extends UA_1.default {
754
718
  this.callAddingInProgress = undefined;
755
719
  }
756
720
  const s = this.getActiveCalls[session.id];
757
- if (s) {
758
- this._activeCallListRemove(s);
759
- }
721
+ this._activeCallListRemove(s);
760
722
  this._stopCallTimer(session.id);
761
723
  this._removeCallStatus(session.id);
762
724
  this._removeCallMetrics(session.id);
763
- if (!Object.keys(this.state.extendedCalls).length) {
725
+ if (!Object.keys(activeCalls).length) {
764
726
  this.isMuted = false;
765
727
  }
766
728
  });
@@ -777,46 +739,14 @@ class OpenSIPSJS extends UA_1.default {
777
739
  this.setCurrentActiveRoomId(roomId);
778
740
  }
779
741
  }
780
- setInitialized(value) {
781
- this.initialized = value;
782
- this.emit('ready', value);
742
+ setInitialized() {
743
+ this.initialized = true;
744
+ this.emit('ready', true);
783
745
  }
784
746
  start() {
785
- this.on('connecting', (res) => {
786
- console.log('ON connecting', res);
787
- });
788
- this.on('connected', (res) => {
789
- console.log('ON connected', res);
790
- });
791
- this.on('disconnected', (res) => {
792
- console.log('ON disconnected', res);
793
- });
794
- this.on('registered', (res) => {
795
- this.setInitialized(true);
796
- console.log('ON registered', res);
797
- });
798
- this.on('unregistered', (res) => {
799
- this.setInitialized(false);
800
- console.log('ON unregistered', res);
801
- });
802
- this.on('registrationFailed', (res) => {
803
- console.log('ON registrationFailed', res);
804
- });
805
- this.on('registrationExpiring', (res) => {
806
- console.log('ON registrationExpiring', res);
807
- });
808
- this.on('newMessage', (res) => {
809
- console.log('ON newMessage', res);
810
- });
811
- this.on('sipEvent', (res) => {
812
- console.log('ON sipEvent', res);
813
- });
814
- this.on('newOptions', (res) => {
815
- console.log('ON newOptions', res);
816
- });
817
747
  this.on(this.newRTCSessionEventName, this.newRTCSessionCallback.bind(this));
818
748
  super.start();
819
- //this.setInitialized()
749
+ this.setInitialized();
820
750
  //this.setDefaultMediaDevices()
821
751
  this.setMediaDevices(true);
822
752
  return this;
@@ -904,8 +834,8 @@ class OpenSIPSJS extends UA_1.default {
904
834
  }
905
835
  callChangeRoom({ callId, roomId }) {
906
836
  return __awaiter(this, void 0, void 0, function* () {
907
- const oldRoomId = this.state.extendedCalls[callId].roomId;
908
- this.state.extendedCalls[callId].roomId = roomId;
837
+ const oldRoomId = activeCalls[callId].roomId;
838
+ activeCalls[callId].roomId = roomId;
909
839
  yield this.setCurrentActiveRoomId(roomId);
910
840
  return Promise.all([
911
841
  this.roomReconfigure(oldRoomId),
@@ -0,0 +1,13 @@
1
+ export declare class MSRPMessage {
2
+ protocol: string;
3
+ ident: null;
4
+ code: null;
5
+ method: null;
6
+ headers: any;
7
+ body: string;
8
+ challenge: null;
9
+ constructor(msg: string);
10
+ addHeader(name: string, content: string): void;
11
+ getHeader(name: string): any;
12
+ toString(): string;
13
+ }
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.MSRPMessage = void 0;
7
+ const Utils_1 = __importDefault(require("jssip/lib/Utils"));
8
+ class MSRPMessage {
9
+ constructor(msg) {
10
+ this.protocol = '';
11
+ this.ident = null;
12
+ this.code = null;
13
+ this.method = null;
14
+ this.headers = {};
15
+ this.body = '';
16
+ this.challenge = null;
17
+ if (msg.length > 0) {
18
+ let _hasBody = false;
19
+ const msgLines = msg.split('\r\n');
20
+ const msgHeadLineArray = msgLines.shift().split(/\s/);
21
+ this.protocol = msgHeadLineArray[0];
22
+ this.ident = msgHeadLineArray[1];
23
+ this.code = (msgHeadLineArray.length > 3) ? msgHeadLineArray[2] : null;
24
+ this.method = (msgHeadLineArray.length > 3) ? msgHeadLineArray[3] : msgHeadLineArray[2];
25
+ for (const msgLine of msgLines) {
26
+ if (msgLine == `-------${this.ident}$`) {
27
+ break;
28
+ }
29
+ if (msgLine === '') {
30
+ _hasBody = true;
31
+ continue;
32
+ }
33
+ if (_hasBody) {
34
+ this.body += msgLine + '\r\n';
35
+ }
36
+ else {
37
+ const msgLineArray = msgLine.split(': ');
38
+ this.addHeader(msgLineArray[0], msgLineArray[1].trim());
39
+ }
40
+ }
41
+ }
42
+ else {
43
+ this.ident = Utils_1.default.createRandomToken(12);
44
+ this.protocol = 'MSRP';
45
+ }
46
+ }
47
+ addHeader(name, content) {
48
+ this.headers[name] = content;
49
+ }
50
+ getHeader(name) {
51
+ return this.headers[name];
52
+ }
53
+ toString() {
54
+ let _msg = `${this.protocol} ${this.ident} ${this.code} ${this.method}`.replaceAll(/null\s/ig, '') + '\r\n';
55
+ for (const _header in this.headers) {
56
+ _msg += `${_header}: ${this.headers[_header]}\r\n`;
57
+ }
58
+ if (this.body) {
59
+ _msg += '\r\n' + this.body;
60
+ }
61
+ _msg += `-------${this.ident}$\r\n`;
62
+ return _msg;
63
+ }
64
+ }
65
+ exports.MSRPMessage = MSRPMessage;
@@ -0,0 +1,21 @@
1
+ import { UA } from 'jssip';
2
+ export declare class MSRPSession {
3
+ _ua: UA;
4
+ _request: any;
5
+ credentials: any;
6
+ webSocket: any;
7
+ status: string;
8
+ target: string;
9
+ message: string;
10
+ constructor(ua: UA);
11
+ connect(): void;
12
+ authenticate(auth: any): void;
13
+ onmessage(msg: any): void;
14
+ onclose(): void;
15
+ onopen(): void;
16
+ onerror(): void;
17
+ stop(): void;
18
+ inviteParty(smgObj: any): void;
19
+ send(target: string, message: string): void;
20
+ parseAuth(content: string): any;
21
+ }
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.MSRPSession = void 0;
7
+ const Utils_1 = __importDefault(require("jssip/lib/Utils"));
8
+ const RequestSender_1 = __importDefault(require("jssip/lib/RequestSender"));
9
+ const DigestAuthentication_1 = __importDefault(require("jssip/lib/DigestAuthentication"));
10
+ const message_1 = require("@/lib/msrp/message");
11
+ const URI_1 = __importDefault(require("jssip/lib/URI"));
12
+ const SIPMessage_1 = __importDefault(require("jssip/lib/SIPMessage"));
13
+ class MSRPSession {
14
+ constructor(ua) {
15
+ this._ua = ua;
16
+ this.credentials = {
17
+ 'username': ua._configuration.authorization_user,
18
+ 'ha1': ua._configuration.ha1,
19
+ 'realm': ua._configuration.realm,
20
+ 'msrprelay': 'msrp://sip06.voicenter.co:2856;ws',
21
+ 'msrpurl': 'ws://sip06.voicenter.co:2856'
22
+ };
23
+ this.status = 'new';
24
+ this.target = '';
25
+ this.message = '';
26
+ }
27
+ connect() {
28
+ this.webSocket = new WebSocket(this.credentials.msrpurl, 'msrp');
29
+ this.webSocket.binaryType = 'arraybuffer';
30
+ this.webSocket.onopen = () => {
31
+ this.onopen();
32
+ };
33
+ this.webSocket.onclose = () => {
34
+ this.onclose();
35
+ };
36
+ this.webSocket.onmessage = (msg) => {
37
+ this.onmessage(msg);
38
+ };
39
+ this.webSocket.onerror = () => {
40
+ this.onerror();
41
+ };
42
+ }
43
+ authenticate(auth) {
44
+ this.status = 'auth';
45
+ const msgObj = new message_1.MSRPMessage('');
46
+ msgObj.method = 'AUTH';
47
+ msgObj.addHeader('To-Path', this.credentials.msrprelay);
48
+ msgObj.addHeader('From-Path', 'msrp://' + this.credentials.username + '.sip06.voicenter.co:2856/' + msgObj.ident + ';ws');
49
+ if (auth) {
50
+ msgObj.addHeader('Authorization', auth.toString());
51
+ }
52
+ this.webSocket.send(msgObj.toString());
53
+ }
54
+ onmessage(msg) {
55
+ const msgObj = new message_1.MSRPMessage(msg.data);
56
+ if (this.status == 'auth' && msgObj.code === '401') {
57
+ const _challenge = this.parseAuth(msgObj.getHeader('WWW-Authenticate'));
58
+ const digestAuthentication = new DigestAuthentication_1.default(this.credentials);
59
+ digestAuthentication.authenticate({ method: 'AUTH', ruri: this.credentials.msrprelay, body: null }, _challenge, Utils_1.default.createRandomToken(12));
60
+ this.authenticate(digestAuthentication);
61
+ }
62
+ if (this.status == 'auth' && msgObj.code === '200') {
63
+ this.status = 'ready';
64
+ this.inviteParty(msgObj);
65
+ // console.log(msgObj)
66
+ }
67
+ }
68
+ onclose() {
69
+ console.log('close');
70
+ }
71
+ onopen() {
72
+ this.authenticate(null);
73
+ }
74
+ onerror() {
75
+ }
76
+ stop() {
77
+ this.webSocket.close();
78
+ }
79
+ inviteParty(smgObj) {
80
+ const requestParams = {};
81
+ const extraHeaders = [];
82
+ requestParams.from_uri = new URI_1.default('sip', this._ua._configuration.uri.user, this._ua.configuration.uri.host);
83
+ requestParams.to_uri = new URI_1.default('sip', this.target, this._ua._configuration.realm);
84
+ extraHeaders.push(`P-Preferred-Identity: ${this._ua._configuration.uri.toString()}`);
85
+ extraHeaders.push(`Contact: ${this._ua.contact.toString({
86
+ outbound: true
87
+ })}`);
88
+ extraHeaders.push('Content-Type: application/sdp');
89
+ const request = new SIPMessage_1.default.InitialOutgoingInviteRequest(this._ua.normalizeTarget(this.target), this._ua, requestParams, extraHeaders, 'v=0\n' +
90
+ 'o=- 4232740119537112802 2 IN IP4 185.138.168.169\n' +
91
+ 'c=IN IP4 185.138.168.169\n' +
92
+ 't=0 0\n' +
93
+ 'm=message 2855 TCP/TLS/MSRP *\n' +
94
+ 'a=accept-types:text/plain text/html\n' +
95
+ 'a=path:' + smgObj.getHeader('Use-Path') + '\n');
96
+ const request_sender = new RequestSender_1.default(this._ua, request, {
97
+ onRequestTimeout: () => {
98
+ console.log('to');
99
+ },
100
+ onTransportError: (err) => {
101
+ console.log(err);
102
+ },
103
+ // Update the request on authentication.
104
+ onAuthenticated: (request) => {
105
+ this._request = request;
106
+ },
107
+ onReceiveResponse: (response) => {
108
+ console.log(response);
109
+ }
110
+ });
111
+ request_sender.send();
112
+ }
113
+ send(target, message) {
114
+ this.target = target;
115
+ this.message = message;
116
+ if (this.status == 'new') {
117
+ this.connect();
118
+ }
119
+ }
120
+ parseAuth(content) {
121
+ const _challenge = {};
122
+ const _challengeArray = content.replace('Digest', '').split(',');
123
+ for (const _authItem of _challengeArray) {
124
+ const _itemArray = _authItem.trim().split('=');
125
+ _challenge[_itemArray[0]] = _itemArray[1].match('^"(.+)"$')[1];
126
+ }
127
+ return _challenge;
128
+ }
129
+ }
130
+ exports.MSRPSession = MSRPSession;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voicenter-team/opensips-js",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "The JS package for opensips",
5
5
  "default": "src/index.ts",
6
6
  "main": "build/index.js",
@@ -42,7 +42,7 @@
42
42
  "regenerator-runtime": "^0.13.11",
43
43
  "tsc-alias": "^1.8.6",
44
44
  "typescript": "^4.9.5",
45
- "vite": "^4.3.8",
45
+ "vite": "4.3.9",
46
46
  "vite-plugin-singlefile": "^0.13.5",
47
47
  "vue": "3.2.25"
48
48
  },
@@ -1,2 +0,0 @@
1
- import { SessionDirection } from 'jssip/lib/RTCSession';
2
- export { SessionDirection };
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SessionDirection = void 0;
4
- const RTCSession_1 = require("jssip/lib/RTCSession");
5
- Object.defineProperty(exports, "SessionDirection", { enumerable: true, get: function () { return RTCSession_1.SessionDirection; } });
@@ -1,5 +0,0 @@
1
- import JsSIP, { UA } from 'jssip';
2
- import { IncomingAckEvent, IncomingEvent, OutgoingAckEvent, OutgoingEvent } from 'jssip/lib/RTCSession';
3
- import { RTCSessionEvent, UAConfiguration, UAEventMap } from 'jssip/lib/UA';
4
- export default JsSIP;
5
- export { UA, IncomingAckEvent, IncomingEvent, OutgoingAckEvent, OutgoingEvent, RTCSessionEvent, UAConfiguration, UAEventMap };
@@ -1,30 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.UA = void 0;
27
- // @ts-nocheck
28
- const jssip_1 = __importStar(require("jssip"));
29
- Object.defineProperty(exports, "UA", { enumerable: true, get: function () { return jssip_1.UA; } });
30
- exports.default = jssip_1.default;