Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function uuid() {
const buf = randomBytes(16);
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
buf[6] = (buf[6] & 0x0f) | 0x40;
buf[8] = (buf[8] & 0x3f) | 0x80;
let i = 0;
return [
bth[buf[i++]],
bth[buf[i++]],
bth[buf[i++]],
bth[buf[i++]],
'-',
bth[buf[i++]],
bth[buf[i++]],
'-',
bth[buf[i++]],
return this.disconnect();
}
}
this.client.emit('stream:data', stanzaObj, name);
});
this.parser.on('error', (err: any) => {
const streamError = {
condition: StreamErrorCondition.InvalidXML
};
this.client.emit('stream:error', streamError, err);
this.send(this.stanzas.export('error', streamError)!.toString());
return this.disconnect();
});
this.conn = new WebSocket(opts.url, 'xmpp');
this.conn.onerror = (e: any) => {
if (e.preventDefault) {
e.preventDefault();
}
console.error(e);
};
this.conn.onclose = (e: any) => {
this.client.emit('disconnected', e);
};
this.conn.onopen = () => {
this.sm.started = false;
this.client.emit('connected');
this.send(this.startHeader());
};
this.conn.onmessage = wsMsg => {
const data = Buffer.from(wsMsg.data as string, 'utf8').toString();
export function H(text: Buffer, alg: string): Buffer {
return Hashes.createHash(alg)
.update(text)
.digest();
}
.digest();
const ha1 = Hashes.createHash('md5')
.update(base)
.update(':')
.update(this.nonce!)
.update(':')
.update(cnonce);
if (credentials.authzid) {
ha1.update(':').update(credentials.authzid);
}
const dha1 = ha1.digest('hex');
const ha2 = Hashes.createHash('md5')
.update('AUTHENTICATE:')
.update(uri);
const dha2 = ha2.digest('hex');
const digest = Hashes.createHash('md5')
.update(dha1)
.update(':')
.update(this.nonce!)
.update(':')
.update(nc)
.update(':')
.update(cnonce)
.update(':')
.update(qop)
.update(':')
.update(dha2)
.digest('hex');
str += ',response=' + digest;
if (this.charset === 'utf-8') {
str += ',charset=utf-8';
}
constructor(opts = {}) {
super();
this.config = {
chunkSize: 16384,
hash: 'sha-1',
...opts
};
this.file = undefined;
this.channel = undefined;
this.hash = Hashes.createHash(this.config.hash);
}
if (!identities || !features || !extensions) {
return null;
}
for (const id of identities) {
append(id);
}
for (const feature of features) {
append(feature);
}
for (const form of extensions) {
append(form);
}
return Hashes.createHash(hashName)
.update(Buffer.concat(S))
.digest('base64');
}
export function createClientNonce(length: number = 32): string {
return Hashes.randomBytes(length).toString('hex');
}
ssl: true,
xrd: true,
...opts
};
const scheme = config.ssl ? 'https://' : 'http://';
return promiseAny([
fetch(`${scheme}${config.host}/.well-known/host-meta.json`).then(async res => {
if (!res.ok) {
throw new Error('could-not-fetch-json');
}
return res.json();
}),
fetch(`${scheme}${config.host}/.well-known/host-meta`).then(async res => {
if (!res.ok) {
throw new Error('could-not-fetch-xml');
}
const data = await res.text();
const xml = JXT.parse(data);
if (xml) {
return registry.import(xml);
}
})
]);
}
) {
if (typeof opts === 'string') {
opts = { host: opts };
}
const config = {
json: true,
ssl: true,
xrd: true,
...opts
};
const scheme = config.ssl ? 'https://' : 'http://';
return promiseAny([
fetch(`${scheme}${config.host}/.well-known/host-meta.json`).then(async res => {
if (!res.ok) {
throw new Error('could-not-fetch-json');
}
return res.json();
}),
fetch(`${scheme}${config.host}/.well-known/host-meta`).then(async res => {
if (!res.ok) {
throw new Error('could-not-fetch-xml');
}
const data = await res.text();
const xml = JXT.parse(data);
if (xml) {
return registry.import(xml);
}
async function retryRequest(
url: string,
opts: RequestInit,
timeout: number,
allowedRetries: number = 5
): Promise {
let attempt = 0;
while (attempt <= allowedRetries) {
try {
const resp = await timeoutPromise(fetch(url, opts), timeout * 1000, () => {
return new Error('Request timed out');
});
if (!resp.ok) {
throw new Error('HTTP Status Error: ' + resp.status);
}
return resp.text();
} catch (err) {
attempt += 1;
if (attempt > allowedRetries) {
throw err;
}
}
await sleep(Math.pow(attempt, 2) * 1000);
}