in digital-shadow/demo-car/js/aws-iot/aws-iot.js [9884:10024]
function connect(opts) {
var opts = opts || {}
, protocolId = opts.protocolId || 'MQTT'
, protocolVersion = opts.protocolVersion || 4
, will = opts.will
, clean = opts.clean
, keepalive = opts.keepalive || 0
, clientId = opts.clientId || ""
, username = opts.username
, password = opts.password
if (clean === undefined) {
clean = true
}
var length = 0
if (!protocolId ||
(typeof protocolId !== "string" && !Buffer.isBuffer(protocolId))) {
throw new Error('Invalid protocol id')
} else {
length += protocolId.length + 2
}
if (!protocolVersion ||
'number' !== typeof protocolVersion ||
protocolVersion > 255 ||
protocolVersion < 0) {
throw new Error('Invalid protocol version')
} else {
length += 1
}
if ((typeof clientId === "string" || Buffer.isBuffer(clientId)) &&
(clientId || protocolVersion == 4) &&
(clientId || clean)) {
length += clientId.length + 2
} else {
if(protocolVersion < 4) {
throw new Error('clientId must be supplied before 3.1.1');
}
if(clean == 0) {
throw new Error('clientId must be given if cleanSession set to 0');
}
}
if ('number' !== typeof keepalive ||
keepalive < 0 ||
keepalive > 65535) {
throw new Error('Invalid keepalive')
} else {
length += 2
}
length += 1
if (will) {
if ('object' !== typeof will) {
throw new Error('Invalid will')
}
if (!will.topic || 'string' !== typeof will.topic) {
throw new Error('Invalid will topic')
} else {
length += Buffer.byteLength(will.topic) + 2
}
if (will.payload && will.payload) {
if (will.payload.length >= 0) {
if ('string' === typeof will.payload) {
length += Buffer.byteLength(will.payload) + 2
} else {
length += will.payload.length + 2
}
} else {
throw new Error('Invalid will payload')
}
} else {
length += 2
}
}
if (username) {
if (username.length) {
length += Buffer.byteLength(username) + 2
} else {
throw new Error('Invalid username')
}
}
if (password) {
if (password.length) {
length += byteLength(password) + 2
} else {
throw new Error('Invalid password')
}
}
var buffer = new Buffer(1 + calcLengthLength(length) + length)
, pos = 0
buffer.writeUInt8(protocol.codes['connect'] << protocol.CMD_SHIFT, pos++, true)
pos += writeLength(buffer, pos, length)
pos += writeStringOrBuffer(buffer, pos, protocolId)
buffer.writeUInt8(protocolVersion, pos++, true)
var flags = 0
flags |= username ? protocol.USERNAME_MASK : 0
flags |= password ? protocol.PASSWORD_MASK : 0
flags |= (will && will.retain) ? protocol.WILL_RETAIN_MASK : 0
flags |= (will && will.qos) ?
will.qos << protocol.WILL_QOS_SHIFT : 0
flags |= will ? protocol.WILL_FLAG_MASK : 0
flags |= clean ? protocol.CLEAN_SESSION_MASK : 0
buffer.writeUInt8(flags, pos++, true)
pos += writeNumber(buffer, pos, keepalive)
pos += writeStringOrBuffer(buffer, pos, clientId)
if (will) {
pos += writeString(buffer, pos, will.topic)
pos += writeStringOrBuffer(buffer, pos, will.payload)
}
if (username)
pos += writeStringOrBuffer(buffer, pos, username)
if (password)
pos += writeStringOrBuffer(buffer, pos, password)
return buffer
}