Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async close(timeout = this.timeout) {
const p = Promise.all([
promise(this.parser, 'end', 'error', timeout),
this.write(this.footer(this.footerElement())),
])
if (this.parser && this.socket) this._status('closing')
const [el] = await p
this.root = null
return el
// The 'close' status is set by the parser 'end' listener
}
async disconnect(timeout = this.timeout) {
if (this.socket) this._status('disconnecting')
this.socket.end()
// The 'disconnect' status is set by the socket 'close' listener
await promise(this.socket, 'close', 'error', timeout)
}
async _onSeeOtherHost(error) {
const {protocol} = parseService(this.options.service)
const host = error.element.getChildText('see-other-host')
const {port} = parseHost(host)
let service
if (port) {
service = `${protocol || 'xmpp:'}//${host}`
} else {
service = (protocol ? `${protocol}//` : '') + host
}
try {
await promise(this, 'disconnect')
const {domain, lang} = this.options
await this.connect(service)
await this.open({domain, lang})
} catch (err) {
this.emit('error', err)
}
}
if (typeof options === 'string') {
options = {domain: options}
}
const {domain, lang, timeout = this.timeout} = options
const headerElement = this.headerElement()
headerElement.attrs.to = domain
headerElement.attrs['xml:lang'] = lang
this.root = headerElement
this._attachParser(new this.Parser())
await this.write(this.header(headerElement))
return promise(this, 'open', 'error', timeout)
}
async request(stanza, timeout = 30 * 1000) {
if (!stanza.attrs.id) {
stanza.attrs.id = xid()
}
const deferred = new Deferred()
this.handlers.set(stanza.attrs.id, deferred)
try {
await this.entity.send(stanza)
await timeoutPromise(deferred.promise, timeout)
} catch (err) {
this.handlers.delete(stanza.attrs.id)
throw err
}
return deferred.promise
}
async request(stanza, timeout = 30 * 1000) {
if (!stanza.attrs.id) {
stanza.attrs.id = xid()
}
const deferred = new Deferred()
this.handlers.set(stanza.attrs.id, deferred)
try {
await this.entity.send(stanza)
await timeoutPromise(deferred.promise, timeout)
} catch (err) {
this.handlers.delete(stanza.attrs.id)
throw err
}
return deferred.promise
}