Skip to content

Commit 159af4d

Browse files
author
James Criscuolo
committed
Merge branch 'master' into release
2 parents fa5b6f3 + 3a948ae commit 159af4d

File tree

5 files changed

+109
-61
lines changed

5 files changed

+109
-61
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "sip.js",
33
"title": "SIP.js",
44
"description": "A simple, intuitive, and powerful JavaScript signaling library",
5-
"version": "0.14.2",
5+
"version": "0.14.3",
66
"license": "MIT",
77
"main": "./lib/index.js",
88
"types": "./lib/index.d.ts",
@@ -47,7 +47,7 @@
4747
"ts-pegjs": "0.2.5",
4848
"tslint": "^5.17.0",
4949
"typescript": "^3.5.1",
50-
"webpack": "^4.32.2",
50+
"webpack": "^4.33.0",
5151
"webpack-cli": "^3.3.2"
5252
},
5353
"engines": {

src/ClientContext.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
import { EventEmitter } from "events";
22

33
import { C } from "./Constants";
4-
import { Body, IncomingResponseMessage, Logger, NameAddrHeader, OutgoingRequestMessage, URI } from "./core";
4+
import { Body, Grammar, IncomingResponseMessage, Logger, NameAddrHeader, OutgoingRequestMessage, URI } from "./core";
55
import { TypeStrings } from "./Enums";
66
import { BodyObj } from "./session-description-handler";
77
import { UA } from "./UA";
88
import { Utils } from "./Utils";
99

10+
export namespace ClientContext {
11+
export interface Options {
12+
body?: string;
13+
contentType?: string;
14+
extraHeaders?: Array<string>;
15+
params?: {
16+
fromUri?: string | URI;
17+
toUri?: string | URI;
18+
toDisplayName?: string;
19+
};
20+
}
21+
}
22+
1023
export class ClientContext extends EventEmitter {
1124
public static initializer(
1225
objToConstruct: ClientContext,
1326
ua: UA,
1427
method: string,
1528
originalTarget: string | URI,
16-
options?: any
29+
options?: ClientContext.Options
1730
): void {
1831
objToConstruct.type = TypeStrings.ClientContext;
1932

@@ -30,6 +43,26 @@ export class ClientContext extends EventEmitter {
3043
if (!target) {
3144
throw new TypeError("Invalid target: " + originalTarget);
3245
}
46+
let fromURI: URI | undefined = ua.userAgentCore.configuration.aor;
47+
if (options && options.params && options.params.fromUri) {
48+
fromURI =
49+
(typeof options.params.fromUri === "string") ?
50+
Grammar.URIParse(options.params.fromUri) :
51+
options.params.fromUri;
52+
if (!fromURI) {
53+
throw new TypeError("Invalid from URI: " + options.params.fromUri);
54+
}
55+
}
56+
let toURI: URI | undefined = target;
57+
if (options && options.params && options.params.toUri) {
58+
toURI =
59+
(typeof options.params.toUri === "string") ?
60+
Grammar.URIParse(options.params.toUri) :
61+
options.params.toUri;
62+
if (!toURI) {
63+
throw new TypeError("Invalid to URI: " + options.params.toUri);
64+
}
65+
}
3366

3467
/* Options
3568
* - extraHeaders
@@ -38,6 +71,7 @@ export class ClientContext extends EventEmitter {
3871
* - body
3972
*/
4073
options = Object.create(options || Object.prototype);
74+
options = options || {};
4175
const extraHeaders = (options.extraHeaders || []).slice();
4276
const params = options.params || {};
4377
let bodyObj: BodyObj | undefined;
@@ -57,8 +91,8 @@ export class ClientContext extends EventEmitter {
5791
objToConstruct.request = ua.userAgentCore.makeOutgoingRequestMessage(
5892
method,
5993
target,
60-
params.fromUri ? params.fromUri : ua.userAgentCore.configuration.aor,
61-
params.toUri ? params.toUri : target,
94+
fromURI,
95+
toURI,
6296
params,
6397
extraHeaders,
6498
body
@@ -86,7 +120,7 @@ export class ClientContext extends EventEmitter {
86120
public localIdentity!: NameAddrHeader;
87121
public remoteIdentity!: NameAddrHeader;
88122

89-
constructor(ua: UA, method: string, target: string | URI, options?: any) {
123+
constructor(ua: UA, method: string, target: string | URI, options?: ClientContext.Options) {
90124
super();
91125

92126
ClientContext.initializer(this, ua, method, target, options);

src/Session.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,15 +1716,30 @@ export class InviteServerContext extends Session implements ServerContext {
17161716

17171717
export namespace InviteClientContext {
17181718
export interface Options {
1719+
/** Anonymous call if true. */
1720+
anonymous?: boolean;
1721+
/** Deprecated. */
1722+
body?: string;
1723+
/** Deprecated. */
1724+
contentType?: string;
17191725
/** Array of extra headers added to the INVITE. */
17201726
extraHeaders?: Array<string>;
17211727
/** If true, send INVITE without SDP. */
17221728
inviteWithoutSdp?: boolean;
1723-
/** Deprecated */
1729+
/** Deprecated. */
1730+
onInfo?: any;
1731+
/** Deprecated. */
17241732
params?: {
1725-
toUri?: string;
1726-
toDisplayName: string;
1733+
fromDisplayName?: string;
1734+
fromTag?: string;
1735+
fromUri?: string | URI;
1736+
toDisplayName?: string;
1737+
toUri?: string | URI;
17271738
};
1739+
/** Deprecated. */
1740+
renderbody?: string;
1741+
/** Deprecated. */
1742+
rendertype?: string;
17281743
/** Options to pass to SessionDescriptionHandler's getDescription() and setDescription(). */
17291744
sessionDescriptionHandlerOptions?: SessionDescriptionHandlerOptions;
17301745
}
@@ -1743,7 +1758,12 @@ export class InviteClientContext extends Session implements ClientContext {
17431758
private earlyMediaSessionDescriptionHandlers: Map<string, SessionDescriptionHandler>;
17441759
private outgoingInviteRequest: OutgoingInviteRequest | undefined;
17451760

1746-
constructor(ua: UA, target: string | URI, options: any = {}, modifiers: any = []) {
1761+
constructor(
1762+
ua: UA,
1763+
target: string | URI,
1764+
options: InviteClientContext.Options = {},
1765+
modifiers: SessionDescriptionHandlerModifiers = []
1766+
) {
17471767
if (!ua.configuration.sessionDescriptionHandlerFactory) {
17481768
ua.logger.warn("Can't build ISC without SDH Factory");
17491769
throw new Error("ICC Constructor Failed");
@@ -1766,8 +1786,8 @@ export class InviteClientContext extends Session implements ClientContext {
17661786

17671787
const extraHeaders: Array<string> = (options.extraHeaders || []).slice();
17681788
if (anonymous && ua.configuration.uri) {
1769-
options.params.from_displayName = "Anonymous";
1770-
options.params.from_uri = "sip:[email protected]";
1789+
options.params.fromDisplayName = "Anonymous";
1790+
options.params.fromUri = "sip:[email protected]";
17711791

17721792
extraHeaders.push("P-Preferred-Identity: " + ua.configuration.uri.toString());
17731793
extraHeaders.push("Privacy: id");

src/UA.ts

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class UA extends EventEmitter {
150150
toString: (options?: any) => string
151151
};
152152
public status: UAStatus;
153-
public transport: Transport | undefined;
153+
public transport: Transport;
154154
public sessions: {[id: string]: InviteClientContext | InviteServerContext};
155155
public subscriptions: {[id: string]: Subscription};
156156
public data: any;
@@ -261,6 +261,14 @@ export class UA extends EventEmitter {
261261
throw e;
262262
}
263263

264+
if (!this.configuration.transportConstructor) {
265+
throw new TransportError("Transport constructor not set");
266+
}
267+
this.transport = new this.configuration.transportConstructor(
268+
this.getLogger("sip.transport"),
269+
this.configuration.transportOptions
270+
);
271+
264272
const userAgentCoreConfiguration = makeUserAgentCoreConfigurationFromUA(this);
265273

266274
// The Replaces header contains information used to match an existing
@@ -400,11 +408,9 @@ export class UA extends EventEmitter {
400408
public unregister(options?: any): this {
401409
this.configuration.register = false;
402410

403-
if (this.transport) {
404-
this.transport.afterConnected(() => {
405-
this.registerContext.unregister(options);
406-
});
407-
}
411+
this.transport.afterConnected(() => {
412+
this.registerContext.unregister(options);
413+
});
408414

409415
return this;
410416
}
@@ -432,20 +438,16 @@ export class UA extends EventEmitter {
432438
// Delay sending actual invite until the next 'tick' if we are already
433439
// connected, so that API consumers can register to events fired by the
434440
// the session.
435-
if (this.transport) {
436-
this.transport.afterConnected(() => {
437-
context.invite();
438-
this.emit("inviteSent", context);
439-
});
440-
}
441+
this.transport.afterConnected(() => {
442+
context.invite();
443+
this.emit("inviteSent", context);
444+
});
441445
return context;
442446
}
443447

444448
public subscribe(target: string | URI, event: string, options: any): Subscription {
445449
const sub: Subscription = new Subscription(this, target, event, options);
446-
if (this.transport) {
447-
this.transport.afterConnected(() => sub.subscribe());
448-
}
450+
this.transport.afterConnected(() => sub.subscribe());
449451
return sub;
450452
}
451453

@@ -462,11 +464,9 @@ export class UA extends EventEmitter {
462464
public publish(target: string | URI, event: string, body: string, options: any): PublishContext {
463465
const pub: PublishContext = new PublishContext(this, target, event, options);
464466

465-
if (this.transport) {
466-
this.transport.afterConnected(() => {
467-
pub.publish(body);
468-
});
469-
}
467+
this.transport.afterConnected(() => {
468+
pub.publish(body);
469+
});
470470
return pub;
471471
}
472472

@@ -494,9 +494,7 @@ export class UA extends EventEmitter {
494494
public request(method: string, target: string | URI, options?: any): ClientContext {
495495
const req: ClientContext = new ClientContext(this, method, target, options);
496496

497-
if (this.transport) {
498-
this.transport.afterConnected(() => req.send());
499-
}
497+
this.transport.afterConnected(() => req.send());
500498
return req;
501499
}
502500

@@ -549,9 +547,7 @@ export class UA extends EventEmitter {
549547
this.status = UAStatus.STATUS_USER_CLOSED;
550548

551549
// Disconnect the transport and reset user agent core
552-
if (this.transport) {
553-
this.transport.disconnect();
554-
}
550+
this.transport.disconnect();
555551
this.userAgentCore.reset();
556552

557553
if (typeof environment.removeEventListener === "function") {
@@ -574,24 +570,13 @@ export class UA extends EventEmitter {
574570
this.logger.log("user requested startup...");
575571
if (this.status === UAStatus.STATUS_INIT) {
576572
this.status = UAStatus.STATUS_STARTING;
577-
if (!this.configuration.transportConstructor) {
578-
throw new TransportError("Transport constructor not set");
579-
}
580-
this.transport = new this.configuration.transportConstructor(
581-
this.getLogger("sip.transport"),
582-
this.configuration.transportOptions
583-
);
584573
this.setTransportListeners();
585574
this.emit("transportCreated", this.transport);
586575
this.transport.connect();
587-
588576
} else if (this.status === UAStatus.STATUS_USER_CLOSED) {
589577
this.logger.log("resuming");
590578
this.status = UAStatus.STATUS_READY;
591-
if (this.transport) {
592-
this.transport.connect();
593-
}
594-
579+
this.transport.connect();
595580
} else if (this.status === UAStatus.STATUS_STARTING) {
596581
this.logger.log("UA is in STARTING status, not opening new connection");
597582
} else if (this.status === UAStatus.STATUS_READY) {
@@ -701,11 +686,9 @@ export class UA extends EventEmitter {
701686
* Helper function. Sets transport listeners
702687
*/
703688
private setTransportListeners(): void {
704-
if (this.transport) {
705-
this.transport.on("connected", () => this.onTransportConnected());
706-
this.transport.on("message", (message: string) => this.onTransportReceiveMsg(message));
707-
this.transport.on("transportError", () => this.onTransportError());
708-
}
689+
this.transport.on("connected", () => this.onTransportConnected());
690+
this.transport.on("message", (message: string) => this.onTransportReceiveMsg(message));
691+
this.transport.on("transportError", () => this.onTransportError());
709692
}
710693

711694
/**
@@ -876,6 +859,8 @@ export class UA extends EventEmitter {
876859
transportConstructor: WebTransport,
877860
transportOptions: {},
878861

862+
usePreloadedRoute: false,
863+
879864
// string to be inserted into User-Agent request header
880865
userAgentString: SIPConstants.USER_AGENT,
881866

@@ -1236,6 +1221,12 @@ export class UA extends EventEmitter {
12361221
}
12371222
},
12381223

1224+
usePreloadedRoute: (usePreloadedRoute: boolean): boolean | undefined => {
1225+
if (typeof usePreloadedRoute === "boolean") {
1226+
return usePreloadedRoute;
1227+
}
1228+
},
1229+
12391230
userAgentString: (userAgentString: string): string | undefined => {
12401231
if (typeof userAgentString === "string") {
12411232
return userAgentString;
@@ -1317,7 +1308,10 @@ export function makeUserAgentCoreConfigurationFromUA(ua: UA): UserAgentCoreConfi
13171308
const contact = ua.contact;
13181309
const displayName = ua.configuration.displayName ? ua.configuration.displayName : "";
13191310
const hackViaTcp = ua.configuration.hackViaTcp ? true : false;
1320-
const routeSet = ua.configuration.usePreloadedRoute && ua.transport ? [ua.transport.server.sipUri] : [];
1311+
const routeSet =
1312+
ua.configuration.usePreloadedRoute && ua.transport.server && ua.transport.server.sipUri ?
1313+
[ua.transport.server.sipUri] :
1314+
[];
13211315
const sipjsId = ua.configuration.sipjsId || Utils.createRandomToken(5);
13221316

13231317
let supportedOptionTags: Array<string> = [];

0 commit comments

Comments
 (0)