Skip to content
Permalink
Browse files

Backport v12 Utils

  • Loading branch information...
legastero committed Apr 25, 2019
1 parent f70992e commit b37f6809254f8ef7e150e6af7e8e04b8c65b0256
Showing with 103 additions and 84 deletions.
  1. +1 −2 package.json
  2. +0 −1 rollup.config.js
  3. +71 −0 src/Utils.ts
  4. +9 −24 src/client.js
  5. +3 −4 src/jingle/Session.js
  6. +2 −1 src/jingle/SessionManager.js
  7. +6 −5 src/plugins/disco.js
  8. +1 −14 src/plugins/keepalive.js
  9. +8 −22 src/plugins/mam.js
  10. +2 −11 src/transports/bosh.js
@@ -32,13 +32,12 @@
"async": "^2.6.2",
"async-es": "^2.6.2",
"cross-fetch": "^3.0.2",
"jxt": "^4.0.0",
"jxt": "^4.0.1",
"punycode": "^2.1.1",
"react-native-randombytes": "^3.5.2",
"readable-stream": "^2.3.6",
"sdp": "^2.9.0",
"tslib": "^1.9.3",
"uuid": "^3.3.2",
"ws": "^6.1.2"
},
"devDependencies": {
@@ -13,7 +13,6 @@ export default {
'readable-stream',
'stream',
'tslib',
'uuid',
'ws'
],
input: 'dist/es/index.js',
@@ -0,0 +1,71 @@
// tslint:disable no-bitwise

import { randomBytes } from './lib/crypto';

const bth: string[] = [];
for (let i = 0; i < 256; ++i) {
bth[i] = (i + 0x100).toString(16).substr(1);
}

export async function timeoutPromise<T>(
target: Promise<T>,
delay: number,
rejectValue: () => any = () => undefined
) {
let timeoutRef: any;
const result = await Promise.race([
target,
new Promise<T>((resolve, reject) => {
timeoutRef = setTimeout(() => reject(rejectValue()), delay);
})
]);
if (timeoutRef) {
clearTimeout(timeoutRef);
}
return result;
}

export async function sleep(time: number): Promise<void> {
return new Promise<void>(resolve => {
setTimeout(() => resolve(), time);
});
}

export function octetCompare(str1: string | Buffer, str2: string | Buffer): number {
const b1 = typeof str1 === 'string' ? Buffer.from(str1, 'utf8') : str1;
const b2 = typeof str2 === 'string' ? Buffer.from(str2, 'utf8') : str2;

return b1.compare(b2);
}

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++]],
bth[buf[i++]],
'-',
bth[buf[i++]],
bth[buf[i++]],
'-',
bth[buf[i++]],
bth[buf[i++]],
bth[buf[i++]],
bth[buf[i++]],
bth[buf[i++]],
bth[buf[i]]
].join('');
}
@@ -1,10 +1,10 @@
import * as uuid from 'uuid';
import jxt from 'jxt';

import * as SASL from './sasl';
import StreamManagement from './sm';
import Protocol from './protocol';
import WildEmitter from './lib/WildEmitter';
import { uuid, timeoutPromise } from './Utils';

import HostMeta from './plugins/hostmeta';
import Features from './plugins/features';
@@ -27,27 +27,6 @@ const SASL_MECHS = {
'x-oauth2': SASL.XOauth2
};

function timeoutRequest(targetPromise, id, delay) {
let timeoutRef;
return Promise.race([
targetPromise,
new Promise(function(resolve, reject) {
timeoutRef = setTimeout(function() {
reject({
error: {
condition: 'timeout'
},
id: id,
type: 'error'
});
}, delay);
})
]).then(function(result) {
clearTimeout(timeoutRef);
return result;
});
}

