stanza 12.17.2 → 12.17.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Client.js CHANGED
@@ -220,6 +220,7 @@ class Client extends events_1.EventEmitter {
220
220
  });
221
221
  }
222
222
  updateConfig(opts = {}) {
223
+ var _a;
223
224
  const currConfig = this.config || {};
224
225
  this.config = {
225
226
  allowResumption: true,
@@ -229,6 +230,7 @@ class Client extends events_1.EventEmitter {
229
230
  websocket: true
230
231
  },
231
232
  useStreamManagement: true,
233
+ transportPreferenceOrder: ['websocket', 'bosh'],
232
234
  ...currConfig,
233
235
  ...opts
234
236
  };
@@ -240,6 +242,9 @@ class Client extends events_1.EventEmitter {
240
242
  this.config.credentials.password = this.config.password;
241
243
  delete this.config.password;
242
244
  }
245
+ if (!this.config.transportPreferenceOrder) {
246
+ this.config.transportPreferenceOrder = Object.keys((_a = this.config.transports) !== null && _a !== void 0 ? _a : {});
247
+ }
243
248
  }
244
249
  get stream() {
245
250
  return this.transport ? this.transport.stream : undefined;
@@ -270,47 +275,60 @@ class Client extends events_1.EventEmitter {
270
275
  return this._getConfiguredCredentials();
271
276
  }
272
277
  async connect() {
278
+ var _a, _b, _c;
273
279
  this.sessionTerminating = false;
274
280
  this.sessionStarting = true;
275
281
  this.emit('--reset-stream-features');
276
282
  if (this.transport) {
277
283
  this.transport.disconnect(false);
278
284
  }
279
- const transportPref = ['websocket', 'bosh'];
285
+ const transportPref = (_a = this.config.transportPreferenceOrder) !== null && _a !== void 0 ? _a : [];
280
286
  let endpoints;
281
287
  for (const name of transportPref) {
282
- let conf = this.config.transports[name];
283
- if (!conf) {
288
+ const settings = this.config.transports[name];
289
+ if (!settings || !this.transports[name]) {
284
290
  continue;
285
291
  }
286
- if (typeof conf === 'string') {
287
- conf = { url: conf };
292
+ let config = {
293
+ acceptLanguages: this.config.acceptLanguages || [(_b = this.config.lang) !== null && _b !== void 0 ? _b : 'en'],
294
+ jid: this.config.jid,
295
+ lang: (_c = this.config.lang) !== null && _c !== void 0 ? _c : 'en',
296
+ server: this.config.server
297
+ };
298
+ const transport = new this.transports[name](this, this.sm, this.stanzas);
299
+ if (typeof settings === 'string') {
300
+ config.url = settings;
288
301
  }
289
- else if (conf === true) {
290
- if (!endpoints) {
291
- try {
292
- endpoints = await this.discoverBindings(this.config.server);
293
- }
294
- catch (err) {
295
- console.error(err);
302
+ else if (settings == true) {
303
+ if (transport.discoverBindings) {
304
+ const discovered = await transport.discoverBindings(this.config.server);
305
+ if (!discovered) {
296
306
  continue;
297
307
  }
308
+ config = {
309
+ ...config,
310
+ ...discovered
311
+ };
298
312
  }
299
- endpoints[name] = (endpoints[name] || []).filter(url => url.startsWith('wss:') || url.startsWith('https:'));
300
- if (!endpoints[name] || !endpoints[name].length) {
301
- continue;
313
+ else {
314
+ if (!endpoints) {
315
+ try {
316
+ endpoints = await this.discoverBindings(this.config.server);
317
+ }
318
+ catch (err) {
319
+ console.error(err);
320
+ continue;
321
+ }
322
+ }
323
+ endpoints[name] = (endpoints[name] || []).filter(url => url.startsWith('wss:') || url.startsWith('https:'));
324
+ if (!endpoints[name] || !endpoints[name].length) {
325
+ continue;
326
+ }
327
+ config.url = endpoints[name][0];
302
328
  }
303
- conf = { url: endpoints[name][0] };
304
329
  }
305
- this.transport = new this.transports[name](this, this.sm, this.stanzas);
306
- this.transport.connect({
307
- acceptLanguages: this.config.acceptLanguages || ['en'],
308
- jid: this.config.jid,
309
- lang: this.config.lang || 'en',
310
- server: this.config.server,
311
- url: conf.url,
312
- ...conf
313
- });
330
+ this.transport = transport;
331
+ this.transport.connect(config);
314
332
  return;
315
333
  }
316
334
  console.error('No endpoints found for the requested transports.');
package/Constants.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.directionToSenders = exports.sendersToDirection = exports.JINGLE_INFO_RECEIVED_5 = exports.JINGLE_INFO_CHECKSUM_5 = exports.JINGLE_INFO_RINGING = exports.JINGLE_INFO_ACTIVE = exports.JINGLE_INFO_UNHOLD = exports.JINGLE_INFO_HOLD = exports.JINGLE_INFO_UNMUTE = exports.JINGLE_INFO_MUTE = exports.JINGLE_INFO = exports.USER_ACTIVITY_SPECIFIC = exports.USER_ACTIVITY_GENERAL = exports.USER_MOODS = exports.JingleReasonCondition = exports.JingleErrorCondition = exports.JingleAction = exports.JingleContentSenders = exports.JingleApplicationDirection = exports.JingleSessionRole = exports.ChatState = exports.PubsubErrorCondition = exports.MUCStatusCode = exports.MUCRole = exports.MUCAffiliation = exports.DataFormFieldType = exports.DataFormType = exports.RosterSubscription = exports.PresenceShow = exports.IQType = exports.PresenceType = exports.MessageType = exports.StanzaErrorCondition = exports.StreamErrorCondition = exports.SASLFailureCondition = exports.StreamType = exports.VERSION = void 0;
4
4
  const Namespaces_1 = require("./Namespaces");
5
- exports.VERSION = '12.17.2';
5
+ exports.VERSION = '12.17.3';
6
6
  // ====================================================================
7
7
  // Frequently Used Values
8
8
  // ====================================================================
package/index.d.ts CHANGED
@@ -105,6 +105,9 @@ export interface Agent extends StrictEventEmitter<EventEmitter, AgentEvents> {
105
105
  sessionStarting: boolean;
106
106
  sessionStarted: boolean;
107
107
  sessionTerminating: boolean;
108
+ transports: {
109
+ [key: string]: new (client: Agent, sm: StreamManagement, registry: JXT.Registry) => Transport;
110
+ };
108
111
  use(plugin: (agent: Agent, registry: JXT.Registry, config: AgentConfig) => void): void;
109
112
  nextId(): string;
110
113
  updateConfig(opts?: AgentConfig): void;
@@ -177,14 +180,21 @@ export interface AgentConfig {
177
180
  *
178
181
  * If a transport is set to a string, that will be used as the connection URL.
179
182
  *
180
- * If a transport is set to an object, it MUST include a <code>url</code> value for
181
- * the connection URL.
182
- *
183
183
  * @default { websocket: true, bosh: true }
184
184
  */
185
185
  transports?: {
186
186
  [key: string]: boolean | string | Partial<TransportConfig>;
187
187
  };
188
+ /**
189
+ * Transport Preference Order
190
+ *
191
+ * Specify the order in which transports should be tried when connecting.
192
+ *
193
+ * If a configured transport type is not listed, it will be skipped.
194
+ *
195
+ * @default ['websocket', 'bosh']
196
+ */
197
+ transportPreferenceOrder?: string[];
188
198
  /**
189
199
  * Account Password
190
200
  *
@@ -208,17 +218,18 @@ export interface Transport {
208
218
  hasStream?: boolean;
209
219
  stream?: Stream;
210
220
  authenticated?: boolean;
221
+ discoverBindings?(host: string): Promise<Partial<TransportConfig> | null>;
211
222
  connect(opts: TransportConfig): void;
212
223
  disconnect(cleanly?: boolean): void;
213
224
  restart(): void;
214
225
  send(name: string, data?: JXT.JSONData): Promise<void>;
215
226
  }
216
227
  export interface TransportConfig {
217
- lang?: string;
218
- acceptLanguages?: string[];
219
228
  server: string;
220
- url: string;
221
229
  jid: string;
230
+ lang?: string;
231
+ acceptLanguages?: string[];
232
+ url?: string;
222
233
  sid?: string;
223
234
  rid?: number;
224
235
  maxRetries?: number;
package/module.js CHANGED
@@ -4012,7 +4012,8 @@ class Hash extends Transform {
4012
4012
  let error = null;
4013
4013
  try {
4014
4014
  this.update(chunk, encoding);
4015
- } catch (err) {
4015
+ }
4016
+ catch (err) {
4016
4017
  error = err;
4017
4018
  }
4018
4019
  callback(error);
@@ -4021,7 +4022,8 @@ class Hash extends Transform {
4021
4022
  let error = null;
4022
4023
  try {
4023
4024
  this.push(this.digest());
4024
- } catch (err) {
4025
+ }
4026
+ catch (err) {
4025
4027
  error = err;
4026
4028
  }
4027
4029
  callback(error);
@@ -4035,7 +4037,7 @@ class Hash extends Transform {
4035
4037
  const blockSize = this._blockSize;
4036
4038
  const length = data.length;
4037
4039
  let accum = this._len;
4038
- for (let offset = 0; offset < length; ) {
4040
+ for (let offset = 0; offset < length;) {
4039
4041
  const assigned = accum % blockSize;
4040
4042
  const remainder = Math.min(length - offset, blockSize - assigned);
4041
4043
  for (let i = 0; i < remainder; i++) {
@@ -4065,17 +4067,20 @@ class Hash extends Transform {
4065
4067
  if (this._bigEndian) {
4066
4068
  this._block.writeUInt32BE(0, this._blockSize - 8);
4067
4069
  this._block.writeUInt32BE(bits, this._blockSize - 4);
4068
- } else {
4070
+ }
4071
+ else {
4069
4072
  this._block.writeUInt32LE(bits, this._blockSize - 8);
4070
4073
  this._block.writeUInt32LE(0, this._blockSize - 4);
4071
4074
  }
4072
- } else {
4075
+ }
4076
+ else {
4073
4077
  const lowBits = (bits & 0xffffffff) >>> 0;
4074
4078
  const highBits = (bits - lowBits) / 0x100000000;
4075
4079
  if (this._bigEndian) {
4076
4080
  this._block.writeUInt32BE(highBits, this._blockSize - 8);
4077
4081
  this._block.writeUInt32BE(lowBits, this._blockSize - 4);
4078
- } else {
4082
+ }
4083
+ else {
4079
4084
  this._block.writeUInt32LE(lowBits, this._blockSize - 8);
4080
4085
  this._block.writeUInt32LE(highBits, this._blockSize - 4);
4081
4086
  }
@@ -4811,7 +4816,8 @@ function createHash(alg) {
4811
4816
  const HashImp = HASH_IMPLEMENTATIONS.get(alg);
4812
4817
  if (HashImp) {
4813
4818
  return new HashImp();
4814
- } else {
4819
+ }
4820
+ else {
4815
4821
  throw new Error('Unsupported hash algorithm: ' + alg);
4816
4822
  }
4817
4823
  }
@@ -4841,7 +4847,8 @@ class Hmac extends Transform {
4841
4847
  key = createHash(alg)
4842
4848
  .update(key)
4843
4849
  .digest();
4844
- } else if (key.length < blocksize) {
4850
+ }
4851
+ else if (key.length < blocksize) {
4845
4852
  key = Buffer.concat([key, ZEROS], blocksize);
4846
4853
  }
4847
4854
  this._ipad = Buffer.alloc(blocksize);
@@ -4856,9 +4863,11 @@ class Hmac extends Transform {
4856
4863
  let err;
4857
4864
  try {
4858
4865
  this.update(data, enc);
4859
- } catch (e) {
4866
+ }
4867
+ catch (e) {
4860
4868
  err = e;
4861
- } finally {
4869
+ }
4870
+ finally {
4862
4871
  next(err);
4863
4872
  }
4864
4873
  }
@@ -4866,7 +4875,8 @@ class Hmac extends Transform {
4866
4875
  let err;
4867
4876
  try {
4868
4877
  this.push(this._final());
4869
- } catch (e) {
4878
+ }
4879
+ catch (e) {
4870
4880
  err = e;
4871
4881
  }
4872
4882
  done(err);
@@ -4894,7 +4904,8 @@ class Hmac extends Transform {
4894
4904
  let root;
4895
4905
  if (typeof window !== 'undefined') {
4896
4906
  root = window;
4897
- } else if (typeof global !== 'undefined') {
4907
+ }
4908
+ else if (typeof global !== 'undefined') {
4898
4909
  root = global;
4899
4910
  }
4900
4911
  function randomBytes(size) {
@@ -4910,7 +4921,7 @@ function getHashes() {
4910
4921
  function createHmac(alg, key) {
4911
4922
  return new Hmac(alg.toLowerCase(), key);
4912
4923
  }
4913
- const nativeFetch = fetch;
4924
+ const nativeFetch = fetch.bind(window);
4914
4925
  const nativeWS = WebSocket;
4915
4926
  const nativeRTCPeerConnection = root.RTCPeerConnection;
4916
4927
 
@@ -6085,7 +6096,7 @@ function Disco (client) {
6085
6096
  });
6086
6097
  }
6087
6098
 
6088
- const VERSION$1 = '12.17.2';
6099
+ const VERSION$1 = '12.17.3';
6089
6100
  // ====================================================================
6090
6101
  // Frequently Used Values
6091
6102
  // ====================================================================
@@ -9276,7 +9287,7 @@ function MUC (client) {
9276
9287
  }
9277
9288
  client.on('session:started', rejoinRooms);
9278
9289
  client.on('message', msg => {
9279
- if (msg.type === 'groupchat' && msg.hasSubject) {
9290
+ if (msg.type === 'groupchat' && msg.hasSubject && !msg.body) {
9280
9291
  client.emit('muc:topic', {
9281
9292
  from: msg.from,
9282
9293
  room: toBare(msg.from),
@@ -14475,6 +14486,10 @@ class WSConnection extends Duplex {
14475
14486
  this.socket.onclose = () => {
14476
14487
  this.push(null);
14477
14488
  };
14489
+ this.socket.onerror = (err) => {
14490
+ console.error(err);
14491
+ this.push(null);
14492
+ };
14478
14493
  }
14479
14494
  disconnect(clean = true) {
14480
14495
  if (this.socket && !this.closing && this.hasStream && clean) {
@@ -14738,11 +14753,12 @@ class Client extends EventEmitter {
14738
14753
  });
14739
14754
  }
14740
14755
  updateConfig(opts = {}) {
14756
+ var _a;
14741
14757
  const currConfig = this.config || {};
14742
14758
  this.config = Object.assign(Object.assign({ allowResumption: true, jid: '', transports: {
14743
14759
  bosh: true,
14744
14760
  websocket: true
14745
- }, useStreamManagement: true }, currConfig), opts);
14761
+ }, useStreamManagement: true, transportPreferenceOrder: ['websocket', 'bosh'] }, currConfig), opts);
14746
14762
  if (!this.config.server) {
14747
14763
  this.config.server = getDomain(this.config.jid);
14748
14764
  }
@@ -14751,6 +14767,9 @@ class Client extends EventEmitter {
14751
14767
  this.config.credentials.password = this.config.password;
14752
14768
  delete this.config.password;
14753
14769
  }
14770
+ if (!this.config.transportPreferenceOrder) {
14771
+ this.config.transportPreferenceOrder = Object.keys((_a = this.config.transports) !== null && _a !== void 0 ? _a : {});
14772
+ }
14754
14773
  }
14755
14774
  get stream() {
14756
14775
  return this.transport ? this.transport.stream : undefined;
@@ -14783,6 +14802,7 @@ class Client extends EventEmitter {
14783
14802
  });
14784
14803
  }
14785
14804
  connect() {
14805
+ var _a, _b, _c;
14786
14806
  return __awaiter(this, void 0, void 0, function* () {
14787
14807
  this.sessionTerminating = false;
14788
14808
  this.sessionStarting = true;
@@ -14790,34 +14810,50 @@ class Client extends EventEmitter {
14790
14810
  if (this.transport) {
14791
14811
  this.transport.disconnect(false);
14792
14812
  }
14793
- const transportPref = ['websocket', 'bosh'];
14813
+ const transportPref = (_a = this.config.transportPreferenceOrder) !== null && _a !== void 0 ? _a : [];
14794
14814
  let endpoints;
14795
14815
  for (const name of transportPref) {
14796
- let conf = this.config.transports[name];
14797
- if (!conf) {
14816
+ const settings = this.config.transports[name];
14817
+ if (!settings || !this.transports[name]) {
14798
14818
  continue;
14799
14819
  }
14800
- if (typeof conf === 'string') {
14801
- conf = { url: conf };
14802
- }
14803
- else if (conf === true) {
14804
- if (!endpoints) {
14805
- try {
14806
- endpoints = yield this.discoverBindings(this.config.server);
14807
- }
14808
- catch (err) {
14809
- console.error(err);
14820
+ let config = {
14821
+ acceptLanguages: this.config.acceptLanguages || [(_b = this.config.lang) !== null && _b !== void 0 ? _b : 'en'],
14822
+ jid: this.config.jid,
14823
+ lang: (_c = this.config.lang) !== null && _c !== void 0 ? _c : 'en',
14824
+ server: this.config.server
14825
+ };
14826
+ const transport = new this.transports[name](this, this.sm, this.stanzas);
14827
+ if (typeof settings === 'string') {
14828
+ config.url = settings;
14829
+ }
14830
+ else if (settings == true) {
14831
+ if (transport.discoverBindings) {
14832
+ const discovered = yield transport.discoverBindings(this.config.server);
14833
+ if (!discovered) {
14810
14834
  continue;
14811
14835
  }
14836
+ config = Object.assign(Object.assign({}, config), discovered);
14812
14837
  }
14813
- endpoints[name] = (endpoints[name] || []).filter(url => url.startsWith('wss:') || url.startsWith('https:'));
14814
- if (!endpoints[name] || !endpoints[name].length) {
14815
- continue;
14838
+ else {
14839
+ if (!endpoints) {
14840
+ try {
14841
+ endpoints = yield this.discoverBindings(this.config.server);
14842
+ }
14843
+ catch (err) {
14844
+ console.error(err);
14845
+ continue;
14846
+ }
14847
+ }
14848
+ endpoints[name] = (endpoints[name] || []).filter(url => url.startsWith('wss:') || url.startsWith('https:'));
14849
+ if (!endpoints[name] || !endpoints[name].length) {
14850
+ continue;
14851
+ }
14852
+ config.url = endpoints[name][0];
14816
14853
  }
14817
- conf = { url: endpoints[name][0] };
14818
14854
  }
14819
- this.transport = new this.transports[name](this, this.sm, this.stanzas);
14820
- this.transport.connect(Object.assign({ acceptLanguages: this.config.acceptLanguages || ['en'], jid: this.config.jid, lang: this.config.lang || 'en', server: this.config.server, url: conf.url }, conf));
14855
+ this.transport = transport;
14856
+ this.transport.connect(config);
14821
14857
  return;
14822
14858
  }
14823
14859
  console.error('No endpoints found for the requested transports.');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "stanza",
3
3
  "description": "Modern XMPP in the browser, with a JSON API",
4
- "version": "12.17.2",
4
+ "version": "12.17.3",
5
5
  "author": "Lance Stout <lancestout@gmail.com>",
6
6
  "bugs": "https://github.com/legastero/stanza/issues",
7
7
  "contributors": [
@@ -14,7 +14,7 @@
14
14
  "@types/punycode": "^2.1.0",
15
15
  "@types/readable-stream": "^2.3.9",
16
16
  "@types/ws": "^7.4.0",
17
- "async": "^3.0.1",
17
+ "async": "^3.2.1",
18
18
  "buffer": "^6.0.3",
19
19
  "node-fetch": "^2.6.1",
20
20
  "process": "^0.11.10",
@@ -22,7 +22,7 @@
22
22
  "react-native-randombytes": "^3.6.0",
23
23
  "readable-stream": "^2.3.6",
24
24
  "sdp": "^3.0.2",
25
- "stanza-shims": "^1.1.1",
25
+ "stanza-shims": "^1.1.2",
26
26
  "tslib": "^2.2.0",
27
27
  "ws": "^7.4.4"
28
28
  },
package/plugins/muc.js CHANGED
@@ -29,7 +29,7 @@ function default_1(client) {
29
29
  }
30
30
  client.on('session:started', rejoinRooms);
31
31
  client.on('message', msg => {
32
- if (msg.type === 'groupchat' && msg.hasSubject) {
32
+ if (msg.type === 'groupchat' && msg.hasSubject && !msg.body) {
33
33
  client.emit('muc:topic', {
34
34
  from: msg.from,
35
35
  room: JID.toBare(msg.from),
@@ -88,6 +88,10 @@ class WSConnection extends readable_stream_1.Duplex {
88
88
  this.socket.onclose = () => {
89
89
  this.push(null);
90
90
  };
91
+ this.socket.onerror = (err) => {
92
+ console.error(err);
93
+ this.push(null);
94
+ };
91
95
  }
92
96
  disconnect(clean = true) {
93
97
  if (this.socket && !this.closing && this.hasStream && clean) {