-
Notifications
You must be signed in to change notification settings - Fork 476
/
connect.js
189 lines (160 loc) · 5.29 KB
/
connect.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
//
//
//
// General-purpose API for glueing everything together.
'use strict';
var URL = require('url-parse');
var QS = require('querystring');
var Connection = require('./connection').Connection;
var fmt = require('util').format;
var credentials = require('./credentials');
function copyInto(obj, target) {
var keys = Object.keys(obj);
var i = keys.length;
while (i--) {
var k = keys[i];
target[k] = obj[k];
}
return target;
}
// Adapted from util._extend, which is too fringe to use.
function clone(obj) {
return copyInto(obj, {});
}
var CLIENT_PROPERTIES = {
"product": "amqplib",
"version": require('../package.json').version,
"platform": fmt('Node.JS %s', process.version),
"information": "http://squaremo.github.io/amqp.node",
"capabilities": {
"publisher_confirms": true,
"exchange_exchange_bindings": true,
"basic.nack": true,
"consumer_cancel_notify": true,
"connection.blocked": true,
"authentication_failure_close": true
}
};
// Construct the main frames used in the opening handshake
function openFrames(vhost, query, credentials, extraClientProperties) {
if (!vhost)
vhost = '/';
else
vhost = QS.unescape(vhost);
var query = query || {};
function intOrDefault(val, def) {
return (val === undefined) ? def : parseInt(val);
}
var clientProperties = Object.create(CLIENT_PROPERTIES);
return {
// start-ok
'clientProperties': copyInto(extraClientProperties, clientProperties),
'mechanism': credentials.mechanism,
'response': credentials.response(),
'locale': query.locale || 'en_US',
// tune-ok
'channelMax': intOrDefault(query.channelMax, 0),
'frameMax': intOrDefault(query.frameMax, 0x1000),
'heartbeat': intOrDefault(query.heartbeat, 0),
// open
'virtualHost': vhost,
'capabilities': '',
'insist': 0
};
}
// Decide on credentials based on what we're supplied.
function credentialsFromUrl(parts) {
var user = 'guest', passwd = 'guest';
if (parts.username != '' || parts.password != '') {
user = (parts.username) ? unescape(parts.username) : '';
passwd = (parts.password) ? unescape(parts.password) : '';
}
return credentials.plain(user, passwd);
}
function connect(url, socketOptions, openCallback) {
// tls.connect uses `util._extend()` on the options given it, which
// copies only properties mentioned in `Object.keys()`, when
// processing the options. So I have to make copies too, rather
// than using `Object.create()`.
var sockopts = clone(socketOptions || {});
url = url || 'amqp://localhost';
var noDelay = !!sockopts.noDelay;
var timeout = sockopts.timeout;
var keepAlive = !!sockopts.keepAlive;
// 0 is default for node
var keepAliveDelay = sockopts.keepAliveDelay || 0;
var extraClientProperties = sockopts.clientProperties || {};
var protocol, fields;
if (typeof url === 'object') {
protocol = (url.protocol || 'amqp') + ':';
sockopts.host = url.hostname;
sockopts.servername = sockopts.servername || url.hostname;
sockopts.port = url.port || ((protocol === 'amqp:') ? 5672 : 5671);
var user, pass;
// Only default if both are missing, to have the same behaviour as
// the stringly URL.
if (url.username == undefined && url.password == undefined) {
user = 'guest'; pass = 'guest';
} else {
user = url.username || '';
pass = url.password || '';
}
var config = {
locale: url.locale,
channelMax: url.channelMax,
frameMax: url.frameMax,
heartbeat: url.heartbeat,
};
fields = openFrames(url.vhost, config, sockopts.credentials || credentials.plain(user, pass), extraClientProperties);
} else {
var parts = URL(url, true); // yes, parse the query string
protocol = parts.protocol;
sockopts.host = parts.hostname;
sockopts.servername = sockopts.servername || parts.hostname;
sockopts.port = parseInt(parts.port) || ((protocol === 'amqp:') ? 5672 : 5671);
var vhost = parts.pathname ? parts.pathname.substr(1) : null;
fields = openFrames(vhost, parts.query, sockopts.credentials || credentialsFromUrl(parts), extraClientProperties);
}
var sockok = false;
var sock;
function onConnect() {
sockok = true;
sock.setNoDelay(noDelay);
if (keepAlive) sock.setKeepAlive(keepAlive, keepAliveDelay);
var c = new Connection(sock);
c.open(fields, function(err, ok) {
// disable timeout once the connection is open, we don't want
// it fouling things
if (timeout) sock.setTimeout(0);
if (err === null) {
openCallback(null, c);
} else {
// The connection isn't closed by the server on e.g. wrong password
sock.end();
sock.destroy();
openCallback(err);
}
});
}
if (protocol === 'amqp:') {
sock = require('net').connect(sockopts, onConnect);
}
else if (protocol === 'amqps:') {
sock = require('tls').connect(sockopts, onConnect);
}
else {
throw new Error("Expected amqp: or amqps: as the protocol; got " + protocol);
}
if (timeout) {
sock.setTimeout(timeout, function() {
sock.end();
sock.destroy();
openCallback(new Error('connect ETIMEDOUT'));
});
}
sock.once('error', function(err) {
if (!sockok) openCallback(err);
});
}
module.exports.connect = connect;
module.exports.credentialsFromUrl = credentialsFromUrl;