@stomp/stompjs 7.1.0 → 7.2.0

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.
@@ -131,7 +131,7 @@ export class CompatClient extends Client {
131
131
  */
132
132
  public disconnect(
133
133
  disconnectCallback?: any,
134
- headers: StompHeaders = {}
134
+ headers: StompHeaders = {},
135
135
  ): void {
136
136
  if (disconnectCallback) {
137
137
  this.onDisconnect = disconnectCallback;
@@ -164,7 +164,7 @@ export class CompatClient extends Client {
164
164
  public send(
165
165
  destination: string,
166
166
  headers: { [key: string]: any } = {},
167
- body: string = ''
167
+ body: string = '',
168
168
  ): void {
169
169
  headers = (Object as any).assign({}, headers);
170
170
 
@@ -108,7 +108,7 @@ export class Stomp {
108
108
  } else {
109
109
  console.warn(
110
110
  'Stomp.over did not receive a factory, auto reconnect will not work. ' +
111
- 'Please see https://stomp-js.github.io/api-docs/latest/classes/Stomp.html#over'
111
+ 'Please see https://stomp-js.github.io/api-docs/latest/classes/Stomp.html#over',
112
112
  );
113
113
  wsFn = () => ws;
114
114
  }
package/src/frame-impl.ts CHANGED
@@ -92,7 +92,7 @@ export class FrameImpl implements IFrame {
92
92
  */
93
93
  public static fromRawFrame(
94
94
  rawFrame: IRawFrameType,
95
- escapeHeaderValues: boolean
95
+ escapeHeaderValues: boolean,
96
96
  ): FrameImpl {
97
97
  const headers: StompHeaders = {};
98
98
  const trim = (str: string): string => str.replace(/^\s+|\s+$/g, '');
@@ -143,7 +143,7 @@ export class FrameImpl implements IFrame {
143
143
  if (this.isBinaryBody) {
144
144
  return FrameImpl.toUnit8Array(
145
145
  cmdAndHeaders,
146
- this._binaryBody as Uint8Array
146
+ this._binaryBody as Uint8Array,
147
147
  ).buffer;
148
148
  } else {
149
149
  return cmdAndHeaders + this._body + BYTE.NULL;
@@ -196,19 +196,19 @@ export class FrameImpl implements IFrame {
196
196
 
197
197
  private static toUnit8Array(
198
198
  cmdAndHeaders: string,
199
- binaryBody: Uint8Array
200
- ): Uint8Array {
199
+ binaryBody: Uint8Array,
200
+ ): Uint8Array<ArrayBuffer> {
201
201
  const uint8CmdAndHeaders = new TextEncoder().encode(cmdAndHeaders);
202
202
  const nullTerminator = new Uint8Array([0]);
203
203
  const uint8Frame = new Uint8Array(
204
- uint8CmdAndHeaders.length + binaryBody.length + nullTerminator.length
204
+ uint8CmdAndHeaders.length + binaryBody.length + nullTerminator.length,
205
205
  );
206
206
 
207
207
  uint8Frame.set(uint8CmdAndHeaders);
208
208
  uint8Frame.set(binaryBody, uint8CmdAndHeaders.length);
209
209
  uint8Frame.set(
210
210
  nullTerminator,
211
- uint8CmdAndHeaders.length + binaryBody.length
211
+ uint8CmdAndHeaders.length + binaryBody.length,
212
212
  );
213
213
 
214
214
  return uint8Frame;
@@ -2,8 +2,6 @@
2
2
  * A Transaction is created by calling [Client#begin]{@link Client#begin}
3
3
  *
4
4
  * Part of `@stomp/stompjs`.
5
- *
6
- * TODO: Example and caveat
7
5
  */
8
6
  export interface ITransaction {
9
7
  /**
package/src/parser.ts CHANGED
@@ -75,14 +75,14 @@ export class Parser {
75
75
 
76
76
  public constructor(
77
77
  public onFrame: (rawFrame: IRawFrameType) => void,
78
- public onIncomingPing: () => void
78
+ public onIncomingPing: () => void,
79
79
  ) {
80
80
  this._initState();
81
81
  }
82
82
 
83
83
  public parseChunk(
84
84
  segment: string | ArrayBuffer,
85
- appendMissingNULLonIncoming: boolean = false
85
+ appendMissingNULLonIncoming: boolean = false,
86
86
  ) {
87
87
  let chunk: Uint8Array;
88
88
 
@@ -193,7 +193,7 @@ export class Parser {
193
193
  const contentLengthHeader = this._results.headers.filter(
194
194
  (header: [string, string]) => {
195
195
  return header[0] === 'content-length';
196
- }
196
+ },
197
197
  )[0];
198
198
 
199
199
  if (contentLengthHeader) {
@@ -229,7 +229,7 @@ export class Parser {
229
229
  } catch (e) {
230
230
  console.log(
231
231
  `Ignoring an exception thrown by a frame handler. Original exception: `,
232
- e
232
+ e,
233
233
  );
234
234
  }
235
235
 
@@ -8,8 +8,10 @@ import {
8
8
  messageCallbackType,
9
9
  ReconnectionTimeMode,
10
10
  wsErrorCallbackType,
11
+ emptyCallbackType,
11
12
  } from './types.js';
12
13
  import { Versions } from './versions.js';
14
+ import { Client } from './client.js';
13
15
 
14
16
  /**
15
17
  * Configuration options for STOMP Client, each key corresponds to
@@ -59,6 +61,11 @@ export class StompConfig {
59
61
  */
60
62
  public heartbeatIncoming?: number;
61
63
 
64
+ /**
65
+ * See [Client#heartbeatToleranceMultiplier]{@link Client#heartbeatToleranceMultiplier}.
66
+ */
67
+ public heartbeatToleranceMultiplier?: number;
68
+
62
69
  /**
63
70
  * See [Client#heartbeatOutgoing]{@link Client#heartbeatOutgoing}.
64
71
  */
@@ -117,7 +124,7 @@ export class StompConfig {
117
124
  /**
118
125
  * See [Client#beforeConnect]{@link Client#beforeConnect}.
119
126
  */
120
- public beforeConnect?: () => void | Promise<void>;
127
+ public beforeConnect?: (client: Client) => void | Promise<void>;
121
128
 
122
129
  /**
123
130
  * See [Client#onConnect]{@link Client#onConnect}.
@@ -144,6 +151,15 @@ export class StompConfig {
144
151
  */
145
152
  public onWebSocketError?: wsErrorCallbackType;
146
153
 
154
+ /**
155
+ * See [Client#onHeartbeatReceived]{@link Client#onHeartbeatReceived}.
156
+ */
157
+ public onHeartbeatReceived?: emptyCallbackType;
158
+
159
+ /**
160
+ * See [Client#onHeartbeatLost]{@link Client#onHeartbeatLost}.
161
+ */
162
+ public onHeartbeatLost?: emptyCallbackType;
147
163
  /**
148
164
  * See [Client#logRawCommunication]{@link Client#logRawCommunication}.
149
165
  */
@@ -11,6 +11,7 @@ import { Ticker } from './ticker.js';
11
11
  import {
12
12
  closeEventCallbackType,
13
13
  debugFnType,
14
+ emptyCallbackType,
14
15
  frameCallbackType,
15
16
  IPublishParams,
16
17
  IStompSocket,
@@ -40,6 +41,8 @@ export class StompHandler {
40
41
 
41
42
  public heartbeatIncoming: number;
42
43
 
44
+ public heartbeatToleranceMultiplier: number;
45
+
43
46
  public heartbeatOutgoing: number;
44
47
 
45
48
  public onUnhandledMessage: messageCallbackType;
@@ -48,6 +51,10 @@ export class StompHandler {
48
51
 
49
52
  public onUnhandledFrame: frameCallbackType;
50
53
 
54
+ public onHeartbeatReceived: emptyCallbackType;
55
+
56
+ public onHeartbeatLost: emptyCallbackType;
57
+
51
58
  public onConnect: frameCallbackType;
52
59
 
53
60
  public onDisconnect: frameCallbackType;
@@ -93,7 +100,7 @@ export class StompHandler {
93
100
  constructor(
94
101
  private _client: Client,
95
102
  public _webSocket: IStompSocket,
96
- config: IStomptHandlerConfig
103
+ config: IStomptHandlerConfig,
97
104
  ) {
98
105
  // used to index subscribers
99
106
  this._counter = 0;
@@ -115,6 +122,7 @@ export class StompHandler {
115
122
  this.connectHeaders = config.connectHeaders;
116
123
  this.disconnectHeaders = config.disconnectHeaders;
117
124
  this.heartbeatIncoming = config.heartbeatIncoming;
125
+ this.heartbeatToleranceMultiplier = config.heartbeatGracePeriods;
118
126
  this.heartbeatOutgoing = config.heartbeatOutgoing;
119
127
  this.splitLargeFrames = config.splitLargeFrames;
120
128
  this.maxWebSocketChunkSize = config.maxWebSocketChunkSize;
@@ -130,6 +138,8 @@ export class StompHandler {
130
138
  this.onUnhandledMessage = config.onUnhandledMessage;
131
139
  this.onUnhandledReceipt = config.onUnhandledReceipt;
132
140
  this.onUnhandledFrame = config.onUnhandledFrame;
141
+ this.onHeartbeatReceived = config.onHeartbeatReceived;
142
+ this.onHeartbeatLost = config.onHeartbeatLost;
133
143
  }
134
144
 
135
145
  public start(): void {
@@ -138,7 +148,7 @@ export class StompHandler {
138
148
  rawFrame => {
139
149
  const frame = FrameImpl.fromRawFrame(
140
150
  rawFrame,
141
- this._escapeHeaderValues
151
+ this._escapeHeaderValues,
142
152
  );
143
153
 
144
154
  // if this.logRawCommunication is set, the rawChunk is logged at this._webSocket.onmessage
@@ -153,7 +163,8 @@ export class StompHandler {
153
163
  // On Incoming Ping
154
164
  () => {
155
165
  this.debug('<<< PONG');
156
- }
166
+ this.onHeartbeatReceived();
167
+ },
157
168
  );
158
169
 
159
170
  this._webSocket.onmessage = (evt: IStompSocketMessageEvent) => {
@@ -170,7 +181,7 @@ export class StompHandler {
170
181
 
171
182
  parser.parseChunk(
172
183
  evt.data as string | ArrayBuffer,
173
- this.appendMissingNULLonIncoming
184
+ this.appendMissingNULLonIncoming,
174
185
  );
175
186
  };
176
187
 
@@ -291,7 +302,11 @@ export class StompHandler {
291
302
  const ttl: number = Math.max(this.heartbeatOutgoing, serverIncoming);
292
303
  this.debug(`send PING every ${ttl}ms`);
293
304
 
294
- this._pinger = new Ticker(ttl, this._client.heartbeatStrategy, this.debug);
305
+ this._pinger = new Ticker(
306
+ ttl,
307
+ this._client.heartbeatStrategy,
308
+ this.debug,
309
+ );
295
310
  this._pinger.start(() => {
296
311
  if (this._webSocket.readyState === StompSocketState.OPEN) {
297
312
  this._webSocket.send(BYTE.LF);
@@ -305,9 +320,10 @@ export class StompHandler {
305
320
  this.debug(`check PONG every ${ttl}ms`);
306
321
  this._ponger = setInterval(() => {
307
322
  const delta = Date.now() - this._lastServerActivityTS;
308
- // We wait twice the TTL to be flexible on window's setInterval calls
309
- if (delta > ttl * 2) {
323
+ // We wait multiple grace periods to be flexible on window's setInterval calls
324
+ if (delta > ttl * this.heartbeatToleranceMultiplier) {
310
325
  this.debug(`did not receive server activity for the last ${delta}ms`);
326
+ this.onHeartbeatLost();
311
327
  this._closeOrDiscardWebsocket();
312
328
  }
313
329
  }, ttl);
@@ -317,7 +333,7 @@ export class StompHandler {
317
333
  private _closeOrDiscardWebsocket() {
318
334
  if (this.discardWebsocketOnCommFailure) {
319
335
  this.debug(
320
- 'Discarding websocket, the underlying socket may linger for a while'
336
+ 'Discarding websocket, the underlying socket may linger for a while',
321
337
  );
322
338
  this.discardWebsocket();
323
339
  } else {
@@ -369,7 +385,8 @@ export class StompHandler {
369
385
  skipContentLengthHeader,
370
386
  });
371
387
 
372
- let rawChunk = frame.serialize();
388
+ let rawChunk: string | ArrayBuffer | Uint8Array<ArrayBuffer> =
389
+ frame.serialize();
373
390
 
374
391
  if (this.logRawCommunication) {
375
392
  this.debug(`>>> ${rawChunk}`);
@@ -400,7 +417,7 @@ export class StompHandler {
400
417
  // clone before updating
401
418
  const disconnectHeaders = (Object as any).assign(
402
419
  {},
403
- this.disconnectHeaders
420
+ this.disconnectHeaders,
404
421
  );
405
422
 
406
423
  if (!disconnectHeaders.receipt) {
@@ -458,7 +475,7 @@ export class StompHandler {
458
475
  public subscribe(
459
476
  destination: string,
460
477
  callback: messageCallbackType,
461
- headers: StompHeaders = {}
478
+ headers: StompHeaders = {},
462
479
  ): StompSubscription {
463
480
  headers = (Object as any).assign({}, headers);
464
481
 
@@ -527,7 +544,7 @@ export class StompHandler {
527
544
  public ack(
528
545
  messageId: string,
529
546
  subscriptionId: string,
530
- headers: StompHeaders = {}
547
+ headers: StompHeaders = {},
531
548
  ): void {
532
549
  headers = (Object as any).assign({}, headers);
533
550
 
@@ -543,7 +560,7 @@ export class StompHandler {
543
560
  public nack(
544
561
  messageId: string,
545
562
  subscriptionId: string,
546
- headers: StompHeaders = {}
563
+ headers: StompHeaders = {},
547
564
  ): void {
548
565
  headers = (Object as any).assign({}, headers);
549
566
 
@@ -1,5 +1,5 @@
1
1
  /**
2
- * STOMP headers. Many functions calls will accept headers as parameters.
2
+ * STOMP headers. Many function calls will accept headers as parameters.
3
3
  * The headers sent by Broker will be available as [IFrame#headers]{@link IFrame#headers}.
4
4
  *
5
5
  * `key` and `value` must be valid strings.
package/src/ticker.ts CHANGED
@@ -14,8 +14,8 @@ export class Ticker {
14
14
  constructor(
15
15
  private readonly _interval: number,
16
16
  private readonly _strategy = TickerStrategy.Interval,
17
- private readonly _debug: debugFnType) {
18
- }
17
+ private readonly _debug: debugFnType,
18
+ ) {}
19
19
 
20
20
  public start(tick: (elapsedTime: number) => void): void {
21
21
  this.stop();
@@ -33,7 +33,9 @@ export class Ticker {
33
33
  }
34
34
 
35
35
  private shouldUseWorker(): boolean {
36
- return typeof(Worker) !== 'undefined' && this._strategy === TickerStrategy.Worker
36
+ return (
37
+ typeof Worker !== 'undefined' && this._strategy === TickerStrategy.Worker
38
+ );
37
39
  }
38
40
 
39
41
  private runWorker(tick: (elapsedTime: number) => void): void {
@@ -41,10 +43,10 @@ export class Ticker {
41
43
  if (!this._worker) {
42
44
  this._worker = new Worker(
43
45
  URL.createObjectURL(
44
- new Blob([this._workerScript], { type: 'text/javascript' })
45
- )
46
+ new Blob([this._workerScript], { type: 'text/javascript' }),
47
+ ),
46
48
  );
47
- this._worker.onmessage = (message) => tick(message.data);
49
+ this._worker.onmessage = message => tick(message.data);
48
50
  }
49
51
  }
50
52
 
package/src/types.ts CHANGED
@@ -24,6 +24,13 @@ export type messageCallbackType = (message: IMessage) => void;
24
24
  */
25
25
  export type frameCallbackType = ((frame: IFrame) => void) | (() => void);
26
26
 
27
+ /**
28
+ * This callback is an "Event" only callback, no parameters provided.
29
+ *
30
+ * Part of `@stomp/stompjs`.
31
+ */
32
+ export type emptyCallbackType = () => void;
33
+
27
34
  /**
28
35
  * This callback will receive a [CloseEvent]{@link https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent}
29
36
  * as parameter.
@@ -113,7 +120,7 @@ export interface IStompSocket {
113
120
  url: string;
114
121
  onclose: ((ev?: any) => any) | undefined | null;
115
122
  onerror: ((ev: any) => any) | undefined | null;
116
- onmessage: ((ev: IStompSocketMessageEvent) => any) | undefined | null;
123
+ onmessage: ((ev: any) => any) | undefined | null;
117
124
  onopen: ((ev?: any) => any) | undefined | null;
118
125
  terminate?: (() => any) | undefined | null;
119
126
 
@@ -135,7 +142,7 @@ export interface IStompSocket {
135
142
  /**
136
143
  * Transmits data using the connection. data can be a string or an ArrayBuffer.
137
144
  */
138
- send(data: string | ArrayBuffer): void;
145
+ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
139
146
  }
140
147
 
141
148
  /**
@@ -162,7 +169,7 @@ export enum ActivationState {
162
169
  */
163
170
  export enum ReconnectionTimeMode {
164
171
  LINEAR,
165
- EXPONENTIAL
172
+ EXPONENTIAL,
166
173
  }
167
174
 
168
175
  /**
@@ -170,7 +177,7 @@ export enum ReconnectionTimeMode {
170
177
  */
171
178
  export enum TickerStrategy {
172
179
  Interval = 'interval',
173
- Worker = 'worker'
180
+ Worker = 'worker',
174
181
  }
175
182
 
176
183
  /**
@@ -182,6 +189,7 @@ export interface IStomptHandlerConfig {
182
189
  connectHeaders: StompHeaders;
183
190
  disconnectHeaders: StompHeaders;
184
191
  heartbeatIncoming: number;
192
+ heartbeatGracePeriods: number;
185
193
  heartbeatOutgoing: number;
186
194
  heartbeatStrategy: TickerStrategy;
187
195
  splitLargeFrames: boolean;
@@ -198,4 +206,6 @@ export interface IStomptHandlerConfig {
198
206
  onUnhandledMessage: messageCallbackType;
199
207
  onUnhandledReceipt: frameCallbackType;
200
208
  onUnhandledFrame: frameCallbackType;
209
+ onHeartbeatReceived: emptyCallbackType;
210
+ onHeartbeatLost: emptyCallbackType;
201
211
  }