|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +var tslib_1 = require("tslib"); |
| 4 | +var events_1 = require("events"); |
| 5 | +var Constants_1 = require("./Constants"); |
| 6 | +var core_1 = require("./core"); |
| 7 | +var Enums_1 = require("./Enums"); |
| 8 | +var Utils_1 = require("./Utils"); |
| 9 | +var ClientContext = /** @class */ (function (_super) { |
| 10 | + tslib_1.__extends(ClientContext, _super); |
| 11 | + function ClientContext(ua, method, target, options) { |
| 12 | + var _this = _super.call(this) || this; |
| 13 | + _this.data = {}; |
| 14 | + ClientContext.initializer(_this, ua, method, target, options); |
| 15 | + return _this; |
| 16 | + } |
| 17 | + ClientContext.initializer = function (objToConstruct, ua, method, originalTarget, options) { |
| 18 | + objToConstruct.type = Enums_1.TypeStrings.ClientContext; |
| 19 | + // Validate arguments |
| 20 | + if (originalTarget === undefined) { |
| 21 | + throw new TypeError("Not enough arguments"); |
| 22 | + } |
| 23 | + objToConstruct.ua = ua; |
| 24 | + objToConstruct.logger = ua.getLogger("sip.clientcontext"); |
| 25 | + objToConstruct.method = method; |
| 26 | + var target = ua.normalizeTarget(originalTarget); |
| 27 | + if (!target) { |
| 28 | + throw new TypeError("Invalid target: " + originalTarget); |
| 29 | + } |
| 30 | + var fromURI = ua.userAgentCore.configuration.aor; |
| 31 | + if (options && options.params && options.params.fromUri) { |
| 32 | + fromURI = |
| 33 | + (typeof options.params.fromUri === "string") ? |
| 34 | + core_1.Grammar.URIParse(options.params.fromUri) : |
| 35 | + options.params.fromUri; |
| 36 | + if (!fromURI) { |
| 37 | + throw new TypeError("Invalid from URI: " + options.params.fromUri); |
| 38 | + } |
| 39 | + } |
| 40 | + var toURI = target; |
| 41 | + if (options && options.params && options.params.toUri) { |
| 42 | + toURI = |
| 43 | + (typeof options.params.toUri === "string") ? |
| 44 | + core_1.Grammar.URIParse(options.params.toUri) : |
| 45 | + options.params.toUri; |
| 46 | + if (!toURI) { |
| 47 | + throw new TypeError("Invalid to URI: " + options.params.toUri); |
| 48 | + } |
| 49 | + } |
| 50 | + /* Options |
| 51 | + * - extraHeaders |
| 52 | + * - params |
| 53 | + * - contentType |
| 54 | + * - body |
| 55 | + */ |
| 56 | + options = Object.create(options || Object.prototype); |
| 57 | + options = options || {}; |
| 58 | + var extraHeaders = (options.extraHeaders || []).slice(); |
| 59 | + var params = options.params || {}; |
| 60 | + var bodyObj; |
| 61 | + if (options.body) { |
| 62 | + bodyObj = { |
| 63 | + body: options.body, |
| 64 | + contentType: options.contentType ? options.contentType : "application/sdp" |
| 65 | + }; |
| 66 | + objToConstruct.body = bodyObj; |
| 67 | + } |
| 68 | + var body; |
| 69 | + if (bodyObj) { |
| 70 | + body = Utils_1.Utils.fromBodyObj(bodyObj); |
| 71 | + } |
| 72 | + // Build the request |
| 73 | + objToConstruct.request = ua.userAgentCore.makeOutgoingRequestMessage(method, target, fromURI, toURI, params, extraHeaders, body); |
| 74 | + /* Set other properties from the request */ |
| 75 | + if (objToConstruct.request.from) { |
| 76 | + objToConstruct.localIdentity = objToConstruct.request.from; |
| 77 | + } |
| 78 | + if (objToConstruct.request.to) { |
| 79 | + objToConstruct.remoteIdentity = objToConstruct.request.to; |
| 80 | + } |
| 81 | + }; |
| 82 | + ClientContext.prototype.send = function () { |
| 83 | + var _this = this; |
| 84 | + this.ua.userAgentCore.request(this.request, { |
| 85 | + onAccept: function (response) { return _this.receiveResponse(response.message); }, |
| 86 | + onProgress: function (response) { return _this.receiveResponse(response.message); }, |
| 87 | + onRedirect: function (response) { return _this.receiveResponse(response.message); }, |
| 88 | + onReject: function (response) { return _this.receiveResponse(response.message); }, |
| 89 | + onTrying: function (response) { return _this.receiveResponse(response.message); } |
| 90 | + }); |
| 91 | + return this; |
| 92 | + }; |
| 93 | + ClientContext.prototype.receiveResponse = function (response) { |
| 94 | + var statusCode = response.statusCode || 0; |
| 95 | + var cause = Utils_1.Utils.getReasonPhrase(statusCode); |
| 96 | + switch (true) { |
| 97 | + case /^1[0-9]{2}$/.test(statusCode.toString()): |
| 98 | + this.emit("progress", response, cause); |
| 99 | + break; |
| 100 | + case /^2[0-9]{2}$/.test(statusCode.toString()): |
| 101 | + if (this.ua.applicants[this.toString()]) { |
| 102 | + delete this.ua.applicants[this.toString()]; |
| 103 | + } |
| 104 | + this.emit("accepted", response, cause); |
| 105 | + break; |
| 106 | + default: |
| 107 | + if (this.ua.applicants[this.toString()]) { |
| 108 | + delete this.ua.applicants[this.toString()]; |
| 109 | + } |
| 110 | + this.emit("rejected", response, cause); |
| 111 | + this.emit("failed", response, cause); |
| 112 | + break; |
| 113 | + } |
| 114 | + }; |
| 115 | + ClientContext.prototype.onRequestTimeout = function () { |
| 116 | + this.emit("failed", undefined, Constants_1.C.causes.REQUEST_TIMEOUT); |
| 117 | + }; |
| 118 | + ClientContext.prototype.onTransportError = function () { |
| 119 | + this.emit("failed", undefined, Constants_1.C.causes.CONNECTION_ERROR); |
| 120 | + }; |
| 121 | + return ClientContext; |
| 122 | +}(events_1.EventEmitter)); |
| 123 | +exports.ClientContext = ClientContext; |
0 commit comments