forked from senchalabs/connect
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrequest.js
264 lines (236 loc) · 8.21 KB
/
request.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const Emitter = require('events').EventEmitter ;
const sip = require('drachtio-sip') ;
const delegate = require('delegates') ;
const assert = require('assert') ;
const noop = require('node-noop').noop;
const debug = require('debug')('drachtio-agent:request');
class Request extends Emitter {
constructor(msg, meta) {
super() ;
if (msg) {
assert(msg instanceof sip.SipMessage) ;
this.msg = msg ;
this.meta = meta ;
}
}
get res() {
return this._res ;
}
set res(res) {
this._res = res ;
return this ;
}
get isNewInvite() {
const to = this.getParsed('to') ;
return this.method === 'INVITE' && !('tag' in to.params) ;
}
get url() {
return this.uri ;
}
set agent(agent) {
this._agent = agent ;
}
get agent() {
return this._agent ;
}
set meta(meta) {
debug(`Request#set meta ${JSON.stringify(meta)}`);
this.source = meta.source ;
this.source_address = meta.address ;
this.source_port = meta.port ? parseInt(meta.port) : 5060 ;
this.protocol = meta.protocol ;
this.stackTime = meta.time ;
this.stackTxnId = meta.transactionId ;
this.stackDialogId = meta.dialogId ;
}
get meta() {
return {
source: this.source,
source_address: this.source_address,
source_port: this.source_port,
protocol: this.protocol,
time: this.stackTime,
transactionId: this.stackTxnId,
dialogId: this.stackDialogId
} ;
}
/**
* Cancel a request that was sent by the application
* @param {Request~cancelCallback} callback - invoked with cancel operation completes
*/
cancel(callback) {
if (!this._agent || this.source !== 'application') {
throw new Error('Request#cancel can only be used for uac Request') ;
}
this._agent.request({uri: this.uri, method: 'CANCEL', stackTxnId: this.stackTxnId}, callback) ;
}
/**
* This callback is invoked when the application has sent a CANCEL for a request.
* @callback Request~cancelCallback
* @param {Error} err - if an error occurred while attempting to send the cancel
* @param {Request} req - the cancel request that was sent
*/
/**
* Proxy an incoming request
* @param {Request~proxyOptions} opts - options governing the proxy operation
* @param {Request~proxyCallback} callback - callback invoked when proxy operation completes
*/
proxy(opts, callback) {
if (this.source !== 'network') {
throw new Error('Request#proxy can only be used for incoming requests') ;
}
opts = opts || {} ;
callback = callback || noop ;
//TODO: throw error if req.res.send has already been called (i.e. can't start off as UAS and then become a proxy)
const destination = opts.destination || this.uri ;
if (typeof destination === 'string') { opts.destination = [destination] ; }
Object.assign(opts, {
stackTxnId: this.stackTxnId,
remainInDialog: opts.remainInDialog || opts.path || opts.recordRoute || false,
provisionalTimeout: opts.provisionalTimeout || '',
finalTimeout: opts.finalTimeout || '',
followRedirects: opts.followRedirects || false,
simultaneous: opts.forking === 'simultaneous',
fullResponse: callback.length === 2
}) ;
//normalize sip uris
opts.destination.forEach((value, index, array) => {
const token = value.split(':') ;
if (token[0] !== 'sip' && token[0] !== 'tel') {
array[index] = 'sip:' + value ;
}
}) ;
const result = {
connected: false,
responses: []
} ;
this._agent.proxy(this, opts, (token, rawMsg, meta) => {
if ('NOK' === token[0]) {
return callback(token[1]) ;
}
if ('done' === token[1]) {
result.connected = (200 === result.finalStatus) ;
return callback(null, result) ;
}
else {
//add a new response to the array
const address = meta.address ;
const port = +meta.port;
const msg = new sip.SipMessage(rawMsg) ;
const obj = {
time: meta.time,
status: msg.status,
msg: msg
} ;
let len = result.responses.length ;
if (len === 0 || address !== result.responses[len - 1].address || port === result.responses[len - 1].port) {
result.responses.push({
address: address,
port: port,
msgs:[]
}) ;
len++ ;
}
result.responses[len - 1].msgs.push(obj) ;
result.finalStatus = msg.status ;
result.finalResponse = obj ;
}
}) ;
}
/**
* Options governing a proxy operation
* @typedef {Object} Request~proxyOptions
* @property {string|Array} destination - an ordered list of one or more SIP URIs to proxy the request to
* @property {boolean} [remainInDialog=false] - if true add a Record-Route header and emain in the SIP dialog
* after the INVITE transaction.
* @property {boolean} [followRedirects=false] - if true respond to 3XX redirect responses by generating
* a new INVITE to the SIP URI in the Contact header of the response
* @property {string} [forking=sequential] - 'simultaneous' or 'sequential'; dicates whether the proxy waits
* for a failure response from one target before trying the next, or forks the request to all targets simultaneously
* @property {string} [provisionalTimeout] - amount of time to wait for a 100 Trying response from a target before
* trying the next target; valid syntax is '2s' or '1500ms' for example
* @property {string} [finalTimeout] - amount of time to wait for a final response from a target before trying
* the next target; syntax is as described above for provisionalTimeout
*/
/**
* This callback is invoked when proxy operation has completed.
* @callback Request~proxyCallback
* @param {Error} err - if an error occurred while attempting to proxy the request
* @param {Request~proxyResults} results - results summarizing the proxy operation
*/
// for compatibility with passport
logIn(user, options, done) {
if (typeof options === 'function') {
done = options;
options = {};
}
options = options || {};
done = done || noop ;
let property = 'user';
if (this._passport && this._passport.instance) {
property = this._passport.instance._userProperty || 'user';
}
const session = (options.session === undefined) ? true : options.session;
this[property] = user;
if (session) {
if (!this._passport) { throw new Error('passport.initialize() middleware not in use'); }
if (typeof done !== 'function') { throw new Error('req#login requires a callback function'); }
this._passport.instance.serializeUser(user, this, (err, obj) => {
if (err) { this[property] = null; return done(err); }
if (!this._passport.session) {
this._passport.session = {};
}
this._passport.session.user = obj;
this.session = this.session || {};
this.session[this._passport.instance._key] = this._passport.session;
done();
});
} else {
done();
}
}
// Terminate an existing login session.
logOut() {
let property = 'user';
if (this._passport && this._passport.instance) {
property = this._passport.instance._userProperty || 'user';
}
this[property] = null;
if (this._passport && this._passport.session) {
delete this._passport.session.user;
}
}
// Test if request is authenticated.
isAuthenticated() {
let property = 'user';
if (this._passport && this._passport.instance) {
property = this._passport.instance._userProperty || 'user';
}
return (this[property]) ? true : false;
}
// Test if request is unauthenticated.
isUnauthenticated() {
return !this.isAuthenticated();
}
}
module.exports = Request ;
delegate(Request.prototype, 'msg')
.method('get')
.method('has')
.method('getParsedHeader')
.method('set')
.access('method')
.access('uri')
.access('headers')
.access('body')
.access('payload')
.getter('type')
.getter('raw')
.getter('callingNumber')
.getter('calledNumber')
.getter('canFormDialog') ;
/**
* response event triggered when a Request sent by the application receives a response from the network
* @event Endpoint#destroy
* @param {Response} res - SIP response received as a result of sending a SIP request
*/