Skip to content

Commit ffb83b7

Browse files
authored
feat(TS) Migrate to ts strophe.stream to TS (#2814)
1 parent c51487b commit ffb83b7

File tree

1 file changed

+106
-44
lines changed

1 file changed

+106
-44
lines changed

modules/xmpp/strophe.stream-management.js renamed to modules/xmpp/strophe.stream-management.ts

Lines changed: 106 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,67 @@ import { $build, Strophe } from 'strophe.js';
1212
*
1313
* @class streamManagement
1414
*/
15-
Strophe.addConnectionPlugin('streamManagement', {
15+
interface IStreamManagementPlugin {
16+
logging: boolean;
17+
autoSendCountOnEveryIncomingStanza: boolean;
18+
requestResponseInterval: number;
19+
_c: any;
20+
_NS: string;
21+
_isStreamManagementEnabled: boolean;
22+
_serverProcesssedStanzasCounter: number | null;
23+
_clientProcessedStanzasCounter: number | null;
24+
_clientSentStanzasCounter: number | null;
25+
_originalXMLOutput: ((elem: Element) => any) | null;
26+
_requestHandler: any;
27+
_incomingHandler: any;
28+
_requestResponseIntervalCount: number;
29+
_isSupported: boolean;
30+
_unacknowledgedStanzas: Element[];
31+
_acknowledgedStanzaListeners: ((stanza: Element) => void)[];
32+
_resumeToken?: string;
33+
_connectionStatus?: number;
34+
_resuming?: boolean;
35+
_connectArgs?: IArguments;
36+
_originalConnect?: (...args: any[]) => any;
37+
_originalOnStreamFeaturesAfterSASL?: (...args: any[]) => any;
38+
_originalDoDisconnect?: (...args: any[]) => any;
39+
_originalDisconnect?: (...args: any[]) => any;
40+
_resumeState?: any;
41+
_storedJid?: string;
42+
_ackHandler?: any;
43+
_enabledHandler?: any;
44+
_resumeFailedHandler?: any;
45+
_resumedHandler?: any;
46+
47+
addAcknowledgedStanzaListener(listener: (stanza: Element) => void): void;
48+
enable(resume: boolean): void;
49+
getResumeToken(): string | undefined;
50+
isSupported(): boolean;
51+
resume(): void;
52+
requestAcknowledgement(): void;
53+
getOutgoingCounter(): number | null;
54+
getIncomingCounter(): number | null;
55+
init(conn: any): void;
56+
statusChanged(status: number): void;
57+
xmlOutput(elem: Element): any;
58+
_interceptDisconnect(): void;
59+
_interceptDoDisconnect(): void;
60+
_interceptConnectArgs(): void;
61+
_onStreamFeaturesAfterSASL(elem: Element): any;
62+
_incomingStanzaHandler(elem: Element): boolean;
63+
_handleEnabled(elem: Element): boolean;
64+
_handleResumeFailed(elem: Element): boolean;
65+
_handleResumed(elem: Element): boolean;
66+
_handleAcknowledgedStanzas(reportedHandledCount: number, lastKnownHandledCount: number): void;
67+
_handleServerRequestHandler(): boolean;
68+
_handleServerAck(elem: Element): boolean;
69+
_answerProcessedStanzas(): void;
70+
_increaseSentStanzasCounter(elem: Element): void;
71+
_increaseReceivedStanzasCounter(): void;
72+
_throwError(msg: string): never;
73+
}
74+
75+
const streamManagement: IStreamManagementPlugin = {
1676

1777
/**
1878
* @property {Boolean} logging: Set to true to enable logging regarding out of sync stanzas.
@@ -117,11 +177,11 @@ Strophe.addConnectionPlugin('streamManagement', {
117177
*/
118178
_acknowledgedStanzaListeners: [],
119179

120-
addAcknowledgedStanzaListener: function(listener) {
180+
addAcknowledgedStanzaListener: function(listener: (stanza: Element) => void): void {
121181
this._acknowledgedStanzaListeners.push(listener);
122182
},
123183

124-
enable: function(resume) {
184+
enable: function(resume: boolean): void {
125185
if (!this._isSupported) {
126186
throw new Error('The server doesn\'t support urn:xmpp:sm:3 namespace');
127187
} else if (this._connectionStatus !== Strophe.Status.CONNECTED) {
@@ -132,15 +192,15 @@ Strophe.addConnectionPlugin('streamManagement', {
132192
this._c.pause();
133193
},
134194

135-
getResumeToken: function() {
195+
getResumeToken: function(): Optional<string> {
136196
return this._resumeToken;
137197
},
138198

139-
isSupported() {
199+
isSupported(): boolean {
140200
return this._isSupported;
141201
},
142202

143-
resume: function() {
203+
resume: function(): void {
144204
if (!this.getResumeToken()) {
145205
throw new Error('No resume token');
146206
}
@@ -151,26 +211,26 @@ Strophe.addConnectionPlugin('streamManagement', {
151211
this._c.options.explicitResourceBinding = true;
152212
this._resuming = true;
153213

154-
this._originalConnect.apply(this._c, this._connectArgs);
214+
this._originalConnect!.apply(this._c, this._connectArgs);
155215
},
156216

157-
requestAcknowledgement: function() {
217+
requestAcknowledgement: function(): void {
158218
if (this._connectionStatus !== Strophe.Status.CONNECTED) {
159219
throw new Error('requestAcknowledgement() can only be called in the CONNECTED state');
160220
}
161221
this._requestResponseIntervalCount = 0;
162222
this._c.send($build('r', { xmlns: this._NS }));
163223
},
164224

165-
getOutgoingCounter: function() {
225+
getOutgoingCounter: function(): Nullable<number> {
166226
return this._clientSentStanzasCounter;
167227
},
168228

169-
getIncomingCounter: function() {
229+
getIncomingCounter: function(): Nullable<number> {
170230
return this._clientProcessedStanzasCounter;
171231
},
172232

173-
init: function(conn) {
233+
init: function(conn: any): void {
174234
this._c = conn;
175235
Strophe.addNamespace('SM', this._NS);
176236

@@ -191,12 +251,12 @@ Strophe.addConnectionPlugin('streamManagement', {
191251
this._c.disconnect = this._interceptDisconnect.bind(this);
192252
},
193253

194-
_interceptDisconnect: function() {
254+
_interceptDisconnect: function(): void {
195255
this._resumeToken = undefined;
196-
this._originalDisconnect.apply(this._c, arguments);
256+
this._originalDisconnect!.apply(this._c, arguments);
197257
},
198258

199-
_interceptDoDisconnect: function() {
259+
_interceptDoDisconnect: function(): void {
200260
if (this.getResumeToken()
201261
&& !this._resuming
202262
&& this._c.connected && !this._c.disconnecting) {
@@ -217,22 +277,22 @@ Strophe.addConnectionPlugin('streamManagement', {
217277
// as they would interfere with the resume flow. They will be resent anyway.
218278
this._c._data = [];
219279

220-
this._originalDoDisconnect.apply(this._c, arguments);
280+
this._originalDoDisconnect!.apply(this._c, arguments);
221281
},
222282

223-
_interceptConnectArgs: function() {
283+
_interceptConnectArgs: function(): void {
224284
this._connectArgs = arguments;
225285

226-
this._originalConnect.apply(this._c, arguments);
286+
this._originalConnect!.apply(this._c, arguments);
227287
},
228288

229-
_onStreamFeaturesAfterSASL: function(elem) {
289+
_onStreamFeaturesAfterSASL: function(elem: Element): any {
230290
this._isSupported = elem.getElementsByTagNameNS(this._NS, "sm").length > 0;
231291

232-
return this._originalOnStreamFeaturesAfterSASL.apply(this._c, arguments);
292+
return this._originalOnStreamFeaturesAfterSASL!.apply(this._c, arguments);
233293
},
234294

235-
statusChanged: function (status) {
295+
statusChanged: function (status: number): void {
236296
this._connectionStatus = status;
237297
if (!this.getResumeToken()
238298
&& (status === Strophe.Status.CONNECTED || status === Strophe.Status.DISCONNECTED)) {
@@ -309,41 +369,41 @@ Strophe.addConnectionPlugin('streamManagement', {
309369
* @method Send
310370
* @public
311371
*/
312-
xmlOutput: function(elem) {
372+
xmlOutput: function(elem: Element): any {
313373
if (Strophe.isTagEqual(elem, 'iq') ||
314374
Strophe.isTagEqual(elem, 'presence') ||
315375
Strophe.isTagEqual(elem, 'message')) {
316376
this._increaseSentStanzasCounter(elem);
317377
}
318378

319-
return this._originalXMLOutput.call(this._c, elem);
379+
return this._originalXMLOutput!.call(this._c, elem);
320380
},
321381

322-
_handleEnabled: function(elem) {
382+
_handleEnabled: function(elem: Element): boolean {
323383
this._isStreamManagementEnabled = true;
324384
// FIXME fail if requested, but not enabled
325-
this._resumeToken = elem.getAttribute('resume') === 'true' && elem.getAttribute('id');
385+
this._resumeToken = elem.getAttribute('resume') === 'true' && elem.getAttribute('id') || undefined;
326386

327387
this._c.resume();
328388

329389
return true;
330390
},
331391

332-
_handleResumeFailed: function(elem) {
392+
_handleResumeFailed: function(elem: Element): boolean {
333393
const error = elem && (
334-
(elem.firstElementChild && elem.firstElementChild.tagName)
335-
|| (elem.firstChild && elem.firstChild.tagName));
394+
(elem.firstElementChild && (elem.firstElementChild as Element).tagName)
395+
|| (elem.firstChild && (elem.firstChild as Element).tagName));
336396

337397
this._c._changeConnectStatus(Strophe.Status.ERROR, error, elem);
338398
this._c._doDisconnect();
339399

340400
return true;
341401
},
342402

343-
_handleResumed: function(elem) {
403+
_handleResumed: function(elem: Element): boolean {
344404
// FIXME check if in the correct state
345-
var handledCount = parseInt(elem.getAttribute('h'));
346-
this._handleAcknowledgedStanzas(handledCount, this._serverProcesssedStanzasCounter);
405+
var handledCount = parseInt(elem.getAttribute('h')!);
406+
this._handleAcknowledgedStanzas(handledCount, this._serverProcesssedStanzasCounter!);
347407

348408
this._resuming = false;
349409
this._c.do_bind = false; // No need to bind our resource anymore
@@ -364,7 +424,7 @@ Strophe.addConnectionPlugin('streamManagement', {
364424
return true;
365425
},
366426

367-
_incomingStanzaHandler: function(elem) {
427+
_incomingStanzaHandler: function(elem: Element): boolean {
368428
if (Strophe.isTagEqual(elem, 'iq') || Strophe.isTagEqual(elem, 'presence') || Strophe.isTagEqual(elem, 'message')) {
369429
this._increaseReceivedStanzasCounter();
370430

@@ -376,7 +436,7 @@ Strophe.addConnectionPlugin('streamManagement', {
376436
return true;
377437
},
378438

379-
_handleAcknowledgedStanzas: function(reportedHandledCount, lastKnownHandledCount) {
439+
_handleAcknowledgedStanzas: function(reportedHandledCount: number, lastKnownHandledCount: number): void {
380440
var delta = reportedHandledCount - lastKnownHandledCount;
381441

382442
if (delta < 0) {
@@ -388,7 +448,7 @@ Strophe.addConnectionPlugin('streamManagement', {
388448
}
389449

390450
for(var i = 0; i < delta; i++) {
391-
var stanza = this._unacknowledgedStanzas.shift();
451+
var stanza = this._unacknowledgedStanzas.shift()!;
392452
for (var j = 0; j < this._acknowledgedStanzaListeners.length; j++) {
393453
this._acknowledgedStanzaListeners[j](stanza);
394454
}
@@ -405,34 +465,34 @@ Strophe.addConnectionPlugin('streamManagement', {
405465
}
406466
},
407467

408-
_handleServerRequestHandler: function() {
468+
_handleServerRequestHandler: function(): boolean {
409469
this._answerProcessedStanzas();
410470

411471
return true;
412472
},
413473

414-
_handleServerAck: function(elem){
415-
var handledCount = parseInt(elem.getAttribute('h'));
416-
this._handleAcknowledgedStanzas(handledCount, this._serverProcesssedStanzasCounter);
474+
_handleServerAck: function(elem: Element): boolean {
475+
var handledCount = parseInt(elem.getAttribute('h')!);
476+
this._handleAcknowledgedStanzas(handledCount, this._serverProcesssedStanzasCounter!);
417477

418478
return true;
419479
},
420480

421-
_answerProcessedStanzas: function() {
481+
_answerProcessedStanzas: function(): void {
422482
if (this._isStreamManagementEnabled) {
423483
this._c.send($build('a', { xmlns: this._NS, h: this._clientProcessedStanzasCounter }));
424484
}
425485
},
426486

427-
_increaseSentStanzasCounter: function(elem) {
487+
_increaseSentStanzasCounter: function(elem: Element): void {
428488
if (this._isStreamManagementEnabled) {
429489
if (this._unacknowledgedStanzas.indexOf(elem) !== -1) {
430490

431491
return;
432492
}
433493

434494
this._unacknowledgedStanzas.push(elem);
435-
this._clientSentStanzasCounter++;
495+
this._clientSentStanzasCounter!++;
436496

437497
if (this.requestResponseInterval > 0) {
438498
this._requestResponseIntervalCount++;
@@ -449,15 +509,17 @@ Strophe.addConnectionPlugin('streamManagement', {
449509
}
450510
},
451511

452-
_increaseReceivedStanzasCounter: function() {
512+
_increaseReceivedStanzasCounter: function(): void {
453513
if (this._isStreamManagementEnabled) {
454-
this._clientProcessedStanzasCounter++;
514+
this._clientProcessedStanzasCounter!++;
455515
}
456516
},
457517

458-
_throwError: function(msg) {
518+
_throwError: function(msg: string): never {
459519
Strophe.error(msg);
460520
throw new Error(msg);
461521
}
462522

463-
});
523+
};
524+
525+
Strophe.addConnectionPlugin('streamManagement', streamManagement);

0 commit comments

Comments
 (0)