export default class Client extends WildEmitter {
constructor(opts) {
super();
@@ -250,7 +229,7 @@ export default class Client extends WildEmitter {
}

nextId() {
return uuid.v4();
return uuid();
}

_getConfiguredCredentials() {
@@ -419,7 +398,13 @@ export default class Client extends WildEmitter {
this.on(respEvent, 'session', handler);
});
this.send(iq);
return timeoutRequest(request, data.id, (this.config.timeout || 15) * 1000).then(
return timeoutPromise(request, (this.config.timeout || 15) * 1000, () => ({
error: {
condition: 'timeout'
},
id,

This comment has been minimized.

Copy link
@kai670

kai670 Apr 29, 2019

Contributor

I'm seeing this error after updating to latest version in a project using stanza:

Error: Uncaught ReferenceError: id is not defined (webpack:///node_modules/stanza/browser-module.js:6986 <- e2e/chat/chat.spec.js:87688)

I think this id parameter introduced in this commit is the caused. I guess you forgot the data. part.

This comment has been minimized.

Copy link
@legastero

legastero Apr 29, 2019

Author Owner

Good catch, thanks! Fixed in 11.3.1.

type: 'error'
})).then(
function(result) {
if (cb) {
cb(null, result);
@@ -1,13 +1,12 @@
import * as uuid from 'uuid';

import { uuid } from '../Utils';
import * as async from '../lib/async';
import WildEmitter from '../lib/WildEmitter';

export default class JingleSession extends WildEmitter {
constructor(opts) {
super();

this.sid = opts.sid || uuid.v4();
this.sid = opts.sid || uuid();
this.peerID = opts.peerID;
this.role = opts.initiator ? 'initiator' : 'responder';
this.parent = opts.parent;
@@ -153,7 +152,7 @@ export default class JingleSession extends WildEmitter {
}

this.emit('send', {
id: uuid.v4(),
id: uuid(),
jingle: data,
to: this.peerID,
type: 'set'
@@ -1,4 +1,5 @@
import WildEmitter from '../lib/WildEmitter';
import { octetCompare } from '../Utils';

import FileSession from './FileTransferSession';
import MediaSession from './MediaSession';
@@ -295,7 +296,7 @@ export default class SessionManager extends WildEmitter {
if (
sess &&
sess.state === 'pending' &&
sess.sid > sid &&
octetCompare(sess.sid, sid) > 0 &&
this.performTieBreak(sess, req)
) {
this._log('info', 'Tie break session-initiate');
@@ -1,6 +1,7 @@
import * as hashes from '../lib/crypto';
import { Namespaces } from '../protocol';
import { JID } from '../protocol/jid';
import { octetCompare } from '../Utils';

function generateVerString(info, hash) {
let S = '';
@@ -26,8 +27,8 @@ function generateVerString(info, hash) {
const idLen = identities.length;
const featureLen = features.length;

identities = [...new Set(identities)].sort();
features = [...new Set(features)].sort();
identities = [...new Set(identities)].sort(octetCompare);
features = [...new Set(features)].sort(octetCompare);

if (featureLen !== features.length || idLen !== identities.length) {
return false;
@@ -56,7 +57,7 @@ function generateVerString(info, hash) {
return false;
}

formOrder.sort();
formOrder.sort(octetCompare);

for (const name of formOrder) {
const ext = formTypes[name];
@@ -72,12 +73,12 @@ function generateVerString(info, hash) {
if (typeof values !== 'object') {
values = values.split('\n');
}
fields[fieldName] = values.sort();
fields[fieldName] = values.sort(octetCompare);
fieldOrder.push(fieldName);
}
}

fieldOrder.sort();
fieldOrder.sort(octetCompare);

for (const fieldName of fieldOrder) {
S += '<' + fieldName;
@@ -1,17 +1,4 @@
function timeoutPromise(targetPromise, delay) {
let timeoutRef;
return Promise.race([
targetPromise,
new Promise(function(resolve, reject) {
timeoutRef = setTimeout(function() {
reject();
}, delay);
})
]).then(function(result) {
clearTimeout(timeoutRef);
return result;
});
}
import { timeoutPromise } from '../Utils';

function checkConnection(client, timeout) {
return timeoutPromise(
@@ -1,27 +1,7 @@
import { timeoutPromise } from '../Utils';
import { Namespaces } from '../protocol';
import { JID } from '../protocol/jid';

function timeoutPromise(targetPromise, queryid, delay) {
let timeoutRef;
return Promise.race([
targetPromise,
new Promise(function(resolve, reject) {
timeoutRef = setTimeout(function() {
reject({
error: {
condition: 'timeout'
},
id: queryid,
type: 'error'
});
}, delay);
})
]).then(function(result) {
clearTimeout(timeoutRef);
return result;
});
}

export default function(client) {
client.disco.addFeature(Namespaces.MAM_2);

@@ -107,7 +87,13 @@ export default function(client) {
type: 'set'
});

return timeoutPromise(mamQuery, queryid, this.config.timeout * 1000 || 15000)
return timeoutPromise(mamQuery, this.config.timeout * 1000 || 15000, () => ({
error: {
condition: 'timeout'
},
id: queryid,
type: 'error'
}))
.then(mamRes => {
mamRes.mamResult.items = results;
this.off('mam:item:' + queryid);
@@ -1,16 +1,7 @@
import fetch from '../lib/fetch';
import { Namespaces } from '../protocol';
import WildEmitter from '../lib/WildEmitter';

function timeoutPromise(targetPromise, delay) {
return new Promise((resolve, reject) => {
const t = setTimeout(reject, delay, new Error('Request timed out'));
targetPromise.then(result => {
clearTimeout(t);
resolve(result);
}, reject);
});
}
import { Namespaces } from '../protocol';
import { timeoutPromise } from '../Utils';

async function retryRequest(url, opts, timeout, allowedRetries) {
try {

0 comments on commit b37f680

Please sign in to comment.
You can’t perform that action at this time.